xref: /linux/drivers/gpu/drm/i915/display/intel_ddi.c (revision 4da27d5dfe669e231ede7f5e4d6eb4093cc7a574)
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"
32379bc100SJani Nikula #include "intel_combo_phy.h"
33379bc100SJani Nikula #include "intel_connector.h"
347c53e628SJani Nikula #include "intel_crtc.h"
35379bc100SJani Nikula #include "intel_ddi.h"
3699092a97SDave Airlie #include "intel_ddi_buf_trans.h"
377785ae0bSVille Syrjälä #include "intel_de.h"
381d455f8dSJani Nikula #include "intel_display_types.h"
39379bc100SJani Nikula #include "intel_dp.h"
40379bc100SJani Nikula #include "intel_dp_link_training.h"
41dcb38f79SDave Airlie #include "intel_dp_mst.h"
42379bc100SJani Nikula #include "intel_dpio_phy.h"
43379bc100SJani Nikula #include "intel_dsi.h"
44dcb38f79SDave Airlie #include "intel_fdi.h"
45379bc100SJani Nikula #include "intel_fifo_underrun.h"
46379bc100SJani Nikula #include "intel_gmbus.h"
47379bc100SJani Nikula #include "intel_hdcp.h"
48379bc100SJani Nikula #include "intel_hdmi.h"
49379bc100SJani Nikula #include "intel_hotplug.h"
50379bc100SJani Nikula #include "intel_lspcon.h"
51379bc100SJani Nikula #include "intel_panel.h"
52abad6805SJani Nikula #include "intel_pps.h"
53379bc100SJani Nikula #include "intel_psr.h"
54865b73eaSMatt Roper #include "intel_snps_phy.h"
55bdacf087SAnshuman Gupta #include "intel_sprite.h"
56bc85328fSImre Deak #include "intel_tc.h"
57379bc100SJani Nikula #include "intel_vdsc.h"
58aa52b39dSManasi Navare #include "intel_vrr.h"
59714b1cdbSDave Airlie #include "skl_scaler.h"
6046d12f91SDave Airlie #include "skl_universal_plane.h"
61379bc100SJani Nikula 
62379bc100SJani Nikula static const u8 index_to_dp_signal_levels[] = {
63379bc100SJani Nikula 	[0] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_0,
64379bc100SJani Nikula 	[1] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_1,
65379bc100SJani Nikula 	[2] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_2,
66379bc100SJani Nikula 	[3] = DP_TRAIN_VOLTAGE_SWING_LEVEL_0 | DP_TRAIN_PRE_EMPH_LEVEL_3,
67379bc100SJani Nikula 	[4] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_0,
68379bc100SJani Nikula 	[5] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_1,
69379bc100SJani Nikula 	[6] = DP_TRAIN_VOLTAGE_SWING_LEVEL_1 | DP_TRAIN_PRE_EMPH_LEVEL_2,
70379bc100SJani Nikula 	[7] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_0,
71379bc100SJani Nikula 	[8] = DP_TRAIN_VOLTAGE_SWING_LEVEL_2 | DP_TRAIN_PRE_EMPH_LEVEL_1,
72379bc100SJani Nikula 	[9] = DP_TRAIN_VOLTAGE_SWING_LEVEL_3 | DP_TRAIN_PRE_EMPH_LEVEL_0,
73379bc100SJani Nikula };
74379bc100SJani Nikula 
75a621860aSVille Syrjälä static int intel_ddi_hdmi_level(struct intel_encoder *encoder,
76a621860aSVille Syrjälä 				const struct intel_crtc_state *crtc_state)
77379bc100SJani Nikula {
780aed3bdeSJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
79379bc100SJani Nikula 	int n_entries, level, default_entry;
80379bc100SJani Nikula 
8199092a97SDave Airlie 	n_entries = intel_ddi_hdmi_num_entries(encoder, crtc_state, &default_entry);
8299092a97SDave Airlie 	if (n_entries == 0)
83379bc100SJani Nikula 		return 0;
840aed3bdeSJani Nikula 	level = intel_bios_hdmi_level_shift(encoder);
850aed3bdeSJani Nikula 	if (level < 0)
867a0073d6SJani Nikula 		level = default_entry;
877a0073d6SJani Nikula 
881de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
89379bc100SJani Nikula 		level = n_entries - 1;
90379bc100SJani Nikula 
91379bc100SJani Nikula 	return level;
92379bc100SJani Nikula }
93379bc100SJani Nikula 
94379bc100SJani Nikula /*
95379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
96379bc100SJani Nikula  * values in advance. This function programs the correct values for
97379bc100SJani Nikula  * DP/eDP/FDI use cases.
98379bc100SJani Nikula  */
99266152aeSVille Syrjälä void hsw_prepare_dp_ddi_buffers(struct intel_encoder *encoder,
100379bc100SJani Nikula 				const struct intel_crtc_state *crtc_state)
101379bc100SJani Nikula {
102379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
103379bc100SJani Nikula 	u32 iboost_bit = 0;
104379bc100SJani Nikula 	int i, n_entries;
105379bc100SJani Nikula 	enum port port = encoder->port;
10687f70743SVille Syrjälä 	const struct intel_ddi_buf_trans *ddi_translations;
107379bc100SJani Nikula 
108c40a253bSVille Syrjälä 	ddi_translations = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
109d6b10b1aSVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
110d6b10b1aSVille Syrjälä 		return;
111379bc100SJani Nikula 
112379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
11393e7e61eSLucas De Marchi 	if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv) &&
1142446e1d6SMatt Roper 	    intel_bios_encoder_dp_boost_level(encoder->devdata))
115379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
116379bc100SJani Nikula 
117379bc100SJani Nikula 	for (i = 0; i < n_entries; i++) {
118f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, i),
11987f70743SVille Syrjälä 			       ddi_translations->entries[i].hsw.trans1 | iboost_bit);
120f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, i),
12187f70743SVille Syrjälä 			       ddi_translations->entries[i].hsw.trans2);
122379bc100SJani Nikula 	}
123379bc100SJani Nikula }
124379bc100SJani Nikula 
125379bc100SJani Nikula /*
126379bc100SJani Nikula  * Starting with Haswell, DDI port buffers must be programmed with correct
127379bc100SJani Nikula  * values in advance. This function programs the correct values for
128379bc100SJani Nikula  * HDMI/DVI use cases.
129379bc100SJani Nikula  */
130266152aeSVille Syrjälä static void hsw_prepare_hdmi_ddi_buffers(struct intel_encoder *encoder,
131d6b10b1aSVille Syrjälä 					 const struct intel_crtc_state *crtc_state,
132379bc100SJani Nikula 					 int level)
133379bc100SJani Nikula {
134379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
135379bc100SJani Nikula 	u32 iboost_bit = 0;
136379bc100SJani Nikula 	int n_entries;
137379bc100SJani Nikula 	enum port port = encoder->port;
13887f70743SVille Syrjälä 	const struct intel_ddi_buf_trans *ddi_translations;
139379bc100SJani Nikula 
140c40a253bSVille Syrjälä 	ddi_translations = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1411de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
142379bc100SJani Nikula 		return;
1431de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
144379bc100SJani Nikula 		level = n_entries - 1;
145379bc100SJani Nikula 
146379bc100SJani Nikula 	/* If we're boosting the current, set bit 31 of trans1 */
14793e7e61eSLucas De Marchi 	if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv) &&
1482446e1d6SMatt Roper 	    intel_bios_encoder_hdmi_boost_level(encoder->devdata))
149379bc100SJani Nikula 		iboost_bit = DDI_BUF_BALANCE_LEG_ENABLE;
150379bc100SJani Nikula 
151379bc100SJani Nikula 	/* Entry 9 is for HDMI: */
152f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_TRANS_LO(port, 9),
15387f70743SVille Syrjälä 		       ddi_translations->entries[level].hsw.trans1 | iboost_bit);
154f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_TRANS_HI(port, 9),
15587f70743SVille Syrjälä 		       ddi_translations->entries[level].hsw.trans2);
156379bc100SJani Nikula }
157379bc100SJani Nikula 
158dcb38f79SDave Airlie void intel_wait_ddi_buf_idle(struct drm_i915_private *dev_priv,
159379bc100SJani Nikula 			     enum port port)
160379bc100SJani Nikula {
1615a2ad99bSManasi Navare 	if (IS_BROXTON(dev_priv)) {
1625a2ad99bSManasi Navare 		udelay(16);
163379bc100SJani Nikula 		return;
164379bc100SJani Nikula 	}
1655a2ad99bSManasi Navare 
1665a2ad99bSManasi Navare 	if (wait_for_us((intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
1675a2ad99bSManasi Navare 			 DDI_BUF_IS_IDLE), 8))
1685a2ad99bSManasi Navare 		drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get idle\n",
16947bdb1caSJani Nikula 			port_name(port));
170379bc100SJani Nikula }
171379bc100SJani Nikula 
172e828da30SManasi Navare static void intel_wait_ddi_buf_active(struct drm_i915_private *dev_priv,
173e828da30SManasi Navare 				      enum port port)
174e828da30SManasi Navare {
175f82f2563SMatt Roper 	int ret;
176f82f2563SMatt Roper 
177e828da30SManasi Navare 	/* Wait > 518 usecs for DDI_BUF_CTL to be non idle */
178ad314fecSVille Syrjälä 	if (DISPLAY_VER(dev_priv) < 10) {
179e828da30SManasi Navare 		usleep_range(518, 1000);
180e828da30SManasi Navare 		return;
181e828da30SManasi Navare 	}
182e828da30SManasi Navare 
183f82f2563SMatt Roper 	ret = _wait_for(!(intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
184f82f2563SMatt Roper 			  DDI_BUF_IS_IDLE), IS_DG2(dev_priv) ? 1200 : 500, 10, 10);
185f82f2563SMatt Roper 
186f82f2563SMatt Roper 	if (ret)
187e828da30SManasi Navare 		drm_err(&dev_priv->drm, "Timeout waiting for DDI BUF %c to get active\n",
188e828da30SManasi Navare 			port_name(port));
189e828da30SManasi Navare }
190e828da30SManasi Navare 
191ad952982SVille Syrjälä static u32 hsw_pll_to_ddi_pll_sel(const struct intel_shared_dpll *pll)
192379bc100SJani Nikula {
193379bc100SJani Nikula 	switch (pll->info->id) {
194379bc100SJani Nikula 	case DPLL_ID_WRPLL1:
195379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL1;
196379bc100SJani Nikula 	case DPLL_ID_WRPLL2:
197379bc100SJani Nikula 		return PORT_CLK_SEL_WRPLL2;
198379bc100SJani Nikula 	case DPLL_ID_SPLL:
199379bc100SJani Nikula 		return PORT_CLK_SEL_SPLL;
200379bc100SJani Nikula 	case DPLL_ID_LCPLL_810:
201379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_810;
202379bc100SJani Nikula 	case DPLL_ID_LCPLL_1350:
203379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_1350;
204379bc100SJani Nikula 	case DPLL_ID_LCPLL_2700:
205379bc100SJani Nikula 		return PORT_CLK_SEL_LCPLL_2700;
206379bc100SJani Nikula 	default:
207379bc100SJani Nikula 		MISSING_CASE(pll->info->id);
208379bc100SJani Nikula 		return PORT_CLK_SEL_NONE;
209379bc100SJani Nikula 	}
210379bc100SJani Nikula }
211379bc100SJani Nikula 
212379bc100SJani Nikula static u32 icl_pll_to_ddi_clk_sel(struct intel_encoder *encoder,
213379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
214379bc100SJani Nikula {
215379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
216379bc100SJani Nikula 	int clock = crtc_state->port_clock;
217379bc100SJani Nikula 	const enum intel_dpll_id id = pll->info->id;
218379bc100SJani Nikula 
219379bc100SJani Nikula 	switch (id) {
220379bc100SJani Nikula 	default:
221379bc100SJani Nikula 		/*
222379bc100SJani Nikula 		 * DPLL_ID_ICL_DPLL0 and DPLL_ID_ICL_DPLL1 should not be used
223379bc100SJani Nikula 		 * here, so do warn if this get passed in
224379bc100SJani Nikula 		 */
225379bc100SJani Nikula 		MISSING_CASE(id);
226379bc100SJani Nikula 		return DDI_CLK_SEL_NONE;
227379bc100SJani Nikula 	case DPLL_ID_ICL_TBTPLL:
228379bc100SJani Nikula 		switch (clock) {
229379bc100SJani Nikula 		case 162000:
230379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_162;
231379bc100SJani Nikula 		case 270000:
232379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_270;
233379bc100SJani Nikula 		case 540000:
234379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_540;
235379bc100SJani Nikula 		case 810000:
236379bc100SJani Nikula 			return DDI_CLK_SEL_TBT_810;
237379bc100SJani Nikula 		default:
238379bc100SJani Nikula 			MISSING_CASE(clock);
239379bc100SJani Nikula 			return DDI_CLK_SEL_NONE;
240379bc100SJani Nikula 		}
241379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL1:
242379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL2:
243379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL3:
244379bc100SJani Nikula 	case DPLL_ID_ICL_MGPLL4:
2456677c3b1SJosé Roberto de Souza 	case DPLL_ID_TGL_MGPLL5:
2466677c3b1SJosé Roberto de Souza 	case DPLL_ID_TGL_MGPLL6:
247379bc100SJani Nikula 		return DDI_CLK_SEL_MG;
248379bc100SJani Nikula 	}
249379bc100SJani Nikula }
250379bc100SJani Nikula 
251414002f1SImre Deak static u32 ddi_buf_phy_link_rate(int port_clock)
252414002f1SImre Deak {
253414002f1SImre Deak 	switch (port_clock) {
254414002f1SImre Deak 	case 162000:
255414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(0);
256414002f1SImre Deak 	case 216000:
257414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(4);
258414002f1SImre Deak 	case 243000:
259414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(5);
260414002f1SImre Deak 	case 270000:
261414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(1);
262414002f1SImre Deak 	case 324000:
263414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(6);
264414002f1SImre Deak 	case 432000:
265414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(7);
266414002f1SImre Deak 	case 540000:
267414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(2);
268414002f1SImre Deak 	case 810000:
269414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(3);
270414002f1SImre Deak 	default:
271414002f1SImre Deak 		MISSING_CASE(port_clock);
272414002f1SImre Deak 		return DDI_BUF_PHY_LINK_RATE(0);
273414002f1SImre Deak 	}
274414002f1SImre Deak }
275414002f1SImre Deak 
276a621860aSVille Syrjälä static void intel_ddi_init_dp_buf_reg(struct intel_encoder *encoder,
277a621860aSVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
278379bc100SJani Nikula {
27955ce306cSJosé Roberto de Souza 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
280b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2817801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
28255ce306cSJosé Roberto de Souza 	enum phy phy = intel_port_to_phy(i915, encoder->port);
283379bc100SJani Nikula 
2847801f3b7SLucas De Marchi 	intel_dp->DP = dig_port->saved_port_bits |
285379bc100SJani Nikula 		DDI_BUF_CTL_ENABLE | DDI_BUF_TRANS_SELECT(0);
286a621860aSVille Syrjälä 	intel_dp->DP |= DDI_PORT_WIDTH(crtc_state->lane_count);
28755ce306cSJosé Roberto de Souza 
288414002f1SImre Deak 	if (IS_ALDERLAKE_P(i915) && intel_phy_is_tc(i915, phy)) {
289414002f1SImre Deak 		intel_dp->DP |= ddi_buf_phy_link_rate(crtc_state->port_clock);
290414002f1SImre Deak 		if (dig_port->tc_mode != TC_PORT_TBT_ALT)
29155ce306cSJosé Roberto de Souza 			intel_dp->DP |= DDI_BUF_CTL_TC_PHY_OWNERSHIP;
292379bc100SJani Nikula 	}
293414002f1SImre Deak }
294379bc100SJani Nikula 
295379bc100SJani Nikula static int icl_calc_tbt_pll_link(struct drm_i915_private *dev_priv,
296379bc100SJani Nikula 				 enum port port)
297379bc100SJani Nikula {
298f7960e7fSJani Nikula 	u32 val = intel_de_read(dev_priv, DDI_CLK_SEL(port)) & DDI_CLK_SEL_MASK;
299379bc100SJani Nikula 
300379bc100SJani Nikula 	switch (val) {
301379bc100SJani Nikula 	case DDI_CLK_SEL_NONE:
302379bc100SJani Nikula 		return 0;
303379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_162:
304379bc100SJani Nikula 		return 162000;
305379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_270:
306379bc100SJani Nikula 		return 270000;
307379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_540:
308379bc100SJani Nikula 		return 540000;
309379bc100SJani Nikula 	case DDI_CLK_SEL_TBT_810:
310379bc100SJani Nikula 		return 810000;
311379bc100SJani Nikula 	default:
312379bc100SJani Nikula 		MISSING_CASE(val);
313379bc100SJani Nikula 		return 0;
314379bc100SJani Nikula 	}
315379bc100SJani Nikula }
316379bc100SJani Nikula 
317379bc100SJani Nikula static void ddi_dotclock_get(struct intel_crtc_state *pipe_config)
318379bc100SJani Nikula {
319379bc100SJani Nikula 	int dotclock;
320379bc100SJani Nikula 
321379bc100SJani Nikula 	if (pipe_config->has_pch_encoder)
322379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
323379bc100SJani Nikula 						    &pipe_config->fdi_m_n);
324379bc100SJani Nikula 	else if (intel_crtc_has_dp_encoder(pipe_config))
325379bc100SJani Nikula 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
326379bc100SJani Nikula 						    &pipe_config->dp_m_n);
3272969a78aSImre Deak 	else if (pipe_config->has_hdmi_sink && pipe_config->pipe_bpp > 24)
3282969a78aSImre Deak 		dotclock = pipe_config->port_clock * 24 / pipe_config->pipe_bpp;
329379bc100SJani Nikula 	else
330379bc100SJani Nikula 		dotclock = pipe_config->port_clock;
331379bc100SJani Nikula 
332379bc100SJani Nikula 	if (pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 &&
333379bc100SJani Nikula 	    !intel_crtc_has_dp_encoder(pipe_config))
334379bc100SJani Nikula 		dotclock *= 2;
335379bc100SJani Nikula 
336379bc100SJani Nikula 	if (pipe_config->pixel_multiplier)
337379bc100SJani Nikula 		dotclock /= pipe_config->pixel_multiplier;
338379bc100SJani Nikula 
3391326a92cSMaarten Lankhorst 	pipe_config->hw.adjusted_mode.crtc_clock = dotclock;
340379bc100SJani Nikula }
341379bc100SJani Nikula 
3420c06fa15SGwan-gyeong Mun void intel_ddi_set_dp_msa(const struct intel_crtc_state *crtc_state,
3430c06fa15SGwan-gyeong Mun 			  const struct drm_connector_state *conn_state)
344379bc100SJani Nikula {
3452225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
346379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
347379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
348379bc100SJani Nikula 	u32 temp;
349379bc100SJani Nikula 
350379bc100SJani Nikula 	if (!intel_crtc_has_dp_encoder(crtc_state))
351379bc100SJani Nikula 		return;
352379bc100SJani Nikula 
3531de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder));
354379bc100SJani Nikula 
3553e706dffSVille Syrjälä 	temp = DP_MSA_MISC_SYNC_CLOCK;
356379bc100SJani Nikula 
357379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
358379bc100SJani Nikula 	case 18:
3593e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_6_BPC;
360379bc100SJani Nikula 		break;
361379bc100SJani Nikula 	case 24:
3623e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_8_BPC;
363379bc100SJani Nikula 		break;
364379bc100SJani Nikula 	case 30:
3653e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_10_BPC;
366379bc100SJani Nikula 		break;
367379bc100SJani Nikula 	case 36:
3683e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_12_BPC;
369379bc100SJani Nikula 		break;
370379bc100SJani Nikula 	default:
371379bc100SJani Nikula 		MISSING_CASE(crtc_state->pipe_bpp);
372379bc100SJani Nikula 		break;
373379bc100SJani Nikula 	}
374379bc100SJani Nikula 
375cae154fcSVille Syrjälä 	/* nonsense combination */
3761de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
377cae154fcSVille Syrjälä 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
378cae154fcSVille Syrjälä 
379cae154fcSVille Syrjälä 	if (crtc_state->limited_color_range)
3803e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_CEA_RGB;
381cae154fcSVille Syrjälä 
382379bc100SJani Nikula 	/*
383379bc100SJani Nikula 	 * As per DP 1.2 spec section 2.3.4.3 while sending
384379bc100SJani Nikula 	 * YCBCR 444 signals we should program MSA MISC1/0 fields with
385646d3dc8SVille Syrjälä 	 * colorspace information.
386379bc100SJani Nikula 	 */
387379bc100SJani Nikula 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
3883e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_YCBCR_444_BT709;
389646d3dc8SVille Syrjälä 
390379bc100SJani Nikula 	/*
391379bc100SJani Nikula 	 * As per DP 1.4a spec section 2.2.4.3 [MSA Field for Indication
392379bc100SJani Nikula 	 * of Color Encoding Format and Content Color Gamut] while sending
3930c06fa15SGwan-gyeong Mun 	 * YCBCR 420, HDR BT.2020 signals we should program MSA MISC1 fields
3940c06fa15SGwan-gyeong Mun 	 * which indicate VSC SDP for the Pixel Encoding/Colorimetry Format.
395379bc100SJani Nikula 	 */
396bd8c9ccaSGwan-gyeong Mun 	if (intel_dp_needs_vsc_sdp(crtc_state, conn_state))
3973e706dffSVille Syrjälä 		temp |= DP_MSA_MISC_COLOR_VSC_SDP;
3980c06fa15SGwan-gyeong Mun 
399f7960e7fSJani Nikula 	intel_de_write(dev_priv, TRANS_MSA_MISC(cpu_transcoder), temp);
400379bc100SJani Nikula }
401379bc100SJani Nikula 
402dc5b8ed5SVille Syrjälä static u32 bdw_trans_port_sync_master_select(enum transcoder master_transcoder)
403dc5b8ed5SVille Syrjälä {
404dc5b8ed5SVille Syrjälä 	if (master_transcoder == TRANSCODER_EDP)
405dc5b8ed5SVille Syrjälä 		return 0;
406dc5b8ed5SVille Syrjälä 	else
407dc5b8ed5SVille Syrjälä 		return master_transcoder + 1;
408dc5b8ed5SVille Syrjälä }
409dc5b8ed5SVille Syrjälä 
41099389390SJosé Roberto de Souza /*
41199389390SJosé Roberto de Souza  * Returns the TRANS_DDI_FUNC_CTL value based on CRTC state.
41299389390SJosé Roberto de Souza  *
41399389390SJosé Roberto de Souza  * Only intended to be used by intel_ddi_enable_transcoder_func() and
41499389390SJosé Roberto de Souza  * intel_ddi_config_transcoder_func().
41599389390SJosé Roberto de Souza  */
41699389390SJosé Roberto de Souza static u32
417eed22a46SVille Syrjälä intel_ddi_transcoder_func_reg_val_get(struct intel_encoder *encoder,
418eed22a46SVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
419379bc100SJani Nikula {
4202225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
421379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
422379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
423379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
424379bc100SJani Nikula 	enum port port = encoder->port;
425379bc100SJani Nikula 	u32 temp;
426379bc100SJani Nikula 
427379bc100SJani Nikula 	/* Enable TRANS_DDI_FUNC_CTL for the pipe to work in HDMI mode */
428379bc100SJani Nikula 	temp = TRANS_DDI_FUNC_ENABLE;
429005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
430df16b636SMahesh Kumar 		temp |= TGL_TRANS_DDI_SELECT_PORT(port);
431df16b636SMahesh Kumar 	else
432379bc100SJani Nikula 		temp |= TRANS_DDI_SELECT_PORT(port);
433379bc100SJani Nikula 
434379bc100SJani Nikula 	switch (crtc_state->pipe_bpp) {
435379bc100SJani Nikula 	case 18:
436379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_6;
437379bc100SJani Nikula 		break;
438379bc100SJani Nikula 	case 24:
439379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_8;
440379bc100SJani Nikula 		break;
441379bc100SJani Nikula 	case 30:
442379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_10;
443379bc100SJani Nikula 		break;
444379bc100SJani Nikula 	case 36:
445379bc100SJani Nikula 		temp |= TRANS_DDI_BPC_12;
446379bc100SJani Nikula 		break;
447379bc100SJani Nikula 	default:
448379bc100SJani Nikula 		BUG();
449379bc100SJani Nikula 	}
450379bc100SJani Nikula 
4511326a92cSMaarten Lankhorst 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PVSYNC)
452379bc100SJani Nikula 		temp |= TRANS_DDI_PVSYNC;
4531326a92cSMaarten Lankhorst 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_PHSYNC)
454379bc100SJani Nikula 		temp |= TRANS_DDI_PHSYNC;
455379bc100SJani Nikula 
456379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP) {
457379bc100SJani Nikula 		switch (pipe) {
458379bc100SJani Nikula 		case PIPE_A:
459379bc100SJani Nikula 			/* On Haswell, can only use the always-on power well for
460379bc100SJani Nikula 			 * eDP when not using the panel fitter, and when not
461379bc100SJani Nikula 			 * using motion blur mitigation (which we don't
462379bc100SJani Nikula 			 * support). */
463379bc100SJani Nikula 			if (crtc_state->pch_pfit.force_thru)
464379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ONOFF;
465379bc100SJani Nikula 			else
466379bc100SJani Nikula 				temp |= TRANS_DDI_EDP_INPUT_A_ON;
467379bc100SJani Nikula 			break;
468379bc100SJani Nikula 		case PIPE_B:
469379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
470379bc100SJani Nikula 			break;
471379bc100SJani Nikula 		case PIPE_C:
472379bc100SJani Nikula 			temp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
473379bc100SJani Nikula 			break;
474379bc100SJani Nikula 		default:
475379bc100SJani Nikula 			BUG();
476379bc100SJani Nikula 			break;
477379bc100SJani Nikula 		}
478379bc100SJani Nikula 	}
479379bc100SJani Nikula 
480379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
481379bc100SJani Nikula 		if (crtc_state->has_hdmi_sink)
482379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_HDMI;
483379bc100SJani Nikula 		else
484379bc100SJani Nikula 			temp |= TRANS_DDI_MODE_SELECT_DVI;
485379bc100SJani Nikula 
486379bc100SJani Nikula 		if (crtc_state->hdmi_scrambling)
487379bc100SJani Nikula 			temp |= TRANS_DDI_HDMI_SCRAMBLING;
488379bc100SJani Nikula 		if (crtc_state->hdmi_high_tmds_clock_ratio)
489379bc100SJani Nikula 			temp |= TRANS_DDI_HIGH_TMDS_CHAR_RATE;
490379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
491379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_FDI;
492379bc100SJani Nikula 		temp |= (crtc_state->fdi_lanes - 1) << 1;
493379bc100SJani Nikula 	} else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
494379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_DP_MST;
495379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
496b3545e08SLucas De Marchi 
497005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12) {
4986671c367SJosé Roberto de Souza 			enum transcoder master;
4996671c367SJosé Roberto de Souza 
5006671c367SJosé Roberto de Souza 			master = crtc_state->mst_master_transcoder;
5011de143ccSPankaj Bharadiya 			drm_WARN_ON(&dev_priv->drm,
5021de143ccSPankaj Bharadiya 				    master == INVALID_TRANSCODER);
5036671c367SJosé Roberto de Souza 			temp |= TRANS_DDI_MST_TRANSPORT_SELECT(master);
5046671c367SJosé Roberto de Souza 		}
505379bc100SJani Nikula 	} else {
506379bc100SJani Nikula 		temp |= TRANS_DDI_MODE_SELECT_DP_SST;
507379bc100SJani Nikula 		temp |= DDI_PORT_WIDTH(crtc_state->lane_count);
508379bc100SJani Nikula 	}
509379bc100SJani Nikula 
51093e7e61eSLucas De Marchi 	if (IS_DISPLAY_VER(dev_priv, 8, 10) &&
511dc5b8ed5SVille Syrjälä 	    crtc_state->master_transcoder != INVALID_TRANSCODER) {
512dc5b8ed5SVille Syrjälä 		u8 master_select =
513dc5b8ed5SVille Syrjälä 			bdw_trans_port_sync_master_select(crtc_state->master_transcoder);
514dc5b8ed5SVille Syrjälä 
515dc5b8ed5SVille Syrjälä 		temp |= TRANS_DDI_PORT_SYNC_ENABLE |
516dc5b8ed5SVille Syrjälä 			TRANS_DDI_PORT_SYNC_MASTER_SELECT(master_select);
517dc5b8ed5SVille Syrjälä 	}
518dc5b8ed5SVille Syrjälä 
51999389390SJosé Roberto de Souza 	return temp;
52099389390SJosé Roberto de Souza }
52199389390SJosé Roberto de Souza 
522eed22a46SVille Syrjälä void intel_ddi_enable_transcoder_func(struct intel_encoder *encoder,
523eed22a46SVille Syrjälä 				      const struct intel_crtc_state *crtc_state)
52499389390SJosé Roberto de Souza {
5252225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
52699389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
52799389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
52899389390SJosé Roberto de Souza 
529005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
530589a4cd6SVille Syrjälä 		enum transcoder master_transcoder = crtc_state->master_transcoder;
531589a4cd6SVille Syrjälä 		u32 ctl2 = 0;
532589a4cd6SVille Syrjälä 
533589a4cd6SVille Syrjälä 		if (master_transcoder != INVALID_TRANSCODER) {
534dc5b8ed5SVille Syrjälä 			u8 master_select =
535dc5b8ed5SVille Syrjälä 				bdw_trans_port_sync_master_select(master_transcoder);
536589a4cd6SVille Syrjälä 
537589a4cd6SVille Syrjälä 			ctl2 |= PORT_SYNC_MODE_ENABLE |
538d4d7d9caSVille Syrjälä 				PORT_SYNC_MODE_MASTER_SELECT(master_select);
539589a4cd6SVille Syrjälä 		}
540589a4cd6SVille Syrjälä 
541589a4cd6SVille Syrjälä 		intel_de_write(dev_priv,
542589a4cd6SVille Syrjälä 			       TRANS_DDI_FUNC_CTL2(cpu_transcoder), ctl2);
543589a4cd6SVille Syrjälä 	}
544589a4cd6SVille Syrjälä 
545580fbdc5SImre Deak 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder),
546580fbdc5SImre Deak 		       intel_ddi_transcoder_func_reg_val_get(encoder,
547580fbdc5SImre Deak 							     crtc_state));
54899389390SJosé Roberto de Souza }
54999389390SJosé Roberto de Souza 
55099389390SJosé Roberto de Souza /*
55199389390SJosé Roberto de Souza  * Same as intel_ddi_enable_transcoder_func(), but it does not set the enable
55299389390SJosé Roberto de Souza  * bit.
55399389390SJosé Roberto de Souza  */
55499389390SJosé Roberto de Souza static void
555eed22a46SVille Syrjälä intel_ddi_config_transcoder_func(struct intel_encoder *encoder,
556eed22a46SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
55799389390SJosé Roberto de Souza {
5582225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
55999389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
56099389390SJosé Roberto de Souza 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
561589a4cd6SVille Syrjälä 	u32 ctl;
56299389390SJosé Roberto de Souza 
563eed22a46SVille Syrjälä 	ctl = intel_ddi_transcoder_func_reg_val_get(encoder, crtc_state);
564589a4cd6SVille Syrjälä 	ctl &= ~TRANS_DDI_FUNC_ENABLE;
565589a4cd6SVille Syrjälä 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
566379bc100SJani Nikula }
567379bc100SJani Nikula 
568379bc100SJani Nikula void intel_ddi_disable_transcoder_func(const struct intel_crtc_state *crtc_state)
569379bc100SJani Nikula {
5702225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
571379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
572379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
573589a4cd6SVille Syrjälä 	u32 ctl;
574c59053dcSJosé Roberto de Souza 
575005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
576589a4cd6SVille Syrjälä 		intel_de_write(dev_priv,
577589a4cd6SVille Syrjälä 			       TRANS_DDI_FUNC_CTL2(cpu_transcoder), 0);
578589a4cd6SVille Syrjälä 
579589a4cd6SVille Syrjälä 	ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
580dc5b8ed5SVille Syrjälä 
5811cfcdbf3SSean Paul 	drm_WARN_ON(crtc->base.dev, ctl & TRANS_DDI_HDCP_SIGNALLING);
5821cfcdbf3SSean Paul 
583589a4cd6SVille Syrjälä 	ctl &= ~TRANS_DDI_FUNC_ENABLE;
584379bc100SJani Nikula 
58593e7e61eSLucas De Marchi 	if (IS_DISPLAY_VER(dev_priv, 8, 10))
586dc5b8ed5SVille Syrjälä 		ctl &= ~(TRANS_DDI_PORT_SYNC_ENABLE |
587dc5b8ed5SVille Syrjälä 			 TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK);
588dc5b8ed5SVille Syrjälä 
589005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
590919e4f07SJosé Roberto de Souza 		if (!intel_dp_mst_is_master_trans(crtc_state)) {
591589a4cd6SVille Syrjälä 			ctl &= ~(TGL_TRANS_DDI_PORT_MASK |
592919e4f07SJosé Roberto de Souza 				 TRANS_DDI_MODE_SELECT_MASK);
593919e4f07SJosé Roberto de Souza 		}
594df16b636SMahesh Kumar 	} else {
595589a4cd6SVille Syrjälä 		ctl &= ~(TRANS_DDI_PORT_MASK | TRANS_DDI_MODE_SELECT_MASK);
596df16b636SMahesh Kumar 	}
597dc5b8ed5SVille Syrjälä 
598589a4cd6SVille Syrjälä 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), ctl);
599379bc100SJani Nikula 
600379bc100SJani Nikula 	if (dev_priv->quirks & QUIRK_INCREASE_DDI_DISABLED_TIME &&
601379bc100SJani Nikula 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
60247bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
60347bdb1caSJani Nikula 			    "Quirk Increase DDI disabled time\n");
604379bc100SJani Nikula 		/* Quirk time at 100ms for reliable operation */
605379bc100SJani Nikula 		msleep(100);
606379bc100SJani Nikula 	}
607379bc100SJani Nikula }
608379bc100SJani Nikula 
6091a67a168SAnshuman Gupta int intel_ddi_toggle_hdcp_bits(struct intel_encoder *intel_encoder,
6100b9c9290SSean Paul 			       enum transcoder cpu_transcoder,
6111a67a168SAnshuman Gupta 			       bool enable, u32 hdcp_mask)
612379bc100SJani Nikula {
613379bc100SJani Nikula 	struct drm_device *dev = intel_encoder->base.dev;
614379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
615379bc100SJani Nikula 	intel_wakeref_t wakeref;
616379bc100SJani Nikula 	int ret = 0;
617379bc100SJani Nikula 	u32 tmp;
618379bc100SJani Nikula 
619379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
620379bc100SJani Nikula 						     intel_encoder->power_domain);
6211de143ccSPankaj Bharadiya 	if (drm_WARN_ON(dev, !wakeref))
622379bc100SJani Nikula 		return -ENXIO;
623379bc100SJani Nikula 
6240b9c9290SSean Paul 	tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
625379bc100SJani Nikula 	if (enable)
6261a67a168SAnshuman Gupta 		tmp |= hdcp_mask;
627379bc100SJani Nikula 	else
6281a67a168SAnshuman Gupta 		tmp &= ~hdcp_mask;
6290b9c9290SSean Paul 	intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder), tmp);
630379bc100SJani Nikula 	intel_display_power_put(dev_priv, intel_encoder->power_domain, wakeref);
631379bc100SJani Nikula 	return ret;
632379bc100SJani Nikula }
633379bc100SJani Nikula 
634379bc100SJani Nikula bool intel_ddi_connector_get_hw_state(struct intel_connector *intel_connector)
635379bc100SJani Nikula {
636379bc100SJani Nikula 	struct drm_device *dev = intel_connector->base.dev;
637379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
638fa7edcd2SVille Syrjälä 	struct intel_encoder *encoder = intel_attached_encoder(intel_connector);
639379bc100SJani Nikula 	int type = intel_connector->base.connector_type;
640379bc100SJani Nikula 	enum port port = encoder->port;
641379bc100SJani Nikula 	enum transcoder cpu_transcoder;
642379bc100SJani Nikula 	intel_wakeref_t wakeref;
643379bc100SJani Nikula 	enum pipe pipe = 0;
644379bc100SJani Nikula 	u32 tmp;
645379bc100SJani Nikula 	bool ret;
646379bc100SJani Nikula 
647379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
648379bc100SJani Nikula 						     encoder->power_domain);
649379bc100SJani Nikula 	if (!wakeref)
650379bc100SJani Nikula 		return false;
651379bc100SJani Nikula 
652379bc100SJani Nikula 	if (!encoder->get_hw_state(encoder, &pipe)) {
653379bc100SJani Nikula 		ret = false;
654379bc100SJani Nikula 		goto out;
655379bc100SJani Nikula 	}
656379bc100SJani Nikula 
65710cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
658379bc100SJani Nikula 		cpu_transcoder = TRANSCODER_EDP;
659379bc100SJani Nikula 	else
660379bc100SJani Nikula 		cpu_transcoder = (enum transcoder) pipe;
661379bc100SJani Nikula 
662f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
663379bc100SJani Nikula 
664379bc100SJani Nikula 	switch (tmp & TRANS_DDI_MODE_SELECT_MASK) {
665379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
666379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
667379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_HDMIA;
668379bc100SJani Nikula 		break;
669379bc100SJani Nikula 
670379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
671379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_eDP ||
672379bc100SJani Nikula 		      type == DRM_MODE_CONNECTOR_DisplayPort;
673379bc100SJani Nikula 		break;
674379bc100SJani Nikula 
675379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
676379bc100SJani Nikula 		/* if the transcoder is in MST state then
677379bc100SJani Nikula 		 * connector isn't connected */
678379bc100SJani Nikula 		ret = false;
679379bc100SJani Nikula 		break;
680379bc100SJani Nikula 
681379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI:
682379bc100SJani Nikula 		ret = type == DRM_MODE_CONNECTOR_VGA;
683379bc100SJani Nikula 		break;
684379bc100SJani Nikula 
685379bc100SJani Nikula 	default:
686379bc100SJani Nikula 		ret = false;
687379bc100SJani Nikula 		break;
688379bc100SJani Nikula 	}
689379bc100SJani Nikula 
690379bc100SJani Nikula out:
691379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
692379bc100SJani Nikula 
693379bc100SJani Nikula 	return ret;
694379bc100SJani Nikula }
695379bc100SJani Nikula 
696379bc100SJani Nikula static void intel_ddi_get_encoder_pipes(struct intel_encoder *encoder,
697379bc100SJani Nikula 					u8 *pipe_mask, bool *is_dp_mst)
698379bc100SJani Nikula {
699379bc100SJani Nikula 	struct drm_device *dev = encoder->base.dev;
700379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
701379bc100SJani Nikula 	enum port port = encoder->port;
702379bc100SJani Nikula 	intel_wakeref_t wakeref;
703379bc100SJani Nikula 	enum pipe p;
704379bc100SJani Nikula 	u32 tmp;
705379bc100SJani Nikula 	u8 mst_pipe_mask;
706379bc100SJani Nikula 
707379bc100SJani Nikula 	*pipe_mask = 0;
708379bc100SJani Nikula 	*is_dp_mst = false;
709379bc100SJani Nikula 
710379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
711379bc100SJani Nikula 						     encoder->power_domain);
712379bc100SJani Nikula 	if (!wakeref)
713379bc100SJani Nikula 		return;
714379bc100SJani Nikula 
715f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, DDI_BUF_CTL(port));
716379bc100SJani Nikula 	if (!(tmp & DDI_BUF_CTL_ENABLE))
717379bc100SJani Nikula 		goto out;
718379bc100SJani Nikula 
71910cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A) {
720f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv,
721f7960e7fSJani Nikula 				    TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
722379bc100SJani Nikula 
723379bc100SJani Nikula 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
724379bc100SJani Nikula 		default:
725379bc100SJani Nikula 			MISSING_CASE(tmp & TRANS_DDI_EDP_INPUT_MASK);
726df561f66SGustavo A. R. Silva 			fallthrough;
727379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ON:
728379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
729379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_A);
730379bc100SJani Nikula 			break;
731379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
732379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_B);
733379bc100SJani Nikula 			break;
734379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
735379bc100SJani Nikula 			*pipe_mask = BIT(PIPE_C);
736379bc100SJani Nikula 			break;
737379bc100SJani Nikula 		}
738379bc100SJani Nikula 
739379bc100SJani Nikula 		goto out;
740379bc100SJani Nikula 	}
741379bc100SJani Nikula 
742379bc100SJani Nikula 	mst_pipe_mask = 0;
743379bc100SJani Nikula 	for_each_pipe(dev_priv, p) {
744379bc100SJani Nikula 		enum transcoder cpu_transcoder = (enum transcoder)p;
745df16b636SMahesh Kumar 		unsigned int port_mask, ddi_select;
7466aa3bef1SJosé Roberto de Souza 		intel_wakeref_t trans_wakeref;
7476aa3bef1SJosé Roberto de Souza 
7486aa3bef1SJosé Roberto de Souza 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
7496aa3bef1SJosé Roberto de Souza 								   POWER_DOMAIN_TRANSCODER(cpu_transcoder));
7506aa3bef1SJosé Roberto de Souza 		if (!trans_wakeref)
7516aa3bef1SJosé Roberto de Souza 			continue;
752df16b636SMahesh Kumar 
753005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12) {
754df16b636SMahesh Kumar 			port_mask = TGL_TRANS_DDI_PORT_MASK;
755df16b636SMahesh Kumar 			ddi_select = TGL_TRANS_DDI_SELECT_PORT(port);
756df16b636SMahesh Kumar 		} else {
757df16b636SMahesh Kumar 			port_mask = TRANS_DDI_PORT_MASK;
758df16b636SMahesh Kumar 			ddi_select = TRANS_DDI_SELECT_PORT(port);
759df16b636SMahesh Kumar 		}
760379bc100SJani Nikula 
761f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv,
762f7960e7fSJani Nikula 				    TRANS_DDI_FUNC_CTL(cpu_transcoder));
7636aa3bef1SJosé Roberto de Souza 		intel_display_power_put(dev_priv, POWER_DOMAIN_TRANSCODER(cpu_transcoder),
7646aa3bef1SJosé Roberto de Souza 					trans_wakeref);
765379bc100SJani Nikula 
766df16b636SMahesh Kumar 		if ((tmp & port_mask) != ddi_select)
767379bc100SJani Nikula 			continue;
768379bc100SJani Nikula 
769379bc100SJani Nikula 		if ((tmp & TRANS_DDI_MODE_SELECT_MASK) ==
770379bc100SJani Nikula 		    TRANS_DDI_MODE_SELECT_DP_MST)
771379bc100SJani Nikula 			mst_pipe_mask |= BIT(p);
772379bc100SJani Nikula 
773379bc100SJani Nikula 		*pipe_mask |= BIT(p);
774379bc100SJani Nikula 	}
775379bc100SJani Nikula 
776379bc100SJani Nikula 	if (!*pipe_mask)
77747bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
77847bdb1caSJani Nikula 			    "No pipe for [ENCODER:%d:%s] found\n",
77966a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name);
780379bc100SJani Nikula 
781379bc100SJani Nikula 	if (!mst_pipe_mask && hweight8(*pipe_mask) > 1) {
78247bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
78347bdb1caSJani Nikula 			    "Multiple pipes for [ENCODER:%d:%s] (pipe_mask %02x)\n",
78466a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name,
78566a990ddSVille Syrjälä 			    *pipe_mask);
786379bc100SJani Nikula 		*pipe_mask = BIT(ffs(*pipe_mask) - 1);
787379bc100SJani Nikula 	}
788379bc100SJani Nikula 
789379bc100SJani Nikula 	if (mst_pipe_mask && mst_pipe_mask != *pipe_mask)
79047bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
79147bdb1caSJani Nikula 			    "Conflicting MST and non-MST state for [ENCODER:%d:%s] (pipe_mask %02x mst_pipe_mask %02x)\n",
79266a990ddSVille Syrjälä 			    encoder->base.base.id, encoder->base.name,
79366a990ddSVille Syrjälä 			    *pipe_mask, mst_pipe_mask);
794379bc100SJani Nikula 	else
795379bc100SJani Nikula 		*is_dp_mst = mst_pipe_mask;
796379bc100SJani Nikula 
797379bc100SJani Nikula out:
7982446e1d6SMatt Roper 	if (*pipe_mask && (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))) {
799f7960e7fSJani Nikula 		tmp = intel_de_read(dev_priv, BXT_PHY_CTL(port));
800379bc100SJani Nikula 		if ((tmp & (BXT_PHY_CMNLANE_POWERDOWN_ACK |
801379bc100SJani Nikula 			    BXT_PHY_LANE_POWERDOWN_ACK |
802379bc100SJani Nikula 			    BXT_PHY_LANE_ENABLED)) != BXT_PHY_LANE_ENABLED)
80347bdb1caSJani Nikula 			drm_err(&dev_priv->drm,
80447bdb1caSJani Nikula 				"[ENCODER:%d:%s] enabled but PHY powered down? (PHY_CTL %08x)\n",
80547bdb1caSJani Nikula 				encoder->base.base.id, encoder->base.name, tmp);
806379bc100SJani Nikula 	}
807379bc100SJani Nikula 
808379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
809379bc100SJani Nikula }
810379bc100SJani Nikula 
811379bc100SJani Nikula bool intel_ddi_get_hw_state(struct intel_encoder *encoder,
812379bc100SJani Nikula 			    enum pipe *pipe)
813379bc100SJani Nikula {
814379bc100SJani Nikula 	u8 pipe_mask;
815379bc100SJani Nikula 	bool is_mst;
816379bc100SJani Nikula 
817379bc100SJani Nikula 	intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
818379bc100SJani Nikula 
819379bc100SJani Nikula 	if (is_mst || !pipe_mask)
820379bc100SJani Nikula 		return false;
821379bc100SJani Nikula 
822379bc100SJani Nikula 	*pipe = ffs(pipe_mask) - 1;
823379bc100SJani Nikula 
824379bc100SJani Nikula 	return true;
825379bc100SJani Nikula }
826379bc100SJani Nikula 
82781b55ef1SJani Nikula static enum intel_display_power_domain
828379bc100SJani Nikula intel_ddi_main_link_aux_domain(struct intel_digital_port *dig_port)
829379bc100SJani Nikula {
830*4da27d5dSLucas De Marchi 	/* ICL+ HW requires corresponding AUX IOs to be powered up for PSR with
831379bc100SJani Nikula 	 * DC states enabled at the same time, while for driver initiated AUX
832379bc100SJani Nikula 	 * transfers we need the same AUX IOs to be powered but with DC states
833379bc100SJani Nikula 	 * disabled. Accordingly use the AUX power domain here which leaves DC
834379bc100SJani Nikula 	 * states enabled.
835379bc100SJani Nikula 	 * However, for non-A AUX ports the corresponding non-EDP transcoders
836379bc100SJani Nikula 	 * would have already enabled power well 2 and DC_OFF. This means we can
837379bc100SJani Nikula 	 * acquire a wider POWER_DOMAIN_AUX_{B,C,D,F} reference instead of a
838379bc100SJani Nikula 	 * specific AUX_IO reference without powering up any extra wells.
839379bc100SJani Nikula 	 * Note that PSR is enabled only on Port A even though this function
840379bc100SJani Nikula 	 * returns the correct domain for other ports too.
841379bc100SJani Nikula 	 */
842379bc100SJani Nikula 	return dig_port->aux_ch == AUX_CH_A ? POWER_DOMAIN_AUX_IO_A :
843379bc100SJani Nikula 					      intel_aux_power_domain(dig_port);
844379bc100SJani Nikula }
845379bc100SJani Nikula 
846379bc100SJani Nikula static void intel_ddi_get_power_domains(struct intel_encoder *encoder,
847379bc100SJani Nikula 					struct intel_crtc_state *crtc_state)
848379bc100SJani Nikula {
849379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
850379bc100SJani Nikula 	struct intel_digital_port *dig_port;
851d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
852379bc100SJani Nikula 
853379bc100SJani Nikula 	/*
854379bc100SJani Nikula 	 * TODO: Add support for MST encoders. Atm, the following should never
855379bc100SJani Nikula 	 * happen since fake-MST encoders don't set their get_power_domains()
856379bc100SJani Nikula 	 * hook.
857379bc100SJani Nikula 	 */
8581de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
8591de143ccSPankaj Bharadiya 			intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)))
860379bc100SJani Nikula 		return;
861379bc100SJani Nikula 
862b7d02c3aSVille Syrjälä 	dig_port = enc_to_dig_port(encoder);
863f77a2db2SImre Deak 
864f77a2db2SImre Deak 	if (!intel_phy_is_tc(dev_priv, phy) ||
865a4550977SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT) {
866a4550977SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
867a4550977SImre Deak 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
868f77a2db2SImre Deak 								   dig_port->ddi_io_power_domain);
869a4550977SImre Deak 	}
870379bc100SJani Nikula 
871379bc100SJani Nikula 	/*
872379bc100SJani Nikula 	 * AUX power is only needed for (e)DP mode, and for HDMI mode on TC
873379bc100SJani Nikula 	 * ports.
874379bc100SJani Nikula 	 */
875379bc100SJani Nikula 	if (intel_crtc_has_dp_encoder(crtc_state) ||
876162e68e1SImre Deak 	    intel_phy_is_tc(dev_priv, phy)) {
877162e68e1SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->aux_wakeref);
878162e68e1SImre Deak 		dig_port->aux_wakeref =
879379bc100SJani Nikula 			intel_display_power_get(dev_priv,
880379bc100SJani Nikula 						intel_ddi_main_link_aux_domain(dig_port));
881379bc100SJani Nikula 	}
882162e68e1SImre Deak }
883379bc100SJani Nikula 
88402a715c3SVille Syrjälä void intel_ddi_enable_pipe_clock(struct intel_encoder *encoder,
88502a715c3SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
886379bc100SJani Nikula {
8872225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
888379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
889379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
890ed2615a8SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
891ed2615a8SMatt Roper 	u32 val;
892379bc100SJani Nikula 
893df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
894ed2615a8SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 13)
895ed2615a8SMatt Roper 			val = TGL_TRANS_CLK_SEL_PORT(phy);
896ed2615a8SMatt Roper 		else if (DISPLAY_VER(dev_priv) >= 12)
897ed2615a8SMatt Roper 			val = TGL_TRANS_CLK_SEL_PORT(encoder->port);
898df16b636SMahesh Kumar 		else
899ed2615a8SMatt Roper 			val = TRANS_CLK_SEL_PORT(encoder->port);
900ed2615a8SMatt Roper 
901ed2615a8SMatt Roper 		intel_de_write(dev_priv, TRANS_CLK_SEL(cpu_transcoder), val);
902379bc100SJani Nikula 	}
903df16b636SMahesh Kumar }
904379bc100SJani Nikula 
905379bc100SJani Nikula void intel_ddi_disable_pipe_clock(const struct intel_crtc_state *crtc_state)
906379bc100SJani Nikula {
9072225f3c6SMaarten Lankhorst 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
908379bc100SJani Nikula 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
909379bc100SJani Nikula 
910df16b636SMahesh Kumar 	if (cpu_transcoder != TRANSCODER_EDP) {
911005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12)
912f7960e7fSJani Nikula 			intel_de_write(dev_priv,
913f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
914df16b636SMahesh Kumar 				       TGL_TRANS_CLK_SEL_DISABLED);
915df16b636SMahesh Kumar 		else
916f7960e7fSJani Nikula 			intel_de_write(dev_priv,
917f7960e7fSJani Nikula 				       TRANS_CLK_SEL(cpu_transcoder),
918379bc100SJani Nikula 				       TRANS_CLK_SEL_DISABLED);
919379bc100SJani Nikula 	}
920df16b636SMahesh Kumar }
921379bc100SJani Nikula 
922379bc100SJani Nikula static void _skl_ddi_set_iboost(struct drm_i915_private *dev_priv,
923379bc100SJani Nikula 				enum port port, u8 iboost)
924379bc100SJani Nikula {
925379bc100SJani Nikula 	u32 tmp;
926379bc100SJani Nikula 
927f7960e7fSJani Nikula 	tmp = intel_de_read(dev_priv, DISPIO_CR_TX_BMU_CR0);
928379bc100SJani Nikula 	tmp &= ~(BALANCE_LEG_MASK(port) | BALANCE_LEG_DISABLE(port));
929379bc100SJani Nikula 	if (iboost)
930379bc100SJani Nikula 		tmp |= iboost << BALANCE_LEG_SHIFT(port);
931379bc100SJani Nikula 	else
932379bc100SJani Nikula 		tmp |= BALANCE_LEG_DISABLE(port);
933f7960e7fSJani Nikula 	intel_de_write(dev_priv, DISPIO_CR_TX_BMU_CR0, tmp);
934379bc100SJani Nikula }
935379bc100SJani Nikula 
936379bc100SJani Nikula static void skl_ddi_set_iboost(struct intel_encoder *encoder,
937a621860aSVille Syrjälä 			       const struct intel_crtc_state *crtc_state,
938a621860aSVille Syrjälä 			       int level)
939379bc100SJani Nikula {
9407801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
941379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
942379bc100SJani Nikula 	u8 iboost;
943379bc100SJani Nikula 
944a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
945c0a950d1SJani Nikula 		iboost = intel_bios_encoder_hdmi_boost_level(encoder->devdata);
946379bc100SJani Nikula 	else
947c0a950d1SJani Nikula 		iboost = intel_bios_encoder_dp_boost_level(encoder->devdata);
948379bc100SJani Nikula 
949379bc100SJani Nikula 	if (iboost == 0) {
95087f70743SVille Syrjälä 		const struct intel_ddi_buf_trans *ddi_translations;
951379bc100SJani Nikula 		int n_entries;
952379bc100SJani Nikula 
953c40a253bSVille Syrjälä 		ddi_translations = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
9541de143ccSPankaj Bharadiya 		if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
955379bc100SJani Nikula 			return;
9561de143ccSPankaj Bharadiya 		if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
957379bc100SJani Nikula 			level = n_entries - 1;
958379bc100SJani Nikula 
95987f70743SVille Syrjälä 		iboost = ddi_translations->entries[level].hsw.i_boost;
960379bc100SJani Nikula 	}
961379bc100SJani Nikula 
962379bc100SJani Nikula 	/* Make sure that the requested I_boost is valid */
963379bc100SJani Nikula 	if (iboost && iboost != 0x1 && iboost != 0x3 && iboost != 0x7) {
96447bdb1caSJani Nikula 		drm_err(&dev_priv->drm, "Invalid I_boost value %u\n", iboost);
965379bc100SJani Nikula 		return;
966379bc100SJani Nikula 	}
967379bc100SJani Nikula 
968f0e86e05SJosé Roberto de Souza 	_skl_ddi_set_iboost(dev_priv, encoder->port, iboost);
969379bc100SJani Nikula 
970f0e86e05SJosé Roberto de Souza 	if (encoder->port == PORT_A && dig_port->max_lanes == 4)
971379bc100SJani Nikula 		_skl_ddi_set_iboost(dev_priv, PORT_E, iboost);
972379bc100SJani Nikula }
973379bc100SJani Nikula 
974379bc100SJani Nikula static void bxt_ddi_vswing_sequence(struct intel_encoder *encoder,
975a621860aSVille Syrjälä 				    const struct intel_crtc_state *crtc_state,
976a621860aSVille Syrjälä 				    int level)
977379bc100SJani Nikula {
978379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
97987f70743SVille Syrjälä 	const struct intel_ddi_buf_trans *ddi_translations;
980379bc100SJani Nikula 	enum port port = encoder->port;
981379bc100SJani Nikula 	int n_entries;
982379bc100SJani Nikula 
983c40a253bSVille Syrjälä 	ddi_translations = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
9841de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
985379bc100SJani Nikula 		return;
9861de143ccSPankaj Bharadiya 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
987379bc100SJani Nikula 		level = n_entries - 1;
988379bc100SJani Nikula 
989379bc100SJani Nikula 	bxt_ddi_phy_set_signal_level(dev_priv, port,
99087f70743SVille Syrjälä 				     ddi_translations->entries[level].bxt.margin,
99187f70743SVille Syrjälä 				     ddi_translations->entries[level].bxt.scale,
99287f70743SVille Syrjälä 				     ddi_translations->entries[level].bxt.enable,
99387f70743SVille Syrjälä 				     ddi_translations->entries[level].bxt.deemphasis);
994379bc100SJani Nikula }
995379bc100SJani Nikula 
996a621860aSVille Syrjälä static u8 intel_ddi_dp_voltage_max(struct intel_dp *intel_dp,
997a621860aSVille Syrjälä 				   const struct intel_crtc_state *crtc_state)
998379bc100SJani Nikula {
99953de0a20SVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1000379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1001379bc100SJani Nikula 	int n_entries;
1002379bc100SJani Nikula 
1003c40a253bSVille Syrjälä 	encoder->get_buf_trans(encoder, crtc_state, &n_entries);
1004379bc100SJani Nikula 
10051de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, n_entries < 1))
1006379bc100SJani Nikula 		n_entries = 1;
10071de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
10081de143ccSPankaj Bharadiya 			n_entries > ARRAY_SIZE(index_to_dp_signal_levels)))
1009379bc100SJani Nikula 		n_entries = ARRAY_SIZE(index_to_dp_signal_levels);
1010379bc100SJani Nikula 
1011379bc100SJani Nikula 	return index_to_dp_signal_levels[n_entries - 1] &
1012379bc100SJani Nikula 		DP_TRAIN_VOLTAGE_SWING_MASK;
1013379bc100SJani Nikula }
1014379bc100SJani Nikula 
1015379bc100SJani Nikula /*
1016379bc100SJani Nikula  * We assume that the full set of pre-emphasis values can be
1017379bc100SJani Nikula  * used on all DDI platforms. Should that change we need to
1018379bc100SJani Nikula  * rethink this code.
1019379bc100SJani Nikula  */
102053de0a20SVille Syrjälä static u8 intel_ddi_dp_preemph_max(struct intel_dp *intel_dp)
1021379bc100SJani Nikula {
1022379bc100SJani Nikula 	return DP_TRAIN_PRE_EMPH_LEVEL_3;
1023379bc100SJani Nikula }
1024379bc100SJani Nikula 
1025a8143150SJosé Roberto de Souza static void icl_ddi_combo_vswing_program(struct intel_encoder *encoder,
1026a621860aSVille Syrjälä 					 const struct intel_crtc_state *crtc_state,
1027a621860aSVille Syrjälä 					 int level)
1028379bc100SJani Nikula {
1029a8143150SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
103087f70743SVille Syrjälä 	const struct intel_ddi_buf_trans *ddi_translations;
1031f0e86e05SJosé Roberto de Souza 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1032a621860aSVille Syrjälä 	int n_entries, ln;
1033a621860aSVille Syrjälä 	u32 val;
1034379bc100SJani Nikula 
1035c40a253bSVille Syrjälä 	ddi_translations = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
103685da0292SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
103785da0292SVille Syrjälä 		return;
103885da0292SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
1039379bc100SJani Nikula 		level = n_entries - 1;
1040379bc100SJani Nikula 
1041a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP)) {
104281619f4aSJosé Roberto de Souza 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
104381619f4aSJosé Roberto de Souza 
104481619f4aSJosé Roberto de Souza 		val = EDP4K2K_MODE_OVRD_EN | EDP4K2K_MODE_OVRD_OPTIMIZED;
104581619f4aSJosé Roberto de Souza 		intel_dp->hobl_active = is_hobl_buf_trans(ddi_translations);
104681619f4aSJosé Roberto de Souza 		intel_de_rmw(dev_priv, ICL_PORT_CL_DW10(phy), val,
104781619f4aSJosé Roberto de Souza 			     intel_dp->hobl_active ? val : 0);
104881619f4aSJosé Roberto de Souza 	}
104981619f4aSJosé Roberto de Souza 
1050379bc100SJani Nikula 	/* Set PORT_TX_DW5 */
1051f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN0(phy));
1052379bc100SJani Nikula 	val &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK |
1053379bc100SJani Nikula 		  TAP2_DISABLE | TAP3_DISABLE);
1054379bc100SJani Nikula 	val |= SCALING_MODE_SEL(0x2);
1055379bc100SJani Nikula 	val |= RTERM_SELECT(0x6);
1056379bc100SJani Nikula 	val |= TAP3_DISABLE;
1057f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1058379bc100SJani Nikula 
1059379bc100SJani Nikula 	/* Program PORT_TX_DW2 */
1060f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW2_LN0(phy));
1061379bc100SJani Nikula 	val &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
1062379bc100SJani Nikula 		 RCOMP_SCALAR_MASK);
106387f70743SVille Syrjälä 	val |= SWING_SEL_UPPER(ddi_translations->entries[level].cnl.dw2_swing_sel);
106487f70743SVille Syrjälä 	val |= SWING_SEL_LOWER(ddi_translations->entries[level].cnl.dw2_swing_sel);
1065379bc100SJani Nikula 	/* Program Rcomp scalar for every table entry */
1066379bc100SJani Nikula 	val |= RCOMP_SCALAR(0x98);
1067f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW2_GRP(phy), val);
1068379bc100SJani Nikula 
1069379bc100SJani Nikula 	/* Program PORT_TX_DW4 */
1070379bc100SJani Nikula 	/* We cannot write to GRP. It would overwrite individual loadgen. */
1071379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
1072f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy));
1073379bc100SJani Nikula 		val &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
1074379bc100SJani Nikula 			 CURSOR_COEFF_MASK);
107587f70743SVille Syrjälä 		val |= POST_CURSOR_1(ddi_translations->entries[level].cnl.dw4_post_cursor_1);
107687f70743SVille Syrjälä 		val |= POST_CURSOR_2(ddi_translations->entries[level].cnl.dw4_post_cursor_2);
107787f70743SVille Syrjälä 		val |= CURSOR_COEFF(ddi_translations->entries[level].cnl.dw4_cursor_coeff);
1078f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy), val);
1079379bc100SJani Nikula 	}
1080379bc100SJani Nikula 
1081379bc100SJani Nikula 	/* Program PORT_TX_DW7 */
1082f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW7_LN0(phy));
1083379bc100SJani Nikula 	val &= ~N_SCALAR_MASK;
108487f70743SVille Syrjälä 	val |= N_SCALAR(ddi_translations->entries[level].cnl.dw7_n_scalar);
1085f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW7_GRP(phy), val);
1086379bc100SJani Nikula }
1087379bc100SJani Nikula 
1088379bc100SJani Nikula static void icl_combo_phy_ddi_vswing_sequence(struct intel_encoder *encoder,
1089a621860aSVille Syrjälä 					      const struct intel_crtc_state *crtc_state,
1090a621860aSVille Syrjälä 					      int level)
1091379bc100SJani Nikula {
1092379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1093dc867bc7SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1094a621860aSVille Syrjälä 	int width, rate, ln;
1095379bc100SJani Nikula 	u32 val;
1096379bc100SJani Nikula 
1097a621860aSVille Syrjälä 	width = crtc_state->lane_count;
1098a621860aSVille Syrjälä 	rate = crtc_state->port_clock;
1099379bc100SJani Nikula 
1100379bc100SJani Nikula 	/*
1101379bc100SJani Nikula 	 * 1. If port type is eDP or DP,
1102379bc100SJani Nikula 	 * set PORT_PCS_DW1 cmnkeeper_enable to 1b,
1103379bc100SJani Nikula 	 * else clear to 0b.
1104379bc100SJani Nikula 	 */
1105f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_PCS_DW1_LN0(phy));
1106a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
1107379bc100SJani Nikula 		val &= ~COMMON_KEEPER_EN;
1108379bc100SJani Nikula 	else
1109379bc100SJani Nikula 		val |= COMMON_KEEPER_EN;
1110f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_PCS_DW1_GRP(phy), val);
1111379bc100SJani Nikula 
1112379bc100SJani Nikula 	/* 2. Program loadgen select */
1113379bc100SJani Nikula 	/*
1114379bc100SJani Nikula 	 * Program PORT_TX_DW4_LN depending on Bit rate and used lanes
1115379bc100SJani Nikula 	 * <= 6 GHz and 4 lanes (LN0=0, LN1=1, LN2=1, LN3=1)
1116379bc100SJani Nikula 	 * <= 6 GHz and 1,2 lanes (LN0=0, LN1=1, LN2=1, LN3=0)
1117379bc100SJani Nikula 	 * > 6 GHz (LN0=0, LN1=0, LN2=0, LN3=0)
1118379bc100SJani Nikula 	 */
1119379bc100SJani Nikula 	for (ln = 0; ln <= 3; ln++) {
1120f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy));
1121379bc100SJani Nikula 		val &= ~LOADGEN_SELECT;
1122379bc100SJani Nikula 
1123379bc100SJani Nikula 		if ((rate <= 600000 && width == 4 && ln >= 1) ||
1124379bc100SJani Nikula 		    (rate <= 600000 && width < 4 && (ln == 1 || ln == 2))) {
1125379bc100SJani Nikula 			val |= LOADGEN_SELECT;
1126379bc100SJani Nikula 		}
1127f7960e7fSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_LN(ln, phy), val);
1128379bc100SJani Nikula 	}
1129379bc100SJani Nikula 
1130379bc100SJani Nikula 	/* 3. Set PORT_CL_DW5 SUS Clock Config to 11b */
1131f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_CL_DW5(phy));
1132379bc100SJani Nikula 	val |= SUS_CLOCK_CONFIG;
1133f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_CL_DW5(phy), val);
1134379bc100SJani Nikula 
1135379bc100SJani Nikula 	/* 4. Clear training enable to change swing values */
1136f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN0(phy));
1137379bc100SJani Nikula 	val &= ~TX_TRAINING_EN;
1138f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1139379bc100SJani Nikula 
1140379bc100SJani Nikula 	/* 5. Program swing and de-emphasis */
1141a621860aSVille Syrjälä 	icl_ddi_combo_vswing_program(encoder, crtc_state, level);
1142379bc100SJani Nikula 
1143379bc100SJani Nikula 	/* 6. Set training enable to trigger update */
1144f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN0(phy));
1145379bc100SJani Nikula 	val |= TX_TRAINING_EN;
1146f7960e7fSJani Nikula 	intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), val);
1147379bc100SJani Nikula }
1148379bc100SJani Nikula 
1149379bc100SJani Nikula static void icl_mg_phy_ddi_vswing_sequence(struct intel_encoder *encoder,
1150a621860aSVille Syrjälä 					   const struct intel_crtc_state *crtc_state,
1151a621860aSVille Syrjälä 					   int level)
1152379bc100SJani Nikula {
1153379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1154f21e8b80SJosé Roberto de Souza 	enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
115587f70743SVille Syrjälä 	const struct intel_ddi_buf_trans *ddi_translations;
1156a621860aSVille Syrjälä 	int n_entries, ln;
1157a621860aSVille Syrjälä 	u32 val;
1158379bc100SJani Nikula 
1159f8c6b615SVille Syrjälä 	if (enc_to_dig_port(encoder)->tc_mode == TC_PORT_TBT_ALT)
1160f8c6b615SVille Syrjälä 		return;
1161f8c6b615SVille Syrjälä 
1162c40a253bSVille Syrjälä 	ddi_translations = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
116385da0292SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
116485da0292SVille Syrjälä 		return;
116585da0292SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
11665ec34647SVille Syrjälä 		level = n_entries - 1;
1167379bc100SJani Nikula 
1168379bc100SJani Nikula 	/* Set MG_TX_LINK_PARAMS cri_use_fs32 to 0. */
1169379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1170f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port));
1171379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
1172f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_LINK_PARAMS(ln, tc_port), val);
1173379bc100SJani Nikula 
1174f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port));
1175379bc100SJani Nikula 		val &= ~CRI_USE_FS32;
1176f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_LINK_PARAMS(ln, tc_port), val);
1177379bc100SJani Nikula 	}
1178379bc100SJani Nikula 
1179379bc100SJani Nikula 	/* Program MG_TX_SWINGCTRL with values from vswing table */
1180379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1181f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port));
1182379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
1183379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
118487f70743SVille Syrjälä 			ddi_translations->entries[level].mg.cri_txdeemph_override_17_12);
1185f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_SWINGCTRL(ln, tc_port), val);
1186379bc100SJani Nikula 
1187f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_SWINGCTRL(ln, tc_port));
1188379bc100SJani Nikula 		val &= ~CRI_TXDEEMPH_OVERRIDE_17_12_MASK;
1189379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_17_12(
119087f70743SVille Syrjälä 			ddi_translations->entries[level].mg.cri_txdeemph_override_17_12);
1191f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_SWINGCTRL(ln, tc_port), val);
1192379bc100SJani Nikula 	}
1193379bc100SJani Nikula 
1194379bc100SJani Nikula 	/* Program MG_TX_DRVCTRL with values from vswing table */
1195379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1196f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_DRVCTRL(ln, tc_port));
1197379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
1198379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
1199379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
120087f70743SVille Syrjälä 			ddi_translations->entries[level].mg.cri_txdeemph_override_5_0) |
1201379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
120287f70743SVille Syrjälä 				ddi_translations->entries[level].mg.cri_txdeemph_override_11_6) |
1203379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
1204f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_DRVCTRL(ln, tc_port), val);
1205379bc100SJani Nikula 
1206f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_DRVCTRL(ln, tc_port));
1207379bc100SJani Nikula 		val &= ~(CRI_TXDEEMPH_OVERRIDE_11_6_MASK |
1208379bc100SJani Nikula 			 CRI_TXDEEMPH_OVERRIDE_5_0_MASK);
1209379bc100SJani Nikula 		val |= CRI_TXDEEMPH_OVERRIDE_5_0(
121087f70743SVille Syrjälä 			ddi_translations->entries[level].mg.cri_txdeemph_override_5_0) |
1211379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_11_6(
121287f70743SVille Syrjälä 				ddi_translations->entries[level].mg.cri_txdeemph_override_11_6) |
1213379bc100SJani Nikula 			CRI_TXDEEMPH_OVERRIDE_EN;
1214f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_DRVCTRL(ln, tc_port), val);
1215379bc100SJani Nikula 
1216379bc100SJani Nikula 		/* FIXME: Program CRI_LOADGEN_SEL after the spec is updated */
1217379bc100SJani Nikula 	}
1218379bc100SJani Nikula 
1219379bc100SJani Nikula 	/*
1220379bc100SJani Nikula 	 * Program MG_CLKHUB<LN, port being used> with value from frequency table
1221379bc100SJani Nikula 	 * In case of Legacy mode on MG PHY, both TX1 and TX2 enabled so use the
1222379bc100SJani Nikula 	 * values from table for which TX1 and TX2 enabled.
1223379bc100SJani Nikula 	 */
1224379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1225f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_CLKHUB(ln, tc_port));
1226a621860aSVille Syrjälä 		if (crtc_state->port_clock < 300000)
1227379bc100SJani Nikula 			val |= CFG_LOW_RATE_LKREN_EN;
1228379bc100SJani Nikula 		else
1229379bc100SJani Nikula 			val &= ~CFG_LOW_RATE_LKREN_EN;
1230f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_CLKHUB(ln, tc_port), val);
1231379bc100SJani Nikula 	}
1232379bc100SJani Nikula 
1233379bc100SJani Nikula 	/* Program the MG_TX_DCC<LN, port being used> based on the link frequency */
1234379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1235f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX1_DCC(ln, tc_port));
1236379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
1237a621860aSVille Syrjälä 		if (crtc_state->port_clock <= 500000) {
1238379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
1239379bc100SJani Nikula 		} else {
1240379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
1241379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
1242379bc100SJani Nikula 		}
1243f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_DCC(ln, tc_port), val);
1244379bc100SJani Nikula 
1245f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, MG_TX2_DCC(ln, tc_port));
1246379bc100SJani Nikula 		val &= ~CFG_AMI_CK_DIV_OVERRIDE_VAL_MASK;
1247a621860aSVille Syrjälä 		if (crtc_state->port_clock <= 500000) {
1248379bc100SJani Nikula 			val &= ~CFG_AMI_CK_DIV_OVERRIDE_EN;
1249379bc100SJani Nikula 		} else {
1250379bc100SJani Nikula 			val |= CFG_AMI_CK_DIV_OVERRIDE_EN |
1251379bc100SJani Nikula 				CFG_AMI_CK_DIV_OVERRIDE_VAL(1);
1252379bc100SJani Nikula 		}
1253f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_DCC(ln, tc_port), val);
1254379bc100SJani Nikula 	}
1255379bc100SJani Nikula 
1256379bc100SJani Nikula 	/* Program MG_TX_PISO_READLOAD with values from vswing table */
1257379bc100SJani Nikula 	for (ln = 0; ln < 2; ln++) {
1258f7960e7fSJani Nikula 		val = intel_de_read(dev_priv,
1259f7960e7fSJani Nikula 				    MG_TX1_PISO_READLOAD(ln, tc_port));
1260379bc100SJani Nikula 		val |= CRI_CALCINIT;
1261f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX1_PISO_READLOAD(ln, tc_port),
1262f7960e7fSJani Nikula 			       val);
1263379bc100SJani Nikula 
1264f7960e7fSJani Nikula 		val = intel_de_read(dev_priv,
1265f7960e7fSJani Nikula 				    MG_TX2_PISO_READLOAD(ln, tc_port));
1266379bc100SJani Nikula 		val |= CRI_CALCINIT;
1267f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_TX2_PISO_READLOAD(ln, tc_port),
1268f7960e7fSJani Nikula 			       val);
1269379bc100SJani Nikula 	}
1270379bc100SJani Nikula }
1271379bc100SJani Nikula 
1272379bc100SJani Nikula static void icl_ddi_vswing_sequence(struct intel_encoder *encoder,
1273a621860aSVille Syrjälä 				    const struct intel_crtc_state *crtc_state,
1274a621860aSVille Syrjälä 				    int level)
1275379bc100SJani Nikula {
1276379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1277d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1278379bc100SJani Nikula 
1279d8fe2ab6SMatt Roper 	if (intel_phy_is_combo(dev_priv, phy))
1280a621860aSVille Syrjälä 		icl_combo_phy_ddi_vswing_sequence(encoder, crtc_state, level);
1281379bc100SJani Nikula 	else
1282a621860aSVille Syrjälä 		icl_mg_phy_ddi_vswing_sequence(encoder, crtc_state, level);
1283379bc100SJani Nikula }
1284379bc100SJani Nikula 
1285978c3e53SClinton A Taylor static void
1286a621860aSVille Syrjälä tgl_dkl_phy_ddi_vswing_sequence(struct intel_encoder *encoder,
1287a621860aSVille Syrjälä 				const struct intel_crtc_state *crtc_state,
1288a621860aSVille Syrjälä 				int level)
1289978c3e53SClinton A Taylor {
1290978c3e53SClinton A Taylor 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1291978c3e53SClinton A Taylor 	enum tc_port tc_port = intel_port_to_tc(dev_priv, encoder->port);
129287f70743SVille Syrjälä 	const struct intel_ddi_buf_trans *ddi_translations;
1293a621860aSVille Syrjälä 	u32 val, dpcnt_mask, dpcnt_val;
1294a621860aSVille Syrjälä 	int n_entries, ln;
1295978c3e53SClinton A Taylor 
1296f8c6b615SVille Syrjälä 	if (enc_to_dig_port(encoder)->tc_mode == TC_PORT_TBT_ALT)
1297f8c6b615SVille Syrjälä 		return;
1298f8c6b615SVille Syrjälä 
1299c40a253bSVille Syrjälä 	ddi_translations = encoder->get_buf_trans(encoder, crtc_state, &n_entries);
130085da0292SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !ddi_translations))
130185da0292SVille Syrjälä 		return;
130285da0292SVille Syrjälä 	if (drm_WARN_ON_ONCE(&dev_priv->drm, level >= n_entries))
1303978c3e53SClinton A Taylor 		level = n_entries - 1;
1304978c3e53SClinton A Taylor 
1305978c3e53SClinton A Taylor 	dpcnt_mask = (DKL_TX_PRESHOOT_COEFF_MASK |
1306978c3e53SClinton A Taylor 		      DKL_TX_DE_EMPAHSIS_COEFF_MASK |
1307978c3e53SClinton A Taylor 		      DKL_TX_VSWING_CONTROL_MASK);
130887f70743SVille Syrjälä 	dpcnt_val = DKL_TX_VSWING_CONTROL(ddi_translations->entries[level].dkl.dkl_vswing_control);
130987f70743SVille Syrjälä 	dpcnt_val |= DKL_TX_DE_EMPHASIS_COEFF(ddi_translations->entries[level].dkl.dkl_de_emphasis_control);
131087f70743SVille Syrjälä 	dpcnt_val |= DKL_TX_PRESHOOT_COEFF(ddi_translations->entries[level].dkl.dkl_preshoot_control);
1311978c3e53SClinton A Taylor 
1312978c3e53SClinton A Taylor 	for (ln = 0; ln < 2; ln++) {
1313f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
1314f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, ln));
1315978c3e53SClinton A Taylor 
1316f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_PMD_LANE_SUS(tc_port), 0);
13172d69c42eSJosé Roberto de Souza 
1318978c3e53SClinton A Taylor 		/* All the registers are RMW */
1319f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL0(tc_port));
1320978c3e53SClinton A Taylor 		val &= ~dpcnt_mask;
1321978c3e53SClinton A Taylor 		val |= dpcnt_val;
1322f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL0(tc_port), val);
1323978c3e53SClinton A Taylor 
1324f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL1(tc_port));
1325978c3e53SClinton A Taylor 		val &= ~dpcnt_mask;
1326978c3e53SClinton A Taylor 		val |= dpcnt_val;
1327f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL1(tc_port), val);
1328978c3e53SClinton A Taylor 
1329f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, DKL_TX_DPCNTL2(tc_port));
1330978c3e53SClinton A Taylor 		val &= ~DKL_TX_DP20BITMODE;
1331f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_TX_DPCNTL2(tc_port), val);
133203bca4a8SMika Kahola 
133303bca4a8SMika Kahola 		if ((intel_crtc_has_dp_encoder(crtc_state) &&
133403bca4a8SMika Kahola 		     crtc_state->port_clock == 162000) ||
133503bca4a8SMika Kahola 		    (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) &&
133603bca4a8SMika Kahola 		     crtc_state->port_clock == 594000))
133703bca4a8SMika Kahola 			val |= DKL_TX_LOADGEN_SHARING_PMD_DISABLE;
133803bca4a8SMika Kahola 		else
133903bca4a8SMika Kahola 			val &= ~DKL_TX_LOADGEN_SHARING_PMD_DISABLE;
1340978c3e53SClinton A Taylor 	}
1341978c3e53SClinton A Taylor }
1342978c3e53SClinton A Taylor 
1343978c3e53SClinton A Taylor static void tgl_ddi_vswing_sequence(struct intel_encoder *encoder,
1344a621860aSVille Syrjälä 				    const struct intel_crtc_state *crtc_state,
1345a621860aSVille Syrjälä 				    int level)
1346978c3e53SClinton A Taylor {
1347978c3e53SClinton A Taylor 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1348978c3e53SClinton A Taylor 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
1349978c3e53SClinton A Taylor 
1350978c3e53SClinton A Taylor 	if (intel_phy_is_combo(dev_priv, phy))
1351a621860aSVille Syrjälä 		icl_combo_phy_ddi_vswing_sequence(encoder, crtc_state, level);
1352978c3e53SClinton A Taylor 	else
1353a621860aSVille Syrjälä 		tgl_dkl_phy_ddi_vswing_sequence(encoder, crtc_state, level);
1354978c3e53SClinton A Taylor }
1355978c3e53SClinton A Taylor 
1356a621860aSVille Syrjälä static int translate_signal_level(struct intel_dp *intel_dp,
1357a621860aSVille Syrjälä 				  u8 signal_levels)
1358379bc100SJani Nikula {
13598b4f2137SPankaj Bharadiya 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
1360379bc100SJani Nikula 	int i;
1361379bc100SJani Nikula 
1362379bc100SJani Nikula 	for (i = 0; i < ARRAY_SIZE(index_to_dp_signal_levels); i++) {
1363379bc100SJani Nikula 		if (index_to_dp_signal_levels[i] == signal_levels)
1364379bc100SJani Nikula 			return i;
1365379bc100SJani Nikula 	}
1366379bc100SJani Nikula 
13678b4f2137SPankaj Bharadiya 	drm_WARN(&i915->drm, 1,
13688b4f2137SPankaj Bharadiya 		 "Unsupported voltage swing/pre-emphasis level: 0x%x\n",
1369379bc100SJani Nikula 		 signal_levels);
1370379bc100SJani Nikula 
1371379bc100SJani Nikula 	return 0;
1372379bc100SJani Nikula }
1373379bc100SJani Nikula 
1374a621860aSVille Syrjälä static int intel_ddi_dp_level(struct intel_dp *intel_dp)
1375379bc100SJani Nikula {
1376379bc100SJani Nikula 	u8 train_set = intel_dp->train_set[0];
1377a621860aSVille Syrjälä 	u8 signal_levels = train_set & (DP_TRAIN_VOLTAGE_SWING_MASK |
1378379bc100SJani Nikula 					DP_TRAIN_PRE_EMPHASIS_MASK);
1379379bc100SJani Nikula 
13808b4f2137SPankaj Bharadiya 	return translate_signal_level(intel_dp, signal_levels);
1381379bc100SJani Nikula }
1382379bc100SJani Nikula 
1383fb83f72cSVille Syrjälä static void
1384a046a0daSMatt Roper dg2_set_signal_levels(struct intel_dp *intel_dp,
1385a046a0daSMatt Roper 		      const struct intel_crtc_state *crtc_state)
1386a046a0daSMatt Roper {
1387a046a0daSMatt Roper 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1388a046a0daSMatt Roper 	int level = intel_ddi_dp_level(intel_dp);
1389a046a0daSMatt Roper 
1390a046a0daSMatt Roper 	intel_snps_phy_ddi_vswing_sequence(encoder, level);
1391a046a0daSMatt Roper }
1392a046a0daSMatt Roper 
1393a046a0daSMatt Roper static void
1394a621860aSVille Syrjälä tgl_set_signal_levels(struct intel_dp *intel_dp,
1395a621860aSVille Syrjälä 		      const struct intel_crtc_state *crtc_state)
1396379bc100SJani Nikula {
1397fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1398379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
1399379bc100SJani Nikula 
1400a621860aSVille Syrjälä 	tgl_ddi_vswing_sequence(encoder, crtc_state, level);
1401379bc100SJani Nikula }
1402379bc100SJani Nikula 
1403fb83f72cSVille Syrjälä static void
1404a621860aSVille Syrjälä icl_set_signal_levels(struct intel_dp *intel_dp,
1405a621860aSVille Syrjälä 		      const struct intel_crtc_state *crtc_state)
1406379bc100SJani Nikula {
1407fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1408379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
1409379bc100SJani Nikula 
1410a621860aSVille Syrjälä 	icl_ddi_vswing_sequence(encoder, crtc_state, level);
1411fb83f72cSVille Syrjälä }
1412fb83f72cSVille Syrjälä 
1413fb83f72cSVille Syrjälä static void
1414a621860aSVille Syrjälä bxt_set_signal_levels(struct intel_dp *intel_dp,
1415a621860aSVille Syrjälä 		      const struct intel_crtc_state *crtc_state)
1416fb83f72cSVille Syrjälä {
1417fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1418fb83f72cSVille Syrjälä 	int level = intel_ddi_dp_level(intel_dp);
1419fb83f72cSVille Syrjälä 
1420a621860aSVille Syrjälä 	bxt_ddi_vswing_sequence(encoder, crtc_state, level);
1421fb83f72cSVille Syrjälä }
1422fb83f72cSVille Syrjälä 
1423fb83f72cSVille Syrjälä static void
1424a621860aSVille Syrjälä hsw_set_signal_levels(struct intel_dp *intel_dp,
1425a621860aSVille Syrjälä 		      const struct intel_crtc_state *crtc_state)
1426fb83f72cSVille Syrjälä {
1427fb83f72cSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
1428fb83f72cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1429fb83f72cSVille Syrjälä 	int level = intel_ddi_dp_level(intel_dp);
1430fb83f72cSVille Syrjälä 	enum port port = encoder->port;
1431fb83f72cSVille Syrjälä 	u32 signal_levels;
1432fb83f72cSVille Syrjälä 
1433fb83f72cSVille Syrjälä 	signal_levels = DDI_BUF_TRANS_SELECT(level);
1434fb83f72cSVille Syrjälä 
1435fb83f72cSVille Syrjälä 	drm_dbg_kms(&dev_priv->drm, "Using signal levels %08x\n",
1436fb83f72cSVille Syrjälä 		    signal_levels);
1437fb83f72cSVille Syrjälä 
1438fb83f72cSVille Syrjälä 	intel_dp->DP &= ~DDI_BUF_EMP_MASK;
1439fb83f72cSVille Syrjälä 	intel_dp->DP |= signal_levels;
1440fb83f72cSVille Syrjälä 
144193e7e61eSLucas De Marchi 	if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv))
1442a621860aSVille Syrjälä 		skl_ddi_set_iboost(encoder, crtc_state, level);
1443379bc100SJani Nikula 
1444fb83f72cSVille Syrjälä 	intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
1445fb83f72cSVille Syrjälä 	intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
1446379bc100SJani Nikula }
1447379bc100SJani Nikula 
1448*4da27d5dSLucas De Marchi static void _icl_ddi_enable_clock(struct drm_i915_private *i915, i915_reg_t reg,
14499c6a5c35SVille Syrjälä 				  u32 clk_sel_mask, u32 clk_sel, u32 clk_off)
14509c6a5c35SVille Syrjälä {
14519c6a5c35SVille Syrjälä 	mutex_lock(&i915->dpll.lock);
14529c6a5c35SVille Syrjälä 
14539c6a5c35SVille Syrjälä 	intel_de_rmw(i915, reg, clk_sel_mask, clk_sel);
14549c6a5c35SVille Syrjälä 
14559c6a5c35SVille Syrjälä 	/*
14569c6a5c35SVille Syrjälä 	 * "This step and the step before must be
14579c6a5c35SVille Syrjälä 	 *  done with separate register writes."
14589c6a5c35SVille Syrjälä 	 */
14599c6a5c35SVille Syrjälä 	intel_de_rmw(i915, reg, clk_off, 0);
14609c6a5c35SVille Syrjälä 
14619c6a5c35SVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
14629c6a5c35SVille Syrjälä }
14639c6a5c35SVille Syrjälä 
1464*4da27d5dSLucas De Marchi static void _icl_ddi_disable_clock(struct drm_i915_private *i915, i915_reg_t reg,
14659c6a5c35SVille Syrjälä 				   u32 clk_off)
14669c6a5c35SVille Syrjälä {
14679c6a5c35SVille Syrjälä 	mutex_lock(&i915->dpll.lock);
14689c6a5c35SVille Syrjälä 
14699c6a5c35SVille Syrjälä 	intel_de_rmw(i915, reg, 0, clk_off);
14709c6a5c35SVille Syrjälä 
14719c6a5c35SVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
14729c6a5c35SVille Syrjälä }
14739c6a5c35SVille Syrjälä 
1474*4da27d5dSLucas De Marchi static bool _icl_ddi_is_clock_enabled(struct drm_i915_private *i915, i915_reg_t reg,
14750fbd8694SVille Syrjälä 				      u32 clk_off)
14760fbd8694SVille Syrjälä {
14770fbd8694SVille Syrjälä 	return !(intel_de_read(i915, reg) & clk_off);
14780fbd8694SVille Syrjälä }
14790fbd8694SVille Syrjälä 
1480351221ffSVille Syrjälä static struct intel_shared_dpll *
1481*4da27d5dSLucas De Marchi _icl_ddi_get_pll(struct drm_i915_private *i915, i915_reg_t reg,
1482351221ffSVille Syrjälä 		 u32 clk_sel_mask, u32 clk_sel_shift)
1483351221ffSVille Syrjälä {
1484351221ffSVille Syrjälä 	enum intel_dpll_id id;
1485351221ffSVille Syrjälä 
1486351221ffSVille Syrjälä 	id = (intel_de_read(i915, reg) & clk_sel_mask) >> clk_sel_shift;
1487351221ffSVille Syrjälä 
1488351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1489351221ffSVille Syrjälä }
1490351221ffSVille Syrjälä 
149140b316d4SVille Syrjälä static void adls_ddi_enable_clock(struct intel_encoder *encoder,
149240b316d4SVille Syrjälä 				  const struct intel_crtc_state *crtc_state)
149340b316d4SVille Syrjälä {
149440b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
149540b316d4SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
149640b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
149740b316d4SVille Syrjälä 
149840b316d4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
149940b316d4SVille Syrjälä 		return;
150040b316d4SVille Syrjälä 
1501*4da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, ADLS_DPCLKA_CFGCR(phy),
150240b316d4SVille Syrjälä 			      ADLS_DPCLKA_CFGCR_DDI_CLK_SEL_MASK(phy),
150340b316d4SVille Syrjälä 			      pll->info->id << ADLS_DPCLKA_CFGCR_DDI_SHIFT(phy),
150440b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
150540b316d4SVille Syrjälä }
150640b316d4SVille Syrjälä 
150740b316d4SVille Syrjälä static void adls_ddi_disable_clock(struct intel_encoder *encoder)
150840b316d4SVille Syrjälä {
150940b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
151040b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
151140b316d4SVille Syrjälä 
1512*4da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, ADLS_DPCLKA_CFGCR(phy),
151340b316d4SVille Syrjälä 			       ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
151440b316d4SVille Syrjälä }
151540b316d4SVille Syrjälä 
15160fbd8694SVille Syrjälä static bool adls_ddi_is_clock_enabled(struct intel_encoder *encoder)
15170fbd8694SVille Syrjälä {
15180fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
15190fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
15200fbd8694SVille Syrjälä 
1521*4da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, ADLS_DPCLKA_CFGCR(phy),
15220fbd8694SVille Syrjälä 					 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
15230fbd8694SVille Syrjälä }
15240fbd8694SVille Syrjälä 
1525351221ffSVille Syrjälä static struct intel_shared_dpll *adls_ddi_get_pll(struct intel_encoder *encoder)
1526351221ffSVille Syrjälä {
1527351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1528351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1529351221ffSVille Syrjälä 
1530*4da27d5dSLucas De Marchi 	return _icl_ddi_get_pll(i915, ADLS_DPCLKA_CFGCR(phy),
1531351221ffSVille Syrjälä 				ADLS_DPCLKA_CFGCR_DDI_CLK_SEL_MASK(phy),
1532351221ffSVille Syrjälä 				ADLS_DPCLKA_CFGCR_DDI_SHIFT(phy));
1533351221ffSVille Syrjälä }
1534351221ffSVille Syrjälä 
153540b316d4SVille Syrjälä static void rkl_ddi_enable_clock(struct intel_encoder *encoder,
153640b316d4SVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
153740b316d4SVille Syrjälä {
153840b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
153940b316d4SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
154040b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
154140b316d4SVille Syrjälä 
154240b316d4SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
154340b316d4SVille Syrjälä 		return;
154440b316d4SVille Syrjälä 
1545*4da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, ICL_DPCLKA_CFGCR0,
154640b316d4SVille Syrjälä 			      RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
154740b316d4SVille Syrjälä 			      RKL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
154840b316d4SVille Syrjälä 			      RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
154940b316d4SVille Syrjälä }
155040b316d4SVille Syrjälä 
155140b316d4SVille Syrjälä static void rkl_ddi_disable_clock(struct intel_encoder *encoder)
155240b316d4SVille Syrjälä {
155340b316d4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
155440b316d4SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
155540b316d4SVille Syrjälä 
1556*4da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, ICL_DPCLKA_CFGCR0,
155740b316d4SVille Syrjälä 			       RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
155840b316d4SVille Syrjälä }
155940b316d4SVille Syrjälä 
15600fbd8694SVille Syrjälä static bool rkl_ddi_is_clock_enabled(struct intel_encoder *encoder)
15610fbd8694SVille Syrjälä {
15620fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
15630fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
15640fbd8694SVille Syrjälä 
1565*4da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, ICL_DPCLKA_CFGCR0,
15660fbd8694SVille Syrjälä 					 RKL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
15670fbd8694SVille Syrjälä }
15680fbd8694SVille Syrjälä 
1569351221ffSVille Syrjälä static struct intel_shared_dpll *rkl_ddi_get_pll(struct intel_encoder *encoder)
1570351221ffSVille Syrjälä {
1571351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1572351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1573351221ffSVille Syrjälä 
1574*4da27d5dSLucas De Marchi 	return _icl_ddi_get_pll(i915, ICL_DPCLKA_CFGCR0,
1575351221ffSVille Syrjälä 				RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1576351221ffSVille Syrjälä 				RKL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy));
1577351221ffSVille Syrjälä }
1578351221ffSVille Syrjälä 
157935bb6b1aSVille Syrjälä static void dg1_ddi_enable_clock(struct intel_encoder *encoder,
158011ffe972SLucas De Marchi 				 const struct intel_crtc_state *crtc_state)
158111ffe972SLucas De Marchi {
158297a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
15839c6a5c35SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
158497a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
158511ffe972SLucas De Marchi 
158697a24a70SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1587f67a008eSVille Syrjälä 		return;
1588f67a008eSVille Syrjälä 
158911ffe972SLucas De Marchi 	/*
159011ffe972SLucas De Marchi 	 * If we fail this, something went very wrong: first 2 PLLs should be
159111ffe972SLucas De Marchi 	 * used by first 2 phys and last 2 PLLs by last phys
159211ffe972SLucas De Marchi 	 */
159397a24a70SVille Syrjälä 	if (drm_WARN_ON(&i915->drm,
159411ffe972SLucas De Marchi 			(pll->info->id < DPLL_ID_DG1_DPLL2 && phy >= PHY_C) ||
159511ffe972SLucas De Marchi 			(pll->info->id >= DPLL_ID_DG1_DPLL2 && phy < PHY_C)))
159611ffe972SLucas De Marchi 		return;
159711ffe972SLucas De Marchi 
1598*4da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, DG1_DPCLKA_CFGCR0(phy),
15997815ed88SVille Syrjälä 			      DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
16009c6a5c35SVille Syrjälä 			      DG1_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
16019c6a5c35SVille Syrjälä 			      DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
160211ffe972SLucas De Marchi }
160311ffe972SLucas De Marchi 
160435bb6b1aSVille Syrjälä static void dg1_ddi_disable_clock(struct intel_encoder *encoder)
160535bb6b1aSVille Syrjälä {
160697a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
160797a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
160835bb6b1aSVille Syrjälä 
1609*4da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, DG1_DPCLKA_CFGCR0(phy),
16109c6a5c35SVille Syrjälä 			       DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
161135bb6b1aSVille Syrjälä }
161235bb6b1aSVille Syrjälä 
16130fbd8694SVille Syrjälä static bool dg1_ddi_is_clock_enabled(struct intel_encoder *encoder)
16140fbd8694SVille Syrjälä {
16150fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
16160fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
16170fbd8694SVille Syrjälä 
1618*4da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, DG1_DPCLKA_CFGCR0(phy),
16190fbd8694SVille Syrjälä 					 DG1_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
16200fbd8694SVille Syrjälä }
16210fbd8694SVille Syrjälä 
1622351221ffSVille Syrjälä static struct intel_shared_dpll *dg1_ddi_get_pll(struct intel_encoder *encoder)
1623351221ffSVille Syrjälä {
1624351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1625351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
16263352d86dSJosé Roberto de Souza 	enum intel_dpll_id id;
16273352d86dSJosé Roberto de Souza 	u32 val;
1628351221ffSVille Syrjälä 
16293352d86dSJosé Roberto de Souza 	val = intel_de_read(i915, DG1_DPCLKA_CFGCR0(phy));
16303352d86dSJosé Roberto de Souza 	val &= DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
16313352d86dSJosé Roberto de Souza 	val >>= DG1_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy);
16323352d86dSJosé Roberto de Souza 	id = val;
16333352d86dSJosé Roberto de Souza 
16343352d86dSJosé Roberto de Souza 	/*
16353352d86dSJosé Roberto de Souza 	 * _DG1_DPCLKA0_CFGCR0 maps between DPLL 0 and 1 with one bit for phy A
16363352d86dSJosé Roberto de Souza 	 * and B while _DG1_DPCLKA1_CFGCR0 maps between DPLL 2 and 3 with one
16373352d86dSJosé Roberto de Souza 	 * bit for phy C and D.
16383352d86dSJosé Roberto de Souza 	 */
16393352d86dSJosé Roberto de Souza 	if (phy >= PHY_C)
16403352d86dSJosé Roberto de Souza 		id += DPLL_ID_DG1_DPLL2;
16413352d86dSJosé Roberto de Souza 
16423352d86dSJosé Roberto de Souza 	return intel_get_shared_dpll_by_id(i915, id);
1643351221ffSVille Syrjälä }
1644351221ffSVille Syrjälä 
164536ecb0ecSVille Syrjälä static void icl_ddi_combo_enable_clock(struct intel_encoder *encoder,
1646379bc100SJani Nikula 				       const struct intel_crtc_state *crtc_state)
1647379bc100SJani Nikula {
164897a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
16499c6a5c35SVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
165097a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1651cd803bb4SMatt Roper 
165297a24a70SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1653f67a008eSVille Syrjälä 		return;
1654f67a008eSVille Syrjälä 
1655*4da27d5dSLucas De Marchi 	_icl_ddi_enable_clock(i915, ICL_DPCLKA_CFGCR0,
165640b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
165740b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy),
165840b316d4SVille Syrjälä 			      ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1659379bc100SJani Nikula }
1660379bc100SJani Nikula 
166136ecb0ecSVille Syrjälä static void icl_ddi_combo_disable_clock(struct intel_encoder *encoder)
1662379bc100SJani Nikula {
166397a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
166497a24a70SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1665379bc100SJani Nikula 
1666*4da27d5dSLucas De Marchi 	_icl_ddi_disable_clock(i915, ICL_DPCLKA_CFGCR0,
166740b316d4SVille Syrjälä 			       ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
1668379bc100SJani Nikula }
1669379bc100SJani Nikula 
16700fbd8694SVille Syrjälä static bool icl_ddi_combo_is_clock_enabled(struct intel_encoder *encoder)
16710fbd8694SVille Syrjälä {
16720fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
16730fbd8694SVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
16740fbd8694SVille Syrjälä 
1675*4da27d5dSLucas De Marchi 	return _icl_ddi_is_clock_enabled(i915, ICL_DPCLKA_CFGCR0,
16760fbd8694SVille Syrjälä 					 ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy));
16770fbd8694SVille Syrjälä }
16780fbd8694SVille Syrjälä 
1679351221ffSVille Syrjälä struct intel_shared_dpll *icl_ddi_combo_get_pll(struct intel_encoder *encoder)
1680351221ffSVille Syrjälä {
1681351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1682351221ffSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
1683351221ffSVille Syrjälä 
1684*4da27d5dSLucas De Marchi 	return _icl_ddi_get_pll(i915, ICL_DPCLKA_CFGCR0,
1685351221ffSVille Syrjälä 				ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy),
1686351221ffSVille Syrjälä 				ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_SHIFT(phy));
1687351221ffSVille Syrjälä }
1688351221ffSVille Syrjälä 
168936ecb0ecSVille Syrjälä static void jsl_ddi_tc_enable_clock(struct intel_encoder *encoder,
1690379bc100SJani Nikula 				    const struct intel_crtc_state *crtc_state)
1691379bc100SJani Nikula {
169236ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1693379bc100SJani Nikula 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
169436ecb0ecSVille Syrjälä 	enum port port = encoder->port;
1695379bc100SJani Nikula 
169636ecb0ecSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1697379bc100SJani Nikula 		return;
1698379bc100SJani Nikula 
1699c2052d6eSJosé Roberto de Souza 	/*
170036ecb0ecSVille Syrjälä 	 * "For DDIC and DDID, program DDI_CLK_SEL to map the MG clock to the port.
170136ecb0ecSVille Syrjälä 	 *  MG does not exist, but the programming is required to ungate DDIC and DDID."
1702c2052d6eSJosé Roberto de Souza 	 */
170336ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_MG);
170436ecb0ecSVille Syrjälä 
170536ecb0ecSVille Syrjälä 	icl_ddi_combo_enable_clock(encoder, crtc_state);
1706379bc100SJani Nikula }
1707379bc100SJani Nikula 
170836ecb0ecSVille Syrjälä static void jsl_ddi_tc_disable_clock(struct intel_encoder *encoder)
1709379bc100SJani Nikula {
171036ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1711379bc100SJani Nikula 	enum port port = encoder->port;
1712379bc100SJani Nikula 
171336ecb0ecSVille Syrjälä 	icl_ddi_combo_disable_clock(encoder);
171436ecb0ecSVille Syrjälä 
171536ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
1716379bc100SJani Nikula }
171736ecb0ecSVille Syrjälä 
17180fbd8694SVille Syrjälä static bool jsl_ddi_tc_is_clock_enabled(struct intel_encoder *encoder)
17190fbd8694SVille Syrjälä {
17200fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
17210fbd8694SVille Syrjälä 	enum port port = encoder->port;
17220fbd8694SVille Syrjälä 	u32 tmp;
17230fbd8694SVille Syrjälä 
17240fbd8694SVille Syrjälä 	tmp = intel_de_read(i915, DDI_CLK_SEL(port));
17250fbd8694SVille Syrjälä 
17260fbd8694SVille Syrjälä 	if ((tmp & DDI_CLK_SEL_MASK) == DDI_CLK_SEL_NONE)
17270fbd8694SVille Syrjälä 		return false;
17280fbd8694SVille Syrjälä 
17290fbd8694SVille Syrjälä 	return icl_ddi_combo_is_clock_enabled(encoder);
17300fbd8694SVille Syrjälä }
17310fbd8694SVille Syrjälä 
173236ecb0ecSVille Syrjälä static void icl_ddi_tc_enable_clock(struct intel_encoder *encoder,
173336ecb0ecSVille Syrjälä 				    const struct intel_crtc_state *crtc_state)
173436ecb0ecSVille Syrjälä {
173536ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
173636ecb0ecSVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
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ä 	if (drm_WARN_ON(&i915->drm, !pll))
174136ecb0ecSVille Syrjälä 		return;
174236ecb0ecSVille Syrjälä 
174336ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port),
174436ecb0ecSVille Syrjälä 		       icl_pll_to_ddi_clk_sel(encoder, crtc_state));
174536ecb0ecSVille Syrjälä 
174636ecb0ecSVille Syrjälä 	mutex_lock(&i915->dpll.lock);
174736ecb0ecSVille Syrjälä 
174836ecb0ecSVille Syrjälä 	intel_de_rmw(i915, ICL_DPCLKA_CFGCR0,
174936ecb0ecSVille Syrjälä 		     ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port), 0);
175036ecb0ecSVille Syrjälä 
175136ecb0ecSVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
175236ecb0ecSVille Syrjälä }
175336ecb0ecSVille Syrjälä 
175436ecb0ecSVille Syrjälä static void icl_ddi_tc_disable_clock(struct intel_encoder *encoder)
175536ecb0ecSVille Syrjälä {
175636ecb0ecSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
175736ecb0ecSVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
175836ecb0ecSVille Syrjälä 	enum port port = encoder->port;
175936ecb0ecSVille Syrjälä 
176036ecb0ecSVille Syrjälä 	mutex_lock(&i915->dpll.lock);
176136ecb0ecSVille Syrjälä 
176236ecb0ecSVille Syrjälä 	intel_de_rmw(i915, ICL_DPCLKA_CFGCR0,
176336ecb0ecSVille Syrjälä 		     0, ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port));
176436ecb0ecSVille Syrjälä 
176536ecb0ecSVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
176636ecb0ecSVille Syrjälä 
176736ecb0ecSVille Syrjälä 	intel_de_write(i915, DDI_CLK_SEL(port), DDI_CLK_SEL_NONE);
1768379bc100SJani Nikula }
1769379bc100SJani Nikula 
17700fbd8694SVille Syrjälä static bool icl_ddi_tc_is_clock_enabled(struct intel_encoder *encoder)
17710fbd8694SVille Syrjälä {
17720fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
17730fbd8694SVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
17740fbd8694SVille Syrjälä 	enum port port = encoder->port;
17750fbd8694SVille Syrjälä 	u32 tmp;
17760fbd8694SVille Syrjälä 
17770fbd8694SVille Syrjälä 	tmp = intel_de_read(i915, DDI_CLK_SEL(port));
17780fbd8694SVille Syrjälä 
17790fbd8694SVille Syrjälä 	if ((tmp & DDI_CLK_SEL_MASK) == DDI_CLK_SEL_NONE)
17800fbd8694SVille Syrjälä 		return false;
17810fbd8694SVille Syrjälä 
17820fbd8694SVille Syrjälä 	tmp = intel_de_read(i915, ICL_DPCLKA_CFGCR0);
17830fbd8694SVille Syrjälä 
17840fbd8694SVille Syrjälä 	return !(tmp & ICL_DPCLKA_CFGCR0_TC_CLK_OFF(tc_port));
17850fbd8694SVille Syrjälä }
17860fbd8694SVille Syrjälä 
1787351221ffSVille Syrjälä static struct intel_shared_dpll *icl_ddi_tc_get_pll(struct intel_encoder *encoder)
1788351221ffSVille Syrjälä {
1789351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1790351221ffSVille Syrjälä 	enum tc_port tc_port = intel_port_to_tc(i915, encoder->port);
1791351221ffSVille Syrjälä 	enum port port = encoder->port;
1792351221ffSVille Syrjälä 	enum intel_dpll_id id;
1793351221ffSVille Syrjälä 	u32 tmp;
1794351221ffSVille Syrjälä 
1795351221ffSVille Syrjälä 	tmp = intel_de_read(i915, DDI_CLK_SEL(port));
1796351221ffSVille Syrjälä 
1797351221ffSVille Syrjälä 	switch (tmp & DDI_CLK_SEL_MASK) {
1798351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_162:
1799351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_270:
1800351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_540:
1801351221ffSVille Syrjälä 	case DDI_CLK_SEL_TBT_810:
1802351221ffSVille Syrjälä 		id = DPLL_ID_ICL_TBTPLL;
1803351221ffSVille Syrjälä 		break;
1804351221ffSVille Syrjälä 	case DDI_CLK_SEL_MG:
1805351221ffSVille Syrjälä 		id = icl_tc_port_to_pll_id(tc_port);
1806351221ffSVille Syrjälä 		break;
1807351221ffSVille Syrjälä 	default:
1808351221ffSVille Syrjälä 		MISSING_CASE(tmp);
1809351221ffSVille Syrjälä 		fallthrough;
1810351221ffSVille Syrjälä 	case DDI_CLK_SEL_NONE:
1811351221ffSVille Syrjälä 		return NULL;
1812351221ffSVille Syrjälä 	}
1813351221ffSVille Syrjälä 
1814351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1815351221ffSVille Syrjälä }
1816351221ffSVille Syrjälä 
1817351221ffSVille Syrjälä static struct intel_shared_dpll *bxt_ddi_get_pll(struct intel_encoder *encoder)
1818351221ffSVille Syrjälä {
1819351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1820351221ffSVille Syrjälä 	enum intel_dpll_id id;
1821351221ffSVille Syrjälä 
1822351221ffSVille Syrjälä 	switch (encoder->port) {
1823351221ffSVille Syrjälä 	case PORT_A:
1824351221ffSVille Syrjälä 		id = DPLL_ID_SKL_DPLL0;
1825351221ffSVille Syrjälä 		break;
1826351221ffSVille Syrjälä 	case PORT_B:
1827351221ffSVille Syrjälä 		id = DPLL_ID_SKL_DPLL1;
1828351221ffSVille Syrjälä 		break;
1829351221ffSVille Syrjälä 	case PORT_C:
1830351221ffSVille Syrjälä 		id = DPLL_ID_SKL_DPLL2;
1831351221ffSVille Syrjälä 		break;
1832351221ffSVille Syrjälä 	default:
1833351221ffSVille Syrjälä 		MISSING_CASE(encoder->port);
1834351221ffSVille Syrjälä 		return NULL;
1835351221ffSVille Syrjälä 	}
1836351221ffSVille Syrjälä 
1837351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1838351221ffSVille Syrjälä }
1839351221ffSVille Syrjälä 
184038e31f1aSVille Syrjälä static void skl_ddi_enable_clock(struct intel_encoder *encoder,
184138e31f1aSVille Syrjälä 				 const struct intel_crtc_state *crtc_state)
184238e31f1aSVille Syrjälä {
184338e31f1aSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
184438e31f1aSVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
184538e31f1aSVille Syrjälä 	enum port port = encoder->port;
184638e31f1aSVille Syrjälä 
184738e31f1aSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
184838e31f1aSVille Syrjälä 		return;
184938e31f1aSVille Syrjälä 
185038e31f1aSVille Syrjälä 	mutex_lock(&i915->dpll.lock);
185138e31f1aSVille Syrjälä 
18527815ed88SVille Syrjälä 	intel_de_rmw(i915, DPLL_CTRL2,
18537815ed88SVille Syrjälä 		     DPLL_CTRL2_DDI_CLK_OFF(port) |
18547815ed88SVille Syrjälä 		     DPLL_CTRL2_DDI_CLK_SEL_MASK(port),
18557815ed88SVille Syrjälä 		     DPLL_CTRL2_DDI_CLK_SEL(pll->info->id, port) |
185638e31f1aSVille Syrjälä 		     DPLL_CTRL2_DDI_SEL_OVERRIDE(port));
185738e31f1aSVille Syrjälä 
185838e31f1aSVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
185938e31f1aSVille Syrjälä }
186038e31f1aSVille Syrjälä 
186138e31f1aSVille Syrjälä static void skl_ddi_disable_clock(struct intel_encoder *encoder)
186238e31f1aSVille Syrjälä {
186338e31f1aSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
186438e31f1aSVille Syrjälä 	enum port port = encoder->port;
186538e31f1aSVille Syrjälä 
1866be317ca0SVille Syrjälä 	mutex_lock(&i915->dpll.lock);
1867be317ca0SVille Syrjälä 
18687815ed88SVille Syrjälä 	intel_de_rmw(i915, DPLL_CTRL2,
18697815ed88SVille Syrjälä 		     0, DPLL_CTRL2_DDI_CLK_OFF(port));
1870be317ca0SVille Syrjälä 
1871be317ca0SVille Syrjälä 	mutex_unlock(&i915->dpll.lock);
187238e31f1aSVille Syrjälä }
187338e31f1aSVille Syrjälä 
18740fbd8694SVille Syrjälä static bool skl_ddi_is_clock_enabled(struct intel_encoder *encoder)
18750fbd8694SVille Syrjälä {
18760fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
18770fbd8694SVille Syrjälä 	enum port port = encoder->port;
18780fbd8694SVille Syrjälä 
18790fbd8694SVille Syrjälä 	/*
18800fbd8694SVille Syrjälä 	 * FIXME Not sure if the override affects both
18810fbd8694SVille Syrjälä 	 * the PLL selection and the CLK_OFF bit.
18820fbd8694SVille Syrjälä 	 */
18830fbd8694SVille Syrjälä 	return !(intel_de_read(i915, DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_OFF(port));
18840fbd8694SVille Syrjälä }
18850fbd8694SVille Syrjälä 
1886351221ffSVille Syrjälä static struct intel_shared_dpll *skl_ddi_get_pll(struct intel_encoder *encoder)
1887351221ffSVille Syrjälä {
1888351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1889351221ffSVille Syrjälä 	enum port port = encoder->port;
1890351221ffSVille Syrjälä 	enum intel_dpll_id id;
1891351221ffSVille Syrjälä 	u32 tmp;
1892351221ffSVille Syrjälä 
1893351221ffSVille Syrjälä 	tmp = intel_de_read(i915, DPLL_CTRL2);
1894351221ffSVille Syrjälä 
1895351221ffSVille Syrjälä 	/*
1896351221ffSVille Syrjälä 	 * FIXME Not sure if the override affects both
1897351221ffSVille Syrjälä 	 * the PLL selection and the CLK_OFF bit.
1898351221ffSVille Syrjälä 	 */
1899351221ffSVille Syrjälä 	if ((tmp & DPLL_CTRL2_DDI_SEL_OVERRIDE(port)) == 0)
1900351221ffSVille Syrjälä 		return NULL;
1901351221ffSVille Syrjälä 
1902351221ffSVille Syrjälä 	id = (tmp & DPLL_CTRL2_DDI_CLK_SEL_MASK(port)) >>
1903351221ffSVille Syrjälä 		DPLL_CTRL2_DDI_CLK_SEL_SHIFT(port);
1904351221ffSVille Syrjälä 
1905351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1906351221ffSVille Syrjälä }
1907351221ffSVille Syrjälä 
1908d135368dSVille Syrjälä void hsw_ddi_enable_clock(struct intel_encoder *encoder,
1909d135368dSVille Syrjälä 			  const struct intel_crtc_state *crtc_state)
1910d135368dSVille Syrjälä {
1911d135368dSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1912d135368dSVille Syrjälä 	const struct intel_shared_dpll *pll = crtc_state->shared_dpll;
1913d135368dSVille Syrjälä 	enum port port = encoder->port;
1914d135368dSVille Syrjälä 
1915d135368dSVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
1916d135368dSVille Syrjälä 		return;
1917d135368dSVille Syrjälä 
1918d135368dSVille Syrjälä 	intel_de_write(i915, PORT_CLK_SEL(port), hsw_pll_to_ddi_pll_sel(pll));
1919d135368dSVille Syrjälä }
1920d135368dSVille Syrjälä 
1921d135368dSVille Syrjälä void hsw_ddi_disable_clock(struct intel_encoder *encoder)
1922d135368dSVille Syrjälä {
1923d135368dSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1924d135368dSVille Syrjälä 	enum port port = encoder->port;
1925d135368dSVille Syrjälä 
1926d135368dSVille Syrjälä 	intel_de_write(i915, PORT_CLK_SEL(port), PORT_CLK_SEL_NONE);
1927d135368dSVille Syrjälä }
1928d135368dSVille Syrjälä 
19290fbd8694SVille Syrjälä bool hsw_ddi_is_clock_enabled(struct intel_encoder *encoder)
19300fbd8694SVille Syrjälä {
19310fbd8694SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
19320fbd8694SVille Syrjälä 	enum port port = encoder->port;
19330fbd8694SVille Syrjälä 
19340fbd8694SVille Syrjälä 	return intel_de_read(i915, PORT_CLK_SEL(port)) != PORT_CLK_SEL_NONE;
19350fbd8694SVille Syrjälä }
19360fbd8694SVille Syrjälä 
1937351221ffSVille Syrjälä static struct intel_shared_dpll *hsw_ddi_get_pll(struct intel_encoder *encoder)
1938351221ffSVille Syrjälä {
1939351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1940351221ffSVille Syrjälä 	enum port port = encoder->port;
1941351221ffSVille Syrjälä 	enum intel_dpll_id id;
1942351221ffSVille Syrjälä 	u32 tmp;
1943351221ffSVille Syrjälä 
1944351221ffSVille Syrjälä 	tmp = intel_de_read(i915, PORT_CLK_SEL(port));
1945351221ffSVille Syrjälä 
1946351221ffSVille Syrjälä 	switch (tmp & PORT_CLK_SEL_MASK) {
1947351221ffSVille Syrjälä 	case PORT_CLK_SEL_WRPLL1:
1948351221ffSVille Syrjälä 		id = DPLL_ID_WRPLL1;
1949351221ffSVille Syrjälä 		break;
1950351221ffSVille Syrjälä 	case PORT_CLK_SEL_WRPLL2:
1951351221ffSVille Syrjälä 		id = DPLL_ID_WRPLL2;
1952351221ffSVille Syrjälä 		break;
1953351221ffSVille Syrjälä 	case PORT_CLK_SEL_SPLL:
1954351221ffSVille Syrjälä 		id = DPLL_ID_SPLL;
1955351221ffSVille Syrjälä 		break;
1956351221ffSVille Syrjälä 	case PORT_CLK_SEL_LCPLL_810:
1957351221ffSVille Syrjälä 		id = DPLL_ID_LCPLL_810;
1958351221ffSVille Syrjälä 		break;
1959351221ffSVille Syrjälä 	case PORT_CLK_SEL_LCPLL_1350:
1960351221ffSVille Syrjälä 		id = DPLL_ID_LCPLL_1350;
1961351221ffSVille Syrjälä 		break;
1962351221ffSVille Syrjälä 	case PORT_CLK_SEL_LCPLL_2700:
1963351221ffSVille Syrjälä 		id = DPLL_ID_LCPLL_2700;
1964351221ffSVille Syrjälä 		break;
1965351221ffSVille Syrjälä 	default:
1966351221ffSVille Syrjälä 		MISSING_CASE(tmp);
1967351221ffSVille Syrjälä 		fallthrough;
1968351221ffSVille Syrjälä 	case PORT_CLK_SEL_NONE:
1969351221ffSVille Syrjälä 		return NULL;
1970351221ffSVille Syrjälä 	}
1971351221ffSVille Syrjälä 
1972351221ffSVille Syrjälä 	return intel_get_shared_dpll_by_id(i915, id);
1973351221ffSVille Syrjälä }
1974351221ffSVille Syrjälä 
1975c133df69SVille Syrjälä void intel_ddi_enable_clock(struct intel_encoder *encoder,
1976c133df69SVille Syrjälä 			    const struct intel_crtc_state *crtc_state)
1977c133df69SVille Syrjälä {
1978c133df69SVille Syrjälä 	if (encoder->enable_clock)
1979c133df69SVille Syrjälä 		encoder->enable_clock(encoder, crtc_state);
1980c133df69SVille Syrjälä }
1981c133df69SVille Syrjälä 
1982c133df69SVille Syrjälä static void intel_ddi_disable_clock(struct intel_encoder *encoder)
1983c133df69SVille Syrjälä {
1984c133df69SVille Syrjälä 	if (encoder->disable_clock)
1985c133df69SVille Syrjälä 		encoder->disable_clock(encoder);
1986c133df69SVille Syrjälä }
1987c133df69SVille Syrjälä 
1988aaca50efSVille Syrjälä void intel_ddi_sanitize_encoder_pll_mapping(struct intel_encoder *encoder)
1989dc1ddac6SVille Syrjälä {
199097a24a70SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1991dc1ddac6SVille Syrjälä 	u32 port_mask;
1992dc1ddac6SVille Syrjälä 	bool ddi_clk_needed;
1993dc1ddac6SVille Syrjälä 
1994dc1ddac6SVille Syrjälä 	/*
1995dc1ddac6SVille Syrjälä 	 * In case of DP MST, we sanitize the primary encoder only, not the
1996dc1ddac6SVille Syrjälä 	 * virtual ones.
1997dc1ddac6SVille Syrjälä 	 */
1998dc1ddac6SVille Syrjälä 	if (encoder->type == INTEL_OUTPUT_DP_MST)
1999dc1ddac6SVille Syrjälä 		return;
2000dc1ddac6SVille Syrjälä 
2001dc1ddac6SVille Syrjälä 	if (!encoder->base.crtc && intel_encoder_is_dp(encoder)) {
2002dc1ddac6SVille Syrjälä 		u8 pipe_mask;
2003dc1ddac6SVille Syrjälä 		bool is_mst;
2004dc1ddac6SVille Syrjälä 
2005dc1ddac6SVille Syrjälä 		intel_ddi_get_encoder_pipes(encoder, &pipe_mask, &is_mst);
2006dc1ddac6SVille Syrjälä 		/*
2007dc1ddac6SVille Syrjälä 		 * In the unlikely case that BIOS enables DP in MST mode, just
2008dc1ddac6SVille Syrjälä 		 * warn since our MST HW readout is incomplete.
2009dc1ddac6SVille Syrjälä 		 */
201097a24a70SVille Syrjälä 		if (drm_WARN_ON(&i915->drm, is_mst))
2011dc1ddac6SVille Syrjälä 			return;
2012dc1ddac6SVille Syrjälä 	}
2013dc1ddac6SVille Syrjälä 
2014dc1ddac6SVille Syrjälä 	port_mask = BIT(encoder->port);
2015dc1ddac6SVille Syrjälä 	ddi_clk_needed = encoder->base.crtc;
2016dc1ddac6SVille Syrjälä 
2017dc1ddac6SVille Syrjälä 	if (encoder->type == INTEL_OUTPUT_DSI) {
2018dc1ddac6SVille Syrjälä 		struct intel_encoder *other_encoder;
2019dc1ddac6SVille Syrjälä 
2020dc1ddac6SVille Syrjälä 		port_mask = intel_dsi_encoder_ports(encoder);
2021dc1ddac6SVille Syrjälä 		/*
2022dc1ddac6SVille Syrjälä 		 * Sanity check that we haven't incorrectly registered another
2023dc1ddac6SVille Syrjälä 		 * encoder using any of the ports of this DSI encoder.
2024dc1ddac6SVille Syrjälä 		 */
202597a24a70SVille Syrjälä 		for_each_intel_encoder(&i915->drm, other_encoder) {
2026dc1ddac6SVille Syrjälä 			if (other_encoder == encoder)
2027dc1ddac6SVille Syrjälä 				continue;
2028dc1ddac6SVille Syrjälä 
202997a24a70SVille Syrjälä 			if (drm_WARN_ON(&i915->drm,
2030dc1ddac6SVille Syrjälä 					port_mask & BIT(other_encoder->port)))
2031dc1ddac6SVille Syrjälä 				return;
2032dc1ddac6SVille Syrjälä 		}
2033dc1ddac6SVille Syrjälä 		/*
2034dc1ddac6SVille Syrjälä 		 * For DSI we keep the ddi clocks gated
2035dc1ddac6SVille Syrjälä 		 * except during enable/disable sequence.
2036dc1ddac6SVille Syrjälä 		 */
2037dc1ddac6SVille Syrjälä 		ddi_clk_needed = false;
2038dc1ddac6SVille Syrjälä 	}
2039dc1ddac6SVille Syrjälä 
2040f82f2563SMatt Roper 	if (ddi_clk_needed || !encoder->is_clock_enabled ||
20410fbd8694SVille Syrjälä 	    !encoder->is_clock_enabled(encoder))
20420fbd8694SVille Syrjälä 		return;
20430fbd8694SVille Syrjälä 
20440fbd8694SVille Syrjälä 	drm_notice(&i915->drm,
20450fbd8694SVille Syrjälä 		   "[ENCODER:%d:%s] is disabled/in DSI mode with an ungated DDI clock, gate it\n",
20460fbd8694SVille Syrjälä 		   encoder->base.base.id, encoder->base.name);
20470fbd8694SVille Syrjälä 
2048dc1ddac6SVille Syrjälä 	encoder->disable_clock(encoder);
2049dc1ddac6SVille Syrjälä }
2050dc1ddac6SVille Syrjälä 
20518aaf5cbdSJosé Roberto de Souza static void
20527801f3b7SLucas De Marchi icl_program_mg_dp_mode(struct intel_digital_port *dig_port,
20533b51be4eSClinton A Taylor 		       const struct intel_crtc_state *crtc_state)
2054379bc100SJani Nikula {
20557801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
20567801f3b7SLucas De Marchi 	enum tc_port tc_port = intel_port_to_tc(dev_priv, dig_port->base.port);
20575b6a9ba9SVille Syrjälä 	enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
20583b51be4eSClinton A Taylor 	u32 ln0, ln1, pin_assignment;
20593b51be4eSClinton A Taylor 	u8 width;
2060379bc100SJani Nikula 
20615b6a9ba9SVille Syrjälä 	if (!intel_phy_is_tc(dev_priv, phy) ||
20625b6a9ba9SVille Syrjälä 	    dig_port->tc_mode == TC_PORT_TBT_ALT)
2063379bc100SJani Nikula 		return;
2064379bc100SJani Nikula 
2065005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
2066f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2067f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x0));
2068f7960e7fSJani Nikula 		ln0 = intel_de_read(dev_priv, DKL_DP_MODE(tc_port));
2069f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2070f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x1));
2071f7960e7fSJani Nikula 		ln1 = intel_de_read(dev_priv, DKL_DP_MODE(tc_port));
2072978c3e53SClinton A Taylor 	} else {
2073f7960e7fSJani Nikula 		ln0 = intel_de_read(dev_priv, MG_DP_MODE(0, tc_port));
2074f7960e7fSJani Nikula 		ln1 = intel_de_read(dev_priv, MG_DP_MODE(1, tc_port));
2075978c3e53SClinton A Taylor 	}
2076379bc100SJani Nikula 
20774f72a8eeSKhaled Almahallawy 	ln0 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
2078379bc100SJani Nikula 	ln1 &= ~(MG_DP_MODE_CFG_DP_X1_MODE | MG_DP_MODE_CFG_DP_X2_MODE);
2079379bc100SJani Nikula 
20803b51be4eSClinton A Taylor 	/* DPPATC */
20817801f3b7SLucas De Marchi 	pin_assignment = intel_tc_port_get_pin_assignment_mask(dig_port);
20823b51be4eSClinton A Taylor 	width = crtc_state->lane_count;
2083379bc100SJani Nikula 
20843b51be4eSClinton A Taylor 	switch (pin_assignment) {
20853b51be4eSClinton A Taylor 	case 0x0:
20861de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm,
20877801f3b7SLucas De Marchi 			    dig_port->tc_mode != TC_PORT_LEGACY);
20883b51be4eSClinton A Taylor 		if (width == 1) {
2089379bc100SJani Nikula 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
20903b51be4eSClinton A Taylor 		} else {
20913b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20923b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
2093379bc100SJani Nikula 		}
2094379bc100SJani Nikula 		break;
20953b51be4eSClinton A Taylor 	case 0x1:
20963b51be4eSClinton A Taylor 		if (width == 4) {
20973b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
20983b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
20993b51be4eSClinton A Taylor 		}
2100379bc100SJani Nikula 		break;
21013b51be4eSClinton A Taylor 	case 0x2:
21023b51be4eSClinton A Taylor 		if (width == 2) {
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;
21073b51be4eSClinton A Taylor 	case 0x3:
21083b51be4eSClinton A Taylor 	case 0x5:
21093b51be4eSClinton A Taylor 		if (width == 1) {
21103b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
21113b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
21123b51be4eSClinton A Taylor 		} else {
21133b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
21143b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
21153b51be4eSClinton A Taylor 		}
21163b51be4eSClinton A Taylor 		break;
21173b51be4eSClinton A Taylor 	case 0x4:
21183b51be4eSClinton A Taylor 	case 0x6:
21193b51be4eSClinton A Taylor 		if (width == 1) {
21203b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X1_MODE;
21213b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X1_MODE;
21223b51be4eSClinton A Taylor 		} else {
21233b51be4eSClinton A Taylor 			ln0 |= MG_DP_MODE_CFG_DP_X2_MODE;
21243b51be4eSClinton A Taylor 			ln1 |= MG_DP_MODE_CFG_DP_X2_MODE;
21253b51be4eSClinton A Taylor 		}
21263b51be4eSClinton A Taylor 		break;
2127379bc100SJani Nikula 	default:
21283b51be4eSClinton A Taylor 		MISSING_CASE(pin_assignment);
2129379bc100SJani Nikula 	}
2130379bc100SJani Nikula 
2131005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
2132f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2133f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x0));
2134f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_DP_MODE(tc_port), ln0);
2135f7960e7fSJani Nikula 		intel_de_write(dev_priv, HIP_INDEX_REG(tc_port),
2136f7960e7fSJani Nikula 			       HIP_INDEX_VAL(tc_port, 0x1));
2137f7960e7fSJani Nikula 		intel_de_write(dev_priv, DKL_DP_MODE(tc_port), ln1);
2138978c3e53SClinton A Taylor 	} else {
2139f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_DP_MODE(0, tc_port), ln0);
2140f7960e7fSJani Nikula 		intel_de_write(dev_priv, MG_DP_MODE(1, tc_port), ln1);
2141379bc100SJani Nikula 	}
2142978c3e53SClinton A Taylor }
2143379bc100SJani Nikula 
2144ef79fafeSVille Syrjälä static enum transcoder
2145ef79fafeSVille Syrjälä tgl_dp_tp_transcoder(const struct intel_crtc_state *crtc_state)
2146ef79fafeSVille Syrjälä {
2147ef79fafeSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST))
2148ef79fafeSVille Syrjälä 		return crtc_state->mst_master_transcoder;
2149ef79fafeSVille Syrjälä 	else
2150ef79fafeSVille Syrjälä 		return crtc_state->cpu_transcoder;
2151ef79fafeSVille Syrjälä }
2152ef79fafeSVille Syrjälä 
2153ef79fafeSVille Syrjälä i915_reg_t dp_tp_ctl_reg(struct intel_encoder *encoder,
2154ef79fafeSVille Syrjälä 			 const struct intel_crtc_state *crtc_state)
2155ef79fafeSVille Syrjälä {
2156ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2157ef79fafeSVille Syrjälä 
2158005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
2159ef79fafeSVille Syrjälä 		return TGL_DP_TP_CTL(tgl_dp_tp_transcoder(crtc_state));
2160ef79fafeSVille Syrjälä 	else
2161ef79fafeSVille Syrjälä 		return DP_TP_CTL(encoder->port);
2162ef79fafeSVille Syrjälä }
2163ef79fafeSVille Syrjälä 
2164ef79fafeSVille Syrjälä i915_reg_t dp_tp_status_reg(struct intel_encoder *encoder,
2165ef79fafeSVille Syrjälä 			    const struct intel_crtc_state *crtc_state)
2166ef79fafeSVille Syrjälä {
2167ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2168ef79fafeSVille Syrjälä 
2169005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
2170ef79fafeSVille Syrjälä 		return TGL_DP_TP_STATUS(tgl_dp_tp_transcoder(crtc_state));
2171ef79fafeSVille Syrjälä 	else
2172ef79fafeSVille Syrjälä 		return DP_TP_STATUS(encoder->port);
2173ef79fafeSVille Syrjälä }
2174ef79fafeSVille Syrjälä 
21751639406aSManasi Navare static void intel_dp_sink_set_msa_timing_par_ignore_state(struct intel_dp *intel_dp,
21761639406aSManasi Navare 							  const struct intel_crtc_state *crtc_state,
21771639406aSManasi Navare 							  bool enable)
21781639406aSManasi Navare {
21791639406aSManasi Navare 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
21801639406aSManasi Navare 
21811639406aSManasi Navare 	if (!crtc_state->vrr.enable)
21821639406aSManasi Navare 		return;
21831639406aSManasi Navare 
21841639406aSManasi Navare 	if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_DOWNSPREAD_CTRL,
21851639406aSManasi Navare 			       enable ? DP_MSA_TIMING_PAR_IGNORE_EN : 0) <= 0)
21861639406aSManasi Navare 		drm_dbg_kms(&i915->drm,
21870868b1ceSVille Syrjälä 			    "Failed to %s MSA_TIMING_PAR_IGNORE in the sink\n",
21880868b1ceSVille Syrjälä 			    enabledisable(enable));
21891639406aSManasi Navare }
21901639406aSManasi Navare 
2191379bc100SJani Nikula static void intel_dp_sink_set_fec_ready(struct intel_dp *intel_dp,
2192379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
2193379bc100SJani Nikula {
219447bdb1caSJani Nikula 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
219547bdb1caSJani Nikula 
2196379bc100SJani Nikula 	if (!crtc_state->fec_enable)
2197379bc100SJani Nikula 		return;
2198379bc100SJani Nikula 
2199379bc100SJani Nikula 	if (drm_dp_dpcd_writeb(&intel_dp->aux, DP_FEC_CONFIGURATION, DP_FEC_READY) <= 0)
220047bdb1caSJani Nikula 		drm_dbg_kms(&i915->drm,
220147bdb1caSJani Nikula 			    "Failed to set FEC_READY in the sink\n");
2202379bc100SJani Nikula }
2203379bc100SJani Nikula 
2204379bc100SJani Nikula static void intel_ddi_enable_fec(struct intel_encoder *encoder,
2205379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state)
2206379bc100SJani Nikula {
2207379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
22084444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
2209379bc100SJani Nikula 	u32 val;
2210379bc100SJani Nikula 
2211379bc100SJani Nikula 	if (!crtc_state->fec_enable)
2212379bc100SJani Nikula 		return;
2213379bc100SJani Nikula 
2214b7d02c3aSVille Syrjälä 	intel_dp = enc_to_intel_dp(encoder);
2215ef79fafeSVille Syrjälä 	val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2216379bc100SJani Nikula 	val |= DP_TP_CTL_FEC_ENABLE;
2217ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2218379bc100SJani Nikula }
2219379bc100SJani Nikula 
2220379bc100SJani Nikula static void intel_ddi_disable_fec_state(struct intel_encoder *encoder,
2221379bc100SJani Nikula 					const struct intel_crtc_state *crtc_state)
2222379bc100SJani Nikula {
2223379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
22244444df6eSLucas De Marchi 	struct intel_dp *intel_dp;
2225379bc100SJani Nikula 	u32 val;
2226379bc100SJani Nikula 
2227379bc100SJani Nikula 	if (!crtc_state->fec_enable)
2228379bc100SJani Nikula 		return;
2229379bc100SJani Nikula 
2230b7d02c3aSVille Syrjälä 	intel_dp = enc_to_intel_dp(encoder);
2231ef79fafeSVille Syrjälä 	val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2232379bc100SJani Nikula 	val &= ~DP_TP_CTL_FEC_ENABLE;
2233ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2234ef79fafeSVille Syrjälä 	intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2235379bc100SJani Nikula }
2236379bc100SJani Nikula 
22375cdf706fSVille Syrjälä static void intel_ddi_power_up_lanes(struct intel_encoder *encoder,
22385cdf706fSVille Syrjälä 				     const struct intel_crtc_state *crtc_state)
22395cdf706fSVille Syrjälä {
22405cdf706fSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
22415cdf706fSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
22425cdf706fSVille Syrjälä 	enum phy phy = intel_port_to_phy(i915, encoder->port);
22435cdf706fSVille Syrjälä 
22445cdf706fSVille Syrjälä 	if (intel_phy_is_combo(i915, phy)) {
22455cdf706fSVille Syrjälä 		bool lane_reversal =
22465cdf706fSVille Syrjälä 			dig_port->saved_port_bits & DDI_BUF_PORT_REVERSAL;
22475cdf706fSVille Syrjälä 
22485cdf706fSVille Syrjälä 		intel_combo_phy_power_up_lanes(i915, phy, false,
22495cdf706fSVille Syrjälä 					       crtc_state->lane_count,
22505cdf706fSVille Syrjälä 					       lane_reversal);
22515cdf706fSVille Syrjälä 	}
22525cdf706fSVille Syrjälä }
22535cdf706fSVille Syrjälä 
22545b616a29SJani Nikula static void intel_ddi_mso_get_config(struct intel_encoder *encoder,
22555b616a29SJani Nikula 				     struct intel_crtc_state *pipe_config)
22565b616a29SJani Nikula {
22575b616a29SJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
22585b616a29SJani Nikula 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
22595b616a29SJani Nikula 	enum pipe pipe = crtc->pipe;
22605b616a29SJani Nikula 	u32 dss1;
22615b616a29SJani Nikula 
22625b616a29SJani Nikula 	if (!HAS_MSO(i915))
22635b616a29SJani Nikula 		return;
22645b616a29SJani Nikula 
22655b616a29SJani Nikula 	dss1 = intel_de_read(i915, ICL_PIPE_DSS_CTL1(pipe));
22665b616a29SJani Nikula 
22675b616a29SJani Nikula 	pipe_config->splitter.enable = dss1 & SPLITTER_ENABLE;
22685b616a29SJani Nikula 	if (!pipe_config->splitter.enable)
22695b616a29SJani Nikula 		return;
22705b616a29SJani Nikula 
22715b616a29SJani Nikula 	/* Splitter enable is supported for pipe A only. */
22725b616a29SJani Nikula 	if (drm_WARN_ON(&i915->drm, pipe != PIPE_A)) {
22735b616a29SJani Nikula 		pipe_config->splitter.enable = false;
22745b616a29SJani Nikula 		return;
22755b616a29SJani Nikula 	}
22765b616a29SJani Nikula 
22775b616a29SJani Nikula 	switch (dss1 & SPLITTER_CONFIGURATION_MASK) {
22785b616a29SJani Nikula 	default:
22795b616a29SJani Nikula 		drm_WARN(&i915->drm, true,
22805b616a29SJani Nikula 			 "Invalid splitter configuration, dss1=0x%08x\n", dss1);
22815b616a29SJani Nikula 		fallthrough;
22825b616a29SJani Nikula 	case SPLITTER_CONFIGURATION_2_SEGMENT:
22835b616a29SJani Nikula 		pipe_config->splitter.link_count = 2;
22845b616a29SJani Nikula 		break;
22855b616a29SJani Nikula 	case SPLITTER_CONFIGURATION_4_SEGMENT:
22865b616a29SJani Nikula 		pipe_config->splitter.link_count = 4;
22875b616a29SJani Nikula 		break;
22885b616a29SJani Nikula 	}
22895b616a29SJani Nikula 
22905b616a29SJani Nikula 	pipe_config->splitter.pixel_overlap = REG_FIELD_GET(OVERLAP_PIXELS_MASK, dss1);
22915b616a29SJani Nikula }
22925b616a29SJani Nikula 
2293bc71194eSJani Nikula static void intel_ddi_mso_configure(const struct intel_crtc_state *crtc_state)
2294bc71194eSJani Nikula {
2295bc71194eSJani Nikula 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2296bc71194eSJani Nikula 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2297bc71194eSJani Nikula 	enum pipe pipe = crtc->pipe;
2298bc71194eSJani Nikula 	u32 dss1 = 0;
2299bc71194eSJani Nikula 
2300bc71194eSJani Nikula 	if (!HAS_MSO(i915))
2301bc71194eSJani Nikula 		return;
2302bc71194eSJani Nikula 
2303bc71194eSJani Nikula 	if (crtc_state->splitter.enable) {
2304bc71194eSJani Nikula 		/* Splitter enable is supported for pipe A only. */
2305bc71194eSJani Nikula 		if (drm_WARN_ON(&i915->drm, pipe != PIPE_A))
2306bc71194eSJani Nikula 			return;
2307bc71194eSJani Nikula 
2308bc71194eSJani Nikula 		dss1 |= SPLITTER_ENABLE;
2309bc71194eSJani Nikula 		dss1 |= OVERLAP_PIXELS(crtc_state->splitter.pixel_overlap);
2310bc71194eSJani Nikula 		if (crtc_state->splitter.link_count == 2)
2311bc71194eSJani Nikula 			dss1 |= SPLITTER_CONFIGURATION_2_SEGMENT;
2312bc71194eSJani Nikula 		else
2313bc71194eSJani Nikula 			dss1 |= SPLITTER_CONFIGURATION_4_SEGMENT;
2314bc71194eSJani Nikula 	}
2315bc71194eSJani Nikula 
2316bc71194eSJani Nikula 	intel_de_rmw(i915, ICL_PIPE_DSS_CTL1(pipe),
2317bc71194eSJani Nikula 		     SPLITTER_ENABLE | SPLITTER_CONFIGURATION_MASK |
2318bc71194eSJani Nikula 		     OVERLAP_PIXELS_MASK, dss1);
2319bc71194eSJani Nikula }
2320bc71194eSJani Nikula 
2321f82f2563SMatt Roper static void dg2_ddi_pre_enable_dp(struct intel_atomic_state *state,
2322f82f2563SMatt Roper 				  struct intel_encoder *encoder,
2323f82f2563SMatt Roper 				  const struct intel_crtc_state *crtc_state,
2324f82f2563SMatt Roper 				  const struct drm_connector_state *conn_state)
2325f82f2563SMatt Roper {
2326f82f2563SMatt Roper 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2327f82f2563SMatt Roper 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2328f82f2563SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2329f82f2563SMatt Roper 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2330f82f2563SMatt Roper 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2331f82f2563SMatt Roper 	int level = intel_ddi_dp_level(intel_dp);
2332f82f2563SMatt Roper 
2333f82f2563SMatt Roper 	intel_dp_set_link_params(intel_dp, crtc_state->port_clock,
2334f82f2563SMatt Roper 				 crtc_state->lane_count);
2335f82f2563SMatt Roper 
2336f82f2563SMatt Roper 	/*
2337f82f2563SMatt Roper 	 * 1. Enable Power Wells
2338f82f2563SMatt Roper 	 *
2339f82f2563SMatt Roper 	 * This was handled at the beginning of intel_atomic_commit_tail(),
2340f82f2563SMatt Roper 	 * before we called down into this function.
2341f82f2563SMatt Roper 	 */
2342f82f2563SMatt Roper 
2343f82f2563SMatt Roper 	/* 2. Enable Panel Power if PPS is required */
2344f82f2563SMatt Roper 	intel_pps_on(intel_dp);
2345f82f2563SMatt Roper 
2346f82f2563SMatt Roper 	/*
2347f82f2563SMatt Roper 	 * 3. Enable the port PLL.
2348f82f2563SMatt Roper 	 */
2349f82f2563SMatt Roper 	intel_ddi_enable_clock(encoder, crtc_state);
2350f82f2563SMatt Roper 
2351f82f2563SMatt Roper 	/* 4. Enable IO power */
2352f82f2563SMatt Roper 	if (!intel_phy_is_tc(dev_priv, phy) ||
2353f82f2563SMatt Roper 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
2354f82f2563SMatt Roper 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2355f82f2563SMatt Roper 								   dig_port->ddi_io_power_domain);
2356f82f2563SMatt Roper 
2357f82f2563SMatt Roper 	/*
2358f82f2563SMatt Roper 	 * 5. The rest of the below are substeps under the bspec's "Enable and
2359f82f2563SMatt Roper 	 * Train Display Port" step.  Note that steps that are specific to
2360f82f2563SMatt Roper 	 * MST will be handled by intel_mst_pre_enable_dp() before/after it
2361f82f2563SMatt Roper 	 * calls into this function.  Also intel_mst_pre_enable_dp() only calls
2362f82f2563SMatt Roper 	 * us when active_mst_links==0, so any steps designated for "single
2363f82f2563SMatt Roper 	 * stream or multi-stream master transcoder" can just be performed
2364f82f2563SMatt Roper 	 * unconditionally here.
2365f82f2563SMatt Roper 	 */
2366f82f2563SMatt Roper 
2367f82f2563SMatt Roper 	/*
2368f82f2563SMatt Roper 	 * 5.a Configure Transcoder Clock Select to direct the Port clock to the
2369f82f2563SMatt Roper 	 * Transcoder.
2370f82f2563SMatt Roper 	 */
2371f82f2563SMatt Roper 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
2372f82f2563SMatt Roper 
2373f82f2563SMatt Roper 	/* 5.b Not relevant to i915 for now */
2374f82f2563SMatt Roper 
2375f82f2563SMatt Roper 	/*
2376f82f2563SMatt Roper 	 * 5.c Configure TRANS_DDI_FUNC_CTL DDI Select, DDI Mode Select & MST
2377f82f2563SMatt Roper 	 * Transport Select
2378f82f2563SMatt Roper 	 */
2379f82f2563SMatt Roper 	intel_ddi_config_transcoder_func(encoder, crtc_state);
2380f82f2563SMatt Roper 
2381f82f2563SMatt Roper 	/*
2382f82f2563SMatt Roper 	 * 5.d Configure & enable DP_TP_CTL with link training pattern 1
2383f82f2563SMatt Roper 	 * selected
2384f82f2563SMatt Roper 	 *
2385f82f2563SMatt Roper 	 * This will be handled by the intel_dp_start_link_train() farther
2386f82f2563SMatt Roper 	 * down this function.
2387f82f2563SMatt Roper 	 */
2388f82f2563SMatt Roper 
2389f82f2563SMatt Roper 	/* 5.e Configure voltage swing and related IO settings */
2390f82f2563SMatt Roper 	intel_snps_phy_ddi_vswing_sequence(encoder, level);
2391f82f2563SMatt Roper 
2392f82f2563SMatt Roper 	/*
2393f82f2563SMatt Roper 	 * 5.f Configure and enable DDI_BUF_CTL
2394f82f2563SMatt Roper 	 * 5.g Wait for DDI_BUF_CTL DDI Idle Status = 0b (Not Idle), timeout
2395f82f2563SMatt Roper 	 *     after 1200 us.
2396f82f2563SMatt Roper 	 *
2397f82f2563SMatt Roper 	 * We only configure what the register value will be here.  Actual
2398f82f2563SMatt Roper 	 * enabling happens during link training farther down.
2399f82f2563SMatt Roper 	 */
2400f82f2563SMatt Roper 	intel_ddi_init_dp_buf_reg(encoder, crtc_state);
2401f82f2563SMatt Roper 
2402f82f2563SMatt Roper 	if (!is_mst)
2403f82f2563SMatt Roper 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
2404f82f2563SMatt Roper 
2405f82f2563SMatt Roper 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state, true);
2406f82f2563SMatt Roper 	/*
2407f82f2563SMatt Roper 	 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
2408f82f2563SMatt Roper 	 * in the FEC_CONFIGURATION register to 1 before initiating link
2409f82f2563SMatt Roper 	 * training
2410f82f2563SMatt Roper 	 */
2411f82f2563SMatt Roper 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
2412f82f2563SMatt Roper 
2413f82f2563SMatt Roper 	/*
2414f82f2563SMatt Roper 	 * 5.h Follow DisplayPort specification training sequence (see notes for
2415f82f2563SMatt Roper 	 *     failure handling)
2416f82f2563SMatt Roper 	 * 5.i If DisplayPort multi-stream - Set DP_TP_CTL link training to Idle
2417f82f2563SMatt Roper 	 *     Pattern, wait for 5 idle patterns (DP_TP_STATUS Min_Idles_Sent)
2418f82f2563SMatt Roper 	 *     (timeout after 800 us)
2419f82f2563SMatt Roper 	 */
2420f82f2563SMatt Roper 	intel_dp_start_link_train(intel_dp, crtc_state);
2421f82f2563SMatt Roper 
2422f82f2563SMatt Roper 	/* 5.j Set DP_TP_CTL link training to Normal */
2423f82f2563SMatt Roper 	if (!is_trans_port_sync_mode(crtc_state))
2424f82f2563SMatt Roper 		intel_dp_stop_link_train(intel_dp, crtc_state);
2425f82f2563SMatt Roper 
2426f82f2563SMatt Roper 	/* 5.k Configure and enable FEC if needed */
2427f82f2563SMatt Roper 	intel_ddi_enable_fec(encoder, crtc_state);
2428f82f2563SMatt Roper 	intel_dsc_enable(encoder, crtc_state);
2429f82f2563SMatt Roper }
2430f82f2563SMatt Roper 
2431ede9771dSVille Syrjälä static void tgl_ddi_pre_enable_dp(struct intel_atomic_state *state,
2432ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
243399389390SJosé Roberto de Souza 				  const struct intel_crtc_state *crtc_state,
243499389390SJosé Roberto de Souza 				  const struct drm_connector_state *conn_state)
243599389390SJosé Roberto de Souza {
2436b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
243799389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
243899389390SJosé Roberto de Souza 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2439b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
244099389390SJosé Roberto de Souza 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
244199389390SJosé Roberto de Souza 	int level = intel_ddi_dp_level(intel_dp);
244299389390SJosé Roberto de Souza 
2443a621860aSVille Syrjälä 	intel_dp_set_link_params(intel_dp,
2444a621860aSVille Syrjälä 				 crtc_state->port_clock,
2445a621860aSVille Syrjälä 				 crtc_state->lane_count);
244699389390SJosé Roberto de Souza 
24475e19c0b0SMatt Roper 	/*
24485e19c0b0SMatt Roper 	 * 1. Enable Power Wells
24495e19c0b0SMatt Roper 	 *
24505e19c0b0SMatt Roper 	 * This was handled at the beginning of intel_atomic_commit_tail(),
24515e19c0b0SMatt Roper 	 * before we called down into this function.
24525e19c0b0SMatt Roper 	 */
245399389390SJosé Roberto de Souza 
24545e19c0b0SMatt Roper 	/* 2. Enable Panel Power if PPS is required */
2455eb46f498SJani Nikula 	intel_pps_on(intel_dp);
245699389390SJosé Roberto de Souza 
245799389390SJosé Roberto de Souza 	/*
24585e19c0b0SMatt Roper 	 * 3. For non-TBT Type-C ports, set FIA lane count
24595e19c0b0SMatt Roper 	 * (DFLEXDPSP.DPX4TXLATC)
24605e19c0b0SMatt Roper 	 *
24615e19c0b0SMatt Roper 	 * This was done before tgl_ddi_pre_enable_dp by
24621e98f88cSLucas De Marchi 	 * hsw_crtc_enable()->intel_encoders_pre_pll_enable().
246399389390SJosé Roberto de Souza 	 */
246499389390SJosé Roberto de Souza 
24655e19c0b0SMatt Roper 	/*
24665e19c0b0SMatt Roper 	 * 4. Enable the port PLL.
24675e19c0b0SMatt Roper 	 *
24685e19c0b0SMatt Roper 	 * The PLL enabling itself was already done before this function by
24691e98f88cSLucas De Marchi 	 * hsw_crtc_enable()->intel_enable_shared_dpll().  We need only
24705e19c0b0SMatt Roper 	 * configure the PLL to port mapping here.
24715e19c0b0SMatt Roper 	 */
2472c133df69SVille Syrjälä 	intel_ddi_enable_clock(encoder, crtc_state);
24736171e58bSClinton A Taylor 
24745e19c0b0SMatt Roper 	/* 5. If IO power is controlled through PWR_WELL_CTL, Enable IO Power */
247599389390SJosé Roberto de Souza 	if (!intel_phy_is_tc(dev_priv, phy) ||
2476a4550977SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT) {
2477a4550977SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2478a4550977SImre Deak 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
247999389390SJosé Roberto de Souza 								   dig_port->ddi_io_power_domain);
2480a4550977SImre Deak 	}
248199389390SJosé Roberto de Souza 
24825e19c0b0SMatt Roper 	/* 6. Program DP_MODE */
24833b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
248499389390SJosé Roberto de Souza 
248599389390SJosé Roberto de Souza 	/*
24865e19c0b0SMatt Roper 	 * 7. The rest of the below are substeps under the bspec's "Enable and
24875e19c0b0SMatt Roper 	 * Train Display Port" step.  Note that steps that are specific to
24885e19c0b0SMatt Roper 	 * MST will be handled by intel_mst_pre_enable_dp() before/after it
24895e19c0b0SMatt Roper 	 * calls into this function.  Also intel_mst_pre_enable_dp() only calls
24905e19c0b0SMatt Roper 	 * us when active_mst_links==0, so any steps designated for "single
24915e19c0b0SMatt Roper 	 * stream or multi-stream master transcoder" can just be performed
24925e19c0b0SMatt Roper 	 * unconditionally here.
24935e19c0b0SMatt Roper 	 */
24945e19c0b0SMatt Roper 
24955e19c0b0SMatt Roper 	/*
24965e19c0b0SMatt Roper 	 * 7.a Configure Transcoder Clock Select to direct the Port clock to the
24975e19c0b0SMatt Roper 	 * Transcoder.
249899389390SJosé Roberto de Souza 	 */
249902a715c3SVille Syrjälä 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
250099389390SJosé Roberto de Souza 
25015e19c0b0SMatt Roper 	/*
25025e19c0b0SMatt Roper 	 * 7.b Configure TRANS_DDI_FUNC_CTL DDI Select, DDI Mode Select & MST
25035e19c0b0SMatt Roper 	 * Transport Select
25045e19c0b0SMatt Roper 	 */
2505eed22a46SVille Syrjälä 	intel_ddi_config_transcoder_func(encoder, crtc_state);
250699389390SJosé Roberto de Souza 
25075e19c0b0SMatt Roper 	/*
25085e19c0b0SMatt Roper 	 * 7.c Configure & enable DP_TP_CTL with link training pattern 1
25095e19c0b0SMatt Roper 	 * selected
25105e19c0b0SMatt Roper 	 *
25115e19c0b0SMatt Roper 	 * This will be handled by the intel_dp_start_link_train() farther
25125e19c0b0SMatt Roper 	 * down this function.
25135e19c0b0SMatt Roper 	 */
25145e19c0b0SMatt Roper 
25155e19c0b0SMatt Roper 	/* 7.e Configure voltage swing and related IO settings */
2516a621860aSVille Syrjälä 	tgl_ddi_vswing_sequence(encoder, crtc_state, level);
251799389390SJosé Roberto de Souza 
25185e19c0b0SMatt Roper 	/*
25195e19c0b0SMatt Roper 	 * 7.f Combo PHY: Configure PORT_CL_DW10 Static Power Down to power up
25205e19c0b0SMatt Roper 	 * the used lanes of the DDI.
25215e19c0b0SMatt Roper 	 */
25225cdf706fSVille Syrjälä 	intel_ddi_power_up_lanes(encoder, crtc_state);
252399389390SJosé Roberto de Souza 
25245e19c0b0SMatt Roper 	/*
2525bc71194eSJani Nikula 	 * 7.g Program CoG/MSO configuration bits in DSS_CTL1 if selected.
2526bc71194eSJani Nikula 	 */
2527bc71194eSJani Nikula 	intel_ddi_mso_configure(crtc_state);
2528bc71194eSJani Nikula 
2529bc71194eSJani Nikula 	/*
25305e19c0b0SMatt Roper 	 * 7.g Configure and enable DDI_BUF_CTL
25315e19c0b0SMatt Roper 	 * 7.h Wait for DDI_BUF_CTL DDI Idle Status = 0b (Not Idle), timeout
25325e19c0b0SMatt Roper 	 *     after 500 us.
25335e19c0b0SMatt Roper 	 *
25345e19c0b0SMatt Roper 	 * We only configure what the register value will be here.  Actual
25355e19c0b0SMatt Roper 	 * enabling happens during link training farther down.
25365e19c0b0SMatt Roper 	 */
2537a621860aSVille Syrjälä 	intel_ddi_init_dp_buf_reg(encoder, crtc_state);
253899389390SJosé Roberto de Souza 
253999389390SJosé Roberto de Souza 	if (!is_mst)
25400e634efdSVille Syrjälä 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
254199389390SJosé Roberto de Souza 
2542522508b6SAnkit Nautiyal 	intel_dp_configure_protocol_converter(intel_dp, crtc_state);
254399389390SJosé Roberto de Souza 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state, true);
254499389390SJosé Roberto de Souza 	/*
254599389390SJosé Roberto de Souza 	 * DDI FEC: "anticipates enabling FEC encoding sets the FEC_READY bit
254699389390SJosé Roberto de Souza 	 * in the FEC_CONFIGURATION register to 1 before initiating link
254799389390SJosé Roberto de Souza 	 * training
254899389390SJosé Roberto de Souza 	 */
254999389390SJosé Roberto de Souza 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
25505e19c0b0SMatt Roper 
25514f3dd47aSAnkit Nautiyal 	intel_dp_check_frl_training(intel_dp);
255210fec80bSAnkit Nautiyal 	intel_dp_pcon_dsc_configure(intel_dp, crtc_state);
25534f3dd47aSAnkit Nautiyal 
25545e19c0b0SMatt Roper 	/*
25555e19c0b0SMatt Roper 	 * 7.i Follow DisplayPort specification training sequence (see notes for
25565e19c0b0SMatt Roper 	 *     failure handling)
25575e19c0b0SMatt Roper 	 * 7.j If DisplayPort multi-stream - Set DP_TP_CTL link training to Idle
25585e19c0b0SMatt Roper 	 *     Pattern, wait for 5 idle patterns (DP_TP_STATUS Min_Idles_Sent)
25595e19c0b0SMatt Roper 	 *     (timeout after 800 us)
25605e19c0b0SMatt Roper 	 */
2561a621860aSVille Syrjälä 	intel_dp_start_link_train(intel_dp, crtc_state);
256299389390SJosé Roberto de Souza 
25635e19c0b0SMatt Roper 	/* 7.k Set DP_TP_CTL link training to Normal */
2564eadf6f91SManasi Navare 	if (!is_trans_port_sync_mode(crtc_state))
2565a621860aSVille Syrjälä 		intel_dp_stop_link_train(intel_dp, crtc_state);
256699389390SJosé Roberto de Souza 
25675e19c0b0SMatt Roper 	/* 7.l Configure and enable FEC if needed */
256899389390SJosé Roberto de Souza 	intel_ddi_enable_fec(encoder, crtc_state);
25694e3cdb45SManasi Navare 	if (!crtc_state->bigjoiner)
257099389390SJosé Roberto de Souza 		intel_dsc_enable(encoder, crtc_state);
257199389390SJosé Roberto de Souza }
257299389390SJosé Roberto de Souza 
2573ede9771dSVille Syrjälä static void hsw_ddi_pre_enable_dp(struct intel_atomic_state *state,
2574ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
2575379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
2576379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
2577379bc100SJani Nikula {
2578b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2579379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2580379bc100SJani Nikula 	enum port port = encoder->port;
2581dc867bc7SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
2582b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2583379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST);
2584379bc100SJani Nikula 	int level = intel_ddi_dp_level(intel_dp);
2585379bc100SJani Nikula 
2586005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 11)
25871de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm,
25881de143ccSPankaj Bharadiya 			    is_mst && (port == PORT_A || port == PORT_E));
2589542dfab5SJosé Roberto de Souza 	else
25901de143ccSPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm, is_mst && port == PORT_A);
2591379bc100SJani Nikula 
2592a621860aSVille Syrjälä 	intel_dp_set_link_params(intel_dp,
2593a621860aSVille Syrjälä 				 crtc_state->port_clock,
2594a621860aSVille Syrjälä 				 crtc_state->lane_count);
2595379bc100SJani Nikula 
2596eb46f498SJani Nikula 	intel_pps_on(intel_dp);
2597379bc100SJani Nikula 
2598c133df69SVille Syrjälä 	intel_ddi_enable_clock(encoder, crtc_state);
2599379bc100SJani Nikula 
2600d8fe2ab6SMatt Roper 	if (!intel_phy_is_tc(dev_priv, phy) ||
2601a4550977SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT) {
2602a4550977SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2603a4550977SImre Deak 		dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
26043b2ed431SImre Deak 								   dig_port->ddi_io_power_domain);
2605a4550977SImre Deak 	}
2606379bc100SJani Nikula 
26073b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
2608379bc100SJani Nikula 
2609005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
2610a621860aSVille Syrjälä 		icl_ddi_vswing_sequence(encoder, crtc_state, level);
26112446e1d6SMatt Roper 	else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
2612a621860aSVille Syrjälä 		bxt_ddi_vswing_sequence(encoder, crtc_state, level);
2613379bc100SJani Nikula 	else
2614266152aeSVille Syrjälä 		hsw_prepare_dp_ddi_buffers(encoder, crtc_state);
2615379bc100SJani Nikula 
26165cdf706fSVille Syrjälä 	intel_ddi_power_up_lanes(encoder, crtc_state);
2617379bc100SJani Nikula 
2618a621860aSVille Syrjälä 	intel_ddi_init_dp_buf_reg(encoder, crtc_state);
2619379bc100SJani Nikula 	if (!is_mst)
26200e634efdSVille Syrjälä 		intel_dp_set_power(intel_dp, DP_SET_POWER_D0);
2621522508b6SAnkit Nautiyal 	intel_dp_configure_protocol_converter(intel_dp, crtc_state);
2622379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, crtc_state,
2623379bc100SJani Nikula 					      true);
2624379bc100SJani Nikula 	intel_dp_sink_set_fec_ready(intel_dp, crtc_state);
2625a621860aSVille Syrjälä 	intel_dp_start_link_train(intel_dp, crtc_state);
2626005e9537SMatt Roper 	if ((port != PORT_A || DISPLAY_VER(dev_priv) >= 9) &&
2627eadf6f91SManasi Navare 	    !is_trans_port_sync_mode(crtc_state))
2628a621860aSVille Syrjälä 		intel_dp_stop_link_train(intel_dp, crtc_state);
2629379bc100SJani Nikula 
2630379bc100SJani Nikula 	intel_ddi_enable_fec(encoder, crtc_state);
2631379bc100SJani Nikula 
2632379bc100SJani Nikula 	if (!is_mst)
263302a715c3SVille Syrjälä 		intel_ddi_enable_pipe_clock(encoder, crtc_state);
2634379bc100SJani Nikula 
26354e3cdb45SManasi Navare 	if (!crtc_state->bigjoiner)
2636379bc100SJani Nikula 		intel_dsc_enable(encoder, crtc_state);
2637379bc100SJani Nikula }
2638379bc100SJani Nikula 
2639ede9771dSVille Syrjälä static void intel_ddi_pre_enable_dp(struct intel_atomic_state *state,
2640ede9771dSVille Syrjälä 				    struct intel_encoder *encoder,
264199389390SJosé Roberto de Souza 				    const struct intel_crtc_state *crtc_state,
264299389390SJosé Roberto de Souza 				    const struct drm_connector_state *conn_state)
264399389390SJosé Roberto de Souza {
264499389390SJosé Roberto de Souza 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
264599389390SJosé Roberto de Souza 
2646f82f2563SMatt Roper 	if (IS_DG2(dev_priv))
2647f82f2563SMatt Roper 		dg2_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
2648f82f2563SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 12)
2649ede9771dSVille Syrjälä 		tgl_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
265099389390SJosé Roberto de Souza 	else
2651ede9771dSVille Syrjälä 		hsw_ddi_pre_enable_dp(state, encoder, crtc_state, conn_state);
26520c06fa15SGwan-gyeong Mun 
2653bd8c9ccaSGwan-gyeong Mun 	/* MST will call a setting of MSA after an allocating of Virtual Channel
2654bd8c9ccaSGwan-gyeong Mun 	 * from MST encoder pre_enable callback.
2655bd8c9ccaSGwan-gyeong Mun 	 */
26561fc1e8d4SJosé Roberto de Souza 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
26570c06fa15SGwan-gyeong Mun 		intel_ddi_set_dp_msa(crtc_state, conn_state);
26581c9d2eb2SJani Nikula 
26591c9d2eb2SJani Nikula 		intel_dp_set_m_n(crtc_state, M1_N1);
266099389390SJosé Roberto de Souza 	}
26611fc1e8d4SJosé Roberto de Souza }
266299389390SJosé Roberto de Souza 
2663ede9771dSVille Syrjälä static void intel_ddi_pre_enable_hdmi(struct intel_atomic_state *state,
2664ede9771dSVille Syrjälä 				      struct intel_encoder *encoder,
2665379bc100SJani Nikula 				      const struct intel_crtc_state *crtc_state,
2666379bc100SJani Nikula 				      const struct drm_connector_state *conn_state)
2667379bc100SJani Nikula {
26680ba7ffeaSLucas De Marchi 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
26690ba7ffeaSLucas De Marchi 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
2670379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2671379bc100SJani Nikula 
2672379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, true);
2673c133df69SVille Syrjälä 	intel_ddi_enable_clock(encoder, crtc_state);
2674379bc100SJani Nikula 
2675a4550977SImre Deak 	drm_WARN_ON(&dev_priv->drm, dig_port->ddi_io_wakeref);
2676a4550977SImre Deak 	dig_port->ddi_io_wakeref = intel_display_power_get(dev_priv,
2677a4550977SImre Deak 							   dig_port->ddi_io_power_domain);
2678379bc100SJani Nikula 
26793b51be4eSClinton A Taylor 	icl_program_mg_dp_mode(dig_port, crtc_state);
2680379bc100SJani Nikula 
268102a715c3SVille Syrjälä 	intel_ddi_enable_pipe_clock(encoder, crtc_state);
2682379bc100SJani Nikula 
26830ba7ffeaSLucas De Marchi 	dig_port->set_infoframes(encoder,
2684379bc100SJani Nikula 				 crtc_state->has_infoframe,
2685379bc100SJani Nikula 				 crtc_state, conn_state);
2686379bc100SJani Nikula }
2687379bc100SJani Nikula 
2688ede9771dSVille Syrjälä static void intel_ddi_pre_enable(struct intel_atomic_state *state,
2689ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
2690379bc100SJani Nikula 				 const struct intel_crtc_state *crtc_state,
2691379bc100SJani Nikula 				 const struct drm_connector_state *conn_state)
2692379bc100SJani Nikula {
26932225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2694379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2695379bc100SJani Nikula 	enum pipe pipe = crtc->pipe;
2696379bc100SJani Nikula 
2697379bc100SJani Nikula 	/*
2698379bc100SJani Nikula 	 * When called from DP MST code:
2699379bc100SJani Nikula 	 * - conn_state will be NULL
2700379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
2701379bc100SJani Nikula 	 * - the main connector associated with this port
2702379bc100SJani Nikula 	 *   won't be active or linked to a crtc
2703379bc100SJani Nikula 	 * - crtc_state will be the state of the first stream to
2704379bc100SJani Nikula 	 *   be activated on this port, and it may not be the same
2705379bc100SJani Nikula 	 *   stream that will be deactivated last, but each stream
2706379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
2707379bc100SJani Nikula 	 *   the DP link parameteres
2708379bc100SJani Nikula 	 */
2709379bc100SJani Nikula 
27101de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, crtc_state->has_pch_encoder);
2711379bc100SJani Nikula 
2712379bc100SJani Nikula 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
2713379bc100SJani Nikula 
2714379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
2715ede9771dSVille Syrjälä 		intel_ddi_pre_enable_hdmi(state, encoder, crtc_state,
2716ede9771dSVille Syrjälä 					  conn_state);
2717379bc100SJani Nikula 	} else {
2718f7af425dSVille Syrjälä 		struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2719379bc100SJani Nikula 
2720ede9771dSVille Syrjälä 		intel_ddi_pre_enable_dp(state, encoder, crtc_state,
2721ede9771dSVille Syrjälä 					conn_state);
2722379bc100SJani Nikula 
2723f7af425dSVille Syrjälä 		/* FIXME precompute everything properly */
27240ea02bb8SJosé Roberto de Souza 		/* FIXME how do we turn infoframes off again? */
2725f7af425dSVille Syrjälä 		if (dig_port->lspcon.active && dig_port->dp.has_hdmi_sink)
2726379bc100SJani Nikula 			dig_port->set_infoframes(encoder,
2727379bc100SJani Nikula 						 crtc_state->has_infoframe,
2728379bc100SJani Nikula 						 crtc_state, conn_state);
2729379bc100SJani Nikula 	}
2730379bc100SJani Nikula }
2731379bc100SJani Nikula 
2732379bc100SJani Nikula static void intel_disable_ddi_buf(struct intel_encoder *encoder,
2733379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
2734379bc100SJani Nikula {
2735379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2736379bc100SJani Nikula 	enum port port = encoder->port;
2737379bc100SJani Nikula 	bool wait = false;
2738379bc100SJani Nikula 	u32 val;
2739379bc100SJani Nikula 
2740f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, DDI_BUF_CTL(port));
2741379bc100SJani Nikula 	if (val & DDI_BUF_CTL_ENABLE) {
2742379bc100SJani Nikula 		val &= ~DDI_BUF_CTL_ENABLE;
2743f7960e7fSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_CTL(port), val);
2744379bc100SJani Nikula 		wait = true;
2745379bc100SJani Nikula 	}
2746379bc100SJani Nikula 
2747e468ff06SLucas De Marchi 	if (intel_crtc_has_dp_encoder(crtc_state)) {
2748ef79fafeSVille Syrjälä 		val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
2749379bc100SJani Nikula 		val &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
2750379bc100SJani Nikula 		val |= DP_TP_CTL_LINK_TRAIN_PAT1;
2751ef79fafeSVille Syrjälä 		intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
2752e468ff06SLucas De Marchi 	}
2753379bc100SJani Nikula 
2754379bc100SJani Nikula 	/* Disable FEC in DP Sink */
2755379bc100SJani Nikula 	intel_ddi_disable_fec_state(encoder, crtc_state);
2756379bc100SJani Nikula 
2757379bc100SJani Nikula 	if (wait)
2758379bc100SJani Nikula 		intel_wait_ddi_buf_idle(dev_priv, port);
2759379bc100SJani Nikula }
2760379bc100SJani Nikula 
2761ede9771dSVille Syrjälä static void intel_ddi_post_disable_dp(struct intel_atomic_state *state,
2762ede9771dSVille Syrjälä 				      struct intel_encoder *encoder,
2763379bc100SJani Nikula 				      const struct intel_crtc_state *old_crtc_state,
2764379bc100SJani Nikula 				      const struct drm_connector_state *old_conn_state)
2765379bc100SJani Nikula {
2766379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2767b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2768379bc100SJani Nikula 	struct intel_dp *intel_dp = &dig_port->dp;
2769379bc100SJani Nikula 	bool is_mst = intel_crtc_has_type(old_crtc_state,
2770379bc100SJani Nikula 					  INTEL_OUTPUT_DP_MST);
2771d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
2772379bc100SJani Nikula 
2773c980216dSImre Deak 	if (!is_mst)
2774c980216dSImre Deak 		intel_dp_set_infoframes(encoder, false,
2775c980216dSImre Deak 					old_crtc_state, old_conn_state);
2776fa37a213SGwan-gyeong Mun 
2777379bc100SJani Nikula 	/*
2778379bc100SJani Nikula 	 * Power down sink before disabling the port, otherwise we end
2779379bc100SJani Nikula 	 * up getting interrupts from the sink on detecting link loss.
2780379bc100SJani Nikula 	 */
27810e634efdSVille Syrjälä 	intel_dp_set_power(intel_dp, DP_SET_POWER_D3);
278278eaaba3SJosé Roberto de Souza 
2783005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
2784c59053dcSJosé Roberto de Souza 		if (is_mst) {
2785c59053dcSJosé Roberto de Souza 			enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
2786c59053dcSJosé Roberto de Souza 			u32 val;
2787c59053dcSJosé Roberto de Souza 
2788f7960e7fSJani Nikula 			val = intel_de_read(dev_priv,
2789f7960e7fSJani Nikula 					    TRANS_DDI_FUNC_CTL(cpu_transcoder));
2790919e4f07SJosé Roberto de Souza 			val &= ~(TGL_TRANS_DDI_PORT_MASK |
2791919e4f07SJosé Roberto de Souza 				 TRANS_DDI_MODE_SELECT_MASK);
2792f7960e7fSJani Nikula 			intel_de_write(dev_priv,
2793f7960e7fSJani Nikula 				       TRANS_DDI_FUNC_CTL(cpu_transcoder),
2794f7960e7fSJani Nikula 				       val);
2795c59053dcSJosé Roberto de Souza 		}
2796c59053dcSJosé Roberto de Souza 	} else {
2797c59053dcSJosé Roberto de Souza 		if (!is_mst)
279850a7efb2SJosé Roberto de Souza 			intel_ddi_disable_pipe_clock(old_crtc_state);
2799c59053dcSJosé Roberto de Souza 	}
2800379bc100SJani Nikula 
2801379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
2802379bc100SJani Nikula 
28033ca8f191SJosé Roberto de Souza 	/*
28043ca8f191SJosé Roberto de Souza 	 * From TGL spec: "If single stream or multi-stream master transcoder:
28053ca8f191SJosé Roberto de Souza 	 * Configure Transcoder Clock select to direct no clock to the
28063ca8f191SJosé Roberto de Souza 	 * transcoder"
28073ca8f191SJosé Roberto de Souza 	 */
2808005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12)
28093ca8f191SJosé Roberto de Souza 		intel_ddi_disable_pipe_clock(old_crtc_state);
28103ca8f191SJosé Roberto de Souza 
2811eb46f498SJani Nikula 	intel_pps_vdd_on(intel_dp);
2812eb46f498SJani Nikula 	intel_pps_off(intel_dp);
2813379bc100SJani Nikula 
2814d8fe2ab6SMatt Roper 	if (!intel_phy_is_tc(dev_priv, phy) ||
28153b2ed431SImre Deak 	    dig_port->tc_mode != TC_PORT_TBT_ALT)
2816a4550977SImre Deak 		intel_display_power_put(dev_priv,
2817a4550977SImre Deak 					dig_port->ddi_io_power_domain,
2818a4550977SImre Deak 					fetch_and_zero(&dig_port->ddi_io_wakeref));
2819379bc100SJani Nikula 
2820c133df69SVille Syrjälä 	intel_ddi_disable_clock(encoder);
2821379bc100SJani Nikula }
2822379bc100SJani Nikula 
2823ede9771dSVille Syrjälä static void intel_ddi_post_disable_hdmi(struct intel_atomic_state *state,
2824ede9771dSVille Syrjälä 					struct intel_encoder *encoder,
2825379bc100SJani Nikula 					const struct intel_crtc_state *old_crtc_state,
2826379bc100SJani Nikula 					const struct drm_connector_state *old_conn_state)
2827379bc100SJani Nikula {
2828379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2829b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2830379bc100SJani Nikula 	struct intel_hdmi *intel_hdmi = &dig_port->hdmi;
2831379bc100SJani Nikula 
2832379bc100SJani Nikula 	dig_port->set_infoframes(encoder, false,
2833379bc100SJani Nikula 				 old_crtc_state, old_conn_state);
2834379bc100SJani Nikula 
2835379bc100SJani Nikula 	intel_ddi_disable_pipe_clock(old_crtc_state);
2836379bc100SJani Nikula 
2837379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
2838379bc100SJani Nikula 
2839a4550977SImre Deak 	intel_display_power_put(dev_priv,
2840a4550977SImre Deak 				dig_port->ddi_io_power_domain,
2841a4550977SImre Deak 				fetch_and_zero(&dig_port->ddi_io_wakeref));
2842379bc100SJani Nikula 
2843c133df69SVille Syrjälä 	intel_ddi_disable_clock(encoder);
2844379bc100SJani Nikula 
2845379bc100SJani Nikula 	intel_dp_dual_mode_set_tmds_output(intel_hdmi, false);
2846379bc100SJani Nikula }
2847379bc100SJani Nikula 
2848ede9771dSVille Syrjälä static void intel_ddi_post_disable(struct intel_atomic_state *state,
2849ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
2850379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
2851379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
2852379bc100SJani Nikula {
2853379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2854b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
285517bef9baSVille Syrjälä 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
285617bef9baSVille Syrjälä 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
2857379bc100SJani Nikula 
28587829c92bSVille Syrjälä 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DP_MST)) {
2859773b4b54SVille Syrjälä 		intel_crtc_vblank_off(old_crtc_state);
2860773b4b54SVille Syrjälä 
2861773b4b54SVille Syrjälä 		intel_disable_pipe(old_crtc_state);
2862773b4b54SVille Syrjälä 
2863f0651232SManasi Navare 		intel_vrr_disable(old_crtc_state);
2864f0651232SManasi Navare 
2865773b4b54SVille Syrjälä 		intel_ddi_disable_transcoder_func(old_crtc_state);
2866773b4b54SVille Syrjälä 
2867773b4b54SVille Syrjälä 		intel_dsc_disable(old_crtc_state);
2868773b4b54SVille Syrjälä 
2869005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 9)
2870f6df4d46SLucas De Marchi 			skl_scaler_disable(old_crtc_state);
2871773b4b54SVille Syrjälä 		else
28729eae5e27SLucas De Marchi 			ilk_pfit_disable(old_crtc_state);
28737829c92bSVille Syrjälä 	}
2874773b4b54SVille Syrjälä 
28754e3cdb45SManasi Navare 	if (old_crtc_state->bigjoiner_linked_crtc) {
28764e3cdb45SManasi Navare 		struct intel_atomic_state *state =
28774e3cdb45SManasi Navare 			to_intel_atomic_state(old_crtc_state->uapi.state);
28784e3cdb45SManasi Navare 		struct intel_crtc *slave =
28794e3cdb45SManasi Navare 			old_crtc_state->bigjoiner_linked_crtc;
28804e3cdb45SManasi Navare 		const struct intel_crtc_state *old_slave_crtc_state =
28814e3cdb45SManasi Navare 			intel_atomic_get_old_crtc_state(state, slave);
28824e3cdb45SManasi Navare 
28834e3cdb45SManasi Navare 		intel_crtc_vblank_off(old_slave_crtc_state);
28844e3cdb45SManasi Navare 
28854e3cdb45SManasi Navare 		intel_dsc_disable(old_slave_crtc_state);
28864e3cdb45SManasi Navare 		skl_scaler_disable(old_slave_crtc_state);
28874e3cdb45SManasi Navare 	}
28884e3cdb45SManasi Navare 
2889379bc100SJani Nikula 	/*
2890379bc100SJani Nikula 	 * When called from DP MST code:
2891379bc100SJani Nikula 	 * - old_conn_state will be NULL
2892379bc100SJani Nikula 	 * - encoder will be the main encoder (ie. mst->primary)
2893379bc100SJani Nikula 	 * - the main connector associated with this port
2894379bc100SJani Nikula 	 *   won't be active or linked to a crtc
2895379bc100SJani Nikula 	 * - old_crtc_state will be the state of the last stream to
2896379bc100SJani Nikula 	 *   be deactivated on this port, and it may not be the same
2897379bc100SJani Nikula 	 *   stream that was activated last, but each stream
2898379bc100SJani Nikula 	 *   should have a state that is identical when it comes to
2899379bc100SJani Nikula 	 *   the DP link parameteres
2900379bc100SJani Nikula 	 */
2901379bc100SJani Nikula 
2902379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
2903ede9771dSVille Syrjälä 		intel_ddi_post_disable_hdmi(state, encoder, old_crtc_state,
2904ede9771dSVille Syrjälä 					    old_conn_state);
2905379bc100SJani Nikula 	else
2906ede9771dSVille Syrjälä 		intel_ddi_post_disable_dp(state, encoder, old_crtc_state,
2907ede9771dSVille Syrjälä 					  old_conn_state);
2908379bc100SJani Nikula 
290917bef9baSVille Syrjälä 	if (intel_crtc_has_dp_encoder(old_crtc_state) || is_tc_port)
2910162e68e1SImre Deak 		intel_display_power_put(dev_priv,
2911162e68e1SImre Deak 					intel_ddi_main_link_aux_domain(dig_port),
2912162e68e1SImre Deak 					fetch_and_zero(&dig_port->aux_wakeref));
291317bef9baSVille Syrjälä 
291417bef9baSVille Syrjälä 	if (is_tc_port)
291517bef9baSVille Syrjälä 		intel_tc_port_put_link(dig_port);
2916379bc100SJani Nikula }
2917379bc100SJani Nikula 
2918ede9771dSVille Syrjälä void intel_ddi_fdi_post_disable(struct intel_atomic_state *state,
2919ede9771dSVille Syrjälä 				struct intel_encoder *encoder,
2920379bc100SJani Nikula 				const struct intel_crtc_state *old_crtc_state,
2921379bc100SJani Nikula 				const struct drm_connector_state *old_conn_state)
2922379bc100SJani Nikula {
2923379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2924379bc100SJani Nikula 	u32 val;
2925379bc100SJani Nikula 
2926379bc100SJani Nikula 	/*
2927379bc100SJani Nikula 	 * Bspec lists this as both step 13 (before DDI_BUF_CTL disable)
2928379bc100SJani Nikula 	 * and step 18 (after clearing PORT_CLK_SEL). Based on a BUN,
2929379bc100SJani Nikula 	 * step 13 is the correct place for it. Step 18 is where it was
2930379bc100SJani Nikula 	 * originally before the BUN.
2931379bc100SJani Nikula 	 */
2932f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
2933379bc100SJani Nikula 	val &= ~FDI_RX_ENABLE;
2934f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), val);
2935379bc100SJani Nikula 
2936379bc100SJani Nikula 	intel_disable_ddi_buf(encoder, old_crtc_state);
2937c133df69SVille Syrjälä 	intel_ddi_disable_clock(encoder);
2938379bc100SJani Nikula 
2939f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_MISC(PIPE_A));
2940379bc100SJani Nikula 	val &= ~(FDI_RX_PWRDN_LANE1_MASK | FDI_RX_PWRDN_LANE0_MASK);
2941379bc100SJani Nikula 	val |= FDI_RX_PWRDN_LANE1_VAL(2) | FDI_RX_PWRDN_LANE0_VAL(2);
2942f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_MISC(PIPE_A), val);
2943379bc100SJani Nikula 
2944f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
2945379bc100SJani Nikula 	val &= ~FDI_PCDCLK;
2946f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), val);
2947379bc100SJani Nikula 
2948f7960e7fSJani Nikula 	val = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
2949379bc100SJani Nikula 	val &= ~FDI_RX_PLL_ENABLE;
2950f7960e7fSJani Nikula 	intel_de_write(dev_priv, FDI_RX_CTL(PIPE_A), val);
2951379bc100SJani Nikula }
2952379bc100SJani Nikula 
2953d82a855aSVille Syrjälä static void trans_port_sync_stop_link_train(struct intel_atomic_state *state,
2954d82a855aSVille Syrjälä 					    struct intel_encoder *encoder,
2955d82a855aSVille Syrjälä 					    const struct intel_crtc_state *crtc_state)
2956d82a855aSVille Syrjälä {
2957d82a855aSVille Syrjälä 	const struct drm_connector_state *conn_state;
2958d82a855aSVille Syrjälä 	struct drm_connector *conn;
2959d82a855aSVille Syrjälä 	int i;
2960d82a855aSVille Syrjälä 
2961d82a855aSVille Syrjälä 	if (!crtc_state->sync_mode_slaves_mask)
2962d82a855aSVille Syrjälä 		return;
2963d82a855aSVille Syrjälä 
2964d82a855aSVille Syrjälä 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
2965d82a855aSVille Syrjälä 		struct intel_encoder *slave_encoder =
2966d82a855aSVille Syrjälä 			to_intel_encoder(conn_state->best_encoder);
2967d82a855aSVille Syrjälä 		struct intel_crtc *slave_crtc = to_intel_crtc(conn_state->crtc);
2968d82a855aSVille Syrjälä 		const struct intel_crtc_state *slave_crtc_state;
2969d82a855aSVille Syrjälä 
2970d82a855aSVille Syrjälä 		if (!slave_crtc)
2971d82a855aSVille Syrjälä 			continue;
2972d82a855aSVille Syrjälä 
2973d82a855aSVille Syrjälä 		slave_crtc_state =
2974d82a855aSVille Syrjälä 			intel_atomic_get_new_crtc_state(state, slave_crtc);
2975d82a855aSVille Syrjälä 
2976d82a855aSVille Syrjälä 		if (slave_crtc_state->master_transcoder !=
2977d82a855aSVille Syrjälä 		    crtc_state->cpu_transcoder)
2978d82a855aSVille Syrjälä 			continue;
2979d82a855aSVille Syrjälä 
2980a621860aSVille Syrjälä 		intel_dp_stop_link_train(enc_to_intel_dp(slave_encoder),
2981a621860aSVille Syrjälä 					 slave_crtc_state);
2982d82a855aSVille Syrjälä 	}
2983d82a855aSVille Syrjälä 
2984d82a855aSVille Syrjälä 	usleep_range(200, 400);
2985d82a855aSVille Syrjälä 
2986a621860aSVille Syrjälä 	intel_dp_stop_link_train(enc_to_intel_dp(encoder),
2987a621860aSVille Syrjälä 				 crtc_state);
2988d82a855aSVille Syrjälä }
2989d82a855aSVille Syrjälä 
2990ede9771dSVille Syrjälä static void intel_enable_ddi_dp(struct intel_atomic_state *state,
2991ede9771dSVille Syrjälä 				struct intel_encoder *encoder,
2992379bc100SJani Nikula 				const struct intel_crtc_state *crtc_state,
2993379bc100SJani Nikula 				const struct drm_connector_state *conn_state)
2994379bc100SJani Nikula {
2995379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
2996b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2997998cc864SUma Shankar 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
2998379bc100SJani Nikula 	enum port port = encoder->port;
2999379bc100SJani Nikula 
3000005e9537SMatt Roper 	if (port == PORT_A && DISPLAY_VER(dev_priv) < 9)
3001a621860aSVille Syrjälä 		intel_dp_stop_link_train(intel_dp, crtc_state);
3002379bc100SJani Nikula 
3003379bc100SJani Nikula 	intel_edp_backlight_on(crtc_state, conn_state);
30047a00e68bSGwan-gyeong Mun 	intel_psr_enable(intel_dp, crtc_state, conn_state);
3005998cc864SUma Shankar 
3006998cc864SUma Shankar 	if (!dig_port->lspcon.active || dig_port->dp.has_hdmi_sink)
30071bf3657cSGwan-gyeong Mun 		intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
3008998cc864SUma Shankar 
3009379bc100SJani Nikula 	intel_edp_drrs_enable(intel_dp, crtc_state);
3010379bc100SJani Nikula 
3011379bc100SJani Nikula 	if (crtc_state->has_audio)
3012379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
3013d82a855aSVille Syrjälä 
3014d82a855aSVille Syrjälä 	trans_port_sync_stop_link_train(state, encoder, crtc_state);
3015379bc100SJani Nikula }
3016379bc100SJani Nikula 
3017379bc100SJani Nikula static i915_reg_t
3018379bc100SJani Nikula gen9_chicken_trans_reg_by_port(struct drm_i915_private *dev_priv,
3019379bc100SJani Nikula 			       enum port port)
3020379bc100SJani Nikula {
302112c4d4c1SVille Syrjälä 	static const enum transcoder trans[] = {
302212c4d4c1SVille Syrjälä 		[PORT_A] = TRANSCODER_EDP,
302312c4d4c1SVille Syrjälä 		[PORT_B] = TRANSCODER_A,
302412c4d4c1SVille Syrjälä 		[PORT_C] = TRANSCODER_B,
302512c4d4c1SVille Syrjälä 		[PORT_D] = TRANSCODER_C,
302612c4d4c1SVille Syrjälä 		[PORT_E] = TRANSCODER_A,
3027379bc100SJani Nikula 	};
3028379bc100SJani Nikula 
3029005e9537SMatt Roper 	drm_WARN_ON(&dev_priv->drm, DISPLAY_VER(dev_priv) < 9);
3030379bc100SJani Nikula 
30311de143ccSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm, port < PORT_A || port > PORT_E))
3032379bc100SJani Nikula 		port = PORT_A;
3033379bc100SJani Nikula 
303412c4d4c1SVille Syrjälä 	return CHICKEN_TRANS(trans[port]);
3035379bc100SJani Nikula }
3036379bc100SJani Nikula 
3037ede9771dSVille Syrjälä static void intel_enable_ddi_hdmi(struct intel_atomic_state *state,
3038ede9771dSVille Syrjälä 				  struct intel_encoder *encoder,
3039379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state,
3040379bc100SJani Nikula 				  const struct drm_connector_state *conn_state)
3041379bc100SJani Nikula {
3042379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3043b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3044379bc100SJani Nikula 	struct drm_connector *connector = conn_state->connector;
3045c9b69041SVille Syrjälä 	int level = intel_ddi_hdmi_level(encoder, crtc_state);
3046379bc100SJani Nikula 	enum port port = encoder->port;
3047379bc100SJani Nikula 
3048379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3049379bc100SJani Nikula 					       crtc_state->hdmi_high_tmds_clock_ratio,
3050379bc100SJani Nikula 					       crtc_state->hdmi_scrambling))
305147bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
305247bdb1caSJani Nikula 			    "[CONNECTOR:%d:%s] Failed to configure sink scrambling/TMDS bit clock ratio\n",
3053379bc100SJani Nikula 			    connector->base.id, connector->name);
3054379bc100SJani Nikula 
3055a046a0daSMatt Roper 	if (IS_DG2(dev_priv))
3056a046a0daSMatt Roper 		intel_snps_phy_ddi_vswing_sequence(encoder, U32_MAX);
3057a046a0daSMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 12)
3058c9b69041SVille Syrjälä 		tgl_ddi_vswing_sequence(encoder, crtc_state, level);
305993e7e61eSLucas De Marchi 	else if (DISPLAY_VER(dev_priv) == 11)
3060c9b69041SVille Syrjälä 		icl_ddi_vswing_sequence(encoder, crtc_state, level);
30612446e1d6SMatt Roper 	else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3062c9b69041SVille Syrjälä 		bxt_ddi_vswing_sequence(encoder, crtc_state, level);
3063c9b69041SVille Syrjälä 	else
3064d6b10b1aSVille Syrjälä 		hsw_prepare_hdmi_ddi_buffers(encoder, crtc_state, level);
3065c9b69041SVille Syrjälä 
306693e7e61eSLucas De Marchi 	if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv))
3067c9b69041SVille Syrjälä 		skl_ddi_set_iboost(encoder, crtc_state, level);
3068c9b69041SVille Syrjälä 
3069379bc100SJani Nikula 	/* Display WA #1143: skl,kbl,cfl */
307093e7e61eSLucas De Marchi 	if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv)) {
3071379bc100SJani Nikula 		/*
3072379bc100SJani Nikula 		 * For some reason these chicken bits have been
3073379bc100SJani Nikula 		 * stuffed into a transcoder register, event though
3074379bc100SJani Nikula 		 * the bits affect a specific DDI port rather than
3075379bc100SJani Nikula 		 * a specific transcoder.
3076379bc100SJani Nikula 		 */
3077379bc100SJani Nikula 		i915_reg_t reg = gen9_chicken_trans_reg_by_port(dev_priv, port);
3078379bc100SJani Nikula 		u32 val;
3079379bc100SJani Nikula 
3080f7960e7fSJani Nikula 		val = intel_de_read(dev_priv, reg);
3081379bc100SJani Nikula 
3082379bc100SJani Nikula 		if (port == PORT_E)
3083379bc100SJani Nikula 			val |= DDIE_TRAINING_OVERRIDE_ENABLE |
3084379bc100SJani Nikula 				DDIE_TRAINING_OVERRIDE_VALUE;
3085379bc100SJani Nikula 		else
3086379bc100SJani Nikula 			val |= DDI_TRAINING_OVERRIDE_ENABLE |
3087379bc100SJani Nikula 				DDI_TRAINING_OVERRIDE_VALUE;
3088379bc100SJani Nikula 
3089f7960e7fSJani Nikula 		intel_de_write(dev_priv, reg, val);
3090f7960e7fSJani Nikula 		intel_de_posting_read(dev_priv, reg);
3091379bc100SJani Nikula 
3092379bc100SJani Nikula 		udelay(1);
3093379bc100SJani Nikula 
3094379bc100SJani Nikula 		if (port == PORT_E)
3095379bc100SJani Nikula 			val &= ~(DDIE_TRAINING_OVERRIDE_ENABLE |
3096379bc100SJani Nikula 				 DDIE_TRAINING_OVERRIDE_VALUE);
3097379bc100SJani Nikula 		else
3098379bc100SJani Nikula 			val &= ~(DDI_TRAINING_OVERRIDE_ENABLE |
3099379bc100SJani Nikula 				 DDI_TRAINING_OVERRIDE_VALUE);
3100379bc100SJani Nikula 
3101f7960e7fSJani Nikula 		intel_de_write(dev_priv, reg, val);
3102379bc100SJani Nikula 	}
3103379bc100SJani Nikula 
31041e0cb7beSVille Syrjälä 	intel_ddi_power_up_lanes(encoder, crtc_state);
31051e0cb7beSVille Syrjälä 
3106379bc100SJani Nikula 	/* In HDMI/DVI mode, the port width, and swing/emphasis values
3107379bc100SJani Nikula 	 * are ignored so nothing special needs to be done besides
3108379bc100SJani Nikula 	 * enabling the port.
3109414002f1SImre Deak 	 *
3110414002f1SImre Deak 	 * On ADL_P the PHY link rate and lane count must be programmed but
3111414002f1SImre Deak 	 * these are both 0 for HDMI.
3112379bc100SJani Nikula 	 */
3113f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_CTL(port),
3114379bc100SJani Nikula 		       dig_port->saved_port_bits | DDI_BUF_CTL_ENABLE);
3115379bc100SJani Nikula 
3116379bc100SJani Nikula 	if (crtc_state->has_audio)
3117379bc100SJani Nikula 		intel_audio_codec_enable(encoder, crtc_state, conn_state);
3118379bc100SJani Nikula }
3119379bc100SJani Nikula 
3120ede9771dSVille Syrjälä static void intel_enable_ddi(struct intel_atomic_state *state,
3121ede9771dSVille Syrjälä 			     struct intel_encoder *encoder,
3122379bc100SJani Nikula 			     const struct intel_crtc_state *crtc_state,
3123379bc100SJani Nikula 			     const struct drm_connector_state *conn_state)
3124379bc100SJani Nikula {
31258b4f2137SPankaj Bharadiya 	drm_WARN_ON(state->base.dev, crtc_state->has_pch_encoder);
312621fd23acSJani Nikula 
31274e3cdb45SManasi Navare 	if (!crtc_state->bigjoiner_slave)
3128eed22a46SVille Syrjälä 		intel_ddi_enable_transcoder_func(encoder, crtc_state);
31297c2fedd7SVille Syrjälä 
3130aa52b39dSManasi Navare 	intel_vrr_enable(encoder, crtc_state);
3131aa52b39dSManasi Navare 
313221fd23acSJani Nikula 	intel_enable_pipe(crtc_state);
313321fd23acSJani Nikula 
313421fd23acSJani Nikula 	intel_crtc_vblank_on(crtc_state);
313521fd23acSJani Nikula 
3136379bc100SJani Nikula 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
3137ede9771dSVille Syrjälä 		intel_enable_ddi_hdmi(state, encoder, crtc_state, conn_state);
3138379bc100SJani Nikula 	else
3139ede9771dSVille Syrjälä 		intel_enable_ddi_dp(state, encoder, crtc_state, conn_state);
3140379bc100SJani Nikula 
3141379bc100SJani Nikula 	/* Enable hdcp if it's desired */
3142379bc100SJani Nikula 	if (conn_state->content_protection ==
3143379bc100SJani Nikula 	    DRM_MODE_CONTENT_PROTECTION_DESIRED)
3144d456512cSRamalingam C 		intel_hdcp_enable(to_intel_connector(conn_state->connector),
3145fc6097d4SAnshuman Gupta 				  crtc_state,
3146d456512cSRamalingam C 				  (u8)conn_state->hdcp_content_type);
3147379bc100SJani Nikula }
3148379bc100SJani Nikula 
3149ede9771dSVille Syrjälä static void intel_disable_ddi_dp(struct intel_atomic_state *state,
3150ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
3151379bc100SJani Nikula 				 const struct intel_crtc_state *old_crtc_state,
3152379bc100SJani Nikula 				 const struct drm_connector_state *old_conn_state)
3153379bc100SJani Nikula {
3154b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3155379bc100SJani Nikula 
3156379bc100SJani Nikula 	intel_dp->link_trained = false;
3157379bc100SJani Nikula 
3158379bc100SJani Nikula 	intel_edp_backlight_off(old_conn_state);
3159379bc100SJani Nikula 	/* Disable the decompression in DP Sink */
3160379bc100SJani Nikula 	intel_dp_sink_set_decompression_state(intel_dp, old_crtc_state,
3161379bc100SJani Nikula 					      false);
31621639406aSManasi Navare 	/* Disable Ignore_MSA bit in DP Sink */
31631639406aSManasi Navare 	intel_dp_sink_set_msa_timing_par_ignore_state(intel_dp, old_crtc_state,
31641639406aSManasi Navare 						      false);
3165379bc100SJani Nikula }
3166379bc100SJani Nikula 
3167ede9771dSVille Syrjälä static void intel_disable_ddi_hdmi(struct intel_atomic_state *state,
3168ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
3169379bc100SJani Nikula 				   const struct intel_crtc_state *old_crtc_state,
3170379bc100SJani Nikula 				   const struct drm_connector_state *old_conn_state)
3171379bc100SJani Nikula {
317247bdb1caSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3173379bc100SJani Nikula 	struct drm_connector *connector = old_conn_state->connector;
3174379bc100SJani Nikula 
3175379bc100SJani Nikula 	if (!intel_hdmi_handle_sink_scrambling(encoder, connector,
3176379bc100SJani Nikula 					       false, false))
317747bdb1caSJani Nikula 		drm_dbg_kms(&i915->drm,
317847bdb1caSJani Nikula 			    "[CONNECTOR:%d:%s] Failed to reset sink scrambling/TMDS bit clock ratio\n",
3179379bc100SJani Nikula 			    connector->base.id, connector->name);
3180379bc100SJani Nikula }
3181379bc100SJani Nikula 
318284030adbSJosé Roberto de Souza static void intel_pre_disable_ddi(struct intel_atomic_state *state,
318384030adbSJosé Roberto de Souza 				  struct intel_encoder *encoder,
318484030adbSJosé Roberto de Souza 				  const struct intel_crtc_state *old_crtc_state,
318584030adbSJosé Roberto de Souza 				  const struct drm_connector_state *old_conn_state)
318684030adbSJosé Roberto de Souza {
318784030adbSJosé Roberto de Souza 	struct intel_dp *intel_dp;
318884030adbSJosé Roberto de Souza 
318984030adbSJosé Roberto de Souza 	if (old_crtc_state->has_audio)
319084030adbSJosé Roberto de Souza 		intel_audio_codec_disable(encoder, old_crtc_state,
319184030adbSJosé Roberto de Souza 					  old_conn_state);
319284030adbSJosé Roberto de Souza 
319384030adbSJosé Roberto de Souza 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
319484030adbSJosé Roberto de Souza 		return;
319584030adbSJosé Roberto de Souza 
319684030adbSJosé Roberto de Souza 	intel_dp = enc_to_intel_dp(encoder);
319784030adbSJosé Roberto de Souza 	intel_edp_drrs_disable(intel_dp, old_crtc_state);
319884030adbSJosé Roberto de Souza 	intel_psr_disable(intel_dp, old_crtc_state);
319984030adbSJosé Roberto de Souza }
320084030adbSJosé Roberto de Souza 
3201ede9771dSVille Syrjälä static void intel_disable_ddi(struct intel_atomic_state *state,
3202ede9771dSVille Syrjälä 			      struct intel_encoder *encoder,
3203379bc100SJani Nikula 			      const struct intel_crtc_state *old_crtc_state,
3204379bc100SJani Nikula 			      const struct drm_connector_state *old_conn_state)
3205379bc100SJani Nikula {
3206379bc100SJani Nikula 	intel_hdcp_disable(to_intel_connector(old_conn_state->connector));
3207379bc100SJani Nikula 
3208379bc100SJani Nikula 	if (intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_HDMI))
3209ede9771dSVille Syrjälä 		intel_disable_ddi_hdmi(state, encoder, old_crtc_state,
3210ede9771dSVille Syrjälä 				       old_conn_state);
3211379bc100SJani Nikula 	else
3212ede9771dSVille Syrjälä 		intel_disable_ddi_dp(state, encoder, old_crtc_state,
3213ede9771dSVille Syrjälä 				     old_conn_state);
3214379bc100SJani Nikula }
3215379bc100SJani Nikula 
3216ede9771dSVille Syrjälä static void intel_ddi_update_pipe_dp(struct intel_atomic_state *state,
3217ede9771dSVille Syrjälä 				     struct intel_encoder *encoder,
3218379bc100SJani Nikula 				     const struct intel_crtc_state *crtc_state,
3219379bc100SJani Nikula 				     const struct drm_connector_state *conn_state)
3220379bc100SJani Nikula {
3221b7d02c3aSVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3222379bc100SJani Nikula 
32230c06fa15SGwan-gyeong Mun 	intel_ddi_set_dp_msa(crtc_state, conn_state);
3224379bc100SJani Nikula 
32257a00e68bSGwan-gyeong Mun 	intel_psr_update(intel_dp, crtc_state, conn_state);
322676d45d06SGwan-gyeong Mun 	intel_dp_set_infoframes(encoder, true, crtc_state, conn_state);
32278040fefaSJosé Roberto de Souza 	intel_edp_drrs_update(intel_dp, crtc_state);
3228379bc100SJani Nikula 
3229ede9771dSVille Syrjälä 	intel_panel_update_backlight(state, encoder, crtc_state, conn_state);
3230379bc100SJani Nikula }
3231379bc100SJani Nikula 
3232f1c7a36bSSean Paul void intel_ddi_update_pipe(struct intel_atomic_state *state,
3233ede9771dSVille Syrjälä 			   struct intel_encoder *encoder,
3234379bc100SJani Nikula 			   const struct intel_crtc_state *crtc_state,
3235379bc100SJani Nikula 			   const struct drm_connector_state *conn_state)
3236379bc100SJani Nikula {
3237d456512cSRamalingam C 
3238f1c7a36bSSean Paul 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) &&
3239f1c7a36bSSean Paul 	    !intel_encoder_is_mst(encoder))
3240ede9771dSVille Syrjälä 		intel_ddi_update_pipe_dp(state, encoder, crtc_state,
3241ede9771dSVille Syrjälä 					 conn_state);
3242379bc100SJani Nikula 
3243ede9771dSVille Syrjälä 	intel_hdcp_update_pipe(state, encoder, crtc_state, conn_state);
3244379bc100SJani Nikula }
3245379bc100SJani Nikula 
3246379bc100SJani Nikula static void
324724a7bfe0SImre Deak intel_ddi_update_prepare(struct intel_atomic_state *state,
324824a7bfe0SImre Deak 			 struct intel_encoder *encoder,
324924a7bfe0SImre Deak 			 struct intel_crtc *crtc)
325024a7bfe0SImre Deak {
325124a7bfe0SImre Deak 	struct intel_crtc_state *crtc_state =
325224a7bfe0SImre Deak 		crtc ? intel_atomic_get_new_crtc_state(state, crtc) : NULL;
325324a7bfe0SImre Deak 	int required_lanes = crtc_state ? crtc_state->lane_count : 1;
325424a7bfe0SImre Deak 
32558b4f2137SPankaj Bharadiya 	drm_WARN_ON(state->base.dev, crtc && crtc->active);
325624a7bfe0SImre Deak 
3257b7d02c3aSVille Syrjälä 	intel_tc_port_get_link(enc_to_dig_port(encoder),
3258b7d02c3aSVille Syrjälä 		               required_lanes);
32591326a92cSMaarten Lankhorst 	if (crtc_state && crtc_state->hw.active)
326024a7bfe0SImre Deak 		intel_update_active_dpll(state, crtc, encoder);
326124a7bfe0SImre Deak }
326224a7bfe0SImre Deak 
326324a7bfe0SImre Deak static void
326424a7bfe0SImre Deak intel_ddi_update_complete(struct intel_atomic_state *state,
326524a7bfe0SImre Deak 			  struct intel_encoder *encoder,
326624a7bfe0SImre Deak 			  struct intel_crtc *crtc)
326724a7bfe0SImre Deak {
3268b7d02c3aSVille Syrjälä 	intel_tc_port_put_link(enc_to_dig_port(encoder));
326924a7bfe0SImre Deak }
327024a7bfe0SImre Deak 
327124a7bfe0SImre Deak static void
3272ede9771dSVille Syrjälä intel_ddi_pre_pll_enable(struct intel_atomic_state *state,
3273ede9771dSVille Syrjälä 			 struct intel_encoder *encoder,
3274379bc100SJani Nikula 			 const struct intel_crtc_state *crtc_state,
3275379bc100SJani Nikula 			 const struct drm_connector_state *conn_state)
3276379bc100SJani Nikula {
3277379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3278b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3279d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
3280d8fe2ab6SMatt Roper 	bool is_tc_port = intel_phy_is_tc(dev_priv, phy);
3281379bc100SJani Nikula 
328224a7bfe0SImre Deak 	if (is_tc_port)
328324a7bfe0SImre Deak 		intel_tc_port_get_link(dig_port, crtc_state->lane_count);
328424a7bfe0SImre Deak 
3285162e68e1SImre Deak 	if (intel_crtc_has_dp_encoder(crtc_state) || is_tc_port) {
3286162e68e1SImre Deak 		drm_WARN_ON(&dev_priv->drm, dig_port->aux_wakeref);
3287162e68e1SImre Deak 		dig_port->aux_wakeref =
3288379bc100SJani Nikula 			intel_display_power_get(dev_priv,
3289379bc100SJani Nikula 						intel_ddi_main_link_aux_domain(dig_port));
3290162e68e1SImre Deak 	}
3291379bc100SJani Nikula 
32929d44dcb9SLucas De Marchi 	if (is_tc_port && dig_port->tc_mode != TC_PORT_TBT_ALT)
32939d44dcb9SLucas De Marchi 		/*
32949d44dcb9SLucas De Marchi 		 * Program the lane count for static/dynamic connections on
32959d44dcb9SLucas De Marchi 		 * Type-C ports.  Skip this step for TBT.
32969d44dcb9SLucas De Marchi 		 */
32979d44dcb9SLucas De Marchi 		intel_tc_port_set_fia_lane_count(dig_port, crtc_state->lane_count);
32982446e1d6SMatt Roper 	else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3299379bc100SJani Nikula 		bxt_ddi_phy_set_lane_optim_mask(encoder,
3300379bc100SJani Nikula 						crtc_state->lane_lat_optim_mask);
3301379bc100SJani Nikula }
3302379bc100SJani Nikula 
3303a621860aSVille Syrjälä static void intel_ddi_prepare_link_retrain(struct intel_dp *intel_dp,
3304a621860aSVille Syrjälä 					   const struct intel_crtc_state *crtc_state)
3305379bc100SJani Nikula {
3306ef79fafeSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3307ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3308ef79fafeSVille Syrjälä 	enum port port = encoder->port;
330935ac28a8SLucas De Marchi 	u32 dp_tp_ctl, ddi_buf_ctl;
3310379bc100SJani Nikula 	bool wait = false;
3311379bc100SJani Nikula 
3312ef79fafeSVille Syrjälä 	dp_tp_ctl = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
331335ac28a8SLucas De Marchi 
331435ac28a8SLucas De Marchi 	if (dp_tp_ctl & DP_TP_CTL_ENABLE) {
3315f7960e7fSJani Nikula 		ddi_buf_ctl = intel_de_read(dev_priv, DDI_BUF_CTL(port));
331635ac28a8SLucas De Marchi 		if (ddi_buf_ctl & DDI_BUF_CTL_ENABLE) {
3317f7960e7fSJani Nikula 			intel_de_write(dev_priv, DDI_BUF_CTL(port),
331835ac28a8SLucas De Marchi 				       ddi_buf_ctl & ~DDI_BUF_CTL_ENABLE);
3319379bc100SJani Nikula 			wait = true;
3320379bc100SJani Nikula 		}
3321379bc100SJani Nikula 
332235ac28a8SLucas De Marchi 		dp_tp_ctl &= ~(DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_MASK);
332335ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_LINK_TRAIN_PAT1;
3324ef79fafeSVille Syrjälä 		intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), dp_tp_ctl);
3325ef79fafeSVille Syrjälä 		intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3326379bc100SJani Nikula 
3327379bc100SJani Nikula 		if (wait)
3328379bc100SJani Nikula 			intel_wait_ddi_buf_idle(dev_priv, port);
3329379bc100SJani Nikula 	}
3330379bc100SJani Nikula 
3331963501bdSImre Deak 	dp_tp_ctl = DP_TP_CTL_ENABLE | DP_TP_CTL_LINK_TRAIN_PAT1;
3332a621860aSVille Syrjälä 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DP_MST)) {
333335ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_MODE_MST;
3334a621860aSVille Syrjälä 	} else {
333535ac28a8SLucas De Marchi 		dp_tp_ctl |= DP_TP_CTL_MODE_SST;
3336379bc100SJani Nikula 		if (drm_dp_enhanced_frame_cap(intel_dp->dpcd))
333735ac28a8SLucas De Marchi 			dp_tp_ctl |= DP_TP_CTL_ENHANCED_FRAME_ENABLE;
3338379bc100SJani Nikula 	}
3339ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), dp_tp_ctl);
3340ef79fafeSVille Syrjälä 	intel_de_posting_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3341379bc100SJani Nikula 
3342379bc100SJani Nikula 	intel_dp->DP |= DDI_BUF_CTL_ENABLE;
3343f7960e7fSJani Nikula 	intel_de_write(dev_priv, DDI_BUF_CTL(port), intel_dp->DP);
3344f7960e7fSJani Nikula 	intel_de_posting_read(dev_priv, DDI_BUF_CTL(port));
3345379bc100SJani Nikula 
3346e828da30SManasi Navare 	intel_wait_ddi_buf_active(dev_priv, port);
3347379bc100SJani Nikula }
3348379bc100SJani Nikula 
3349eee3f911SVille Syrjälä static void intel_ddi_set_link_train(struct intel_dp *intel_dp,
3350a621860aSVille Syrjälä 				     const struct intel_crtc_state *crtc_state,
3351eee3f911SVille Syrjälä 				     u8 dp_train_pat)
3352eee3f911SVille Syrjälä {
3353ef79fafeSVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
3354ef79fafeSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3355eee3f911SVille Syrjälä 	u32 temp;
3356eee3f911SVille Syrjälä 
3357ef79fafeSVille Syrjälä 	temp = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
3358eee3f911SVille Syrjälä 
3359eee3f911SVille Syrjälä 	temp &= ~DP_TP_CTL_LINK_TRAIN_MASK;
33606777a855SImre Deak 	switch (intel_dp_training_pattern_symbol(dp_train_pat)) {
3361eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_DISABLE:
3362eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_NORMAL;
3363eee3f911SVille Syrjälä 		break;
3364eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_1:
3365eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT1;
3366eee3f911SVille Syrjälä 		break;
3367eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_2:
3368eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT2;
3369eee3f911SVille Syrjälä 		break;
3370eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_3:
3371eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT3;
3372eee3f911SVille Syrjälä 		break;
3373eee3f911SVille Syrjälä 	case DP_TRAINING_PATTERN_4:
3374eee3f911SVille Syrjälä 		temp |= DP_TP_CTL_LINK_TRAIN_PAT4;
3375eee3f911SVille Syrjälä 		break;
3376eee3f911SVille Syrjälä 	}
3377eee3f911SVille Syrjälä 
3378ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), temp);
3379eee3f911SVille Syrjälä }
3380eee3f911SVille Syrjälä 
3381a621860aSVille Syrjälä static void intel_ddi_set_idle_link_train(struct intel_dp *intel_dp,
3382a621860aSVille Syrjälä 					  const struct intel_crtc_state *crtc_state)
33838fdda385SVille Syrjälä {
33848fdda385SVille Syrjälä 	struct intel_encoder *encoder = &dp_to_dig_port(intel_dp)->base;
33858fdda385SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
33868fdda385SVille Syrjälä 	enum port port = encoder->port;
33878fdda385SVille Syrjälä 	u32 val;
33888fdda385SVille Syrjälä 
3389ef79fafeSVille Syrjälä 	val = intel_de_read(dev_priv, dp_tp_ctl_reg(encoder, crtc_state));
33908fdda385SVille Syrjälä 	val &= ~DP_TP_CTL_LINK_TRAIN_MASK;
33918fdda385SVille Syrjälä 	val |= DP_TP_CTL_LINK_TRAIN_IDLE;
3392ef79fafeSVille Syrjälä 	intel_de_write(dev_priv, dp_tp_ctl_reg(encoder, crtc_state), val);
33938fdda385SVille Syrjälä 
33948fdda385SVille Syrjälä 	/*
33958fdda385SVille Syrjälä 	 * Until TGL on PORT_A we can have only eDP in SST mode. There the only
33968fdda385SVille Syrjälä 	 * reason we need to set idle transmission mode is to work around a HW
33978fdda385SVille Syrjälä 	 * issue where we enable the pipe while not in idle link-training mode.
33988fdda385SVille Syrjälä 	 * In this case there is requirement to wait for a minimum number of
33998fdda385SVille Syrjälä 	 * idle patterns to be sent.
34008fdda385SVille Syrjälä 	 */
3401005e9537SMatt Roper 	if (port == PORT_A && DISPLAY_VER(dev_priv) < 12)
34028fdda385SVille Syrjälä 		return;
34038fdda385SVille Syrjälä 
3404ef79fafeSVille Syrjälä 	if (intel_de_wait_for_set(dev_priv,
3405ef79fafeSVille Syrjälä 				  dp_tp_status_reg(encoder, crtc_state),
34068fdda385SVille Syrjälä 				  DP_TP_STATUS_IDLE_DONE, 1))
34078fdda385SVille Syrjälä 		drm_err(&dev_priv->drm,
34088fdda385SVille Syrjälä 			"Timed out waiting for DP idle patterns\n");
34098fdda385SVille Syrjälä }
34108fdda385SVille Syrjälä 
3411379bc100SJani Nikula static bool intel_ddi_is_audio_enabled(struct drm_i915_private *dev_priv,
3412379bc100SJani Nikula 				       enum transcoder cpu_transcoder)
3413379bc100SJani Nikula {
3414379bc100SJani Nikula 	if (cpu_transcoder == TRANSCODER_EDP)
3415379bc100SJani Nikula 		return false;
3416379bc100SJani Nikula 
3417379bc100SJani Nikula 	if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_AUDIO))
3418379bc100SJani Nikula 		return false;
3419379bc100SJani Nikula 
3420f7960e7fSJani Nikula 	return intel_de_read(dev_priv, HSW_AUD_PIN_ELD_CP_VLD) &
3421379bc100SJani Nikula 		AUDIO_OUTPUT_ENABLE(cpu_transcoder);
3422379bc100SJani Nikula }
3423379bc100SJani Nikula 
3424379bc100SJani Nikula void intel_ddi_compute_min_voltage_level(struct drm_i915_private *dev_priv,
3425379bc100SJani Nikula 					 struct intel_crtc_state *crtc_state)
3426379bc100SJani Nikula {
3427005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12 && crtc_state->port_clock > 594000)
34280fde0b1dSMatt Roper 		crtc_state->min_voltage_level = 2;
342924ea098bSTejas Upadhyay 	else if (IS_JSL_EHL(dev_priv) && crtc_state->port_clock > 594000)
34309d5fd37eSMatt Roper 		crtc_state->min_voltage_level = 3;
3431005e9537SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 11 && crtc_state->port_clock > 594000)
3432379bc100SJani Nikula 		crtc_state->min_voltage_level = 1;
3433379bc100SJani Nikula }
3434379bc100SJani Nikula 
3435dc5b8ed5SVille Syrjälä static enum transcoder bdw_transcoder_master_readout(struct drm_i915_private *dev_priv,
343602d8ea47SVille Syrjälä 						     enum transcoder cpu_transcoder)
343702d8ea47SVille Syrjälä {
3438dc5b8ed5SVille Syrjälä 	u32 master_select;
343902d8ea47SVille Syrjälä 
3440005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
3441dc5b8ed5SVille Syrjälä 		u32 ctl2 = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL2(cpu_transcoder));
344202d8ea47SVille Syrjälä 
344302d8ea47SVille Syrjälä 		if ((ctl2 & PORT_SYNC_MODE_ENABLE) == 0)
344402d8ea47SVille Syrjälä 			return INVALID_TRANSCODER;
344502d8ea47SVille Syrjälä 
3446d4d7d9caSVille Syrjälä 		master_select = REG_FIELD_GET(PORT_SYNC_MODE_MASTER_SELECT_MASK, ctl2);
3447dc5b8ed5SVille Syrjälä 	} else {
3448dc5b8ed5SVille Syrjälä 		u32 ctl = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
3449dc5b8ed5SVille Syrjälä 
3450dc5b8ed5SVille Syrjälä 		if ((ctl & TRANS_DDI_PORT_SYNC_ENABLE) == 0)
3451dc5b8ed5SVille Syrjälä 			return INVALID_TRANSCODER;
3452dc5b8ed5SVille Syrjälä 
3453dc5b8ed5SVille Syrjälä 		master_select = REG_FIELD_GET(TRANS_DDI_PORT_SYNC_MASTER_SELECT_MASK, ctl);
3454dc5b8ed5SVille Syrjälä 	}
345502d8ea47SVille Syrjälä 
345602d8ea47SVille Syrjälä 	if (master_select == 0)
345702d8ea47SVille Syrjälä 		return TRANSCODER_EDP;
345802d8ea47SVille Syrjälä 	else
345902d8ea47SVille Syrjälä 		return master_select - 1;
346002d8ea47SVille Syrjälä }
346102d8ea47SVille Syrjälä 
3462dc5b8ed5SVille Syrjälä static void bdw_get_trans_port_sync_config(struct intel_crtc_state *crtc_state)
346302d8ea47SVille Syrjälä {
346402d8ea47SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
346502d8ea47SVille Syrjälä 	u32 transcoders = BIT(TRANSCODER_A) | BIT(TRANSCODER_B) |
346602d8ea47SVille Syrjälä 		BIT(TRANSCODER_C) | BIT(TRANSCODER_D);
346702d8ea47SVille Syrjälä 	enum transcoder cpu_transcoder;
346802d8ea47SVille Syrjälä 
346902d8ea47SVille Syrjälä 	crtc_state->master_transcoder =
3470dc5b8ed5SVille Syrjälä 		bdw_transcoder_master_readout(dev_priv, crtc_state->cpu_transcoder);
347102d8ea47SVille Syrjälä 
347202d8ea47SVille Syrjälä 	for_each_cpu_transcoder_masked(dev_priv, cpu_transcoder, transcoders) {
347302d8ea47SVille Syrjälä 		enum intel_display_power_domain power_domain;
347402d8ea47SVille Syrjälä 		intel_wakeref_t trans_wakeref;
347502d8ea47SVille Syrjälä 
347602d8ea47SVille Syrjälä 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
347702d8ea47SVille Syrjälä 		trans_wakeref = intel_display_power_get_if_enabled(dev_priv,
347802d8ea47SVille Syrjälä 								   power_domain);
347902d8ea47SVille Syrjälä 
348002d8ea47SVille Syrjälä 		if (!trans_wakeref)
348102d8ea47SVille Syrjälä 			continue;
348202d8ea47SVille Syrjälä 
3483dc5b8ed5SVille Syrjälä 		if (bdw_transcoder_master_readout(dev_priv, cpu_transcoder) ==
348402d8ea47SVille Syrjälä 		    crtc_state->cpu_transcoder)
348502d8ea47SVille Syrjälä 			crtc_state->sync_mode_slaves_mask |= BIT(cpu_transcoder);
348602d8ea47SVille Syrjälä 
348702d8ea47SVille Syrjälä 		intel_display_power_put(dev_priv, power_domain, trans_wakeref);
348802d8ea47SVille Syrjälä 	}
348902d8ea47SVille Syrjälä 
349002d8ea47SVille Syrjälä 	drm_WARN_ON(&dev_priv->drm,
349102d8ea47SVille Syrjälä 		    crtc_state->master_transcoder != INVALID_TRANSCODER &&
349202d8ea47SVille Syrjälä 		    crtc_state->sync_mode_slaves_mask);
349302d8ea47SVille Syrjälä }
349402d8ea47SVille Syrjälä 
34950385eceaSManasi Navare static void intel_ddi_read_func_ctl(struct intel_encoder *encoder,
3496379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config)
3497379bc100SJani Nikula {
3498379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3499f15f01a7SVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3500379bc100SJani Nikula 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
3501a44289b9SUma Shankar 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
3502379bc100SJani Nikula 	u32 temp, flags = 0;
3503379bc100SJani Nikula 
3504f7960e7fSJani Nikula 	temp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
3505379bc100SJani Nikula 	if (temp & TRANS_DDI_PHSYNC)
3506379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PHSYNC;
3507379bc100SJani Nikula 	else
3508379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NHSYNC;
3509379bc100SJani Nikula 	if (temp & TRANS_DDI_PVSYNC)
3510379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_PVSYNC;
3511379bc100SJani Nikula 	else
3512379bc100SJani Nikula 		flags |= DRM_MODE_FLAG_NVSYNC;
3513379bc100SJani Nikula 
35141326a92cSMaarten Lankhorst 	pipe_config->hw.adjusted_mode.flags |= flags;
3515379bc100SJani Nikula 
3516379bc100SJani Nikula 	switch (temp & TRANS_DDI_BPC_MASK) {
3517379bc100SJani Nikula 	case TRANS_DDI_BPC_6:
3518379bc100SJani Nikula 		pipe_config->pipe_bpp = 18;
3519379bc100SJani Nikula 		break;
3520379bc100SJani Nikula 	case TRANS_DDI_BPC_8:
3521379bc100SJani Nikula 		pipe_config->pipe_bpp = 24;
3522379bc100SJani Nikula 		break;
3523379bc100SJani Nikula 	case TRANS_DDI_BPC_10:
3524379bc100SJani Nikula 		pipe_config->pipe_bpp = 30;
3525379bc100SJani Nikula 		break;
3526379bc100SJani Nikula 	case TRANS_DDI_BPC_12:
3527379bc100SJani Nikula 		pipe_config->pipe_bpp = 36;
3528379bc100SJani Nikula 		break;
3529379bc100SJani Nikula 	default:
3530379bc100SJani Nikula 		break;
3531379bc100SJani Nikula 	}
3532379bc100SJani Nikula 
3533379bc100SJani Nikula 	switch (temp & TRANS_DDI_MODE_SELECT_MASK) {
3534379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_HDMI:
3535379bc100SJani Nikula 		pipe_config->has_hdmi_sink = true;
3536379bc100SJani Nikula 
3537379bc100SJani Nikula 		pipe_config->infoframes.enable |=
3538379bc100SJani Nikula 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
3539379bc100SJani Nikula 
3540379bc100SJani Nikula 		if (pipe_config->infoframes.enable)
3541379bc100SJani Nikula 			pipe_config->has_infoframe = true;
3542379bc100SJani Nikula 
3543379bc100SJani Nikula 		if (temp & TRANS_DDI_HDMI_SCRAMBLING)
3544379bc100SJani Nikula 			pipe_config->hdmi_scrambling = true;
3545379bc100SJani Nikula 		if (temp & TRANS_DDI_HIGH_TMDS_CHAR_RATE)
3546379bc100SJani Nikula 			pipe_config->hdmi_high_tmds_clock_ratio = true;
3547df561f66SGustavo A. R. Silva 		fallthrough;
3548379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DVI:
3549379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_HDMI);
3550379bc100SJani Nikula 		pipe_config->lane_count = 4;
3551379bc100SJani Nikula 		break;
3552379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_FDI:
3553379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_ANALOG);
3554379bc100SJani Nikula 		break;
3555379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_SST:
3556379bc100SJani Nikula 		if (encoder->type == INTEL_OUTPUT_EDP)
3557379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_EDP);
3558379bc100SJani Nikula 		else
3559379bc100SJani Nikula 			pipe_config->output_types |= BIT(INTEL_OUTPUT_DP);
3560379bc100SJani Nikula 		pipe_config->lane_count =
3561379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
3562f15f01a7SVille Syrjälä 		intel_dp_get_m_n(crtc, pipe_config);
35638aa940c8SMaarten Lankhorst 
3564005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 11) {
3565ef79fafeSVille Syrjälä 			i915_reg_t dp_tp_ctl = dp_tp_ctl_reg(encoder, pipe_config);
35668aa940c8SMaarten Lankhorst 
35678aa940c8SMaarten Lankhorst 			pipe_config->fec_enable =
3568f7960e7fSJani Nikula 				intel_de_read(dev_priv, dp_tp_ctl) & DP_TP_CTL_FEC_ENABLE;
35698aa940c8SMaarten Lankhorst 
357047bdb1caSJani Nikula 			drm_dbg_kms(&dev_priv->drm,
357147bdb1caSJani Nikula 				    "[ENCODER:%d:%s] Fec status: %u\n",
35728aa940c8SMaarten Lankhorst 				    encoder->base.base.id, encoder->base.name,
35738aa940c8SMaarten Lankhorst 				    pipe_config->fec_enable);
35748aa940c8SMaarten Lankhorst 		}
35758aa940c8SMaarten Lankhorst 
3576a44289b9SUma Shankar 		if (dig_port->lspcon.active && dig_port->dp.has_hdmi_sink)
3577a44289b9SUma Shankar 			pipe_config->infoframes.enable |=
3578a44289b9SUma Shankar 				intel_lspcon_infoframes_enabled(encoder, pipe_config);
3579a44289b9SUma Shankar 		else
3580dee66f3eSGwan-gyeong Mun 			pipe_config->infoframes.enable |=
3581dee66f3eSGwan-gyeong Mun 				intel_hdmi_infoframes_enabled(encoder, pipe_config);
3582379bc100SJani Nikula 		break;
3583379bc100SJani Nikula 	case TRANS_DDI_MODE_SELECT_DP_MST:
3584379bc100SJani Nikula 		pipe_config->output_types |= BIT(INTEL_OUTPUT_DP_MST);
3585379bc100SJani Nikula 		pipe_config->lane_count =
3586379bc100SJani Nikula 			((temp & DDI_PORT_WIDTH_MASK) >> DDI_PORT_WIDTH_SHIFT) + 1;
35876671c367SJosé Roberto de Souza 
3588005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12)
35896671c367SJosé Roberto de Souza 			pipe_config->mst_master_transcoder =
35906671c367SJosé Roberto de Souza 					REG_FIELD_GET(TRANS_DDI_MST_TRANSPORT_SELECT_MASK, temp);
35916671c367SJosé Roberto de Souza 
3592f15f01a7SVille Syrjälä 		intel_dp_get_m_n(crtc, pipe_config);
3593dee66f3eSGwan-gyeong Mun 
3594dee66f3eSGwan-gyeong Mun 		pipe_config->infoframes.enable |=
3595dee66f3eSGwan-gyeong Mun 			intel_hdmi_infoframes_enabled(encoder, pipe_config);
3596379bc100SJani Nikula 		break;
3597379bc100SJani Nikula 	default:
3598379bc100SJani Nikula 		break;
3599379bc100SJani Nikula 	}
36000385eceaSManasi Navare }
36010385eceaSManasi Navare 
3602351221ffSVille Syrjälä static void intel_ddi_get_config(struct intel_encoder *encoder,
36030385eceaSManasi Navare 				 struct intel_crtc_state *pipe_config)
36040385eceaSManasi Navare {
36050385eceaSManasi Navare 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
36060385eceaSManasi Navare 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
36070385eceaSManasi Navare 
36080385eceaSManasi Navare 	/* XXX: DSI transcoder paranoia */
36090385eceaSManasi Navare 	if (drm_WARN_ON(&dev_priv->drm, transcoder_is_dsi(cpu_transcoder)))
36100385eceaSManasi Navare 		return;
36110385eceaSManasi Navare 
36120385eceaSManasi Navare 	if (pipe_config->bigjoiner_slave) {
36130385eceaSManasi Navare 		/* read out pipe settings from master */
36140385eceaSManasi Navare 		enum transcoder save = pipe_config->cpu_transcoder;
36150385eceaSManasi Navare 
36160385eceaSManasi Navare 		/* Our own transcoder needs to be disabled when reading it in intel_ddi_read_func_ctl() */
36170385eceaSManasi Navare 		WARN_ON(pipe_config->output_types);
36180385eceaSManasi Navare 		pipe_config->cpu_transcoder = (enum transcoder)pipe_config->bigjoiner_linked_crtc->pipe;
36190385eceaSManasi Navare 		intel_ddi_read_func_ctl(encoder, pipe_config);
36200385eceaSManasi Navare 		pipe_config->cpu_transcoder = save;
36210385eceaSManasi Navare 	} else {
36220385eceaSManasi Navare 		intel_ddi_read_func_ctl(encoder, pipe_config);
36230385eceaSManasi Navare 	}
3624379bc100SJani Nikula 
36255b616a29SJani Nikula 	intel_ddi_mso_get_config(encoder, pipe_config);
36265b616a29SJani Nikula 
3627379bc100SJani Nikula 	pipe_config->has_audio =
3628379bc100SJani Nikula 		intel_ddi_is_audio_enabled(dev_priv, cpu_transcoder);
3629379bc100SJani Nikula 
3630379bc100SJani Nikula 	if (encoder->type == INTEL_OUTPUT_EDP && dev_priv->vbt.edp.bpp &&
3631379bc100SJani Nikula 	    pipe_config->pipe_bpp > dev_priv->vbt.edp.bpp) {
3632379bc100SJani Nikula 		/*
3633379bc100SJani Nikula 		 * This is a big fat ugly hack.
3634379bc100SJani Nikula 		 *
3635379bc100SJani Nikula 		 * Some machines in UEFI boot mode provide us a VBT that has 18
3636379bc100SJani Nikula 		 * bpp and 1.62 GHz link bandwidth for eDP, which for reasons
3637379bc100SJani Nikula 		 * unknown we fail to light up. Yet the same BIOS boots up with
3638379bc100SJani Nikula 		 * 24 bpp and 2.7 GHz link. Use the same bpp as the BIOS uses as
3639379bc100SJani Nikula 		 * max, not what it tells us to use.
3640379bc100SJani Nikula 		 *
3641379bc100SJani Nikula 		 * Note: This will still be broken if the eDP panel is not lit
3642379bc100SJani Nikula 		 * up by the BIOS, and thus we can't get the mode at module
3643379bc100SJani Nikula 		 * load.
3644379bc100SJani Nikula 		 */
364547bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
364647bdb1caSJani Nikula 			    "pipe has %d bpp for eDP panel, overriding BIOS-provided max %d bpp\n",
3647379bc100SJani Nikula 			    pipe_config->pipe_bpp, dev_priv->vbt.edp.bpp);
3648379bc100SJani Nikula 		dev_priv->vbt.edp.bpp = pipe_config->pipe_bpp;
3649379bc100SJani Nikula 	}
3650379bc100SJani Nikula 
36510385eceaSManasi Navare 	if (!pipe_config->bigjoiner_slave)
3652351221ffSVille Syrjälä 		ddi_dotclock_get(pipe_config);
3653379bc100SJani Nikula 
36542446e1d6SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3655379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
3656379bc100SJani Nikula 			bxt_ddi_phy_get_lane_lat_optim_mask(encoder);
3657379bc100SJani Nikula 
3658379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
3659379bc100SJani Nikula 
3660379bc100SJani Nikula 	intel_hdmi_read_gcp_infoframe(encoder, pipe_config);
3661379bc100SJani Nikula 
3662379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3663379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_AVI,
3664379bc100SJani Nikula 			     &pipe_config->infoframes.avi);
3665379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3666379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_SPD,
3667379bc100SJani Nikula 			     &pipe_config->infoframes.spd);
3668379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3669379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_VENDOR,
3670379bc100SJani Nikula 			     &pipe_config->infoframes.hdmi);
3671379bc100SJani Nikula 	intel_read_infoframe(encoder, pipe_config,
3672379bc100SJani Nikula 			     HDMI_INFOFRAME_TYPE_DRM,
3673379bc100SJani Nikula 			     &pipe_config->infoframes.drm);
367402d8ea47SVille Syrjälä 
3675005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 8)
3676dc5b8ed5SVille Syrjälä 		bdw_get_trans_port_sync_config(pipe_config);
3677dee66f3eSGwan-gyeong Mun 
3678dee66f3eSGwan-gyeong Mun 	intel_read_dp_sdp(encoder, pipe_config, HDMI_PACKET_TYPE_GAMUT_METADATA);
36792c3928e4SGwan-gyeong Mun 	intel_read_dp_sdp(encoder, pipe_config, DP_SDP_VSC);
368078b772e1SJosé Roberto de Souza 
368178b772e1SJosé Roberto de Souza 	intel_psr_get_config(encoder, pipe_config);
3682379bc100SJani Nikula }
3683379bc100SJani Nikula 
3684351221ffSVille Syrjälä void intel_ddi_get_clock(struct intel_encoder *encoder,
3685351221ffSVille Syrjälä 			 struct intel_crtc_state *crtc_state,
3686351221ffSVille Syrjälä 			 struct intel_shared_dpll *pll)
3687351221ffSVille Syrjälä {
3688351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3689351221ffSVille Syrjälä 	enum icl_port_dpll_id port_dpll_id = ICL_PORT_DPLL_DEFAULT;
3690351221ffSVille Syrjälä 	struct icl_port_dpll *port_dpll = &crtc_state->icl_port_dplls[port_dpll_id];
3691351221ffSVille Syrjälä 	bool pll_active;
3692351221ffSVille Syrjälä 
3693086877a1SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
3694086877a1SVille Syrjälä 		return;
3695086877a1SVille Syrjälä 
3696351221ffSVille Syrjälä 	port_dpll->pll = pll;
3697351221ffSVille Syrjälä 	pll_active = intel_dpll_get_hw_state(i915, pll, &port_dpll->hw_state);
3698351221ffSVille Syrjälä 	drm_WARN_ON(&i915->drm, !pll_active);
3699351221ffSVille Syrjälä 
3700351221ffSVille Syrjälä 	icl_set_active_port_dpll(crtc_state, port_dpll_id);
3701351221ffSVille Syrjälä 
3702351221ffSVille Syrjälä 	crtc_state->port_clock = intel_dpll_get_freq(i915, crtc_state->shared_dpll,
3703351221ffSVille Syrjälä 						     &crtc_state->dpll_hw_state);
3704351221ffSVille Syrjälä }
3705351221ffSVille Syrjälä 
3706865b73eaSMatt Roper static void dg2_ddi_get_config(struct intel_encoder *encoder,
3707865b73eaSMatt Roper 				struct intel_crtc_state *crtc_state)
3708865b73eaSMatt Roper {
3709865b73eaSMatt Roper 	intel_mpllb_readout_hw_state(encoder, &crtc_state->mpllb_state);
3710865b73eaSMatt Roper 	crtc_state->port_clock = intel_mpllb_calc_port_clock(encoder, &crtc_state->mpllb_state);
3711865b73eaSMatt Roper 
3712865b73eaSMatt Roper 	intel_ddi_get_config(encoder, crtc_state);
3713865b73eaSMatt Roper }
3714865b73eaSMatt Roper 
3715351221ffSVille Syrjälä static void adls_ddi_get_config(struct intel_encoder *encoder,
3716351221ffSVille Syrjälä 				struct intel_crtc_state *crtc_state)
3717351221ffSVille Syrjälä {
3718351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, adls_ddi_get_pll(encoder));
3719351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3720351221ffSVille Syrjälä }
3721351221ffSVille Syrjälä 
3722351221ffSVille Syrjälä static void rkl_ddi_get_config(struct intel_encoder *encoder,
3723351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3724351221ffSVille Syrjälä {
3725351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, rkl_ddi_get_pll(encoder));
3726351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3727351221ffSVille Syrjälä }
3728351221ffSVille Syrjälä 
3729351221ffSVille Syrjälä static void dg1_ddi_get_config(struct intel_encoder *encoder,
3730351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3731351221ffSVille Syrjälä {
3732351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, dg1_ddi_get_pll(encoder));
3733351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3734351221ffSVille Syrjälä }
3735351221ffSVille Syrjälä 
3736351221ffSVille Syrjälä static void icl_ddi_combo_get_config(struct intel_encoder *encoder,
3737351221ffSVille Syrjälä 				     struct intel_crtc_state *crtc_state)
3738351221ffSVille Syrjälä {
3739351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, icl_ddi_combo_get_pll(encoder));
3740351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3741351221ffSVille Syrjälä }
3742351221ffSVille Syrjälä 
3743086877a1SVille Syrjälä static void icl_ddi_tc_get_clock(struct intel_encoder *encoder,
3744086877a1SVille Syrjälä 				 struct intel_crtc_state *crtc_state,
3745086877a1SVille Syrjälä 				 struct intel_shared_dpll *pll)
3746351221ffSVille Syrjälä {
3747351221ffSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3748351221ffSVille Syrjälä 	enum icl_port_dpll_id port_dpll_id;
3749351221ffSVille Syrjälä 	struct icl_port_dpll *port_dpll;
3750351221ffSVille Syrjälä 	bool pll_active;
3751351221ffSVille Syrjälä 
3752086877a1SVille Syrjälä 	if (drm_WARN_ON(&i915->drm, !pll))
3753086877a1SVille Syrjälä 		return;
3754351221ffSVille Syrjälä 
3755351221ffSVille Syrjälä 	if (intel_get_shared_dpll_id(i915, pll) == DPLL_ID_ICL_TBTPLL)
3756351221ffSVille Syrjälä 		port_dpll_id = ICL_PORT_DPLL_DEFAULT;
3757351221ffSVille Syrjälä 	else
3758351221ffSVille Syrjälä 		port_dpll_id = ICL_PORT_DPLL_MG_PHY;
3759351221ffSVille Syrjälä 
3760351221ffSVille Syrjälä 	port_dpll = &crtc_state->icl_port_dplls[port_dpll_id];
3761351221ffSVille Syrjälä 
3762351221ffSVille Syrjälä 	port_dpll->pll = pll;
3763351221ffSVille Syrjälä 	pll_active = intel_dpll_get_hw_state(i915, pll, &port_dpll->hw_state);
3764351221ffSVille Syrjälä 	drm_WARN_ON(&i915->drm, !pll_active);
3765351221ffSVille Syrjälä 
3766351221ffSVille Syrjälä 	icl_set_active_port_dpll(crtc_state, port_dpll_id);
3767351221ffSVille Syrjälä 
3768351221ffSVille Syrjälä 	if (intel_get_shared_dpll_id(i915, crtc_state->shared_dpll) == DPLL_ID_ICL_TBTPLL)
3769351221ffSVille Syrjälä 		crtc_state->port_clock = icl_calc_tbt_pll_link(i915, encoder->port);
3770351221ffSVille Syrjälä 	else
3771351221ffSVille Syrjälä 		crtc_state->port_clock = intel_dpll_get_freq(i915, crtc_state->shared_dpll,
3772351221ffSVille Syrjälä 							     &crtc_state->dpll_hw_state);
3773086877a1SVille Syrjälä }
3774351221ffSVille Syrjälä 
3775086877a1SVille Syrjälä static void icl_ddi_tc_get_config(struct intel_encoder *encoder,
3776086877a1SVille Syrjälä 				  struct intel_crtc_state *crtc_state)
3777086877a1SVille Syrjälä {
3778086877a1SVille Syrjälä 	icl_ddi_tc_get_clock(encoder, crtc_state, icl_ddi_tc_get_pll(encoder));
3779351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3780351221ffSVille Syrjälä }
3781351221ffSVille Syrjälä 
3782351221ffSVille Syrjälä static void bxt_ddi_get_config(struct intel_encoder *encoder,
3783351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3784351221ffSVille Syrjälä {
3785351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, bxt_ddi_get_pll(encoder));
3786351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3787351221ffSVille Syrjälä }
3788351221ffSVille Syrjälä 
3789351221ffSVille Syrjälä static void skl_ddi_get_config(struct intel_encoder *encoder,
3790351221ffSVille Syrjälä 			       struct intel_crtc_state *crtc_state)
3791351221ffSVille Syrjälä {
3792351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, skl_ddi_get_pll(encoder));
3793351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3794351221ffSVille Syrjälä }
3795351221ffSVille Syrjälä 
3796351221ffSVille Syrjälä void hsw_ddi_get_config(struct intel_encoder *encoder,
3797351221ffSVille Syrjälä 			struct intel_crtc_state *crtc_state)
3798351221ffSVille Syrjälä {
3799351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, crtc_state, hsw_ddi_get_pll(encoder));
3800351221ffSVille Syrjälä 	intel_ddi_get_config(encoder, crtc_state);
3801351221ffSVille Syrjälä }
3802351221ffSVille Syrjälä 
3803f9e76a6eSImre Deak static void intel_ddi_sync_state(struct intel_encoder *encoder,
3804f9e76a6eSImre Deak 				 const struct intel_crtc_state *crtc_state)
3805f9e76a6eSImre Deak {
3806f9e76a6eSImre Deak 	if (intel_crtc_has_dp_encoder(crtc_state))
3807f9e76a6eSImre Deak 		intel_dp_sync_state(encoder, crtc_state);
3808f9e76a6eSImre Deak }
3809f9e76a6eSImre Deak 
3810b671d6efSImre Deak static bool intel_ddi_initial_fastset_check(struct intel_encoder *encoder,
3811b671d6efSImre Deak 					    struct intel_crtc_state *crtc_state)
3812b671d6efSImre Deak {
3813b671d6efSImre Deak 	if (intel_crtc_has_dp_encoder(crtc_state))
3814b671d6efSImre Deak 		return intel_dp_initial_fastset_check(encoder, crtc_state);
3815b671d6efSImre Deak 
3816b671d6efSImre Deak 	return true;
3817b671d6efSImre Deak }
3818b671d6efSImre Deak 
3819379bc100SJani Nikula static enum intel_output_type
3820379bc100SJani Nikula intel_ddi_compute_output_type(struct intel_encoder *encoder,
3821379bc100SJani Nikula 			      struct intel_crtc_state *crtc_state,
3822379bc100SJani Nikula 			      struct drm_connector_state *conn_state)
3823379bc100SJani Nikula {
3824379bc100SJani Nikula 	switch (conn_state->connector->connector_type) {
3825379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_HDMIA:
3826379bc100SJani Nikula 		return INTEL_OUTPUT_HDMI;
3827379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_eDP:
3828379bc100SJani Nikula 		return INTEL_OUTPUT_EDP;
3829379bc100SJani Nikula 	case DRM_MODE_CONNECTOR_DisplayPort:
3830379bc100SJani Nikula 		return INTEL_OUTPUT_DP;
3831379bc100SJani Nikula 	default:
3832379bc100SJani Nikula 		MISSING_CASE(conn_state->connector->connector_type);
3833379bc100SJani Nikula 		return INTEL_OUTPUT_UNUSED;
3834379bc100SJani Nikula 	}
3835379bc100SJani Nikula }
3836379bc100SJani Nikula 
3837379bc100SJani Nikula static int intel_ddi_compute_config(struct intel_encoder *encoder,
3838379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config,
3839379bc100SJani Nikula 				    struct drm_connector_state *conn_state)
3840379bc100SJani Nikula {
38412225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
3842379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3843379bc100SJani Nikula 	enum port port = encoder->port;
3844379bc100SJani Nikula 	int ret;
3845379bc100SJani Nikula 
384610cf8e75SVille Syrjälä 	if (HAS_TRANSCODER(dev_priv, TRANSCODER_EDP) && port == PORT_A)
3847379bc100SJani Nikula 		pipe_config->cpu_transcoder = TRANSCODER_EDP;
3848379bc100SJani Nikula 
3849bdacf087SAnshuman Gupta 	if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_HDMI)) {
3850379bc100SJani Nikula 		ret = intel_hdmi_compute_config(encoder, pipe_config, conn_state);
3851bdacf087SAnshuman Gupta 	} else {
3852379bc100SJani Nikula 		ret = intel_dp_compute_config(encoder, pipe_config, conn_state);
3853bdacf087SAnshuman Gupta 	}
3854bdacf087SAnshuman Gupta 
3855379bc100SJani Nikula 	if (ret)
3856379bc100SJani Nikula 		return ret;
3857379bc100SJani Nikula 
3858379bc100SJani Nikula 	if (IS_HASWELL(dev_priv) && crtc->pipe == PIPE_A &&
3859379bc100SJani Nikula 	    pipe_config->cpu_transcoder == TRANSCODER_EDP)
3860379bc100SJani Nikula 		pipe_config->pch_pfit.force_thru =
3861379bc100SJani Nikula 			pipe_config->pch_pfit.enabled ||
3862379bc100SJani Nikula 			pipe_config->crc_enabled;
3863379bc100SJani Nikula 
38642446e1d6SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
3865379bc100SJani Nikula 		pipe_config->lane_lat_optim_mask =
3866379bc100SJani Nikula 			bxt_ddi_phy_calc_lane_lat_optim_mask(pipe_config->lane_count);
3867379bc100SJani Nikula 
3868379bc100SJani Nikula 	intel_ddi_compute_min_voltage_level(dev_priv, pipe_config);
3869379bc100SJani Nikula 
3870379bc100SJani Nikula 	return 0;
3871379bc100SJani Nikula }
3872379bc100SJani Nikula 
3873b50a1aa6SManasi Navare static bool mode_equal(const struct drm_display_mode *mode1,
3874b50a1aa6SManasi Navare 		       const struct drm_display_mode *mode2)
3875b50a1aa6SManasi Navare {
3876b50a1aa6SManasi Navare 	return drm_mode_match(mode1, mode2,
3877b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_TIMINGS |
3878b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_FLAGS |
3879b50a1aa6SManasi Navare 			      DRM_MODE_MATCH_3D_FLAGS) &&
3880b50a1aa6SManasi Navare 		mode1->clock == mode2->clock; /* we want an exact match */
3881b50a1aa6SManasi Navare }
3882b50a1aa6SManasi Navare 
3883b50a1aa6SManasi Navare static bool m_n_equal(const struct intel_link_m_n *m_n_1,
3884b50a1aa6SManasi Navare 		      const struct intel_link_m_n *m_n_2)
3885b50a1aa6SManasi Navare {
3886b50a1aa6SManasi Navare 	return m_n_1->tu == m_n_2->tu &&
3887b50a1aa6SManasi Navare 		m_n_1->gmch_m == m_n_2->gmch_m &&
3888b50a1aa6SManasi Navare 		m_n_1->gmch_n == m_n_2->gmch_n &&
3889b50a1aa6SManasi Navare 		m_n_1->link_m == m_n_2->link_m &&
3890b50a1aa6SManasi Navare 		m_n_1->link_n == m_n_2->link_n;
3891b50a1aa6SManasi Navare }
3892b50a1aa6SManasi Navare 
3893b50a1aa6SManasi Navare static bool crtcs_port_sync_compatible(const struct intel_crtc_state *crtc_state1,
3894b50a1aa6SManasi Navare 				       const struct intel_crtc_state *crtc_state2)
3895b50a1aa6SManasi Navare {
3896b50a1aa6SManasi Navare 	return crtc_state1->hw.active && crtc_state2->hw.active &&
3897b50a1aa6SManasi Navare 		crtc_state1->output_types == crtc_state2->output_types &&
3898b50a1aa6SManasi Navare 		crtc_state1->output_format == crtc_state2->output_format &&
3899b50a1aa6SManasi Navare 		crtc_state1->lane_count == crtc_state2->lane_count &&
3900b50a1aa6SManasi Navare 		crtc_state1->port_clock == crtc_state2->port_clock &&
3901b50a1aa6SManasi Navare 		mode_equal(&crtc_state1->hw.adjusted_mode,
3902b50a1aa6SManasi Navare 			   &crtc_state2->hw.adjusted_mode) &&
3903b50a1aa6SManasi Navare 		m_n_equal(&crtc_state1->dp_m_n, &crtc_state2->dp_m_n);
3904b50a1aa6SManasi Navare }
3905b50a1aa6SManasi Navare 
3906b50a1aa6SManasi Navare static u8
3907b50a1aa6SManasi Navare intel_ddi_port_sync_transcoders(const struct intel_crtc_state *ref_crtc_state,
3908b50a1aa6SManasi Navare 				int tile_group_id)
3909b50a1aa6SManasi Navare {
3910b50a1aa6SManasi Navare 	struct drm_connector *connector;
3911b50a1aa6SManasi Navare 	const struct drm_connector_state *conn_state;
3912b50a1aa6SManasi Navare 	struct drm_i915_private *dev_priv = to_i915(ref_crtc_state->uapi.crtc->dev);
3913b50a1aa6SManasi Navare 	struct intel_atomic_state *state =
3914b50a1aa6SManasi Navare 		to_intel_atomic_state(ref_crtc_state->uapi.state);
3915b50a1aa6SManasi Navare 	u8 transcoders = 0;
3916b50a1aa6SManasi Navare 	int i;
3917b50a1aa6SManasi Navare 
3918dc5b8ed5SVille Syrjälä 	/*
3919dc5b8ed5SVille Syrjälä 	 * We don't enable port sync on BDW due to missing w/as and
3920dc5b8ed5SVille Syrjälä 	 * due to not having adjusted the modeset sequence appropriately.
3921dc5b8ed5SVille Syrjälä 	 */
3922005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) < 9)
3923b50a1aa6SManasi Navare 		return 0;
3924b50a1aa6SManasi Navare 
3925b50a1aa6SManasi Navare 	if (!intel_crtc_has_type(ref_crtc_state, INTEL_OUTPUT_DP))
3926b50a1aa6SManasi Navare 		return 0;
3927b50a1aa6SManasi Navare 
3928b50a1aa6SManasi Navare 	for_each_new_connector_in_state(&state->base, connector, conn_state, i) {
3929b50a1aa6SManasi Navare 		struct intel_crtc *crtc = to_intel_crtc(conn_state->crtc);
3930b50a1aa6SManasi Navare 		const struct intel_crtc_state *crtc_state;
3931b50a1aa6SManasi Navare 
3932b50a1aa6SManasi Navare 		if (!crtc)
3933b50a1aa6SManasi Navare 			continue;
3934b50a1aa6SManasi Navare 
3935b50a1aa6SManasi Navare 		if (!connector->has_tile ||
3936b50a1aa6SManasi Navare 		    connector->tile_group->id !=
3937b50a1aa6SManasi Navare 		    tile_group_id)
3938b50a1aa6SManasi Navare 			continue;
3939b50a1aa6SManasi Navare 		crtc_state = intel_atomic_get_new_crtc_state(state,
3940b50a1aa6SManasi Navare 							     crtc);
3941b50a1aa6SManasi Navare 		if (!crtcs_port_sync_compatible(ref_crtc_state,
3942b50a1aa6SManasi Navare 						crtc_state))
3943b50a1aa6SManasi Navare 			continue;
3944b50a1aa6SManasi Navare 		transcoders |= BIT(crtc_state->cpu_transcoder);
3945b50a1aa6SManasi Navare 	}
3946b50a1aa6SManasi Navare 
3947b50a1aa6SManasi Navare 	return transcoders;
3948b50a1aa6SManasi Navare }
3949b50a1aa6SManasi Navare 
3950b50a1aa6SManasi Navare static int intel_ddi_compute_config_late(struct intel_encoder *encoder,
3951b50a1aa6SManasi Navare 					 struct intel_crtc_state *crtc_state,
3952b50a1aa6SManasi Navare 					 struct drm_connector_state *conn_state)
3953b50a1aa6SManasi Navare {
395447bdb1caSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
3955b50a1aa6SManasi Navare 	struct drm_connector *connector = conn_state->connector;
3956b50a1aa6SManasi Navare 	u8 port_sync_transcoders = 0;
3957b50a1aa6SManasi Navare 
395847bdb1caSJani Nikula 	drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] [CRTC:%d:%s]",
3959b50a1aa6SManasi Navare 		    encoder->base.base.id, encoder->base.name,
3960b50a1aa6SManasi Navare 		    crtc_state->uapi.crtc->base.id, crtc_state->uapi.crtc->name);
3961b50a1aa6SManasi Navare 
3962b50a1aa6SManasi Navare 	if (connector->has_tile)
3963b50a1aa6SManasi Navare 		port_sync_transcoders = intel_ddi_port_sync_transcoders(crtc_state,
3964b50a1aa6SManasi Navare 									connector->tile_group->id);
3965b50a1aa6SManasi Navare 
3966b50a1aa6SManasi Navare 	/*
3967b50a1aa6SManasi Navare 	 * EDP Transcoders cannot be ensalved
3968b50a1aa6SManasi Navare 	 * make them a master always when present
3969b50a1aa6SManasi Navare 	 */
3970b50a1aa6SManasi Navare 	if (port_sync_transcoders & BIT(TRANSCODER_EDP))
3971b50a1aa6SManasi Navare 		crtc_state->master_transcoder = TRANSCODER_EDP;
3972b50a1aa6SManasi Navare 	else
3973b50a1aa6SManasi Navare 		crtc_state->master_transcoder = ffs(port_sync_transcoders) - 1;
3974b50a1aa6SManasi Navare 
3975b50a1aa6SManasi Navare 	if (crtc_state->master_transcoder == crtc_state->cpu_transcoder) {
3976b50a1aa6SManasi Navare 		crtc_state->master_transcoder = INVALID_TRANSCODER;
3977b50a1aa6SManasi Navare 		crtc_state->sync_mode_slaves_mask =
3978b50a1aa6SManasi Navare 			port_sync_transcoders & ~BIT(crtc_state->cpu_transcoder);
3979b50a1aa6SManasi Navare 	}
3980b50a1aa6SManasi Navare 
3981b50a1aa6SManasi Navare 	return 0;
3982b50a1aa6SManasi Navare }
3983b50a1aa6SManasi Navare 
3984379bc100SJani Nikula static void intel_ddi_encoder_destroy(struct drm_encoder *encoder)
3985379bc100SJani Nikula {
39864a300e65SImre Deak 	struct drm_i915_private *i915 = to_i915(encoder->dev);
3987b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(to_intel_encoder(encoder));
3988379bc100SJani Nikula 
3989379bc100SJani Nikula 	intel_dp_encoder_flush_work(encoder);
39904a300e65SImre Deak 	intel_display_power_flush_work(i915);
3991379bc100SJani Nikula 
3992379bc100SJani Nikula 	drm_encoder_cleanup(encoder);
3993a6c6eac9SAnshuman Gupta 	if (dig_port)
3994a6c6eac9SAnshuman Gupta 		kfree(dig_port->hdcp_port_data.streams);
3995379bc100SJani Nikula 	kfree(dig_port);
3996379bc100SJani Nikula }
3997379bc100SJani Nikula 
3998764f6729SVille Syrjälä static void intel_ddi_encoder_reset(struct drm_encoder *encoder)
3999764f6729SVille Syrjälä {
4000764f6729SVille Syrjälä 	struct intel_dp *intel_dp = enc_to_intel_dp(to_intel_encoder(encoder));
4001764f6729SVille Syrjälä 
4002764f6729SVille Syrjälä 	intel_dp->reset_link_params = true;
4003764f6729SVille Syrjälä 
4004764f6729SVille Syrjälä 	intel_pps_encoder_reset(intel_dp);
4005764f6729SVille Syrjälä }
4006764f6729SVille Syrjälä 
4007379bc100SJani Nikula static const struct drm_encoder_funcs intel_ddi_funcs = {
4008764f6729SVille Syrjälä 	.reset = intel_ddi_encoder_reset,
4009379bc100SJani Nikula 	.destroy = intel_ddi_encoder_destroy,
4010379bc100SJani Nikula };
4011379bc100SJani Nikula 
4012379bc100SJani Nikula static struct intel_connector *
40137801f3b7SLucas De Marchi intel_ddi_init_dp_connector(struct intel_digital_port *dig_port)
4014379bc100SJani Nikula {
40157801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4016379bc100SJani Nikula 	struct intel_connector *connector;
40177801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4018379bc100SJani Nikula 
4019379bc100SJani Nikula 	connector = intel_connector_alloc();
4020379bc100SJani Nikula 	if (!connector)
4021379bc100SJani Nikula 		return NULL;
4022379bc100SJani Nikula 
40237801f3b7SLucas De Marchi 	dig_port->dp.output_reg = DDI_BUF_CTL(port);
40247801f3b7SLucas De Marchi 	dig_port->dp.prepare_link_retrain = intel_ddi_prepare_link_retrain;
40257801f3b7SLucas De Marchi 	dig_port->dp.set_link_train = intel_ddi_set_link_train;
40267801f3b7SLucas De Marchi 	dig_port->dp.set_idle_link_train = intel_ddi_set_idle_link_train;
4027eee3f911SVille Syrjälä 
4028a046a0daSMatt Roper 	if (IS_DG2(dev_priv))
4029a046a0daSMatt Roper 		dig_port->dp.set_signal_levels = dg2_set_signal_levels;
4030a046a0daSMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 12)
40317801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = tgl_set_signal_levels;
4032005e9537SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 11)
40337801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = icl_set_signal_levels;
40342446e1d6SMatt Roper 	else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
40357801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = bxt_set_signal_levels;
4036fb83f72cSVille Syrjälä 	else
40377801f3b7SLucas De Marchi 		dig_port->dp.set_signal_levels = hsw_set_signal_levels;
4038fb83f72cSVille Syrjälä 
40397801f3b7SLucas De Marchi 	dig_port->dp.voltage_max = intel_ddi_dp_voltage_max;
40407801f3b7SLucas De Marchi 	dig_port->dp.preemph_max = intel_ddi_dp_preemph_max;
404153de0a20SVille Syrjälä 
40427801f3b7SLucas De Marchi 	if (!intel_dp_init_connector(dig_port, connector)) {
4043379bc100SJani Nikula 		kfree(connector);
4044379bc100SJani Nikula 		return NULL;
4045379bc100SJani Nikula 	}
4046379bc100SJani Nikula 
4047379bc100SJani Nikula 	return connector;
4048379bc100SJani Nikula }
4049379bc100SJani Nikula 
4050379bc100SJani Nikula static int modeset_pipe(struct drm_crtc *crtc,
4051379bc100SJani Nikula 			struct drm_modeset_acquire_ctx *ctx)
4052379bc100SJani Nikula {
4053379bc100SJani Nikula 	struct drm_atomic_state *state;
4054379bc100SJani Nikula 	struct drm_crtc_state *crtc_state;
4055379bc100SJani Nikula 	int ret;
4056379bc100SJani Nikula 
4057379bc100SJani Nikula 	state = drm_atomic_state_alloc(crtc->dev);
4058379bc100SJani Nikula 	if (!state)
4059379bc100SJani Nikula 		return -ENOMEM;
4060379bc100SJani Nikula 
4061379bc100SJani Nikula 	state->acquire_ctx = ctx;
4062379bc100SJani Nikula 
4063379bc100SJani Nikula 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
4064379bc100SJani Nikula 	if (IS_ERR(crtc_state)) {
4065379bc100SJani Nikula 		ret = PTR_ERR(crtc_state);
4066379bc100SJani Nikula 		goto out;
4067379bc100SJani Nikula 	}
4068379bc100SJani Nikula 
4069379bc100SJani Nikula 	crtc_state->connectors_changed = true;
4070379bc100SJani Nikula 
4071379bc100SJani Nikula 	ret = drm_atomic_commit(state);
4072379bc100SJani Nikula out:
4073379bc100SJani Nikula 	drm_atomic_state_put(state);
4074379bc100SJani Nikula 
4075379bc100SJani Nikula 	return ret;
4076379bc100SJani Nikula }
4077379bc100SJani Nikula 
4078379bc100SJani Nikula static int intel_hdmi_reset_link(struct intel_encoder *encoder,
4079379bc100SJani Nikula 				 struct drm_modeset_acquire_ctx *ctx)
4080379bc100SJani Nikula {
4081379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4082b7d02c3aSVille Syrjälä 	struct intel_hdmi *hdmi = enc_to_intel_hdmi(encoder);
4083379bc100SJani Nikula 	struct intel_connector *connector = hdmi->attached_connector;
4084379bc100SJani Nikula 	struct i2c_adapter *adapter =
4085379bc100SJani Nikula 		intel_gmbus_get_adapter(dev_priv, hdmi->ddc_bus);
4086379bc100SJani Nikula 	struct drm_connector_state *conn_state;
4087379bc100SJani Nikula 	struct intel_crtc_state *crtc_state;
4088379bc100SJani Nikula 	struct intel_crtc *crtc;
4089379bc100SJani Nikula 	u8 config;
4090379bc100SJani Nikula 	int ret;
4091379bc100SJani Nikula 
4092379bc100SJani Nikula 	if (!connector || connector->base.status != connector_status_connected)
4093379bc100SJani Nikula 		return 0;
4094379bc100SJani Nikula 
4095379bc100SJani Nikula 	ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
4096379bc100SJani Nikula 			       ctx);
4097379bc100SJani Nikula 	if (ret)
4098379bc100SJani Nikula 		return ret;
4099379bc100SJani Nikula 
4100379bc100SJani Nikula 	conn_state = connector->base.state;
4101379bc100SJani Nikula 
4102379bc100SJani Nikula 	crtc = to_intel_crtc(conn_state->crtc);
4103379bc100SJani Nikula 	if (!crtc)
4104379bc100SJani Nikula 		return 0;
4105379bc100SJani Nikula 
4106379bc100SJani Nikula 	ret = drm_modeset_lock(&crtc->base.mutex, ctx);
4107379bc100SJani Nikula 	if (ret)
4108379bc100SJani Nikula 		return ret;
4109379bc100SJani Nikula 
4110379bc100SJani Nikula 	crtc_state = to_intel_crtc_state(crtc->base.state);
4111379bc100SJani Nikula 
41121de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm,
41131de143ccSPankaj Bharadiya 		    !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI));
4114379bc100SJani Nikula 
41151326a92cSMaarten Lankhorst 	if (!crtc_state->hw.active)
4116379bc100SJani Nikula 		return 0;
4117379bc100SJani Nikula 
4118379bc100SJani Nikula 	if (!crtc_state->hdmi_high_tmds_clock_ratio &&
4119379bc100SJani Nikula 	    !crtc_state->hdmi_scrambling)
4120379bc100SJani Nikula 		return 0;
4121379bc100SJani Nikula 
4122379bc100SJani Nikula 	if (conn_state->commit &&
4123379bc100SJani Nikula 	    !try_wait_for_completion(&conn_state->commit->hw_done))
4124379bc100SJani Nikula 		return 0;
4125379bc100SJani Nikula 
4126379bc100SJani Nikula 	ret = drm_scdc_readb(adapter, SCDC_TMDS_CONFIG, &config);
4127379bc100SJani Nikula 	if (ret < 0) {
412847bdb1caSJani Nikula 		drm_err(&dev_priv->drm, "Failed to read TMDS config: %d\n",
412947bdb1caSJani Nikula 			ret);
4130379bc100SJani Nikula 		return 0;
4131379bc100SJani Nikula 	}
4132379bc100SJani Nikula 
4133379bc100SJani Nikula 	if (!!(config & SCDC_TMDS_BIT_CLOCK_RATIO_BY_40) ==
4134379bc100SJani Nikula 	    crtc_state->hdmi_high_tmds_clock_ratio &&
4135379bc100SJani Nikula 	    !!(config & SCDC_SCRAMBLING_ENABLE) ==
4136379bc100SJani Nikula 	    crtc_state->hdmi_scrambling)
4137379bc100SJani Nikula 		return 0;
4138379bc100SJani Nikula 
4139379bc100SJani Nikula 	/*
4140379bc100SJani Nikula 	 * HDMI 2.0 says that one should not send scrambled data
4141379bc100SJani Nikula 	 * prior to configuring the sink scrambling, and that
4142379bc100SJani Nikula 	 * TMDS clock/data transmission should be suspended when
4143379bc100SJani Nikula 	 * changing the TMDS clock rate in the sink. So let's
4144379bc100SJani Nikula 	 * just do a full modeset here, even though some sinks
4145379bc100SJani Nikula 	 * would be perfectly happy if were to just reconfigure
4146379bc100SJani Nikula 	 * the SCDC settings on the fly.
4147379bc100SJani Nikula 	 */
4148379bc100SJani Nikula 	return modeset_pipe(&crtc->base, ctx);
4149379bc100SJani Nikula }
4150379bc100SJani Nikula 
41513944709dSImre Deak static enum intel_hotplug_state
41523944709dSImre Deak intel_ddi_hotplug(struct intel_encoder *encoder,
41538c8919c7SImre Deak 		  struct intel_connector *connector)
4154379bc100SJani Nikula {
4155b4df5405SImre Deak 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
4156b7d02c3aSVille Syrjälä 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
4157699390f7SVille Syrjälä 	struct intel_dp *intel_dp = &dig_port->dp;
4158b4df5405SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4159b4df5405SImre Deak 	bool is_tc = intel_phy_is_tc(i915, phy);
4160379bc100SJani Nikula 	struct drm_modeset_acquire_ctx ctx;
41613944709dSImre Deak 	enum intel_hotplug_state state;
4162379bc100SJani Nikula 	int ret;
4163379bc100SJani Nikula 
4164699390f7SVille Syrjälä 	if (intel_dp->compliance.test_active &&
4165699390f7SVille Syrjälä 	    intel_dp->compliance.test_type == DP_TEST_LINK_PHY_TEST_PATTERN) {
4166699390f7SVille Syrjälä 		intel_dp_phy_test(encoder);
4167699390f7SVille Syrjälä 		/* just do the PHY test and nothing else */
4168699390f7SVille Syrjälä 		return INTEL_HOTPLUG_UNCHANGED;
4169699390f7SVille Syrjälä 	}
4170699390f7SVille Syrjälä 
41718c8919c7SImre Deak 	state = intel_encoder_hotplug(encoder, connector);
4172379bc100SJani Nikula 
4173379bc100SJani Nikula 	drm_modeset_acquire_init(&ctx, 0);
4174379bc100SJani Nikula 
4175379bc100SJani Nikula 	for (;;) {
4176379bc100SJani Nikula 		if (connector->base.connector_type == DRM_MODE_CONNECTOR_HDMIA)
4177379bc100SJani Nikula 			ret = intel_hdmi_reset_link(encoder, &ctx);
4178379bc100SJani Nikula 		else
4179379bc100SJani Nikula 			ret = intel_dp_retrain_link(encoder, &ctx);
4180379bc100SJani Nikula 
4181379bc100SJani Nikula 		if (ret == -EDEADLK) {
4182379bc100SJani Nikula 			drm_modeset_backoff(&ctx);
4183379bc100SJani Nikula 			continue;
4184379bc100SJani Nikula 		}
4185379bc100SJani Nikula 
4186379bc100SJani Nikula 		break;
4187379bc100SJani Nikula 	}
4188379bc100SJani Nikula 
4189379bc100SJani Nikula 	drm_modeset_drop_locks(&ctx);
4190379bc100SJani Nikula 	drm_modeset_acquire_fini(&ctx);
41913a47ae20SPankaj Bharadiya 	drm_WARN(encoder->base.dev, ret,
41923a47ae20SPankaj Bharadiya 		 "Acquiring modeset locks failed with %i\n", ret);
4193379bc100SJani Nikula 
4194bb80c925SJosé Roberto de Souza 	/*
4195bb80c925SJosé Roberto de Souza 	 * Unpowered type-c dongles can take some time to boot and be
4196bb80c925SJosé Roberto de Souza 	 * responsible, so here giving some time to those dongles to power up
4197bb80c925SJosé Roberto de Souza 	 * and then retrying the probe.
4198bb80c925SJosé Roberto de Souza 	 *
4199bb80c925SJosé Roberto de Souza 	 * On many platforms the HDMI live state signal is known to be
4200bb80c925SJosé Roberto de Souza 	 * unreliable, so we can't use it to detect if a sink is connected or
4201bb80c925SJosé Roberto de Souza 	 * not. Instead we detect if it's connected based on whether we can
4202bb80c925SJosé Roberto de Souza 	 * read the EDID or not. That in turn has a problem during disconnect,
4203bb80c925SJosé Roberto de Souza 	 * since the HPD interrupt may be raised before the DDC lines get
4204bb80c925SJosé Roberto de Souza 	 * disconnected (due to how the required length of DDC vs. HPD
4205bb80c925SJosé Roberto de Souza 	 * connector pins are specified) and so we'll still be able to get a
4206bb80c925SJosé Roberto de Souza 	 * valid EDID. To solve this schedule another detection cycle if this
4207bb80c925SJosé Roberto de Souza 	 * time around we didn't detect any change in the sink's connection
4208bb80c925SJosé Roberto de Souza 	 * status.
4209b4df5405SImre Deak 	 *
4210b4df5405SImre Deak 	 * Type-c connectors which get their HPD signal deasserted then
4211b4df5405SImre Deak 	 * reasserted, without unplugging/replugging the sink from the
4212b4df5405SImre Deak 	 * connector, introduce a delay until the AUX channel communication
4213b4df5405SImre Deak 	 * becomes functional. Retry the detection for 5 seconds on type-c
4214b4df5405SImre Deak 	 * connectors to account for this delay.
4215bb80c925SJosé Roberto de Souza 	 */
4216b4df5405SImre Deak 	if (state == INTEL_HOTPLUG_UNCHANGED &&
4217b4df5405SImre Deak 	    connector->hotplug_retries < (is_tc ? 5 : 1) &&
4218bb80c925SJosé Roberto de Souza 	    !dig_port->dp.is_mst)
4219bb80c925SJosé Roberto de Souza 		state = INTEL_HOTPLUG_RETRY;
4220bb80c925SJosé Roberto de Souza 
42213944709dSImre Deak 	return state;
4222379bc100SJani Nikula }
4223379bc100SJani Nikula 
4224edc0e09cSVille Syrjälä static bool lpt_digital_port_connected(struct intel_encoder *encoder)
4225edc0e09cSVille Syrjälä {
4226edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4227c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.pch_hpd[encoder->hpd_pin];
4228edc0e09cSVille Syrjälä 
4229edc0e09cSVille Syrjälä 	return intel_de_read(dev_priv, SDEISR) & bit;
4230edc0e09cSVille Syrjälä }
4231edc0e09cSVille Syrjälä 
4232edc0e09cSVille Syrjälä static bool hsw_digital_port_connected(struct intel_encoder *encoder)
4233edc0e09cSVille Syrjälä {
4234edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4235c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.hpd[encoder->hpd_pin];
4236edc0e09cSVille Syrjälä 
4237c7e8a3d6SVille Syrjälä 	return intel_de_read(dev_priv, DEISR) & bit;
4238edc0e09cSVille Syrjälä }
4239edc0e09cSVille Syrjälä 
4240edc0e09cSVille Syrjälä static bool bdw_digital_port_connected(struct intel_encoder *encoder)
4241edc0e09cSVille Syrjälä {
4242edc0e09cSVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
4243c7e8a3d6SVille Syrjälä 	u32 bit = dev_priv->hotplug.hpd[encoder->hpd_pin];
4244edc0e09cSVille Syrjälä 
4245edc0e09cSVille Syrjälä 	return intel_de_read(dev_priv, GEN8_DE_PORT_ISR) & bit;
4246edc0e09cSVille Syrjälä }
4247edc0e09cSVille Syrjälä 
4248379bc100SJani Nikula static struct intel_connector *
42497801f3b7SLucas De Marchi intel_ddi_init_hdmi_connector(struct intel_digital_port *dig_port)
4250379bc100SJani Nikula {
4251379bc100SJani Nikula 	struct intel_connector *connector;
42527801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4253379bc100SJani Nikula 
4254379bc100SJani Nikula 	connector = intel_connector_alloc();
4255379bc100SJani Nikula 	if (!connector)
4256379bc100SJani Nikula 		return NULL;
4257379bc100SJani Nikula 
42587801f3b7SLucas De Marchi 	dig_port->hdmi.hdmi_reg = DDI_BUF_CTL(port);
42597801f3b7SLucas De Marchi 	intel_hdmi_init_connector(dig_port, connector);
4260379bc100SJani Nikula 
4261379bc100SJani Nikula 	return connector;
4262379bc100SJani Nikula }
4263379bc100SJani Nikula 
42647801f3b7SLucas De Marchi static bool intel_ddi_a_force_4_lanes(struct intel_digital_port *dig_port)
4265379bc100SJani Nikula {
42667801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4267379bc100SJani Nikula 
42687801f3b7SLucas De Marchi 	if (dig_port->base.port != PORT_A)
4269379bc100SJani Nikula 		return false;
4270379bc100SJani Nikula 
42717801f3b7SLucas De Marchi 	if (dig_port->saved_port_bits & DDI_A_4_LANES)
4272379bc100SJani Nikula 		return false;
4273379bc100SJani Nikula 
4274379bc100SJani Nikula 	/* Broxton/Geminilake: Bspec says that DDI_A_4_LANES is the only
4275379bc100SJani Nikula 	 *                     supported configuration
4276379bc100SJani Nikula 	 */
42772446e1d6SMatt Roper 	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
4278379bc100SJani Nikula 		return true;
4279379bc100SJani Nikula 
4280379bc100SJani Nikula 	return false;
4281379bc100SJani Nikula }
4282379bc100SJani Nikula 
4283379bc100SJani Nikula static int
42847801f3b7SLucas De Marchi intel_ddi_max_lanes(struct intel_digital_port *dig_port)
4285379bc100SJani Nikula {
42867801f3b7SLucas De Marchi 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
42877801f3b7SLucas De Marchi 	enum port port = dig_port->base.port;
4288379bc100SJani Nikula 	int max_lanes = 4;
4289379bc100SJani Nikula 
4290005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
4291379bc100SJani Nikula 		return max_lanes;
4292379bc100SJani Nikula 
4293379bc100SJani Nikula 	if (port == PORT_A || port == PORT_E) {
4294f7960e7fSJani Nikula 		if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
4295379bc100SJani Nikula 			max_lanes = port == PORT_A ? 4 : 0;
4296379bc100SJani Nikula 		else
4297379bc100SJani Nikula 			/* Both A and E share 2 lanes */
4298379bc100SJani Nikula 			max_lanes = 2;
4299379bc100SJani Nikula 	}
4300379bc100SJani Nikula 
4301379bc100SJani Nikula 	/*
4302379bc100SJani Nikula 	 * Some BIOS might fail to set this bit on port A if eDP
4303379bc100SJani Nikula 	 * wasn't lit up at boot.  Force this bit set when needed
4304379bc100SJani Nikula 	 * so we use the proper lane count for our calculations.
4305379bc100SJani Nikula 	 */
43067801f3b7SLucas De Marchi 	if (intel_ddi_a_force_4_lanes(dig_port)) {
430747bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
430847bdb1caSJani Nikula 			    "Forcing DDI_A_4_LANES for port A\n");
43097801f3b7SLucas De Marchi 		dig_port->saved_port_bits |= DDI_A_4_LANES;
4310379bc100SJani Nikula 		max_lanes = 4;
4311379bc100SJani Nikula 	}
4312379bc100SJani Nikula 
4313379bc100SJani Nikula 	return max_lanes;
4314379bc100SJani Nikula }
4315379bc100SJani Nikula 
4316ddff9a60SMatt Roper static bool hti_uses_phy(struct drm_i915_private *i915, enum phy phy)
4317ddff9a60SMatt Roper {
4318ddff9a60SMatt Roper 	return i915->hti_state & HDPORT_ENABLED &&
4319ff7fb44dSJosé Roberto de Souza 	       i915->hti_state & HDPORT_DDI_USED(phy);
4320ddff9a60SMatt Roper }
4321ddff9a60SMatt Roper 
4322ed2615a8SMatt Roper static enum hpd_pin xelpd_hpd_pin(struct drm_i915_private *dev_priv,
4323ed2615a8SMatt Roper 				  enum port port)
4324ed2615a8SMatt Roper {
4325ed2615a8SMatt Roper 	if (port >= PORT_D_XELPD)
4326ed2615a8SMatt Roper 		return HPD_PORT_D + port - PORT_D_XELPD;
4327ed2615a8SMatt Roper 	else if (port >= PORT_TC1)
4328ed2615a8SMatt Roper 		return HPD_PORT_TC1 + port - PORT_TC1;
4329ed2615a8SMatt Roper 	else
4330ed2615a8SMatt Roper 		return HPD_PORT_A + port - PORT_A;
4331ed2615a8SMatt Roper }
4332ed2615a8SMatt Roper 
4333229f31e2SLucas De Marchi static enum hpd_pin dg1_hpd_pin(struct drm_i915_private *dev_priv,
4334229f31e2SLucas De Marchi 				enum port port)
4335229f31e2SLucas De Marchi {
43361d8ca002SVille Syrjälä 	if (port >= PORT_TC1)
43371d8ca002SVille Syrjälä 		return HPD_PORT_C + port - PORT_TC1;
4338229f31e2SLucas De Marchi 	else
4339229f31e2SLucas De Marchi 		return HPD_PORT_A + port - PORT_A;
4340229f31e2SLucas De Marchi }
4341229f31e2SLucas De Marchi 
4342da51e4baSVille Syrjälä static enum hpd_pin tgl_hpd_pin(struct drm_i915_private *dev_priv,
4343da51e4baSVille Syrjälä 				enum port port)
4344da51e4baSVille Syrjälä {
43451d8ca002SVille Syrjälä 	if (port >= PORT_TC1)
43461d8ca002SVille Syrjälä 		return HPD_PORT_TC1 + port - PORT_TC1;
4347da51e4baSVille Syrjälä 	else
4348da51e4baSVille Syrjälä 		return HPD_PORT_A + port - PORT_A;
4349da51e4baSVille Syrjälä }
4350da51e4baSVille Syrjälä 
4351da51e4baSVille Syrjälä static enum hpd_pin rkl_hpd_pin(struct drm_i915_private *dev_priv,
4352da51e4baSVille Syrjälä 				enum port port)
4353da51e4baSVille Syrjälä {
4354da51e4baSVille Syrjälä 	if (HAS_PCH_TGP(dev_priv))
4355da51e4baSVille Syrjälä 		return tgl_hpd_pin(dev_priv, port);
4356da51e4baSVille Syrjälä 
43571d8ca002SVille Syrjälä 	if (port >= PORT_TC1)
43581d8ca002SVille Syrjälä 		return HPD_PORT_C + port - PORT_TC1;
4359da51e4baSVille Syrjälä 	else
4360da51e4baSVille Syrjälä 		return HPD_PORT_A + port - PORT_A;
4361da51e4baSVille Syrjälä }
4362da51e4baSVille Syrjälä 
4363da51e4baSVille Syrjälä static enum hpd_pin icl_hpd_pin(struct drm_i915_private *dev_priv,
4364da51e4baSVille Syrjälä 				enum port port)
4365da51e4baSVille Syrjälä {
4366da51e4baSVille Syrjälä 	if (port >= PORT_C)
4367da51e4baSVille Syrjälä 		return HPD_PORT_TC1 + port - PORT_C;
4368da51e4baSVille Syrjälä 	else
4369da51e4baSVille Syrjälä 		return HPD_PORT_A + port - PORT_A;
4370da51e4baSVille Syrjälä }
4371da51e4baSVille Syrjälä 
4372da51e4baSVille Syrjälä static enum hpd_pin ehl_hpd_pin(struct drm_i915_private *dev_priv,
4373da51e4baSVille Syrjälä 				enum port port)
4374da51e4baSVille Syrjälä {
4375da51e4baSVille Syrjälä 	if (port == PORT_D)
4376da51e4baSVille Syrjälä 		return HPD_PORT_A;
4377da51e4baSVille Syrjälä 
4378da51e4baSVille Syrjälä 	if (HAS_PCH_MCC(dev_priv))
4379da51e4baSVille Syrjälä 		return icl_hpd_pin(dev_priv, port);
4380da51e4baSVille Syrjälä 
4381da51e4baSVille Syrjälä 	return HPD_PORT_A + port - PORT_A;
4382da51e4baSVille Syrjälä }
4383da51e4baSVille Syrjälä 
4384c8455098SLyude Paul static enum hpd_pin skl_hpd_pin(struct drm_i915_private *dev_priv, enum port port)
4385c8455098SLyude Paul {
4386c8455098SLyude Paul 	if (HAS_PCH_TGP(dev_priv))
4387c8455098SLyude Paul 		return icl_hpd_pin(dev_priv, port);
4388c8455098SLyude Paul 
4389c8455098SLyude Paul 	return HPD_PORT_A + port - PORT_A;
4390c8455098SLyude Paul }
4391c8455098SLyude Paul 
439236ecb0ecSVille Syrjälä static bool intel_ddi_is_tc(struct drm_i915_private *i915, enum port port)
439336ecb0ecSVille Syrjälä {
4394005e9537SMatt Roper 	if (DISPLAY_VER(i915) >= 12)
439536ecb0ecSVille Syrjälä 		return port >= PORT_TC1;
4396005e9537SMatt Roper 	else if (DISPLAY_VER(i915) >= 11)
439736ecb0ecSVille Syrjälä 		return port >= PORT_C;
439836ecb0ecSVille Syrjälä 	else
439936ecb0ecSVille Syrjälä 		return false;
440036ecb0ecSVille Syrjälä }
440136ecb0ecSVille Syrjälä 
4402151ec347SImre Deak static void intel_ddi_encoder_suspend(struct intel_encoder *encoder)
4403151ec347SImre Deak {
4404151ec347SImre Deak 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4405151ec347SImre Deak 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4406151ec347SImre Deak 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4407151ec347SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4408151ec347SImre Deak 
4409151ec347SImre Deak 	intel_dp_encoder_suspend(encoder);
4410151ec347SImre Deak 
4411151ec347SImre Deak 	if (!intel_phy_is_tc(i915, phy))
4412151ec347SImre Deak 		return;
4413151ec347SImre Deak 
4414151ec347SImre Deak 	intel_tc_port_disconnect_phy(dig_port);
4415151ec347SImre Deak }
4416151ec347SImre Deak 
4417151ec347SImre Deak static void intel_ddi_encoder_shutdown(struct intel_encoder *encoder)
4418151ec347SImre Deak {
4419151ec347SImre Deak 	struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4420151ec347SImre Deak 	struct drm_i915_private *i915 = dp_to_i915(intel_dp);
4421151ec347SImre Deak 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
4422151ec347SImre Deak 	enum phy phy = intel_port_to_phy(i915, encoder->port);
4423151ec347SImre Deak 
4424151ec347SImre Deak 	intel_dp_encoder_shutdown(encoder);
4425151ec347SImre Deak 
4426151ec347SImre Deak 	if (!intel_phy_is_tc(i915, phy))
4427151ec347SImre Deak 		return;
4428151ec347SImre Deak 
4429151ec347SImre Deak 	intel_tc_port_disconnect_phy(dig_port);
4430151ec347SImre Deak }
4431151ec347SImre Deak 
443283566d13SVille Syrjälä #define port_tc_name(port) ((port) - PORT_TC1 + '1')
443383566d13SVille Syrjälä #define tc_port_name(tc_port) ((tc_port) - TC_PORT_1 + '1')
443483566d13SVille Syrjälä 
4435379bc100SJani Nikula void intel_ddi_init(struct drm_i915_private *dev_priv, enum port port)
4436379bc100SJani Nikula {
44377801f3b7SLucas De Marchi 	struct intel_digital_port *dig_port;
443870dfbc29SLucas De Marchi 	struct intel_encoder *encoder;
443945c0673aSJani Nikula 	const struct intel_bios_encoder_data *devdata;
4440f542d671SKai-Heng Feng 	bool init_hdmi, init_dp;
4441d8fe2ab6SMatt Roper 	enum phy phy = intel_port_to_phy(dev_priv, port);
4442379bc100SJani Nikula 
4443ddff9a60SMatt Roper 	/*
4444ddff9a60SMatt Roper 	 * On platforms with HTI (aka HDPORT), if it's enabled at boot it may
4445ddff9a60SMatt Roper 	 * have taken over some of the PHYs and made them unavailable to the
4446ddff9a60SMatt Roper 	 * driver.  In that case we should skip initializing the corresponding
4447ddff9a60SMatt Roper 	 * outputs.
4448ddff9a60SMatt Roper 	 */
4449ddff9a60SMatt Roper 	if (hti_uses_phy(dev_priv, phy)) {
4450ddff9a60SMatt Roper 		drm_dbg_kms(&dev_priv->drm, "PORT %c / PHY %c reserved by HTI\n",
4451ddff9a60SMatt Roper 			    port_name(port), phy_name(phy));
4452ddff9a60SMatt Roper 		return;
4453ddff9a60SMatt Roper 	}
4454ddff9a60SMatt Roper 
445545c0673aSJani Nikula 	devdata = intel_bios_encoder_data_lookup(dev_priv, port);
445645c0673aSJani Nikula 	if (!devdata) {
445745c0673aSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
445845c0673aSJani Nikula 			    "VBT says port %c is not present\n",
445945c0673aSJani Nikula 			    port_name(port));
446045c0673aSJani Nikula 		return;
446145c0673aSJani Nikula 	}
446245c0673aSJani Nikula 
446345c0673aSJani Nikula 	init_hdmi = intel_bios_encoder_supports_dvi(devdata) ||
446445c0673aSJani Nikula 		intel_bios_encoder_supports_hdmi(devdata);
446545c0673aSJani Nikula 	init_dp = intel_bios_encoder_supports_dp(devdata);
4466379bc100SJani Nikula 
4467379bc100SJani Nikula 	if (intel_bios_is_lspcon_present(dev_priv, port)) {
4468379bc100SJani Nikula 		/*
4469379bc100SJani Nikula 		 * Lspcon device needs to be driven with DP connector
4470379bc100SJani Nikula 		 * with special detection sequence. So make sure DP
4471379bc100SJani Nikula 		 * is initialized before lspcon.
4472379bc100SJani Nikula 		 */
4473379bc100SJani Nikula 		init_dp = true;
4474379bc100SJani Nikula 		init_hdmi = false;
447547bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm, "VBT says port %c has lspcon\n",
447647bdb1caSJani Nikula 			    port_name(port));
4477379bc100SJani Nikula 	}
4478379bc100SJani Nikula 
4479379bc100SJani Nikula 	if (!init_dp && !init_hdmi) {
448047bdb1caSJani Nikula 		drm_dbg_kms(&dev_priv->drm,
448147bdb1caSJani Nikula 			    "VBT says port %c is not DVI/HDMI/DP compatible, respect it\n",
4482379bc100SJani Nikula 			    port_name(port));
4483379bc100SJani Nikula 		return;
4484379bc100SJani Nikula 	}
4485379bc100SJani Nikula 
44867801f3b7SLucas De Marchi 	dig_port = kzalloc(sizeof(*dig_port), GFP_KERNEL);
44877801f3b7SLucas De Marchi 	if (!dig_port)
4488379bc100SJani Nikula 		return;
4489379bc100SJani Nikula 
44907801f3b7SLucas De Marchi 	encoder = &dig_port->base;
4491c0a950d1SJani Nikula 	encoder->devdata = devdata;
4492379bc100SJani Nikula 
4493ed2615a8SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 13 && port >= PORT_D_XELPD) {
4494ed2615a8SMatt Roper 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
4495ed2615a8SMatt Roper 				 DRM_MODE_ENCODER_TMDS,
4496ed2615a8SMatt Roper 				 "DDI %c/PHY %c",
4497ed2615a8SMatt Roper 				 port_name(port - PORT_D_XELPD + PORT_D),
4498ed2615a8SMatt Roper 				 phy_name(phy));
4499ed2615a8SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 12) {
45002d709a5aSVille Syrjälä 		enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
45012d709a5aSVille Syrjälä 
450270dfbc29SLucas De Marchi 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
45032d709a5aSVille Syrjälä 				 DRM_MODE_ENCODER_TMDS,
45042d709a5aSVille Syrjälä 				 "DDI %s%c/PHY %s%c",
45052d709a5aSVille Syrjälä 				 port >= PORT_TC1 ? "TC" : "",
450683566d13SVille Syrjälä 				 port >= PORT_TC1 ? port_tc_name(port) : port_name(port),
45072d709a5aSVille Syrjälä 				 tc_port != TC_PORT_NONE ? "TC" : "",
450883566d13SVille Syrjälä 				 tc_port != TC_PORT_NONE ? tc_port_name(tc_port) : phy_name(phy));
4509005e9537SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 11) {
45102d709a5aSVille Syrjälä 		enum tc_port tc_port = intel_port_to_tc(dev_priv, port);
45112d709a5aSVille Syrjälä 
45122d709a5aSVille Syrjälä 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
45132d709a5aSVille Syrjälä 				 DRM_MODE_ENCODER_TMDS,
45142d709a5aSVille Syrjälä 				 "DDI %c%s/PHY %s%c",
45152d709a5aSVille Syrjälä 				 port_name(port),
45162d709a5aSVille Syrjälä 				 port >= PORT_C ? " (TC)" : "",
45172d709a5aSVille Syrjälä 				 tc_port != TC_PORT_NONE ? "TC" : "",
451883566d13SVille Syrjälä 				 tc_port != TC_PORT_NONE ? tc_port_name(tc_port) : phy_name(phy));
45192d709a5aSVille Syrjälä 	} else {
45202d709a5aSVille Syrjälä 		drm_encoder_init(&dev_priv->drm, &encoder->base, &intel_ddi_funcs,
45212d709a5aSVille Syrjälä 				 DRM_MODE_ENCODER_TMDS,
45222d709a5aSVille Syrjälä 				 "DDI %c/PHY %c", port_name(port),  phy_name(phy));
45232d709a5aSVille Syrjälä 	}
4524379bc100SJani Nikula 
452536e5e704SSean Paul 	mutex_init(&dig_port->hdcp_mutex);
452636e5e704SSean Paul 	dig_port->num_hdcp_streams = 0;
452736e5e704SSean Paul 
452870dfbc29SLucas De Marchi 	encoder->hotplug = intel_ddi_hotplug;
452970dfbc29SLucas De Marchi 	encoder->compute_output_type = intel_ddi_compute_output_type;
453070dfbc29SLucas De Marchi 	encoder->compute_config = intel_ddi_compute_config;
4531b50a1aa6SManasi Navare 	encoder->compute_config_late = intel_ddi_compute_config_late;
453270dfbc29SLucas De Marchi 	encoder->enable = intel_enable_ddi;
453370dfbc29SLucas De Marchi 	encoder->pre_pll_enable = intel_ddi_pre_pll_enable;
453470dfbc29SLucas De Marchi 	encoder->pre_enable = intel_ddi_pre_enable;
453584030adbSJosé Roberto de Souza 	encoder->pre_disable = intel_pre_disable_ddi;
453670dfbc29SLucas De Marchi 	encoder->disable = intel_disable_ddi;
453770dfbc29SLucas De Marchi 	encoder->post_disable = intel_ddi_post_disable;
453870dfbc29SLucas De Marchi 	encoder->update_pipe = intel_ddi_update_pipe;
453970dfbc29SLucas De Marchi 	encoder->get_hw_state = intel_ddi_get_hw_state;
4540f9e76a6eSImre Deak 	encoder->sync_state = intel_ddi_sync_state;
4541b671d6efSImre Deak 	encoder->initial_fastset_check = intel_ddi_initial_fastset_check;
4542151ec347SImre Deak 	encoder->suspend = intel_ddi_encoder_suspend;
4543151ec347SImre Deak 	encoder->shutdown = intel_ddi_encoder_shutdown;
454470dfbc29SLucas De Marchi 	encoder->get_power_domains = intel_ddi_get_power_domains;
454570dfbc29SLucas De Marchi 
454670dfbc29SLucas De Marchi 	encoder->type = INTEL_OUTPUT_DDI;
454770dfbc29SLucas De Marchi 	encoder->power_domain = intel_port_to_power_domain(port);
454870dfbc29SLucas De Marchi 	encoder->port = port;
454970dfbc29SLucas De Marchi 	encoder->cloneable = 0;
455070dfbc29SLucas De Marchi 	encoder->pipe_mask = ~0;
4551da51e4baSVille Syrjälä 
4552865b73eaSMatt Roper 	if (IS_DG2(dev_priv)) {
4553f82f2563SMatt Roper 		encoder->enable_clock = intel_mpllb_enable;
4554f82f2563SMatt Roper 		encoder->disable_clock = intel_mpllb_disable;
4555865b73eaSMatt Roper 		encoder->get_config = dg2_ddi_get_config;
4556865b73eaSMatt Roper 	} else if (IS_ALDERLAKE_S(dev_priv)) {
455740b316d4SVille Syrjälä 		encoder->enable_clock = adls_ddi_enable_clock;
455840b316d4SVille Syrjälä 		encoder->disable_clock = adls_ddi_disable_clock;
45590fbd8694SVille Syrjälä 		encoder->is_clock_enabled = adls_ddi_is_clock_enabled;
4560351221ffSVille Syrjälä 		encoder->get_config = adls_ddi_get_config;
456140b316d4SVille Syrjälä 	} else if (IS_ROCKETLAKE(dev_priv)) {
456240b316d4SVille Syrjälä 		encoder->enable_clock = rkl_ddi_enable_clock;
456340b316d4SVille Syrjälä 		encoder->disable_clock = rkl_ddi_disable_clock;
45640fbd8694SVille Syrjälä 		encoder->is_clock_enabled = rkl_ddi_is_clock_enabled;
4565351221ffSVille Syrjälä 		encoder->get_config = rkl_ddi_get_config;
456636ecb0ecSVille Syrjälä 	} else if (IS_DG1(dev_priv)) {
456735bb6b1aSVille Syrjälä 		encoder->enable_clock = dg1_ddi_enable_clock;
456835bb6b1aSVille Syrjälä 		encoder->disable_clock = dg1_ddi_disable_clock;
45690fbd8694SVille Syrjälä 		encoder->is_clock_enabled = dg1_ddi_is_clock_enabled;
4570351221ffSVille Syrjälä 		encoder->get_config = dg1_ddi_get_config;
457136ecb0ecSVille Syrjälä 	} else if (IS_JSL_EHL(dev_priv)) {
457236ecb0ecSVille Syrjälä 		if (intel_ddi_is_tc(dev_priv, port)) {
457336ecb0ecSVille Syrjälä 			encoder->enable_clock = jsl_ddi_tc_enable_clock;
457436ecb0ecSVille Syrjälä 			encoder->disable_clock = jsl_ddi_tc_disable_clock;
45750fbd8694SVille Syrjälä 			encoder->is_clock_enabled = jsl_ddi_tc_is_clock_enabled;
4576351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_combo_get_config;
457736ecb0ecSVille Syrjälä 		} else {
457836ecb0ecSVille Syrjälä 			encoder->enable_clock = icl_ddi_combo_enable_clock;
457936ecb0ecSVille Syrjälä 			encoder->disable_clock = icl_ddi_combo_disable_clock;
45800fbd8694SVille Syrjälä 			encoder->is_clock_enabled = icl_ddi_combo_is_clock_enabled;
4581351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_combo_get_config;
458236ecb0ecSVille Syrjälä 		}
4583005e9537SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 11) {
458436ecb0ecSVille Syrjälä 		if (intel_ddi_is_tc(dev_priv, port)) {
458536ecb0ecSVille Syrjälä 			encoder->enable_clock = icl_ddi_tc_enable_clock;
458636ecb0ecSVille Syrjälä 			encoder->disable_clock = icl_ddi_tc_disable_clock;
45870fbd8694SVille Syrjälä 			encoder->is_clock_enabled = icl_ddi_tc_is_clock_enabled;
4588351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_tc_get_config;
458936ecb0ecSVille Syrjälä 		} else {
459036ecb0ecSVille Syrjälä 			encoder->enable_clock = icl_ddi_combo_enable_clock;
459136ecb0ecSVille Syrjälä 			encoder->disable_clock = icl_ddi_combo_disable_clock;
45920fbd8694SVille Syrjälä 			encoder->is_clock_enabled = icl_ddi_combo_is_clock_enabled;
4593351221ffSVille Syrjälä 			encoder->get_config = icl_ddi_combo_get_config;
459436ecb0ecSVille Syrjälä 		}
45952446e1d6SMatt Roper 	} else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
4596351221ffSVille Syrjälä 		/* BXT/GLK have fixed PLL->port mapping */
4597351221ffSVille Syrjälä 		encoder->get_config = bxt_ddi_get_config;
459893e7e61eSLucas De Marchi 	} else if (DISPLAY_VER(dev_priv) == 9) {
459938e31f1aSVille Syrjälä 		encoder->enable_clock = skl_ddi_enable_clock;
460038e31f1aSVille Syrjälä 		encoder->disable_clock = skl_ddi_disable_clock;
46010fbd8694SVille Syrjälä 		encoder->is_clock_enabled = skl_ddi_is_clock_enabled;
4602351221ffSVille Syrjälä 		encoder->get_config = skl_ddi_get_config;
460338e31f1aSVille Syrjälä 	} else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
4604d135368dSVille Syrjälä 		encoder->enable_clock = hsw_ddi_enable_clock;
4605d135368dSVille Syrjälä 		encoder->disable_clock = hsw_ddi_disable_clock;
46060fbd8694SVille Syrjälä 		encoder->is_clock_enabled = hsw_ddi_is_clock_enabled;
4607351221ffSVille Syrjälä 		encoder->get_config = hsw_ddi_get_config;
4608d135368dSVille Syrjälä 	}
4609d135368dSVille Syrjälä 
4610c40a253bSVille Syrjälä 	intel_ddi_buf_trans_init(encoder);
4611c40a253bSVille Syrjälä 
4612ed2615a8SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 13)
4613ed2615a8SMatt Roper 		encoder->hpd_pin = xelpd_hpd_pin(dev_priv, port);
4614ed2615a8SMatt Roper 	else if (IS_DG1(dev_priv))
4615229f31e2SLucas De Marchi 		encoder->hpd_pin = dg1_hpd_pin(dev_priv, port);
4616229f31e2SLucas De Marchi 	else if (IS_ROCKETLAKE(dev_priv))
4617da51e4baSVille Syrjälä 		encoder->hpd_pin = rkl_hpd_pin(dev_priv, port);
4618005e9537SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 12)
4619da51e4baSVille Syrjälä 		encoder->hpd_pin = tgl_hpd_pin(dev_priv, port);
462024ea098bSTejas Upadhyay 	else if (IS_JSL_EHL(dev_priv))
4621da51e4baSVille Syrjälä 		encoder->hpd_pin = ehl_hpd_pin(dev_priv, port);
462293e7e61eSLucas De Marchi 	else if (DISPLAY_VER(dev_priv) == 11)
4623da51e4baSVille Syrjälä 		encoder->hpd_pin = icl_hpd_pin(dev_priv, port);
462493e7e61eSLucas De Marchi 	else if (DISPLAY_VER(dev_priv) == 9 && !IS_BROXTON(dev_priv))
4625c8455098SLyude Paul 		encoder->hpd_pin = skl_hpd_pin(dev_priv, port);
4626da51e4baSVille Syrjälä 	else
462703c7e4f1SVille Syrjälä 		encoder->hpd_pin = intel_hpd_pin_default(dev_priv, port);
4628379bc100SJani Nikula 
4629005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11)
46307801f3b7SLucas De Marchi 		dig_port->saved_port_bits =
46317801f3b7SLucas De Marchi 			intel_de_read(dev_priv, DDI_BUF_CTL(port))
46327801f3b7SLucas De Marchi 			& DDI_BUF_PORT_REVERSAL;
4633379bc100SJani Nikula 	else
46347801f3b7SLucas De Marchi 		dig_port->saved_port_bits =
46357801f3b7SLucas De Marchi 			intel_de_read(dev_priv, DDI_BUF_CTL(port))
46367801f3b7SLucas De Marchi 			& (DDI_BUF_PORT_REVERSAL | DDI_A_4_LANES);
463770dfbc29SLucas De Marchi 
4638aaab24bbSUma Shankar 	if (intel_bios_is_lane_reversal_needed(dev_priv, port))
4639aaab24bbSUma Shankar 		dig_port->saved_port_bits |= DDI_BUF_PORT_REVERSAL;
4640aaab24bbSUma Shankar 
46417801f3b7SLucas De Marchi 	dig_port->dp.output_reg = INVALID_MMIO_REG;
46427801f3b7SLucas De Marchi 	dig_port->max_lanes = intel_ddi_max_lanes(dig_port);
46437801f3b7SLucas De Marchi 	dig_port->aux_ch = intel_bios_port_aux_ch(dev_priv, port);
4644379bc100SJani Nikula 
4645d8fe2ab6SMatt Roper 	if (intel_phy_is_tc(dev_priv, phy)) {
4646c5faae5aSJani Nikula 		bool is_legacy =
4647f08fbe6aSJani Nikula 			!intel_bios_encoder_supports_typec_usb(devdata) &&
4648f08fbe6aSJani Nikula 			!intel_bios_encoder_supports_tbt(devdata);
4649379bc100SJani Nikula 
46507801f3b7SLucas De Marchi 		intel_tc_port_init(dig_port, is_legacy);
465124a7bfe0SImre Deak 
465270dfbc29SLucas De Marchi 		encoder->update_prepare = intel_ddi_update_prepare;
465370dfbc29SLucas De Marchi 		encoder->update_complete = intel_ddi_update_complete;
4654ab7bc4e1SImre Deak 	}
4655ab7bc4e1SImre Deak 
46561de143ccSPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, port > PORT_I);
46577801f3b7SLucas De Marchi 	dig_port->ddi_io_power_domain = POWER_DOMAIN_PORT_DDI_A_IO +
4658327f8d8cSLucas De Marchi 					      port - PORT_A;
4659379bc100SJani Nikula 
4660379bc100SJani Nikula 	if (init_dp) {
46617801f3b7SLucas De Marchi 		if (!intel_ddi_init_dp_connector(dig_port))
4662379bc100SJani Nikula 			goto err;
4663379bc100SJani Nikula 
46647801f3b7SLucas De Marchi 		dig_port->hpd_pulse = intel_dp_hpd_pulse;
4665bc71194eSJani Nikula 
46667bc188ccSJani Nikula 		/* Splitter enable for eDP MSO is limited to certain pipes. */
46677bc188ccSJani Nikula 		if (dig_port->dp.mso_link_count) {
4668bc71194eSJani Nikula 			encoder->pipe_mask = BIT(PIPE_A);
46697bc188ccSJani Nikula 			if (IS_ALDERLAKE_P(dev_priv))
46707bc188ccSJani Nikula 				encoder->pipe_mask |= BIT(PIPE_B);
46717bc188ccSJani Nikula 		}
4672379bc100SJani Nikula 	}
4673379bc100SJani Nikula 
4674379bc100SJani Nikula 	/* In theory we don't need the encoder->type check, but leave it just in
4675379bc100SJani Nikula 	 * case we have some really bad VBTs... */
467670dfbc29SLucas De Marchi 	if (encoder->type != INTEL_OUTPUT_EDP && init_hdmi) {
46777801f3b7SLucas De Marchi 		if (!intel_ddi_init_hdmi_connector(dig_port))
4678379bc100SJani Nikula 			goto err;
4679379bc100SJani Nikula 	}
4680379bc100SJani Nikula 
4681005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11) {
4682edc0e09cSVille Syrjälä 		if (intel_phy_is_tc(dev_priv, phy))
46837801f3b7SLucas De Marchi 			dig_port->connected = intel_tc_port_connected;
4684edc0e09cSVille Syrjälä 		else
46857801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
4686005e9537SMatt Roper 	} else if (DISPLAY_VER(dev_priv) >= 8) {
46872446e1d6SMatt Roper 		if (port == PORT_A || IS_GEMINILAKE(dev_priv) ||
46882446e1d6SMatt Roper 		    IS_BROXTON(dev_priv))
46897801f3b7SLucas De Marchi 			dig_port->connected = bdw_digital_port_connected;
4690edc0e09cSVille Syrjälä 		else
46917801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
4692edc0e09cSVille Syrjälä 	} else {
4693c7e8a3d6SVille Syrjälä 		if (port == PORT_A)
46947801f3b7SLucas De Marchi 			dig_port->connected = hsw_digital_port_connected;
4695edc0e09cSVille Syrjälä 		else
46967801f3b7SLucas De Marchi 			dig_port->connected = lpt_digital_port_connected;
4697edc0e09cSVille Syrjälä 	}
4698edc0e09cSVille Syrjälä 
46997801f3b7SLucas De Marchi 	intel_infoframe_init(dig_port);
4700379bc100SJani Nikula 
4701379bc100SJani Nikula 	return;
4702379bc100SJani Nikula 
4703379bc100SJani Nikula err:
470470dfbc29SLucas De Marchi 	drm_encoder_cleanup(&encoder->base);
47057801f3b7SLucas De Marchi 	kfree(dig_port);
4706379bc100SJani Nikula }
4707