1 /*
2  * Copyright © 2018 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Madhav Chauhan <madhav.chauhan@intel.com>
25  *   Jani Nikula <jani.nikula@intel.com>
26  */
27 
28 #include <drm/drm_atomic_helper.h>
29 #include <drm/drm_mipi_dsi.h>
30 
31 #include "intel_atomic.h"
32 #include "intel_backlight.h"
33 #include "intel_combo_phy.h"
34 #include "intel_connector.h"
35 #include "intel_crtc.h"
36 #include "intel_ddi.h"
37 #include "intel_de.h"
38 #include "intel_dsi.h"
39 #include "intel_panel.h"
40 #include "intel_vdsc.h"
41 #include "skl_scaler.h"
42 #include "skl_universal_plane.h"
43 
44 static int header_credits_available(struct drm_i915_private *dev_priv,
45 				    enum transcoder dsi_trans)
46 {
47 	return (intel_de_read(dev_priv, DSI_CMD_TXCTL(dsi_trans)) & FREE_HEADER_CREDIT_MASK)
48 		>> FREE_HEADER_CREDIT_SHIFT;
49 }
50 
51 static int payload_credits_available(struct drm_i915_private *dev_priv,
52 				     enum transcoder dsi_trans)
53 {
54 	return (intel_de_read(dev_priv, DSI_CMD_TXCTL(dsi_trans)) & FREE_PLOAD_CREDIT_MASK)
55 		>> FREE_PLOAD_CREDIT_SHIFT;
56 }
57 
58 static bool wait_for_header_credits(struct drm_i915_private *dev_priv,
59 				    enum transcoder dsi_trans, int hdr_credit)
60 {
61 	if (wait_for_us(header_credits_available(dev_priv, dsi_trans) >=
62 			hdr_credit, 100)) {
63 		drm_err(&dev_priv->drm, "DSI header credits not released\n");
64 		return false;
65 	}
66 
67 	return true;
68 }
69 
70 static bool wait_for_payload_credits(struct drm_i915_private *dev_priv,
71 				     enum transcoder dsi_trans, int payld_credit)
72 {
73 	if (wait_for_us(payload_credits_available(dev_priv, dsi_trans) >=
74 			payld_credit, 100)) {
75 		drm_err(&dev_priv->drm, "DSI payload credits not released\n");
76 		return false;
77 	}
78 
79 	return true;
80 }
81 
82 static enum transcoder dsi_port_to_transcoder(enum port port)
83 {
84 	if (port == PORT_A)
85 		return TRANSCODER_DSI_0;
86 	else
87 		return TRANSCODER_DSI_1;
88 }
89 
90 static void wait_for_cmds_dispatched_to_panel(struct intel_encoder *encoder)
91 {
92 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
93 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
94 	struct mipi_dsi_device *dsi;
95 	enum port port;
96 	enum transcoder dsi_trans;
97 	int ret;
98 
99 	/* wait for header/payload credits to be released */
100 	for_each_dsi_port(port, intel_dsi->ports) {
101 		dsi_trans = dsi_port_to_transcoder(port);
102 		wait_for_header_credits(dev_priv, dsi_trans, MAX_HEADER_CREDIT);
103 		wait_for_payload_credits(dev_priv, dsi_trans, MAX_PLOAD_CREDIT);
104 	}
105 
106 	/* send nop DCS command */
107 	for_each_dsi_port(port, intel_dsi->ports) {
108 		dsi = intel_dsi->dsi_hosts[port]->device;
109 		dsi->mode_flags |= MIPI_DSI_MODE_LPM;
110 		dsi->channel = 0;
111 		ret = mipi_dsi_dcs_nop(dsi);
112 		if (ret < 0)
113 			drm_err(&dev_priv->drm,
114 				"error sending DCS NOP command\n");
115 	}
116 
117 	/* wait for header credits to be released */
118 	for_each_dsi_port(port, intel_dsi->ports) {
119 		dsi_trans = dsi_port_to_transcoder(port);
120 		wait_for_header_credits(dev_priv, dsi_trans, MAX_HEADER_CREDIT);
121 	}
122 
123 	/* wait for LP TX in progress bit to be cleared */
124 	for_each_dsi_port(port, intel_dsi->ports) {
125 		dsi_trans = dsi_port_to_transcoder(port);
126 		if (wait_for_us(!(intel_de_read(dev_priv, DSI_LP_MSG(dsi_trans)) &
127 				  LPTX_IN_PROGRESS), 20))
128 			drm_err(&dev_priv->drm, "LPTX bit not cleared\n");
129 	}
130 }
131 
132 static int dsi_send_pkt_payld(struct intel_dsi_host *host,
133 			      const struct mipi_dsi_packet *packet)
134 {
135 	struct intel_dsi *intel_dsi = host->intel_dsi;
136 	struct drm_i915_private *i915 = to_i915(intel_dsi->base.base.dev);
137 	enum transcoder dsi_trans = dsi_port_to_transcoder(host->port);
138 	const u8 *data = packet->payload;
139 	u32 len = packet->payload_length;
140 	int i, j;
141 
142 	/* payload queue can accept *256 bytes*, check limit */
143 	if (len > MAX_PLOAD_CREDIT * 4) {
144 		drm_err(&i915->drm, "payload size exceeds max queue limit\n");
145 		return -EINVAL;
146 	}
147 
148 	for (i = 0; i < len; i += 4) {
149 		u32 tmp = 0;
150 
151 		if (!wait_for_payload_credits(i915, dsi_trans, 1))
152 			return -EBUSY;
153 
154 		for (j = 0; j < min_t(u32, len - i, 4); j++)
155 			tmp |= *data++ << 8 * j;
156 
157 		intel_de_write(i915, DSI_CMD_TXPYLD(dsi_trans), tmp);
158 	}
159 
160 	return 0;
161 }
162 
163 static int dsi_send_pkt_hdr(struct intel_dsi_host *host,
164 			    const struct mipi_dsi_packet *packet,
165 			    bool enable_lpdt)
166 {
167 	struct intel_dsi *intel_dsi = host->intel_dsi;
168 	struct drm_i915_private *dev_priv = to_i915(intel_dsi->base.base.dev);
169 	enum transcoder dsi_trans = dsi_port_to_transcoder(host->port);
170 	u32 tmp;
171 
172 	if (!wait_for_header_credits(dev_priv, dsi_trans, 1))
173 		return -EBUSY;
174 
175 	tmp = intel_de_read(dev_priv, DSI_CMD_TXHDR(dsi_trans));
176 
177 	if (packet->payload)
178 		tmp |= PAYLOAD_PRESENT;
179 	else
180 		tmp &= ~PAYLOAD_PRESENT;
181 
182 	tmp &= ~VBLANK_FENCE;
183 
184 	if (enable_lpdt)
185 		tmp |= LP_DATA_TRANSFER;
186 
187 	tmp &= ~(PARAM_WC_MASK | VC_MASK | DT_MASK);
188 	tmp |= ((packet->header[0] & VC_MASK) << VC_SHIFT);
189 	tmp |= ((packet->header[0] & DT_MASK) << DT_SHIFT);
190 	tmp |= (packet->header[1] << PARAM_WC_LOWER_SHIFT);
191 	tmp |= (packet->header[2] << PARAM_WC_UPPER_SHIFT);
192 	intel_de_write(dev_priv, DSI_CMD_TXHDR(dsi_trans), tmp);
193 
194 	return 0;
195 }
196 
197 void icl_dsi_frame_update(struct intel_crtc_state *crtc_state)
198 {
199 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
200 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
201 	u32 tmp, mode_flags;
202 	enum port port;
203 
204 	mode_flags = crtc_state->mode_flags;
205 
206 	/*
207 	 * case 1 also covers dual link
208 	 * In case of dual link, frame update should be set on
209 	 * DSI_0
210 	 */
211 	if (mode_flags & I915_MODE_FLAG_DSI_USE_TE0)
212 		port = PORT_A;
213 	else if (mode_flags & I915_MODE_FLAG_DSI_USE_TE1)
214 		port = PORT_B;
215 	else
216 		return;
217 
218 	tmp = intel_de_read(dev_priv, DSI_CMD_FRMCTL(port));
219 	tmp |= DSI_FRAME_UPDATE_REQUEST;
220 	intel_de_write(dev_priv, DSI_CMD_FRMCTL(port), tmp);
221 }
222 
223 static void dsi_program_swing_and_deemphasis(struct intel_encoder *encoder)
224 {
225 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
226 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
227 	enum phy phy;
228 	u32 tmp;
229 	int lane;
230 
231 	for_each_dsi_phy(phy, intel_dsi->phys) {
232 		/*
233 		 * Program voltage swing and pre-emphasis level values as per
234 		 * table in BSPEC under DDI buffer programing
235 		 */
236 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
237 		tmp &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK);
238 		tmp |= SCALING_MODE_SEL(0x2);
239 		tmp |= TAP2_DISABLE | TAP3_DISABLE;
240 		tmp |= RTERM_SELECT(0x6);
241 		intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), tmp);
242 
243 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW5_AUX(phy));
244 		tmp &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK);
245 		tmp |= SCALING_MODE_SEL(0x2);
246 		tmp |= TAP2_DISABLE | TAP3_DISABLE;
247 		tmp |= RTERM_SELECT(0x6);
248 		intel_de_write(dev_priv, ICL_PORT_TX_DW5_AUX(phy), tmp);
249 
250 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW2_LN(0, phy));
251 		tmp &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
252 			 RCOMP_SCALAR_MASK);
253 		tmp |= SWING_SEL_UPPER(0x2);
254 		tmp |= SWING_SEL_LOWER(0x2);
255 		tmp |= RCOMP_SCALAR(0x98);
256 		intel_de_write(dev_priv, ICL_PORT_TX_DW2_GRP(phy), tmp);
257 
258 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW2_AUX(phy));
259 		tmp &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
260 			 RCOMP_SCALAR_MASK);
261 		tmp |= SWING_SEL_UPPER(0x2);
262 		tmp |= SWING_SEL_LOWER(0x2);
263 		tmp |= RCOMP_SCALAR(0x98);
264 		intel_de_write(dev_priv, ICL_PORT_TX_DW2_AUX(phy), tmp);
265 
266 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW4_AUX(phy));
267 		tmp &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
268 			 CURSOR_COEFF_MASK);
269 		tmp |= POST_CURSOR_1(0x0);
270 		tmp |= POST_CURSOR_2(0x0);
271 		tmp |= CURSOR_COEFF(0x3f);
272 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_AUX(phy), tmp);
273 
274 		for (lane = 0; lane <= 3; lane++) {
275 			/* Bspec: must not use GRP register for write */
276 			tmp = intel_de_read(dev_priv,
277 					    ICL_PORT_TX_DW4_LN(lane, phy));
278 			tmp &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
279 				 CURSOR_COEFF_MASK);
280 			tmp |= POST_CURSOR_1(0x0);
281 			tmp |= POST_CURSOR_2(0x0);
282 			tmp |= CURSOR_COEFF(0x3f);
283 			intel_de_write(dev_priv,
284 				       ICL_PORT_TX_DW4_LN(lane, phy), tmp);
285 		}
286 	}
287 }
288 
289 static void configure_dual_link_mode(struct intel_encoder *encoder,
290 				     const struct intel_crtc_state *pipe_config)
291 {
292 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
293 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
294 	u32 dss_ctl1;
295 
296 	dss_ctl1 = intel_de_read(dev_priv, DSS_CTL1);
297 	dss_ctl1 |= SPLITTER_ENABLE;
298 	dss_ctl1 &= ~OVERLAP_PIXELS_MASK;
299 	dss_ctl1 |= OVERLAP_PIXELS(intel_dsi->pixel_overlap);
300 
301 	if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
302 		const struct drm_display_mode *adjusted_mode =
303 					&pipe_config->hw.adjusted_mode;
304 		u32 dss_ctl2;
305 		u16 hactive = adjusted_mode->crtc_hdisplay;
306 		u16 dl_buffer_depth;
307 
308 		dss_ctl1 &= ~DUAL_LINK_MODE_INTERLEAVE;
309 		dl_buffer_depth = hactive / 2 + intel_dsi->pixel_overlap;
310 
311 		if (dl_buffer_depth > MAX_DL_BUFFER_TARGET_DEPTH)
312 			drm_err(&dev_priv->drm,
313 				"DL buffer depth exceed max value\n");
314 
315 		dss_ctl1 &= ~LEFT_DL_BUF_TARGET_DEPTH_MASK;
316 		dss_ctl1 |= LEFT_DL_BUF_TARGET_DEPTH(dl_buffer_depth);
317 		dss_ctl2 = intel_de_read(dev_priv, DSS_CTL2);
318 		dss_ctl2 &= ~RIGHT_DL_BUF_TARGET_DEPTH_MASK;
319 		dss_ctl2 |= RIGHT_DL_BUF_TARGET_DEPTH(dl_buffer_depth);
320 		intel_de_write(dev_priv, DSS_CTL2, dss_ctl2);
321 	} else {
322 		/* Interleave */
323 		dss_ctl1 |= DUAL_LINK_MODE_INTERLEAVE;
324 	}
325 
326 	intel_de_write(dev_priv, DSS_CTL1, dss_ctl1);
327 }
328 
329 /* aka DSI 8X clock */
330 static int afe_clk(struct intel_encoder *encoder,
331 		   const struct intel_crtc_state *crtc_state)
332 {
333 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
334 	int bpp;
335 
336 	if (crtc_state->dsc.compression_enable)
337 		bpp = crtc_state->dsc.compressed_bpp;
338 	else
339 		bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
340 
341 	return DIV_ROUND_CLOSEST(intel_dsi->pclk * bpp, intel_dsi->lane_count);
342 }
343 
344 static void gen11_dsi_program_esc_clk_div(struct intel_encoder *encoder,
345 					  const struct intel_crtc_state *crtc_state)
346 {
347 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
348 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
349 	enum port port;
350 	int afe_clk_khz;
351 	int theo_word_clk, act_word_clk;
352 	u32 esc_clk_div_m, esc_clk_div_m_phy;
353 
354 	afe_clk_khz = afe_clk(encoder, crtc_state);
355 
356 	if (IS_ALDERLAKE_S(dev_priv) || IS_ALDERLAKE_P(dev_priv)) {
357 		theo_word_clk = DIV_ROUND_UP(afe_clk_khz, 8 * DSI_MAX_ESC_CLK);
358 		act_word_clk = max(3, theo_word_clk + (theo_word_clk + 1) % 2);
359 		esc_clk_div_m = act_word_clk * 8;
360 		esc_clk_div_m_phy = (act_word_clk - 1) / 2;
361 	} else {
362 		esc_clk_div_m = DIV_ROUND_UP(afe_clk_khz, DSI_MAX_ESC_CLK);
363 	}
364 
365 	for_each_dsi_port(port, intel_dsi->ports) {
366 		intel_de_write(dev_priv, ICL_DSI_ESC_CLK_DIV(port),
367 			       esc_clk_div_m & ICL_ESC_CLK_DIV_MASK);
368 		intel_de_posting_read(dev_priv, ICL_DSI_ESC_CLK_DIV(port));
369 	}
370 
371 	for_each_dsi_port(port, intel_dsi->ports) {
372 		intel_de_write(dev_priv, ICL_DPHY_ESC_CLK_DIV(port),
373 			       esc_clk_div_m & ICL_ESC_CLK_DIV_MASK);
374 		intel_de_posting_read(dev_priv, ICL_DPHY_ESC_CLK_DIV(port));
375 	}
376 
377 	if (IS_ALDERLAKE_S(dev_priv) || IS_ALDERLAKE_P(dev_priv)) {
378 		for_each_dsi_port(port, intel_dsi->ports) {
379 			intel_de_write(dev_priv, ADL_MIPIO_DW(port, 8),
380 				       esc_clk_div_m_phy & TX_ESC_CLK_DIV_PHY);
381 			intel_de_posting_read(dev_priv, ADL_MIPIO_DW(port, 8));
382 		}
383 	}
384 }
385 
386 static void get_dsi_io_power_domains(struct drm_i915_private *dev_priv,
387 				     struct intel_dsi *intel_dsi)
388 {
389 	enum port port;
390 
391 	for_each_dsi_port(port, intel_dsi->ports) {
392 		drm_WARN_ON(&dev_priv->drm, intel_dsi->io_wakeref[port]);
393 		intel_dsi->io_wakeref[port] =
394 			intel_display_power_get(dev_priv,
395 						port == PORT_A ?
396 						POWER_DOMAIN_PORT_DDI_A_IO :
397 						POWER_DOMAIN_PORT_DDI_B_IO);
398 	}
399 }
400 
401 static void gen11_dsi_enable_io_power(struct intel_encoder *encoder)
402 {
403 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
404 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
405 	enum port port;
406 	u32 tmp;
407 
408 	for_each_dsi_port(port, intel_dsi->ports) {
409 		tmp = intel_de_read(dev_priv, ICL_DSI_IO_MODECTL(port));
410 		tmp |= COMBO_PHY_MODE_DSI;
411 		intel_de_write(dev_priv, ICL_DSI_IO_MODECTL(port), tmp);
412 	}
413 
414 	get_dsi_io_power_domains(dev_priv, intel_dsi);
415 }
416 
417 static void gen11_dsi_power_up_lanes(struct intel_encoder *encoder)
418 {
419 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
420 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
421 	enum phy phy;
422 
423 	for_each_dsi_phy(phy, intel_dsi->phys)
424 		intel_combo_phy_power_up_lanes(dev_priv, phy, true,
425 					       intel_dsi->lane_count, false);
426 }
427 
428 static void gen11_dsi_config_phy_lanes_sequence(struct intel_encoder *encoder)
429 {
430 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
431 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
432 	enum phy phy;
433 	u32 tmp;
434 	int lane;
435 
436 	/* Step 4b(i) set loadgen select for transmit and aux lanes */
437 	for_each_dsi_phy(phy, intel_dsi->phys) {
438 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW4_AUX(phy));
439 		tmp &= ~LOADGEN_SELECT;
440 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_AUX(phy), tmp);
441 		for (lane = 0; lane <= 3; lane++) {
442 			tmp = intel_de_read(dev_priv,
443 					    ICL_PORT_TX_DW4_LN(lane, phy));
444 			tmp &= ~LOADGEN_SELECT;
445 			if (lane != 2)
446 				tmp |= LOADGEN_SELECT;
447 			intel_de_write(dev_priv,
448 				       ICL_PORT_TX_DW4_LN(lane, phy), tmp);
449 		}
450 	}
451 
452 	/* Step 4b(ii) set latency optimization for transmit and aux lanes */
453 	for_each_dsi_phy(phy, intel_dsi->phys) {
454 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW2_AUX(phy));
455 		tmp &= ~FRC_LATENCY_OPTIM_MASK;
456 		tmp |= FRC_LATENCY_OPTIM_VAL(0x5);
457 		intel_de_write(dev_priv, ICL_PORT_TX_DW2_AUX(phy), tmp);
458 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW2_LN(0, phy));
459 		tmp &= ~FRC_LATENCY_OPTIM_MASK;
460 		tmp |= FRC_LATENCY_OPTIM_VAL(0x5);
461 		intel_de_write(dev_priv, ICL_PORT_TX_DW2_GRP(phy), tmp);
462 
463 		/* For EHL, TGL, set latency optimization for PCS_DW1 lanes */
464 		if (IS_JSL_EHL(dev_priv) || (DISPLAY_VER(dev_priv) >= 12)) {
465 			tmp = intel_de_read(dev_priv,
466 					    ICL_PORT_PCS_DW1_AUX(phy));
467 			tmp &= ~LATENCY_OPTIM_MASK;
468 			tmp |= LATENCY_OPTIM_VAL(0);
469 			intel_de_write(dev_priv, ICL_PORT_PCS_DW1_AUX(phy),
470 				       tmp);
471 
472 			tmp = intel_de_read(dev_priv,
473 					    ICL_PORT_PCS_DW1_LN(0, phy));
474 			tmp &= ~LATENCY_OPTIM_MASK;
475 			tmp |= LATENCY_OPTIM_VAL(0x1);
476 			intel_de_write(dev_priv, ICL_PORT_PCS_DW1_GRP(phy),
477 				       tmp);
478 		}
479 	}
480 
481 }
482 
483 static void gen11_dsi_voltage_swing_program_seq(struct intel_encoder *encoder)
484 {
485 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
486 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
487 	u32 tmp;
488 	enum phy phy;
489 
490 	/* clear common keeper enable bit */
491 	for_each_dsi_phy(phy, intel_dsi->phys) {
492 		tmp = intel_de_read(dev_priv, ICL_PORT_PCS_DW1_LN(0, phy));
493 		tmp &= ~COMMON_KEEPER_EN;
494 		intel_de_write(dev_priv, ICL_PORT_PCS_DW1_GRP(phy), tmp);
495 		tmp = intel_de_read(dev_priv, ICL_PORT_PCS_DW1_AUX(phy));
496 		tmp &= ~COMMON_KEEPER_EN;
497 		intel_de_write(dev_priv, ICL_PORT_PCS_DW1_AUX(phy), tmp);
498 	}
499 
500 	/*
501 	 * Set SUS Clock Config bitfield to 11b
502 	 * Note: loadgen select program is done
503 	 * as part of lane phy sequence configuration
504 	 */
505 	for_each_dsi_phy(phy, intel_dsi->phys) {
506 		tmp = intel_de_read(dev_priv, ICL_PORT_CL_DW5(phy));
507 		tmp |= SUS_CLOCK_CONFIG;
508 		intel_de_write(dev_priv, ICL_PORT_CL_DW5(phy), tmp);
509 	}
510 
511 	/* Clear training enable to change swing values */
512 	for_each_dsi_phy(phy, intel_dsi->phys) {
513 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
514 		tmp &= ~TX_TRAINING_EN;
515 		intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), tmp);
516 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW5_AUX(phy));
517 		tmp &= ~TX_TRAINING_EN;
518 		intel_de_write(dev_priv, ICL_PORT_TX_DW5_AUX(phy), tmp);
519 	}
520 
521 	/* Program swing and de-emphasis */
522 	dsi_program_swing_and_deemphasis(encoder);
523 
524 	/* Set training enable to trigger update */
525 	for_each_dsi_phy(phy, intel_dsi->phys) {
526 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
527 		tmp |= TX_TRAINING_EN;
528 		intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), tmp);
529 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW5_AUX(phy));
530 		tmp |= TX_TRAINING_EN;
531 		intel_de_write(dev_priv, ICL_PORT_TX_DW5_AUX(phy), tmp);
532 	}
533 }
534 
535 static void gen11_dsi_enable_ddi_buffer(struct intel_encoder *encoder)
536 {
537 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
538 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
539 	u32 tmp;
540 	enum port port;
541 
542 	for_each_dsi_port(port, intel_dsi->ports) {
543 		tmp = intel_de_read(dev_priv, DDI_BUF_CTL(port));
544 		tmp |= DDI_BUF_CTL_ENABLE;
545 		intel_de_write(dev_priv, DDI_BUF_CTL(port), tmp);
546 
547 		if (wait_for_us(!(intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
548 				  DDI_BUF_IS_IDLE),
549 				  500))
550 			drm_err(&dev_priv->drm, "DDI port:%c buffer idle\n",
551 				port_name(port));
552 	}
553 }
554 
555 static void
556 gen11_dsi_setup_dphy_timings(struct intel_encoder *encoder,
557 			     const struct intel_crtc_state *crtc_state)
558 {
559 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
560 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
561 	u32 tmp;
562 	enum port port;
563 	enum phy phy;
564 
565 	/* Program T-INIT master registers */
566 	for_each_dsi_port(port, intel_dsi->ports) {
567 		tmp = intel_de_read(dev_priv, ICL_DSI_T_INIT_MASTER(port));
568 		tmp &= ~MASTER_INIT_TIMER_MASK;
569 		tmp |= intel_dsi->init_count;
570 		intel_de_write(dev_priv, ICL_DSI_T_INIT_MASTER(port), tmp);
571 	}
572 
573 	/* Program DPHY clock lanes timings */
574 	for_each_dsi_port(port, intel_dsi->ports) {
575 		intel_de_write(dev_priv, DPHY_CLK_TIMING_PARAM(port),
576 			       intel_dsi->dphy_reg);
577 
578 		/* shadow register inside display core */
579 		intel_de_write(dev_priv, DSI_CLK_TIMING_PARAM(port),
580 			       intel_dsi->dphy_reg);
581 	}
582 
583 	/* Program DPHY data lanes timings */
584 	for_each_dsi_port(port, intel_dsi->ports) {
585 		intel_de_write(dev_priv, DPHY_DATA_TIMING_PARAM(port),
586 			       intel_dsi->dphy_data_lane_reg);
587 
588 		/* shadow register inside display core */
589 		intel_de_write(dev_priv, DSI_DATA_TIMING_PARAM(port),
590 			       intel_dsi->dphy_data_lane_reg);
591 	}
592 
593 	/*
594 	 * If DSI link operating at or below an 800 MHz,
595 	 * TA_SURE should be override and programmed to
596 	 * a value '0' inside TA_PARAM_REGISTERS otherwise
597 	 * leave all fields at HW default values.
598 	 */
599 	if (DISPLAY_VER(dev_priv) == 11) {
600 		if (afe_clk(encoder, crtc_state) <= 800000) {
601 			for_each_dsi_port(port, intel_dsi->ports) {
602 				tmp = intel_de_read(dev_priv,
603 						    DPHY_TA_TIMING_PARAM(port));
604 				tmp &= ~TA_SURE_MASK;
605 				tmp |= TA_SURE_OVERRIDE | TA_SURE(0);
606 				intel_de_write(dev_priv,
607 					       DPHY_TA_TIMING_PARAM(port),
608 					       tmp);
609 
610 				/* shadow register inside display core */
611 				tmp = intel_de_read(dev_priv,
612 						    DSI_TA_TIMING_PARAM(port));
613 				tmp &= ~TA_SURE_MASK;
614 				tmp |= TA_SURE_OVERRIDE | TA_SURE(0);
615 				intel_de_write(dev_priv,
616 					       DSI_TA_TIMING_PARAM(port), tmp);
617 			}
618 		}
619 	}
620 
621 	if (IS_JSL_EHL(dev_priv)) {
622 		for_each_dsi_phy(phy, intel_dsi->phys) {
623 			tmp = intel_de_read(dev_priv, ICL_DPHY_CHKN(phy));
624 			tmp |= ICL_DPHY_CHKN_AFE_OVER_PPI_STRAP;
625 			intel_de_write(dev_priv, ICL_DPHY_CHKN(phy), tmp);
626 		}
627 	}
628 }
629 
630 static void gen11_dsi_gate_clocks(struct intel_encoder *encoder)
631 {
632 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
633 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
634 	u32 tmp;
635 	enum phy phy;
636 
637 	mutex_lock(&dev_priv->dpll.lock);
638 	tmp = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
639 	for_each_dsi_phy(phy, intel_dsi->phys)
640 		tmp |= ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy);
641 
642 	intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, tmp);
643 	mutex_unlock(&dev_priv->dpll.lock);
644 }
645 
646 static void gen11_dsi_ungate_clocks(struct intel_encoder *encoder)
647 {
648 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
649 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
650 	u32 tmp;
651 	enum phy phy;
652 
653 	mutex_lock(&dev_priv->dpll.lock);
654 	tmp = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
655 	for_each_dsi_phy(phy, intel_dsi->phys)
656 		tmp &= ~ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy);
657 
658 	intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, tmp);
659 	mutex_unlock(&dev_priv->dpll.lock);
660 }
661 
662 static bool gen11_dsi_is_clock_enabled(struct intel_encoder *encoder)
663 {
664 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
665 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
666 	bool clock_enabled = false;
667 	enum phy phy;
668 	u32 tmp;
669 
670 	tmp = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
671 
672 	for_each_dsi_phy(phy, intel_dsi->phys) {
673 		if (!(tmp & ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy)))
674 			clock_enabled = true;
675 	}
676 
677 	return clock_enabled;
678 }
679 
680 static void gen11_dsi_map_pll(struct intel_encoder *encoder,
681 			      const struct intel_crtc_state *crtc_state)
682 {
683 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
684 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
685 	struct intel_shared_dpll *pll = crtc_state->shared_dpll;
686 	enum phy phy;
687 	u32 val;
688 
689 	mutex_lock(&dev_priv->dpll.lock);
690 
691 	val = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
692 	for_each_dsi_phy(phy, intel_dsi->phys) {
693 		val &= ~ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
694 		val |= ICL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy);
695 	}
696 	intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, val);
697 
698 	for_each_dsi_phy(phy, intel_dsi->phys) {
699 		if (DISPLAY_VER(dev_priv) >= 12)
700 			val |= ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy);
701 		else
702 			val &= ~ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy);
703 	}
704 	intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, val);
705 
706 	intel_de_posting_read(dev_priv, ICL_DPCLKA_CFGCR0);
707 
708 	mutex_unlock(&dev_priv->dpll.lock);
709 }
710 
711 static void
712 gen11_dsi_configure_transcoder(struct intel_encoder *encoder,
713 			       const struct intel_crtc_state *pipe_config)
714 {
715 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
716 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
717 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
718 	enum pipe pipe = crtc->pipe;
719 	u32 tmp;
720 	enum port port;
721 	enum transcoder dsi_trans;
722 
723 	for_each_dsi_port(port, intel_dsi->ports) {
724 		dsi_trans = dsi_port_to_transcoder(port);
725 		tmp = intel_de_read(dev_priv, DSI_TRANS_FUNC_CONF(dsi_trans));
726 
727 		if (intel_dsi->eotp_pkt)
728 			tmp &= ~EOTP_DISABLED;
729 		else
730 			tmp |= EOTP_DISABLED;
731 
732 		/* enable link calibration if freq > 1.5Gbps */
733 		if (afe_clk(encoder, pipe_config) >= 1500 * 1000) {
734 			tmp &= ~LINK_CALIBRATION_MASK;
735 			tmp |= CALIBRATION_ENABLED_INITIAL_ONLY;
736 		}
737 
738 		/* configure continuous clock */
739 		tmp &= ~CONTINUOUS_CLK_MASK;
740 		if (intel_dsi->clock_stop)
741 			tmp |= CLK_ENTER_LP_AFTER_DATA;
742 		else
743 			tmp |= CLK_HS_CONTINUOUS;
744 
745 		/* configure buffer threshold limit to minimum */
746 		tmp &= ~PIX_BUF_THRESHOLD_MASK;
747 		tmp |= PIX_BUF_THRESHOLD_1_4;
748 
749 		/* set virtual channel to '0' */
750 		tmp &= ~PIX_VIRT_CHAN_MASK;
751 		tmp |= PIX_VIRT_CHAN(0);
752 
753 		/* program BGR transmission */
754 		if (intel_dsi->bgr_enabled)
755 			tmp |= BGR_TRANSMISSION;
756 
757 		/* select pixel format */
758 		tmp &= ~PIX_FMT_MASK;
759 		if (pipe_config->dsc.compression_enable) {
760 			tmp |= PIX_FMT_COMPRESSED;
761 		} else {
762 			switch (intel_dsi->pixel_format) {
763 			default:
764 				MISSING_CASE(intel_dsi->pixel_format);
765 				fallthrough;
766 			case MIPI_DSI_FMT_RGB565:
767 				tmp |= PIX_FMT_RGB565;
768 				break;
769 			case MIPI_DSI_FMT_RGB666_PACKED:
770 				tmp |= PIX_FMT_RGB666_PACKED;
771 				break;
772 			case MIPI_DSI_FMT_RGB666:
773 				tmp |= PIX_FMT_RGB666_LOOSE;
774 				break;
775 			case MIPI_DSI_FMT_RGB888:
776 				tmp |= PIX_FMT_RGB888;
777 				break;
778 			}
779 		}
780 
781 		if (DISPLAY_VER(dev_priv) >= 12) {
782 			if (is_vid_mode(intel_dsi))
783 				tmp |= BLANKING_PACKET_ENABLE;
784 		}
785 
786 		/* program DSI operation mode */
787 		if (is_vid_mode(intel_dsi)) {
788 			tmp &= ~OP_MODE_MASK;
789 			switch (intel_dsi->video_mode_format) {
790 			default:
791 				MISSING_CASE(intel_dsi->video_mode_format);
792 				fallthrough;
793 			case VIDEO_MODE_NON_BURST_WITH_SYNC_EVENTS:
794 				tmp |= VIDEO_MODE_SYNC_EVENT;
795 				break;
796 			case VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE:
797 				tmp |= VIDEO_MODE_SYNC_PULSE;
798 				break;
799 			}
800 		} else {
801 			/*
802 			 * FIXME: Retrieve this info from VBT.
803 			 * As per the spec when dsi transcoder is operating
804 			 * in TE GATE mode, TE comes from GPIO
805 			 * which is UTIL PIN for DSI 0.
806 			 * Also this GPIO would not be used for other
807 			 * purposes is an assumption.
808 			 */
809 			tmp &= ~OP_MODE_MASK;
810 			tmp |= CMD_MODE_TE_GATE;
811 			tmp |= TE_SOURCE_GPIO;
812 		}
813 
814 		intel_de_write(dev_priv, DSI_TRANS_FUNC_CONF(dsi_trans), tmp);
815 	}
816 
817 	/* enable port sync mode if dual link */
818 	if (intel_dsi->dual_link) {
819 		for_each_dsi_port(port, intel_dsi->ports) {
820 			dsi_trans = dsi_port_to_transcoder(port);
821 			tmp = intel_de_read(dev_priv,
822 					    TRANS_DDI_FUNC_CTL2(dsi_trans));
823 			tmp |= PORT_SYNC_MODE_ENABLE;
824 			intel_de_write(dev_priv,
825 				       TRANS_DDI_FUNC_CTL2(dsi_trans), tmp);
826 		}
827 
828 		/* configure stream splitting */
829 		configure_dual_link_mode(encoder, pipe_config);
830 	}
831 
832 	for_each_dsi_port(port, intel_dsi->ports) {
833 		dsi_trans = dsi_port_to_transcoder(port);
834 
835 		/* select data lane width */
836 		tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(dsi_trans));
837 		tmp &= ~DDI_PORT_WIDTH_MASK;
838 		tmp |= DDI_PORT_WIDTH(intel_dsi->lane_count);
839 
840 		/* select input pipe */
841 		tmp &= ~TRANS_DDI_EDP_INPUT_MASK;
842 		switch (pipe) {
843 		default:
844 			MISSING_CASE(pipe);
845 			fallthrough;
846 		case PIPE_A:
847 			tmp |= TRANS_DDI_EDP_INPUT_A_ON;
848 			break;
849 		case PIPE_B:
850 			tmp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
851 			break;
852 		case PIPE_C:
853 			tmp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
854 			break;
855 		case PIPE_D:
856 			tmp |= TRANS_DDI_EDP_INPUT_D_ONOFF;
857 			break;
858 		}
859 
860 		/* enable DDI buffer */
861 		tmp |= TRANS_DDI_FUNC_ENABLE;
862 		intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(dsi_trans), tmp);
863 	}
864 
865 	/* wait for link ready */
866 	for_each_dsi_port(port, intel_dsi->ports) {
867 		dsi_trans = dsi_port_to_transcoder(port);
868 		if (wait_for_us((intel_de_read(dev_priv, DSI_TRANS_FUNC_CONF(dsi_trans)) &
869 				 LINK_READY), 2500))
870 			drm_err(&dev_priv->drm, "DSI link not ready\n");
871 	}
872 }
873 
874 static void
875 gen11_dsi_set_transcoder_timings(struct intel_encoder *encoder,
876 				 const struct intel_crtc_state *crtc_state)
877 {
878 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
879 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
880 	const struct drm_display_mode *adjusted_mode =
881 		&crtc_state->hw.adjusted_mode;
882 	enum port port;
883 	enum transcoder dsi_trans;
884 	/* horizontal timings */
885 	u16 htotal, hactive, hsync_start, hsync_end, hsync_size;
886 	u16 hback_porch;
887 	/* vertical timings */
888 	u16 vtotal, vactive, vsync_start, vsync_end, vsync_shift;
889 	int mul = 1, div = 1;
890 
891 	/*
892 	 * Adjust horizontal timings (htotal, hsync_start, hsync_end) to account
893 	 * for slower link speed if DSC is enabled.
894 	 *
895 	 * The compression frequency ratio is the ratio between compressed and
896 	 * non-compressed link speeds, and simplifies down to the ratio between
897 	 * compressed and non-compressed bpp.
898 	 */
899 	if (crtc_state->dsc.compression_enable) {
900 		mul = crtc_state->dsc.compressed_bpp;
901 		div = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
902 	}
903 
904 	hactive = adjusted_mode->crtc_hdisplay;
905 
906 	if (is_vid_mode(intel_dsi))
907 		htotal = DIV_ROUND_UP(adjusted_mode->crtc_htotal * mul, div);
908 	else
909 		htotal = DIV_ROUND_UP((hactive + 160) * mul, div);
910 
911 	hsync_start = DIV_ROUND_UP(adjusted_mode->crtc_hsync_start * mul, div);
912 	hsync_end = DIV_ROUND_UP(adjusted_mode->crtc_hsync_end * mul, div);
913 	hsync_size  = hsync_end - hsync_start;
914 	hback_porch = (adjusted_mode->crtc_htotal -
915 		       adjusted_mode->crtc_hsync_end);
916 	vactive = adjusted_mode->crtc_vdisplay;
917 
918 	if (is_vid_mode(intel_dsi)) {
919 		vtotal = adjusted_mode->crtc_vtotal;
920 	} else {
921 		int bpp, line_time_us, byte_clk_period_ns;
922 
923 		if (crtc_state->dsc.compression_enable)
924 			bpp = crtc_state->dsc.compressed_bpp;
925 		else
926 			bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
927 
928 		byte_clk_period_ns = 1000000 / afe_clk(encoder, crtc_state);
929 		line_time_us = (htotal * (bpp / 8) * byte_clk_period_ns) / (1000 * intel_dsi->lane_count);
930 		vtotal = vactive + DIV_ROUND_UP(400, line_time_us);
931 	}
932 	vsync_start = adjusted_mode->crtc_vsync_start;
933 	vsync_end = adjusted_mode->crtc_vsync_end;
934 	vsync_shift = hsync_start - htotal / 2;
935 
936 	if (intel_dsi->dual_link) {
937 		hactive /= 2;
938 		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
939 			hactive += intel_dsi->pixel_overlap;
940 		htotal /= 2;
941 	}
942 
943 	/* minimum hactive as per bspec: 256 pixels */
944 	if (adjusted_mode->crtc_hdisplay < 256)
945 		drm_err(&dev_priv->drm, "hactive is less then 256 pixels\n");
946 
947 	/* if RGB666 format, then hactive must be multiple of 4 pixels */
948 	if (intel_dsi->pixel_format == MIPI_DSI_FMT_RGB666 && hactive % 4 != 0)
949 		drm_err(&dev_priv->drm,
950 			"hactive pixels are not multiple of 4\n");
951 
952 	/* program TRANS_HTOTAL register */
953 	for_each_dsi_port(port, intel_dsi->ports) {
954 		dsi_trans = dsi_port_to_transcoder(port);
955 		intel_de_write(dev_priv, HTOTAL(dsi_trans),
956 			       (hactive - 1) | ((htotal - 1) << 16));
957 	}
958 
959 	/* TRANS_HSYNC register to be programmed only for video mode */
960 	if (is_vid_mode(intel_dsi)) {
961 		if (intel_dsi->video_mode_format ==
962 		    VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE) {
963 			/* BSPEC: hsync size should be atleast 16 pixels */
964 			if (hsync_size < 16)
965 				drm_err(&dev_priv->drm,
966 					"hsync size < 16 pixels\n");
967 		}
968 
969 		if (hback_porch < 16)
970 			drm_err(&dev_priv->drm, "hback porch < 16 pixels\n");
971 
972 		if (intel_dsi->dual_link) {
973 			hsync_start /= 2;
974 			hsync_end /= 2;
975 		}
976 
977 		for_each_dsi_port(port, intel_dsi->ports) {
978 			dsi_trans = dsi_port_to_transcoder(port);
979 			intel_de_write(dev_priv, HSYNC(dsi_trans),
980 				       (hsync_start - 1) | ((hsync_end - 1) << 16));
981 		}
982 	}
983 
984 	/* program TRANS_VTOTAL register */
985 	for_each_dsi_port(port, intel_dsi->ports) {
986 		dsi_trans = dsi_port_to_transcoder(port);
987 		/*
988 		 * FIXME: Programing this by assuming progressive mode, since
989 		 * non-interlaced info from VBT is not saved inside
990 		 * struct drm_display_mode.
991 		 * For interlace mode: program required pixel minus 2
992 		 */
993 		intel_de_write(dev_priv, VTOTAL(dsi_trans),
994 			       (vactive - 1) | ((vtotal - 1) << 16));
995 	}
996 
997 	if (vsync_end < vsync_start || vsync_end > vtotal)
998 		drm_err(&dev_priv->drm, "Invalid vsync_end value\n");
999 
1000 	if (vsync_start < vactive)
1001 		drm_err(&dev_priv->drm, "vsync_start less than vactive\n");
1002 
1003 	/* program TRANS_VSYNC register for video mode only */
1004 	if (is_vid_mode(intel_dsi)) {
1005 		for_each_dsi_port(port, intel_dsi->ports) {
1006 			dsi_trans = dsi_port_to_transcoder(port);
1007 			intel_de_write(dev_priv, VSYNC(dsi_trans),
1008 				       (vsync_start - 1) | ((vsync_end - 1) << 16));
1009 		}
1010 	}
1011 
1012 	/*
1013 	 * FIXME: It has to be programmed only for video modes and interlaced
1014 	 * modes. Put the check condition here once interlaced
1015 	 * info available as described above.
1016 	 * program TRANS_VSYNCSHIFT register
1017 	 */
1018 	if (is_vid_mode(intel_dsi)) {
1019 		for_each_dsi_port(port, intel_dsi->ports) {
1020 			dsi_trans = dsi_port_to_transcoder(port);
1021 			intel_de_write(dev_priv, VSYNCSHIFT(dsi_trans),
1022 				       vsync_shift);
1023 		}
1024 	}
1025 
1026 	/* program TRANS_VBLANK register, should be same as vtotal programmed */
1027 	if (DISPLAY_VER(dev_priv) >= 12) {
1028 		for_each_dsi_port(port, intel_dsi->ports) {
1029 			dsi_trans = dsi_port_to_transcoder(port);
1030 			intel_de_write(dev_priv, VBLANK(dsi_trans),
1031 				       (vactive - 1) | ((vtotal - 1) << 16));
1032 		}
1033 	}
1034 }
1035 
1036 static void gen11_dsi_enable_transcoder(struct intel_encoder *encoder)
1037 {
1038 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1039 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1040 	enum port port;
1041 	enum transcoder dsi_trans;
1042 	u32 tmp;
1043 
1044 	for_each_dsi_port(port, intel_dsi->ports) {
1045 		dsi_trans = dsi_port_to_transcoder(port);
1046 		tmp = intel_de_read(dev_priv, PIPECONF(dsi_trans));
1047 		tmp |= PIPECONF_ENABLE;
1048 		intel_de_write(dev_priv, PIPECONF(dsi_trans), tmp);
1049 
1050 		/* wait for transcoder to be enabled */
1051 		if (intel_de_wait_for_set(dev_priv, PIPECONF(dsi_trans),
1052 					  I965_PIPECONF_ACTIVE, 10))
1053 			drm_err(&dev_priv->drm,
1054 				"DSI transcoder not enabled\n");
1055 	}
1056 }
1057 
1058 static void gen11_dsi_setup_timeouts(struct intel_encoder *encoder,
1059 				     const struct intel_crtc_state *crtc_state)
1060 {
1061 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1062 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1063 	enum port port;
1064 	enum transcoder dsi_trans;
1065 	u32 tmp, hs_tx_timeout, lp_rx_timeout, ta_timeout, divisor, mul;
1066 
1067 	/*
1068 	 * escape clock count calculation:
1069 	 * BYTE_CLK_COUNT = TIME_NS/(8 * UI)
1070 	 * UI (nsec) = (10^6)/Bitrate
1071 	 * TIME_NS = (BYTE_CLK_COUNT * 8 * 10^6)/ Bitrate
1072 	 * ESCAPE_CLK_COUNT  = TIME_NS/ESC_CLK_NS
1073 	 */
1074 	divisor = intel_dsi_tlpx_ns(intel_dsi) * afe_clk(encoder, crtc_state) * 1000;
1075 	mul = 8 * 1000000;
1076 	hs_tx_timeout = DIV_ROUND_UP(intel_dsi->hs_tx_timeout * mul,
1077 				     divisor);
1078 	lp_rx_timeout = DIV_ROUND_UP(intel_dsi->lp_rx_timeout * mul, divisor);
1079 	ta_timeout = DIV_ROUND_UP(intel_dsi->turn_arnd_val * mul, divisor);
1080 
1081 	for_each_dsi_port(port, intel_dsi->ports) {
1082 		dsi_trans = dsi_port_to_transcoder(port);
1083 
1084 		/* program hst_tx_timeout */
1085 		tmp = intel_de_read(dev_priv, DSI_HSTX_TO(dsi_trans));
1086 		tmp &= ~HSTX_TIMEOUT_VALUE_MASK;
1087 		tmp |= HSTX_TIMEOUT_VALUE(hs_tx_timeout);
1088 		intel_de_write(dev_priv, DSI_HSTX_TO(dsi_trans), tmp);
1089 
1090 		/* FIXME: DSI_CALIB_TO */
1091 
1092 		/* program lp_rx_host timeout */
1093 		tmp = intel_de_read(dev_priv, DSI_LPRX_HOST_TO(dsi_trans));
1094 		tmp &= ~LPRX_TIMEOUT_VALUE_MASK;
1095 		tmp |= LPRX_TIMEOUT_VALUE(lp_rx_timeout);
1096 		intel_de_write(dev_priv, DSI_LPRX_HOST_TO(dsi_trans), tmp);
1097 
1098 		/* FIXME: DSI_PWAIT_TO */
1099 
1100 		/* program turn around timeout */
1101 		tmp = intel_de_read(dev_priv, DSI_TA_TO(dsi_trans));
1102 		tmp &= ~TA_TIMEOUT_VALUE_MASK;
1103 		tmp |= TA_TIMEOUT_VALUE(ta_timeout);
1104 		intel_de_write(dev_priv, DSI_TA_TO(dsi_trans), tmp);
1105 	}
1106 }
1107 
1108 static void gen11_dsi_config_util_pin(struct intel_encoder *encoder,
1109 				      bool enable)
1110 {
1111 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1112 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1113 	u32 tmp;
1114 
1115 	/*
1116 	 * used as TE i/p for DSI0,
1117 	 * for dual link/DSI1 TE is from slave DSI1
1118 	 * through GPIO.
1119 	 */
1120 	if (is_vid_mode(intel_dsi) || (intel_dsi->ports & BIT(PORT_B)))
1121 		return;
1122 
1123 	tmp = intel_de_read(dev_priv, UTIL_PIN_CTL);
1124 
1125 	if (enable) {
1126 		tmp |= UTIL_PIN_DIRECTION_INPUT;
1127 		tmp |= UTIL_PIN_ENABLE;
1128 	} else {
1129 		tmp &= ~UTIL_PIN_ENABLE;
1130 	}
1131 	intel_de_write(dev_priv, UTIL_PIN_CTL, tmp);
1132 }
1133 
1134 static void
1135 gen11_dsi_enable_port_and_phy(struct intel_encoder *encoder,
1136 			      const struct intel_crtc_state *crtc_state)
1137 {
1138 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1139 
1140 	/* step 4a: power up all lanes of the DDI used by DSI */
1141 	gen11_dsi_power_up_lanes(encoder);
1142 
1143 	/* step 4b: configure lane sequencing of the Combo-PHY transmitters */
1144 	gen11_dsi_config_phy_lanes_sequence(encoder);
1145 
1146 	/* step 4c: configure voltage swing and skew */
1147 	gen11_dsi_voltage_swing_program_seq(encoder);
1148 
1149 	/* enable DDI buffer */
1150 	gen11_dsi_enable_ddi_buffer(encoder);
1151 
1152 	/* setup D-PHY timings */
1153 	gen11_dsi_setup_dphy_timings(encoder, crtc_state);
1154 
1155 	/* Since transcoder is configured to take events from GPIO */
1156 	gen11_dsi_config_util_pin(encoder, true);
1157 
1158 	/* step 4h: setup DSI protocol timeouts */
1159 	gen11_dsi_setup_timeouts(encoder, crtc_state);
1160 
1161 	/* Step (4h, 4i, 4j, 4k): Configure transcoder */
1162 	gen11_dsi_configure_transcoder(encoder, crtc_state);
1163 
1164 	/* Step 4l: Gate DDI clocks */
1165 	if (DISPLAY_VER(dev_priv) == 11)
1166 		gen11_dsi_gate_clocks(encoder);
1167 }
1168 
1169 static void gen11_dsi_powerup_panel(struct intel_encoder *encoder)
1170 {
1171 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1172 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1173 	struct mipi_dsi_device *dsi;
1174 	enum port port;
1175 	enum transcoder dsi_trans;
1176 	u32 tmp;
1177 	int ret;
1178 
1179 	/* set maximum return packet size */
1180 	for_each_dsi_port(port, intel_dsi->ports) {
1181 		dsi_trans = dsi_port_to_transcoder(port);
1182 
1183 		/*
1184 		 * FIXME: This uses the number of DW's currently in the payload
1185 		 * receive queue. This is probably not what we want here.
1186 		 */
1187 		tmp = intel_de_read(dev_priv, DSI_CMD_RXCTL(dsi_trans));
1188 		tmp &= NUMBER_RX_PLOAD_DW_MASK;
1189 		/* multiply "Number Rx Payload DW" by 4 to get max value */
1190 		tmp = tmp * 4;
1191 		dsi = intel_dsi->dsi_hosts[port]->device;
1192 		ret = mipi_dsi_set_maximum_return_packet_size(dsi, tmp);
1193 		if (ret < 0)
1194 			drm_err(&dev_priv->drm,
1195 				"error setting max return pkt size%d\n", tmp);
1196 	}
1197 
1198 	/* panel power on related mipi dsi vbt sequences */
1199 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_ON);
1200 	intel_dsi_msleep(intel_dsi, intel_dsi->panel_on_delay);
1201 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DEASSERT_RESET);
1202 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_INIT_OTP);
1203 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
1204 
1205 	/* ensure all panel commands dispatched before enabling transcoder */
1206 	wait_for_cmds_dispatched_to_panel(encoder);
1207 }
1208 
1209 static void gen11_dsi_pre_pll_enable(struct intel_atomic_state *state,
1210 				     struct intel_encoder *encoder,
1211 				     const struct intel_crtc_state *crtc_state,
1212 				     const struct drm_connector_state *conn_state)
1213 {
1214 	/* step2: enable IO power */
1215 	gen11_dsi_enable_io_power(encoder);
1216 
1217 	/* step3: enable DSI PLL */
1218 	gen11_dsi_program_esc_clk_div(encoder, crtc_state);
1219 }
1220 
1221 static void gen11_dsi_pre_enable(struct intel_atomic_state *state,
1222 				 struct intel_encoder *encoder,
1223 				 const struct intel_crtc_state *pipe_config,
1224 				 const struct drm_connector_state *conn_state)
1225 {
1226 	/* step3b */
1227 	gen11_dsi_map_pll(encoder, pipe_config);
1228 
1229 	/* step4: enable DSI port and DPHY */
1230 	gen11_dsi_enable_port_and_phy(encoder, pipe_config);
1231 
1232 	/* step5: program and powerup panel */
1233 	gen11_dsi_powerup_panel(encoder);
1234 
1235 	intel_dsc_enable(encoder, pipe_config);
1236 
1237 	/* step6c: configure transcoder timings */
1238 	gen11_dsi_set_transcoder_timings(encoder, pipe_config);
1239 }
1240 
1241 /*
1242  * Wa_1409054076:icl,jsl,ehl
1243  * When pipe A is disabled and MIPI DSI is enabled on pipe B,
1244  * the AMT KVMR feature will incorrectly see pipe A as enabled.
1245  * Set 0x42080 bit 23=1 before enabling DSI on pipe B and leave
1246  * it set while DSI is enabled on pipe B
1247  */
1248 static void icl_apply_kvmr_pipe_a_wa(struct intel_encoder *encoder,
1249 				     enum pipe pipe, bool enable)
1250 {
1251 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1252 
1253 	if (DISPLAY_VER(dev_priv) == 11 && pipe == PIPE_B)
1254 		intel_de_rmw(dev_priv, CHICKEN_PAR1_1,
1255 			     IGNORE_KVMR_PIPE_A,
1256 			     enable ? IGNORE_KVMR_PIPE_A : 0);
1257 }
1258 
1259 /*
1260  * Wa_16012360555:adl-p
1261  * SW will have to program the "LP to HS Wakeup Guardband"
1262  * to account for the repeaters on the HS Request/Ready
1263  * PPI signaling between the Display engine and the DPHY.
1264  */
1265 static void adlp_set_lp_hs_wakeup_gb(struct intel_encoder *encoder)
1266 {
1267 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1268 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1269 	enum port port;
1270 
1271 	if (DISPLAY_VER(i915) == 13) {
1272 		for_each_dsi_port(port, intel_dsi->ports)
1273 			intel_de_rmw(i915, TGL_DSI_CHKN_REG(port),
1274 				     TGL_DSI_CHKN_LSHS_GB, 0x4);
1275 	}
1276 }
1277 
1278 static void gen11_dsi_enable(struct intel_atomic_state *state,
1279 			     struct intel_encoder *encoder,
1280 			     const struct intel_crtc_state *crtc_state,
1281 			     const struct drm_connector_state *conn_state)
1282 {
1283 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1284 	struct intel_crtc *crtc = to_intel_crtc(conn_state->crtc);
1285 
1286 	drm_WARN_ON(state->base.dev, crtc_state->has_pch_encoder);
1287 
1288 	/* Wa_1409054076:icl,jsl,ehl */
1289 	icl_apply_kvmr_pipe_a_wa(encoder, crtc->pipe, true);
1290 
1291 	/* Wa_16012360555:adl-p */
1292 	adlp_set_lp_hs_wakeup_gb(encoder);
1293 
1294 	/* step6d: enable dsi transcoder */
1295 	gen11_dsi_enable_transcoder(encoder);
1296 
1297 	/* step7: enable backlight */
1298 	intel_backlight_enable(crtc_state, conn_state);
1299 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_ON);
1300 
1301 	intel_crtc_vblank_on(crtc_state);
1302 }
1303 
1304 static void gen11_dsi_disable_transcoder(struct intel_encoder *encoder)
1305 {
1306 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1307 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1308 	enum port port;
1309 	enum transcoder dsi_trans;
1310 	u32 tmp;
1311 
1312 	for_each_dsi_port(port, intel_dsi->ports) {
1313 		dsi_trans = dsi_port_to_transcoder(port);
1314 
1315 		/* disable transcoder */
1316 		tmp = intel_de_read(dev_priv, PIPECONF(dsi_trans));
1317 		tmp &= ~PIPECONF_ENABLE;
1318 		intel_de_write(dev_priv, PIPECONF(dsi_trans), tmp);
1319 
1320 		/* wait for transcoder to be disabled */
1321 		if (intel_de_wait_for_clear(dev_priv, PIPECONF(dsi_trans),
1322 					    I965_PIPECONF_ACTIVE, 50))
1323 			drm_err(&dev_priv->drm,
1324 				"DSI trancoder not disabled\n");
1325 	}
1326 }
1327 
1328 static void gen11_dsi_powerdown_panel(struct intel_encoder *encoder)
1329 {
1330 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1331 
1332 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_OFF);
1333 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_ASSERT_RESET);
1334 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_OFF);
1335 
1336 	/* ensure cmds dispatched to panel */
1337 	wait_for_cmds_dispatched_to_panel(encoder);
1338 }
1339 
1340 static void gen11_dsi_deconfigure_trancoder(struct intel_encoder *encoder)
1341 {
1342 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1343 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1344 	enum port port;
1345 	enum transcoder dsi_trans;
1346 	u32 tmp;
1347 
1348 	/* disable periodic update mode */
1349 	if (is_cmd_mode(intel_dsi)) {
1350 		for_each_dsi_port(port, intel_dsi->ports) {
1351 			tmp = intel_de_read(dev_priv, DSI_CMD_FRMCTL(port));
1352 			tmp &= ~DSI_PERIODIC_FRAME_UPDATE_ENABLE;
1353 			intel_de_write(dev_priv, DSI_CMD_FRMCTL(port), tmp);
1354 		}
1355 	}
1356 
1357 	/* put dsi link in ULPS */
1358 	for_each_dsi_port(port, intel_dsi->ports) {
1359 		dsi_trans = dsi_port_to_transcoder(port);
1360 		tmp = intel_de_read(dev_priv, DSI_LP_MSG(dsi_trans));
1361 		tmp |= LINK_ENTER_ULPS;
1362 		tmp &= ~LINK_ULPS_TYPE_LP11;
1363 		intel_de_write(dev_priv, DSI_LP_MSG(dsi_trans), tmp);
1364 
1365 		if (wait_for_us((intel_de_read(dev_priv, DSI_LP_MSG(dsi_trans)) &
1366 				 LINK_IN_ULPS),
1367 				10))
1368 			drm_err(&dev_priv->drm, "DSI link not in ULPS\n");
1369 	}
1370 
1371 	/* disable ddi function */
1372 	for_each_dsi_port(port, intel_dsi->ports) {
1373 		dsi_trans = dsi_port_to_transcoder(port);
1374 		tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(dsi_trans));
1375 		tmp &= ~TRANS_DDI_FUNC_ENABLE;
1376 		intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(dsi_trans), tmp);
1377 	}
1378 
1379 	/* disable port sync mode if dual link */
1380 	if (intel_dsi->dual_link) {
1381 		for_each_dsi_port(port, intel_dsi->ports) {
1382 			dsi_trans = dsi_port_to_transcoder(port);
1383 			tmp = intel_de_read(dev_priv,
1384 					    TRANS_DDI_FUNC_CTL2(dsi_trans));
1385 			tmp &= ~PORT_SYNC_MODE_ENABLE;
1386 			intel_de_write(dev_priv,
1387 				       TRANS_DDI_FUNC_CTL2(dsi_trans), tmp);
1388 		}
1389 	}
1390 }
1391 
1392 static void gen11_dsi_disable_port(struct intel_encoder *encoder)
1393 {
1394 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1395 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1396 	u32 tmp;
1397 	enum port port;
1398 
1399 	gen11_dsi_ungate_clocks(encoder);
1400 	for_each_dsi_port(port, intel_dsi->ports) {
1401 		tmp = intel_de_read(dev_priv, DDI_BUF_CTL(port));
1402 		tmp &= ~DDI_BUF_CTL_ENABLE;
1403 		intel_de_write(dev_priv, DDI_BUF_CTL(port), tmp);
1404 
1405 		if (wait_for_us((intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
1406 				 DDI_BUF_IS_IDLE),
1407 				 8))
1408 			drm_err(&dev_priv->drm,
1409 				"DDI port:%c buffer not idle\n",
1410 				port_name(port));
1411 	}
1412 	gen11_dsi_gate_clocks(encoder);
1413 }
1414 
1415 static void gen11_dsi_disable_io_power(struct intel_encoder *encoder)
1416 {
1417 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1418 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1419 	enum port port;
1420 	u32 tmp;
1421 
1422 	for_each_dsi_port(port, intel_dsi->ports) {
1423 		intel_wakeref_t wakeref;
1424 
1425 		wakeref = fetch_and_zero(&intel_dsi->io_wakeref[port]);
1426 		intel_display_power_put(dev_priv,
1427 					port == PORT_A ?
1428 					POWER_DOMAIN_PORT_DDI_A_IO :
1429 					POWER_DOMAIN_PORT_DDI_B_IO,
1430 					wakeref);
1431 	}
1432 
1433 	/* set mode to DDI */
1434 	for_each_dsi_port(port, intel_dsi->ports) {
1435 		tmp = intel_de_read(dev_priv, ICL_DSI_IO_MODECTL(port));
1436 		tmp &= ~COMBO_PHY_MODE_DSI;
1437 		intel_de_write(dev_priv, ICL_DSI_IO_MODECTL(port), tmp);
1438 	}
1439 }
1440 
1441 static void gen11_dsi_disable(struct intel_atomic_state *state,
1442 			      struct intel_encoder *encoder,
1443 			      const struct intel_crtc_state *old_crtc_state,
1444 			      const struct drm_connector_state *old_conn_state)
1445 {
1446 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1447 	struct intel_crtc *crtc = to_intel_crtc(old_conn_state->crtc);
1448 
1449 	/* step1: turn off backlight */
1450 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_OFF);
1451 	intel_backlight_disable(old_conn_state);
1452 
1453 	/* step2d,e: disable transcoder and wait */
1454 	gen11_dsi_disable_transcoder(encoder);
1455 
1456 	/* Wa_1409054076:icl,jsl,ehl */
1457 	icl_apply_kvmr_pipe_a_wa(encoder, crtc->pipe, false);
1458 
1459 	/* step2f,g: powerdown panel */
1460 	gen11_dsi_powerdown_panel(encoder);
1461 
1462 	/* step2h,i,j: deconfig trancoder */
1463 	gen11_dsi_deconfigure_trancoder(encoder);
1464 
1465 	/* step3: disable port */
1466 	gen11_dsi_disable_port(encoder);
1467 
1468 	gen11_dsi_config_util_pin(encoder, false);
1469 
1470 	/* step4: disable IO power */
1471 	gen11_dsi_disable_io_power(encoder);
1472 }
1473 
1474 static void gen11_dsi_post_disable(struct intel_atomic_state *state,
1475 				   struct intel_encoder *encoder,
1476 				   const struct intel_crtc_state *old_crtc_state,
1477 				   const struct drm_connector_state *old_conn_state)
1478 {
1479 	intel_crtc_vblank_off(old_crtc_state);
1480 
1481 	intel_dsc_disable(old_crtc_state);
1482 
1483 	skl_scaler_disable(old_crtc_state);
1484 }
1485 
1486 static enum drm_mode_status gen11_dsi_mode_valid(struct drm_connector *connector,
1487 						 struct drm_display_mode *mode)
1488 {
1489 	/* FIXME: DSC? */
1490 	return intel_dsi_mode_valid(connector, mode);
1491 }
1492 
1493 static void gen11_dsi_get_timings(struct intel_encoder *encoder,
1494 				  struct intel_crtc_state *pipe_config)
1495 {
1496 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1497 	struct drm_display_mode *adjusted_mode =
1498 					&pipe_config->hw.adjusted_mode;
1499 
1500 	if (pipe_config->dsc.compressed_bpp) {
1501 		int div = pipe_config->dsc.compressed_bpp;
1502 		int mul = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
1503 
1504 		adjusted_mode->crtc_htotal =
1505 			DIV_ROUND_UP(adjusted_mode->crtc_htotal * mul, div);
1506 		adjusted_mode->crtc_hsync_start =
1507 			DIV_ROUND_UP(adjusted_mode->crtc_hsync_start * mul, div);
1508 		adjusted_mode->crtc_hsync_end =
1509 			DIV_ROUND_UP(adjusted_mode->crtc_hsync_end * mul, div);
1510 	}
1511 
1512 	if (intel_dsi->dual_link) {
1513 		adjusted_mode->crtc_hdisplay *= 2;
1514 		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
1515 			adjusted_mode->crtc_hdisplay -=
1516 						intel_dsi->pixel_overlap;
1517 		adjusted_mode->crtc_htotal *= 2;
1518 	}
1519 	adjusted_mode->crtc_hblank_start = adjusted_mode->crtc_hdisplay;
1520 	adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_htotal;
1521 
1522 	if (intel_dsi->operation_mode == INTEL_DSI_VIDEO_MODE) {
1523 		if (intel_dsi->dual_link) {
1524 			adjusted_mode->crtc_hsync_start *= 2;
1525 			adjusted_mode->crtc_hsync_end *= 2;
1526 		}
1527 	}
1528 	adjusted_mode->crtc_vblank_start = adjusted_mode->crtc_vdisplay;
1529 	adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vtotal;
1530 }
1531 
1532 static bool gen11_dsi_is_periodic_cmd_mode(struct intel_dsi *intel_dsi)
1533 {
1534 	struct drm_device *dev = intel_dsi->base.base.dev;
1535 	struct drm_i915_private *dev_priv = to_i915(dev);
1536 	enum transcoder dsi_trans;
1537 	u32 val;
1538 
1539 	if (intel_dsi->ports == BIT(PORT_B))
1540 		dsi_trans = TRANSCODER_DSI_1;
1541 	else
1542 		dsi_trans = TRANSCODER_DSI_0;
1543 
1544 	val = intel_de_read(dev_priv, DSI_TRANS_FUNC_CONF(dsi_trans));
1545 	return (val & DSI_PERIODIC_FRAME_UPDATE_ENABLE);
1546 }
1547 
1548 static void gen11_dsi_get_cmd_mode_config(struct intel_dsi *intel_dsi,
1549 					  struct intel_crtc_state *pipe_config)
1550 {
1551 	if (intel_dsi->ports == (BIT(PORT_B) | BIT(PORT_A)))
1552 		pipe_config->mode_flags |= I915_MODE_FLAG_DSI_USE_TE1 |
1553 					    I915_MODE_FLAG_DSI_USE_TE0;
1554 	else if (intel_dsi->ports == BIT(PORT_B))
1555 		pipe_config->mode_flags |= I915_MODE_FLAG_DSI_USE_TE1;
1556 	else
1557 		pipe_config->mode_flags |= I915_MODE_FLAG_DSI_USE_TE0;
1558 }
1559 
1560 static void gen11_dsi_get_config(struct intel_encoder *encoder,
1561 				 struct intel_crtc_state *pipe_config)
1562 {
1563 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
1564 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1565 
1566 	intel_ddi_get_clock(encoder, pipe_config, icl_ddi_combo_get_pll(encoder));
1567 
1568 	pipe_config->hw.adjusted_mode.crtc_clock = intel_dsi->pclk;
1569 	if (intel_dsi->dual_link)
1570 		pipe_config->hw.adjusted_mode.crtc_clock *= 2;
1571 
1572 	gen11_dsi_get_timings(encoder, pipe_config);
1573 	pipe_config->output_types |= BIT(INTEL_OUTPUT_DSI);
1574 	pipe_config->pipe_bpp = bdw_get_pipemisc_bpp(crtc);
1575 
1576 	/* Get the details on which TE should be enabled */
1577 	if (is_cmd_mode(intel_dsi))
1578 		gen11_dsi_get_cmd_mode_config(intel_dsi, pipe_config);
1579 
1580 	if (gen11_dsi_is_periodic_cmd_mode(intel_dsi))
1581 		pipe_config->mode_flags |= I915_MODE_FLAG_DSI_PERIODIC_CMD_MODE;
1582 }
1583 
1584 static void gen11_dsi_sync_state(struct intel_encoder *encoder,
1585 				 const struct intel_crtc_state *crtc_state)
1586 {
1587 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1588 	struct intel_crtc *intel_crtc;
1589 	enum pipe pipe;
1590 
1591 	if (!crtc_state)
1592 		return;
1593 
1594 	intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
1595 	pipe = intel_crtc->pipe;
1596 
1597 	/* wa verify 1409054076:icl,jsl,ehl */
1598 	if (DISPLAY_VER(dev_priv) == 11 && pipe == PIPE_B &&
1599 	    !(intel_de_read(dev_priv, CHICKEN_PAR1_1) & IGNORE_KVMR_PIPE_A))
1600 		drm_dbg_kms(&dev_priv->drm,
1601 			    "[ENCODER:%d:%s] BIOS left IGNORE_KVMR_PIPE_A cleared with pipe B enabled\n",
1602 			    encoder->base.base.id,
1603 			    encoder->base.name);
1604 }
1605 
1606 static int gen11_dsi_dsc_compute_config(struct intel_encoder *encoder,
1607 					struct intel_crtc_state *crtc_state)
1608 {
1609 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1610 	struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
1611 	int dsc_max_bpc = DISPLAY_VER(dev_priv) >= 12 ? 12 : 10;
1612 	bool use_dsc;
1613 	int ret;
1614 
1615 	use_dsc = intel_bios_get_dsc_params(encoder, crtc_state, dsc_max_bpc);
1616 	if (!use_dsc)
1617 		return 0;
1618 
1619 	if (crtc_state->pipe_bpp < 8 * 3)
1620 		return -EINVAL;
1621 
1622 	/* FIXME: split only when necessary */
1623 	if (crtc_state->dsc.slice_count > 1)
1624 		crtc_state->dsc.dsc_split = true;
1625 
1626 	vdsc_cfg->convert_rgb = true;
1627 
1628 	/* FIXME: initialize from VBT */
1629 	vdsc_cfg->rc_model_size = DSC_RC_MODEL_SIZE_CONST;
1630 
1631 	ret = intel_dsc_compute_params(encoder, crtc_state);
1632 	if (ret)
1633 		return ret;
1634 
1635 	/* DSI specific sanity checks on the common code */
1636 	drm_WARN_ON(&dev_priv->drm, vdsc_cfg->vbr_enable);
1637 	drm_WARN_ON(&dev_priv->drm, vdsc_cfg->simple_422);
1638 	drm_WARN_ON(&dev_priv->drm,
1639 		    vdsc_cfg->pic_width % vdsc_cfg->slice_width);
1640 	drm_WARN_ON(&dev_priv->drm, vdsc_cfg->slice_height < 8);
1641 	drm_WARN_ON(&dev_priv->drm,
1642 		    vdsc_cfg->pic_height % vdsc_cfg->slice_height);
1643 
1644 	ret = drm_dsc_compute_rc_parameters(vdsc_cfg);
1645 	if (ret)
1646 		return ret;
1647 
1648 	crtc_state->dsc.compression_enable = true;
1649 
1650 	return 0;
1651 }
1652 
1653 static int gen11_dsi_compute_config(struct intel_encoder *encoder,
1654 				    struct intel_crtc_state *pipe_config,
1655 				    struct drm_connector_state *conn_state)
1656 {
1657 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1658 	struct intel_dsi *intel_dsi = container_of(encoder, struct intel_dsi,
1659 						   base);
1660 	struct intel_connector *intel_connector = intel_dsi->attached_connector;
1661 	struct drm_display_mode *adjusted_mode =
1662 		&pipe_config->hw.adjusted_mode;
1663 	int ret;
1664 
1665 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
1666 
1667 	ret = intel_panel_compute_config(intel_connector, adjusted_mode);
1668 	if (ret)
1669 		return ret;
1670 
1671 	ret = intel_panel_fitting(pipe_config, conn_state);
1672 	if (ret)
1673 		return ret;
1674 
1675 	adjusted_mode->flags = 0;
1676 
1677 	/* Dual link goes to trancoder DSI'0' */
1678 	if (intel_dsi->ports == BIT(PORT_B))
1679 		pipe_config->cpu_transcoder = TRANSCODER_DSI_1;
1680 	else
1681 		pipe_config->cpu_transcoder = TRANSCODER_DSI_0;
1682 
1683 	if (intel_dsi->pixel_format == MIPI_DSI_FMT_RGB888)
1684 		pipe_config->pipe_bpp = 24;
1685 	else
1686 		pipe_config->pipe_bpp = 18;
1687 
1688 	pipe_config->clock_set = true;
1689 
1690 	if (gen11_dsi_dsc_compute_config(encoder, pipe_config))
1691 		drm_dbg_kms(&i915->drm, "Attempting to use DSC failed\n");
1692 
1693 	pipe_config->port_clock = afe_clk(encoder, pipe_config) / 5;
1694 
1695 	/*
1696 	 * In case of TE GATE cmd mode, we
1697 	 * receive TE from the slave if
1698 	 * dual link is enabled
1699 	 */
1700 	if (is_cmd_mode(intel_dsi))
1701 		gen11_dsi_get_cmd_mode_config(intel_dsi, pipe_config);
1702 
1703 	return 0;
1704 }
1705 
1706 static void gen11_dsi_get_power_domains(struct intel_encoder *encoder,
1707 					struct intel_crtc_state *crtc_state)
1708 {
1709 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1710 
1711 	get_dsi_io_power_domains(i915,
1712 				 enc_to_intel_dsi(encoder));
1713 }
1714 
1715 static bool gen11_dsi_get_hw_state(struct intel_encoder *encoder,
1716 				   enum pipe *pipe)
1717 {
1718 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1719 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1720 	enum transcoder dsi_trans;
1721 	intel_wakeref_t wakeref;
1722 	enum port port;
1723 	bool ret = false;
1724 	u32 tmp;
1725 
1726 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1727 						     encoder->power_domain);
1728 	if (!wakeref)
1729 		return false;
1730 
1731 	for_each_dsi_port(port, intel_dsi->ports) {
1732 		dsi_trans = dsi_port_to_transcoder(port);
1733 		tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(dsi_trans));
1734 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
1735 		case TRANS_DDI_EDP_INPUT_A_ON:
1736 			*pipe = PIPE_A;
1737 			break;
1738 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
1739 			*pipe = PIPE_B;
1740 			break;
1741 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
1742 			*pipe = PIPE_C;
1743 			break;
1744 		case TRANS_DDI_EDP_INPUT_D_ONOFF:
1745 			*pipe = PIPE_D;
1746 			break;
1747 		default:
1748 			drm_err(&dev_priv->drm, "Invalid PIPE input\n");
1749 			goto out;
1750 		}
1751 
1752 		tmp = intel_de_read(dev_priv, PIPECONF(dsi_trans));
1753 		ret = tmp & PIPECONF_ENABLE;
1754 	}
1755 out:
1756 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
1757 	return ret;
1758 }
1759 
1760 static bool gen11_dsi_initial_fastset_check(struct intel_encoder *encoder,
1761 					    struct intel_crtc_state *crtc_state)
1762 {
1763 	if (crtc_state->dsc.compression_enable) {
1764 		drm_dbg_kms(encoder->base.dev, "Forcing full modeset due to DSC being enabled\n");
1765 		crtc_state->uapi.mode_changed = true;
1766 
1767 		return false;
1768 	}
1769 
1770 	return true;
1771 }
1772 
1773 static void gen11_dsi_encoder_destroy(struct drm_encoder *encoder)
1774 {
1775 	intel_encoder_destroy(encoder);
1776 }
1777 
1778 static const struct drm_encoder_funcs gen11_dsi_encoder_funcs = {
1779 	.destroy = gen11_dsi_encoder_destroy,
1780 };
1781 
1782 static const struct drm_connector_funcs gen11_dsi_connector_funcs = {
1783 	.detect = intel_panel_detect,
1784 	.late_register = intel_connector_register,
1785 	.early_unregister = intel_connector_unregister,
1786 	.destroy = intel_connector_destroy,
1787 	.fill_modes = drm_helper_probe_single_connector_modes,
1788 	.atomic_get_property = intel_digital_connector_atomic_get_property,
1789 	.atomic_set_property = intel_digital_connector_atomic_set_property,
1790 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1791 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
1792 };
1793 
1794 static const struct drm_connector_helper_funcs gen11_dsi_connector_helper_funcs = {
1795 	.get_modes = intel_dsi_get_modes,
1796 	.mode_valid = gen11_dsi_mode_valid,
1797 	.atomic_check = intel_digital_connector_atomic_check,
1798 };
1799 
1800 static int gen11_dsi_host_attach(struct mipi_dsi_host *host,
1801 				 struct mipi_dsi_device *dsi)
1802 {
1803 	return 0;
1804 }
1805 
1806 static int gen11_dsi_host_detach(struct mipi_dsi_host *host,
1807 				 struct mipi_dsi_device *dsi)
1808 {
1809 	return 0;
1810 }
1811 
1812 static ssize_t gen11_dsi_host_transfer(struct mipi_dsi_host *host,
1813 				       const struct mipi_dsi_msg *msg)
1814 {
1815 	struct intel_dsi_host *intel_dsi_host = to_intel_dsi_host(host);
1816 	struct mipi_dsi_packet dsi_pkt;
1817 	ssize_t ret;
1818 	bool enable_lpdt = false;
1819 
1820 	ret = mipi_dsi_create_packet(&dsi_pkt, msg);
1821 	if (ret < 0)
1822 		return ret;
1823 
1824 	if (msg->flags & MIPI_DSI_MSG_USE_LPM)
1825 		enable_lpdt = true;
1826 
1827 	/* only long packet contains payload */
1828 	if (mipi_dsi_packet_format_is_long(msg->type)) {
1829 		ret = dsi_send_pkt_payld(intel_dsi_host, &dsi_pkt);
1830 		if (ret < 0)
1831 			return ret;
1832 	}
1833 
1834 	/* send packet header */
1835 	ret  = dsi_send_pkt_hdr(intel_dsi_host, &dsi_pkt, enable_lpdt);
1836 	if (ret < 0)
1837 		return ret;
1838 
1839 	//TODO: add payload receive code if needed
1840 
1841 	ret = sizeof(dsi_pkt.header) + dsi_pkt.payload_length;
1842 
1843 	return ret;
1844 }
1845 
1846 static const struct mipi_dsi_host_ops gen11_dsi_host_ops = {
1847 	.attach = gen11_dsi_host_attach,
1848 	.detach = gen11_dsi_host_detach,
1849 	.transfer = gen11_dsi_host_transfer,
1850 };
1851 
1852 #define ICL_PREPARE_CNT_MAX	0x7
1853 #define ICL_CLK_ZERO_CNT_MAX	0xf
1854 #define ICL_TRAIL_CNT_MAX	0x7
1855 #define ICL_TCLK_PRE_CNT_MAX	0x3
1856 #define ICL_TCLK_POST_CNT_MAX	0x7
1857 #define ICL_HS_ZERO_CNT_MAX	0xf
1858 #define ICL_EXIT_ZERO_CNT_MAX	0x7
1859 
1860 static void icl_dphy_param_init(struct intel_dsi *intel_dsi)
1861 {
1862 	struct drm_device *dev = intel_dsi->base.base.dev;
1863 	struct drm_i915_private *dev_priv = to_i915(dev);
1864 	struct mipi_config *mipi_config = dev_priv->vbt.dsi.config;
1865 	u32 tlpx_ns;
1866 	u32 prepare_cnt, exit_zero_cnt, clk_zero_cnt, trail_cnt;
1867 	u32 ths_prepare_ns, tclk_trail_ns;
1868 	u32 hs_zero_cnt;
1869 	u32 tclk_pre_cnt, tclk_post_cnt;
1870 
1871 	tlpx_ns = intel_dsi_tlpx_ns(intel_dsi);
1872 
1873 	tclk_trail_ns = max(mipi_config->tclk_trail, mipi_config->ths_trail);
1874 	ths_prepare_ns = max(mipi_config->ths_prepare,
1875 			     mipi_config->tclk_prepare);
1876 
1877 	/*
1878 	 * prepare cnt in escape clocks
1879 	 * this field represents a hexadecimal value with a precision
1880 	 * of 1.2 – i.e. the most significant bit is the integer
1881 	 * and the least significant 2 bits are fraction bits.
1882 	 * so, the field can represent a range of 0.25 to 1.75
1883 	 */
1884 	prepare_cnt = DIV_ROUND_UP(ths_prepare_ns * 4, tlpx_ns);
1885 	if (prepare_cnt > ICL_PREPARE_CNT_MAX) {
1886 		drm_dbg_kms(&dev_priv->drm, "prepare_cnt out of range (%d)\n",
1887 			    prepare_cnt);
1888 		prepare_cnt = ICL_PREPARE_CNT_MAX;
1889 	}
1890 
1891 	/* clk zero count in escape clocks */
1892 	clk_zero_cnt = DIV_ROUND_UP(mipi_config->tclk_prepare_clkzero -
1893 				    ths_prepare_ns, tlpx_ns);
1894 	if (clk_zero_cnt > ICL_CLK_ZERO_CNT_MAX) {
1895 		drm_dbg_kms(&dev_priv->drm,
1896 			    "clk_zero_cnt out of range (%d)\n", clk_zero_cnt);
1897 		clk_zero_cnt = ICL_CLK_ZERO_CNT_MAX;
1898 	}
1899 
1900 	/* trail cnt in escape clocks*/
1901 	trail_cnt = DIV_ROUND_UP(tclk_trail_ns, tlpx_ns);
1902 	if (trail_cnt > ICL_TRAIL_CNT_MAX) {
1903 		drm_dbg_kms(&dev_priv->drm, "trail_cnt out of range (%d)\n",
1904 			    trail_cnt);
1905 		trail_cnt = ICL_TRAIL_CNT_MAX;
1906 	}
1907 
1908 	/* tclk pre count in escape clocks */
1909 	tclk_pre_cnt = DIV_ROUND_UP(mipi_config->tclk_pre, tlpx_ns);
1910 	if (tclk_pre_cnt > ICL_TCLK_PRE_CNT_MAX) {
1911 		drm_dbg_kms(&dev_priv->drm,
1912 			    "tclk_pre_cnt out of range (%d)\n", tclk_pre_cnt);
1913 		tclk_pre_cnt = ICL_TCLK_PRE_CNT_MAX;
1914 	}
1915 
1916 	/* tclk post count in escape clocks */
1917 	tclk_post_cnt = DIV_ROUND_UP(mipi_config->tclk_post, tlpx_ns);
1918 	if (tclk_post_cnt > ICL_TCLK_POST_CNT_MAX) {
1919 		drm_dbg_kms(&dev_priv->drm,
1920 			    "tclk_post_cnt out of range (%d)\n",
1921 			    tclk_post_cnt);
1922 		tclk_post_cnt = ICL_TCLK_POST_CNT_MAX;
1923 	}
1924 
1925 	/* hs zero cnt in escape clocks */
1926 	hs_zero_cnt = DIV_ROUND_UP(mipi_config->ths_prepare_hszero -
1927 				   ths_prepare_ns, tlpx_ns);
1928 	if (hs_zero_cnt > ICL_HS_ZERO_CNT_MAX) {
1929 		drm_dbg_kms(&dev_priv->drm, "hs_zero_cnt out of range (%d)\n",
1930 			    hs_zero_cnt);
1931 		hs_zero_cnt = ICL_HS_ZERO_CNT_MAX;
1932 	}
1933 
1934 	/* hs exit zero cnt in escape clocks */
1935 	exit_zero_cnt = DIV_ROUND_UP(mipi_config->ths_exit, tlpx_ns);
1936 	if (exit_zero_cnt > ICL_EXIT_ZERO_CNT_MAX) {
1937 		drm_dbg_kms(&dev_priv->drm,
1938 			    "exit_zero_cnt out of range (%d)\n",
1939 			    exit_zero_cnt);
1940 		exit_zero_cnt = ICL_EXIT_ZERO_CNT_MAX;
1941 	}
1942 
1943 	/* clock lane dphy timings */
1944 	intel_dsi->dphy_reg = (CLK_PREPARE_OVERRIDE |
1945 			       CLK_PREPARE(prepare_cnt) |
1946 			       CLK_ZERO_OVERRIDE |
1947 			       CLK_ZERO(clk_zero_cnt) |
1948 			       CLK_PRE_OVERRIDE |
1949 			       CLK_PRE(tclk_pre_cnt) |
1950 			       CLK_POST_OVERRIDE |
1951 			       CLK_POST(tclk_post_cnt) |
1952 			       CLK_TRAIL_OVERRIDE |
1953 			       CLK_TRAIL(trail_cnt));
1954 
1955 	/* data lanes dphy timings */
1956 	intel_dsi->dphy_data_lane_reg = (HS_PREPARE_OVERRIDE |
1957 					 HS_PREPARE(prepare_cnt) |
1958 					 HS_ZERO_OVERRIDE |
1959 					 HS_ZERO(hs_zero_cnt) |
1960 					 HS_TRAIL_OVERRIDE |
1961 					 HS_TRAIL(trail_cnt) |
1962 					 HS_EXIT_OVERRIDE |
1963 					 HS_EXIT(exit_zero_cnt));
1964 
1965 	intel_dsi_log_params(intel_dsi);
1966 }
1967 
1968 static void icl_dsi_add_properties(struct intel_connector *connector)
1969 {
1970 	u32 allowed_scalers;
1971 
1972 	allowed_scalers = BIT(DRM_MODE_SCALE_ASPECT) |
1973 			   BIT(DRM_MODE_SCALE_FULLSCREEN) |
1974 			   BIT(DRM_MODE_SCALE_CENTER);
1975 
1976 	drm_connector_attach_scaling_mode_property(&connector->base,
1977 						   allowed_scalers);
1978 
1979 	connector->base.state->scaling_mode = DRM_MODE_SCALE_ASPECT;
1980 
1981 	drm_connector_set_panel_orientation_with_quirk(&connector->base,
1982 				intel_dsi_get_panel_orientation(connector),
1983 				connector->panel.fixed_mode->hdisplay,
1984 				connector->panel.fixed_mode->vdisplay);
1985 }
1986 
1987 void icl_dsi_init(struct drm_i915_private *dev_priv)
1988 {
1989 	struct drm_device *dev = &dev_priv->drm;
1990 	struct intel_dsi *intel_dsi;
1991 	struct intel_encoder *encoder;
1992 	struct intel_connector *intel_connector;
1993 	struct drm_connector *connector;
1994 	struct drm_display_mode *fixed_mode;
1995 	enum port port;
1996 
1997 	if (!intel_bios_is_dsi_present(dev_priv, &port))
1998 		return;
1999 
2000 	intel_dsi = kzalloc(sizeof(*intel_dsi), GFP_KERNEL);
2001 	if (!intel_dsi)
2002 		return;
2003 
2004 	intel_connector = intel_connector_alloc();
2005 	if (!intel_connector) {
2006 		kfree(intel_dsi);
2007 		return;
2008 	}
2009 
2010 	encoder = &intel_dsi->base;
2011 	intel_dsi->attached_connector = intel_connector;
2012 	connector = &intel_connector->base;
2013 
2014 	/* register DSI encoder with DRM subsystem */
2015 	drm_encoder_init(dev, &encoder->base, &gen11_dsi_encoder_funcs,
2016 			 DRM_MODE_ENCODER_DSI, "DSI %c", port_name(port));
2017 
2018 	encoder->pre_pll_enable = gen11_dsi_pre_pll_enable;
2019 	encoder->pre_enable = gen11_dsi_pre_enable;
2020 	encoder->enable = gen11_dsi_enable;
2021 	encoder->disable = gen11_dsi_disable;
2022 	encoder->post_disable = gen11_dsi_post_disable;
2023 	encoder->port = port;
2024 	encoder->get_config = gen11_dsi_get_config;
2025 	encoder->sync_state = gen11_dsi_sync_state;
2026 	encoder->update_pipe = intel_backlight_update;
2027 	encoder->compute_config = gen11_dsi_compute_config;
2028 	encoder->get_hw_state = gen11_dsi_get_hw_state;
2029 	encoder->initial_fastset_check = gen11_dsi_initial_fastset_check;
2030 	encoder->type = INTEL_OUTPUT_DSI;
2031 	encoder->cloneable = 0;
2032 	encoder->pipe_mask = ~0;
2033 	encoder->power_domain = POWER_DOMAIN_PORT_DSI;
2034 	encoder->get_power_domains = gen11_dsi_get_power_domains;
2035 	encoder->disable_clock = gen11_dsi_gate_clocks;
2036 	encoder->is_clock_enabled = gen11_dsi_is_clock_enabled;
2037 
2038 	/* register DSI connector with DRM subsystem */
2039 	drm_connector_init(dev, connector, &gen11_dsi_connector_funcs,
2040 			   DRM_MODE_CONNECTOR_DSI);
2041 	drm_connector_helper_add(connector, &gen11_dsi_connector_helper_funcs);
2042 	connector->display_info.subpixel_order = SubPixelHorizontalRGB;
2043 	connector->interlace_allowed = false;
2044 	connector->doublescan_allowed = false;
2045 	intel_connector->get_hw_state = intel_connector_get_hw_state;
2046 
2047 	/* attach connector to encoder */
2048 	intel_connector_attach_encoder(intel_connector, encoder);
2049 
2050 	mutex_lock(&dev->mode_config.mutex);
2051 	fixed_mode = intel_panel_vbt_fixed_mode(intel_connector);
2052 	mutex_unlock(&dev->mode_config.mutex);
2053 
2054 	if (!fixed_mode) {
2055 		drm_err(&dev_priv->drm, "DSI fixed mode info missing\n");
2056 		goto err;
2057 	}
2058 
2059 	intel_panel_init(&intel_connector->panel, fixed_mode, NULL);
2060 	intel_backlight_setup(intel_connector, INVALID_PIPE);
2061 
2062 	if (dev_priv->vbt.dsi.config->dual_link)
2063 		intel_dsi->ports = BIT(PORT_A) | BIT(PORT_B);
2064 	else
2065 		intel_dsi->ports = BIT(port);
2066 
2067 	intel_dsi->dcs_backlight_ports = dev_priv->vbt.dsi.bl_ports;
2068 	intel_dsi->dcs_cabc_ports = dev_priv->vbt.dsi.cabc_ports;
2069 
2070 	for_each_dsi_port(port, intel_dsi->ports) {
2071 		struct intel_dsi_host *host;
2072 
2073 		host = intel_dsi_host_init(intel_dsi, &gen11_dsi_host_ops, port);
2074 		if (!host)
2075 			goto err;
2076 
2077 		intel_dsi->dsi_hosts[port] = host;
2078 	}
2079 
2080 	if (!intel_dsi_vbt_init(intel_dsi, MIPI_DSI_GENERIC_PANEL_ID)) {
2081 		drm_dbg_kms(&dev_priv->drm, "no device found\n");
2082 		goto err;
2083 	}
2084 
2085 	icl_dphy_param_init(intel_dsi);
2086 
2087 	icl_dsi_add_properties(intel_connector);
2088 	return;
2089 
2090 err:
2091 	drm_connector_cleanup(connector);
2092 	drm_encoder_cleanup(&encoder->base);
2093 	kfree(intel_dsi);
2094 	kfree(intel_connector);
2095 }
2096