xref: /linux/drivers/gpu/drm/i915/display/icl_dsi.c (revision 6a6d914de30f15472b2dc36e8ac6bf016cfbaed5)
1379bc100SJani Nikula /*
2379bc100SJani Nikula  * Copyright © 2018 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
21379bc100SJani Nikula  * DEALINGS IN THE SOFTWARE.
22379bc100SJani Nikula  *
23379bc100SJani Nikula  * Authors:
24379bc100SJani Nikula  *   Madhav Chauhan <madhav.chauhan@intel.com>
25379bc100SJani Nikula  *   Jani Nikula <jani.nikula@intel.com>
26379bc100SJani Nikula  */
27379bc100SJani Nikula 
28379bc100SJani Nikula #include <drm/drm_atomic_helper.h>
29379bc100SJani Nikula #include <drm/drm_mipi_dsi.h>
30379bc100SJani Nikula 
31617ed6c2SJani Nikula #include "icl_dsi.h"
32379bc100SJani Nikula #include "intel_atomic.h"
336cc42fbeSJani Nikula #include "intel_backlight.h"
34379bc100SJani Nikula #include "intel_combo_phy.h"
35d0864ee4SMatt Roper #include "intel_combo_phy_regs.h"
36379bc100SJani Nikula #include "intel_connector.h"
377c53e628SJani Nikula #include "intel_crtc.h"
38379bc100SJani Nikula #include "intel_ddi.h"
397785ae0bSVille Syrjälä #include "intel_de.h"
40379bc100SJani Nikula #include "intel_dsi.h"
41aebdd742SJani Nikula #include "intel_dsi_vbt.h"
42379bc100SJani Nikula #include "intel_panel.h"
432b68392eSJani Nikula #include "intel_vdsc.h"
44714b1cdbSDave Airlie #include "skl_scaler.h"
4546d12f91SDave Airlie #include "skl_universal_plane.h"
46379bc100SJani Nikula 
4781b55ef1SJani Nikula static int header_credits_available(struct drm_i915_private *dev_priv,
48379bc100SJani Nikula 				    enum transcoder dsi_trans)
49379bc100SJani Nikula {
501c63f6dfSJani Nikula 	return (intel_de_read(dev_priv, DSI_CMD_TXCTL(dsi_trans)) & FREE_HEADER_CREDIT_MASK)
51379bc100SJani Nikula 		>> FREE_HEADER_CREDIT_SHIFT;
52379bc100SJani Nikula }
53379bc100SJani Nikula 
5481b55ef1SJani Nikula static int payload_credits_available(struct drm_i915_private *dev_priv,
55379bc100SJani Nikula 				     enum transcoder dsi_trans)
56379bc100SJani Nikula {
571c63f6dfSJani Nikula 	return (intel_de_read(dev_priv, DSI_CMD_TXCTL(dsi_trans)) & FREE_PLOAD_CREDIT_MASK)
58379bc100SJani Nikula 		>> FREE_PLOAD_CREDIT_SHIFT;
59379bc100SJani Nikula }
60379bc100SJani Nikula 
6143315f86SLee Shawn C static bool wait_for_header_credits(struct drm_i915_private *dev_priv,
6243315f86SLee Shawn C 				    enum transcoder dsi_trans, int hdr_credit)
63379bc100SJani Nikula {
64379bc100SJani Nikula 	if (wait_for_us(header_credits_available(dev_priv, dsi_trans) >=
6543315f86SLee Shawn C 			hdr_credit, 100)) {
66b5280cd0SWambui Karuga 		drm_err(&dev_priv->drm, "DSI header credits not released\n");
6743315f86SLee Shawn C 		return false;
68379bc100SJani Nikula 	}
69379bc100SJani Nikula 
7043315f86SLee Shawn C 	return true;
7143315f86SLee Shawn C }
7243315f86SLee Shawn C 
7343315f86SLee Shawn C static bool wait_for_payload_credits(struct drm_i915_private *dev_priv,
7443315f86SLee Shawn C 				     enum transcoder dsi_trans, int payld_credit)
75379bc100SJani Nikula {
76379bc100SJani Nikula 	if (wait_for_us(payload_credits_available(dev_priv, dsi_trans) >=
7743315f86SLee Shawn C 			payld_credit, 100)) {
78b5280cd0SWambui Karuga 		drm_err(&dev_priv->drm, "DSI payload credits not released\n");
7943315f86SLee Shawn C 		return false;
8043315f86SLee Shawn C 	}
8143315f86SLee Shawn C 
8243315f86SLee Shawn C 	return true;
83379bc100SJani Nikula }
84379bc100SJani Nikula 
85379bc100SJani Nikula static enum transcoder dsi_port_to_transcoder(enum port port)
86379bc100SJani Nikula {
87379bc100SJani Nikula 	if (port == PORT_A)
88379bc100SJani Nikula 		return TRANSCODER_DSI_0;
89379bc100SJani Nikula 	else
90379bc100SJani Nikula 		return TRANSCODER_DSI_1;
91379bc100SJani Nikula }
92379bc100SJani Nikula 
93379bc100SJani Nikula static void wait_for_cmds_dispatched_to_panel(struct intel_encoder *encoder)
94379bc100SJani Nikula {
95379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
96b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
97379bc100SJani Nikula 	struct mipi_dsi_device *dsi;
98379bc100SJani Nikula 	enum port port;
99379bc100SJani Nikula 	enum transcoder dsi_trans;
100379bc100SJani Nikula 	int ret;
101379bc100SJani Nikula 
102379bc100SJani Nikula 	/* wait for header/payload credits to be released */
103379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
104379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
10543315f86SLee Shawn C 		wait_for_header_credits(dev_priv, dsi_trans, MAX_HEADER_CREDIT);
10643315f86SLee Shawn C 		wait_for_payload_credits(dev_priv, dsi_trans, MAX_PLOAD_CREDIT);
107379bc100SJani Nikula 	}
108379bc100SJani Nikula 
109379bc100SJani Nikula 	/* send nop DCS command */
110379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
111379bc100SJani Nikula 		dsi = intel_dsi->dsi_hosts[port]->device;
112379bc100SJani Nikula 		dsi->mode_flags |= MIPI_DSI_MODE_LPM;
113379bc100SJani Nikula 		dsi->channel = 0;
114379bc100SJani Nikula 		ret = mipi_dsi_dcs_nop(dsi);
115379bc100SJani Nikula 		if (ret < 0)
116b5280cd0SWambui Karuga 			drm_err(&dev_priv->drm,
117b5280cd0SWambui Karuga 				"error sending DCS NOP command\n");
118379bc100SJani Nikula 	}
119379bc100SJani Nikula 
120379bc100SJani Nikula 	/* wait for header credits to be released */
121379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
122379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
12343315f86SLee Shawn C 		wait_for_header_credits(dev_priv, dsi_trans, MAX_HEADER_CREDIT);
124379bc100SJani Nikula 	}
125379bc100SJani Nikula 
126379bc100SJani Nikula 	/* wait for LP TX in progress bit to be cleared */
127379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
128379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
1291c63f6dfSJani Nikula 		if (wait_for_us(!(intel_de_read(dev_priv, DSI_LP_MSG(dsi_trans)) &
130379bc100SJani Nikula 				  LPTX_IN_PROGRESS), 20))
131b5280cd0SWambui Karuga 			drm_err(&dev_priv->drm, "LPTX bit not cleared\n");
132379bc100SJani Nikula 	}
133379bc100SJani Nikula }
134379bc100SJani Nikula 
135207ea507SJani Nikula static int dsi_send_pkt_payld(struct intel_dsi_host *host,
136207ea507SJani Nikula 			      const struct mipi_dsi_packet *packet)
137379bc100SJani Nikula {
138379bc100SJani Nikula 	struct intel_dsi *intel_dsi = host->intel_dsi;
139207ea507SJani Nikula 	struct drm_i915_private *i915 = to_i915(intel_dsi->base.base.dev);
140379bc100SJani Nikula 	enum transcoder dsi_trans = dsi_port_to_transcoder(host->port);
141207ea507SJani Nikula 	const u8 *data = packet->payload;
142207ea507SJani Nikula 	u32 len = packet->payload_length;
143379bc100SJani Nikula 	int i, j;
144379bc100SJani Nikula 
145207ea507SJani Nikula 	/* payload queue can accept *256 bytes*, check limit */
146207ea507SJani Nikula 	if (len > MAX_PLOAD_CREDIT * 4) {
147207ea507SJani Nikula 		drm_err(&i915->drm, "payload size exceeds max queue limit\n");
148207ea507SJani Nikula 		return -EINVAL;
149207ea507SJani Nikula 	}
150207ea507SJani Nikula 
151379bc100SJani Nikula 	for (i = 0; i < len; i += 4) {
152379bc100SJani Nikula 		u32 tmp = 0;
153379bc100SJani Nikula 
154207ea507SJani Nikula 		if (!wait_for_payload_credits(i915, dsi_trans, 1))
155207ea507SJani Nikula 			return -EBUSY;
156379bc100SJani Nikula 
157379bc100SJani Nikula 		for (j = 0; j < min_t(u32, len - i, 4); j++)
158379bc100SJani Nikula 			tmp |= *data++ << 8 * j;
159379bc100SJani Nikula 
160207ea507SJani Nikula 		intel_de_write(i915, DSI_CMD_TXPYLD(dsi_trans), tmp);
161379bc100SJani Nikula 	}
162379bc100SJani Nikula 
163207ea507SJani Nikula 	return 0;
164379bc100SJani Nikula }
165379bc100SJani Nikula 
166379bc100SJani Nikula static int dsi_send_pkt_hdr(struct intel_dsi_host *host,
1673e2947cdSJani Nikula 			    const struct mipi_dsi_packet *packet,
1683e2947cdSJani Nikula 			    bool enable_lpdt)
169379bc100SJani Nikula {
170379bc100SJani Nikula 	struct intel_dsi *intel_dsi = host->intel_dsi;
171379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(intel_dsi->base.base.dev);
172379bc100SJani Nikula 	enum transcoder dsi_trans = dsi_port_to_transcoder(host->port);
173379bc100SJani Nikula 	u32 tmp;
174379bc100SJani Nikula 
17543315f86SLee Shawn C 	if (!wait_for_header_credits(dev_priv, dsi_trans, 1))
176b90acd09SJani Nikula 		return -EBUSY;
177379bc100SJani Nikula 
1781c63f6dfSJani Nikula 	tmp = intel_de_read(dev_priv, DSI_CMD_TXHDR(dsi_trans));
179379bc100SJani Nikula 
1803e2947cdSJani Nikula 	if (packet->payload)
181379bc100SJani Nikula 		tmp |= PAYLOAD_PRESENT;
182379bc100SJani Nikula 	else
183379bc100SJani Nikula 		tmp &= ~PAYLOAD_PRESENT;
184379bc100SJani Nikula 
185379bc100SJani Nikula 	tmp &= ~VBLANK_FENCE;
186379bc100SJani Nikula 
187379bc100SJani Nikula 	if (enable_lpdt)
188379bc100SJani Nikula 		tmp |= LP_DATA_TRANSFER;
18938a1b50cSWilliam Tseng 	else
19038a1b50cSWilliam Tseng 		tmp &= ~LP_DATA_TRANSFER;
191379bc100SJani Nikula 
192379bc100SJani Nikula 	tmp &= ~(PARAM_WC_MASK | VC_MASK | DT_MASK);
1933e2947cdSJani Nikula 	tmp |= ((packet->header[0] & VC_MASK) << VC_SHIFT);
1943e2947cdSJani Nikula 	tmp |= ((packet->header[0] & DT_MASK) << DT_SHIFT);
1953e2947cdSJani Nikula 	tmp |= (packet->header[1] << PARAM_WC_LOWER_SHIFT);
1963e2947cdSJani Nikula 	tmp |= (packet->header[2] << PARAM_WC_UPPER_SHIFT);
1971c63f6dfSJani Nikula 	intel_de_write(dev_priv, DSI_CMD_TXHDR(dsi_trans), tmp);
198379bc100SJani Nikula 
199379bc100SJani Nikula 	return 0;
200379bc100SJani Nikula }
201379bc100SJani Nikula 
20226fb0d55SVandita Kulkarni void icl_dsi_frame_update(struct intel_crtc_state *crtc_state)
20326fb0d55SVandita Kulkarni {
20426fb0d55SVandita Kulkarni 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
20526fb0d55SVandita Kulkarni 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
20626fb0d55SVandita Kulkarni 	u32 tmp, mode_flags;
20726fb0d55SVandita Kulkarni 	enum port port;
20826fb0d55SVandita Kulkarni 
20926fb0d55SVandita Kulkarni 	mode_flags = crtc_state->mode_flags;
21026fb0d55SVandita Kulkarni 
21126fb0d55SVandita Kulkarni 	/*
21226fb0d55SVandita Kulkarni 	 * case 1 also covers dual link
21326fb0d55SVandita Kulkarni 	 * In case of dual link, frame update should be set on
21426fb0d55SVandita Kulkarni 	 * DSI_0
21526fb0d55SVandita Kulkarni 	 */
21626fb0d55SVandita Kulkarni 	if (mode_flags & I915_MODE_FLAG_DSI_USE_TE0)
21726fb0d55SVandita Kulkarni 		port = PORT_A;
21826fb0d55SVandita Kulkarni 	else if (mode_flags & I915_MODE_FLAG_DSI_USE_TE1)
21926fb0d55SVandita Kulkarni 		port = PORT_B;
22026fb0d55SVandita Kulkarni 	else
22126fb0d55SVandita Kulkarni 		return;
22226fb0d55SVandita Kulkarni 
22326fb0d55SVandita Kulkarni 	tmp = intel_de_read(dev_priv, DSI_CMD_FRMCTL(port));
22426fb0d55SVandita Kulkarni 	tmp |= DSI_FRAME_UPDATE_REQUEST;
22526fb0d55SVandita Kulkarni 	intel_de_write(dev_priv, DSI_CMD_FRMCTL(port), tmp);
22626fb0d55SVandita Kulkarni }
22726fb0d55SVandita Kulkarni 
228379bc100SJani Nikula static void dsi_program_swing_and_deemphasis(struct intel_encoder *encoder)
229379bc100SJani Nikula {
230379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
231b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
232dc867bc7SMatt Roper 	enum phy phy;
233379bc100SJani Nikula 	u32 tmp;
234379bc100SJani Nikula 	int lane;
235379bc100SJani Nikula 
236dc867bc7SMatt Roper 	for_each_dsi_phy(phy, intel_dsi->phys) {
237379bc100SJani Nikula 		/*
238379bc100SJani Nikula 		 * Program voltage swing and pre-emphasis level values as per
239379bc100SJani Nikula 		 * table in BSPEC under DDI buffer programing
240379bc100SJani Nikula 		 */
241e6908588SVille Syrjälä 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
242379bc100SJani Nikula 		tmp &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK);
243379bc100SJani Nikula 		tmp |= SCALING_MODE_SEL(0x2);
244379bc100SJani Nikula 		tmp |= TAP2_DISABLE | TAP3_DISABLE;
245379bc100SJani Nikula 		tmp |= RTERM_SELECT(0x6);
2461c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), tmp);
247379bc100SJani Nikula 
2481c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW5_AUX(phy));
249379bc100SJani Nikula 		tmp &= ~(SCALING_MODE_SEL_MASK | RTERM_SELECT_MASK);
250379bc100SJani Nikula 		tmp |= SCALING_MODE_SEL(0x2);
251379bc100SJani Nikula 		tmp |= TAP2_DISABLE | TAP3_DISABLE;
252379bc100SJani Nikula 		tmp |= RTERM_SELECT(0x6);
2531c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW5_AUX(phy), tmp);
254379bc100SJani Nikula 
255e6908588SVille Syrjälä 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW2_LN(0, phy));
256379bc100SJani Nikula 		tmp &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
257379bc100SJani Nikula 			 RCOMP_SCALAR_MASK);
258379bc100SJani Nikula 		tmp |= SWING_SEL_UPPER(0x2);
259379bc100SJani Nikula 		tmp |= SWING_SEL_LOWER(0x2);
260379bc100SJani Nikula 		tmp |= RCOMP_SCALAR(0x98);
2611c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW2_GRP(phy), tmp);
262379bc100SJani Nikula 
2631c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW2_AUX(phy));
264379bc100SJani Nikula 		tmp &= ~(SWING_SEL_LOWER_MASK | SWING_SEL_UPPER_MASK |
265379bc100SJani Nikula 			 RCOMP_SCALAR_MASK);
266379bc100SJani Nikula 		tmp |= SWING_SEL_UPPER(0x2);
267379bc100SJani Nikula 		tmp |= SWING_SEL_LOWER(0x2);
268379bc100SJani Nikula 		tmp |= RCOMP_SCALAR(0x98);
2691c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW2_AUX(phy), tmp);
270379bc100SJani Nikula 
2711c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW4_AUX(phy));
272379bc100SJani Nikula 		tmp &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
273379bc100SJani Nikula 			 CURSOR_COEFF_MASK);
274379bc100SJani Nikula 		tmp |= POST_CURSOR_1(0x0);
275379bc100SJani Nikula 		tmp |= POST_CURSOR_2(0x0);
276379bc100SJani Nikula 		tmp |= CURSOR_COEFF(0x3f);
2771c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_AUX(phy), tmp);
278379bc100SJani Nikula 
279379bc100SJani Nikula 		for (lane = 0; lane <= 3; lane++) {
280379bc100SJani Nikula 			/* Bspec: must not use GRP register for write */
2811c63f6dfSJani Nikula 			tmp = intel_de_read(dev_priv,
2821c63f6dfSJani Nikula 					    ICL_PORT_TX_DW4_LN(lane, phy));
283379bc100SJani Nikula 			tmp &= ~(POST_CURSOR_1_MASK | POST_CURSOR_2_MASK |
284379bc100SJani Nikula 				 CURSOR_COEFF_MASK);
285379bc100SJani Nikula 			tmp |= POST_CURSOR_1(0x0);
286379bc100SJani Nikula 			tmp |= POST_CURSOR_2(0x0);
287379bc100SJani Nikula 			tmp |= CURSOR_COEFF(0x3f);
2881c63f6dfSJani Nikula 			intel_de_write(dev_priv,
2891c63f6dfSJani Nikula 				       ICL_PORT_TX_DW4_LN(lane, phy), tmp);
290379bc100SJani Nikula 		}
291379bc100SJani Nikula 	}
292379bc100SJani Nikula }
293379bc100SJani Nikula 
294379bc100SJani Nikula static void configure_dual_link_mode(struct intel_encoder *encoder,
295379bc100SJani Nikula 				     const struct intel_crtc_state *pipe_config)
296379bc100SJani Nikula {
297379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
298b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
299379bc100SJani Nikula 	u32 dss_ctl1;
300379bc100SJani Nikula 
3011c63f6dfSJani Nikula 	dss_ctl1 = intel_de_read(dev_priv, DSS_CTL1);
302379bc100SJani Nikula 	dss_ctl1 |= SPLITTER_ENABLE;
303379bc100SJani Nikula 	dss_ctl1 &= ~OVERLAP_PIXELS_MASK;
304379bc100SJani Nikula 	dss_ctl1 |= OVERLAP_PIXELS(intel_dsi->pixel_overlap);
305379bc100SJani Nikula 
306379bc100SJani Nikula 	if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
307379bc100SJani Nikula 		const struct drm_display_mode *adjusted_mode =
3081326a92cSMaarten Lankhorst 					&pipe_config->hw.adjusted_mode;
309379bc100SJani Nikula 		u32 dss_ctl2;
310379bc100SJani Nikula 		u16 hactive = adjusted_mode->crtc_hdisplay;
311379bc100SJani Nikula 		u16 dl_buffer_depth;
312379bc100SJani Nikula 
313379bc100SJani Nikula 		dss_ctl1 &= ~DUAL_LINK_MODE_INTERLEAVE;
314379bc100SJani Nikula 		dl_buffer_depth = hactive / 2 + intel_dsi->pixel_overlap;
315379bc100SJani Nikula 
316379bc100SJani Nikula 		if (dl_buffer_depth > MAX_DL_BUFFER_TARGET_DEPTH)
317b5280cd0SWambui Karuga 			drm_err(&dev_priv->drm,
318b5280cd0SWambui Karuga 				"DL buffer depth exceed max value\n");
319379bc100SJani Nikula 
320379bc100SJani Nikula 		dss_ctl1 &= ~LEFT_DL_BUF_TARGET_DEPTH_MASK;
321379bc100SJani Nikula 		dss_ctl1 |= LEFT_DL_BUF_TARGET_DEPTH(dl_buffer_depth);
3221c63f6dfSJani Nikula 		dss_ctl2 = intel_de_read(dev_priv, DSS_CTL2);
323379bc100SJani Nikula 		dss_ctl2 &= ~RIGHT_DL_BUF_TARGET_DEPTH_MASK;
324379bc100SJani Nikula 		dss_ctl2 |= RIGHT_DL_BUF_TARGET_DEPTH(dl_buffer_depth);
3251c63f6dfSJani Nikula 		intel_de_write(dev_priv, DSS_CTL2, dss_ctl2);
326379bc100SJani Nikula 	} else {
327379bc100SJani Nikula 		/* Interleave */
328379bc100SJani Nikula 		dss_ctl1 |= DUAL_LINK_MODE_INTERLEAVE;
329379bc100SJani Nikula 	}
330379bc100SJani Nikula 
3311c63f6dfSJani Nikula 	intel_de_write(dev_priv, DSS_CTL1, dss_ctl1);
332379bc100SJani Nikula }
333379bc100SJani Nikula 
33454ed6902SJani Nikula /* aka DSI 8X clock */
33504865139SJani Nikula static int afe_clk(struct intel_encoder *encoder,
33604865139SJani Nikula 		   const struct intel_crtc_state *crtc_state)
33754ed6902SJani Nikula {
338b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
33954ed6902SJani Nikula 	int bpp;
34054ed6902SJani Nikula 
34104865139SJani Nikula 	if (crtc_state->dsc.compression_enable)
34204865139SJani Nikula 		bpp = crtc_state->dsc.compressed_bpp;
34304865139SJani Nikula 	else
34454ed6902SJani Nikula 		bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
34554ed6902SJani Nikula 
34654ed6902SJani Nikula 	return DIV_ROUND_CLOSEST(intel_dsi->pclk * bpp, intel_dsi->lane_count);
34754ed6902SJani Nikula }
34854ed6902SJani Nikula 
34904865139SJani Nikula static void gen11_dsi_program_esc_clk_div(struct intel_encoder *encoder,
35004865139SJani Nikula 					  const struct intel_crtc_state *crtc_state)
351379bc100SJani Nikula {
352379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
353b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
354379bc100SJani Nikula 	enum port port;
35554ed6902SJani Nikula 	int afe_clk_khz;
356510b2814SMika Kahola 	int theo_word_clk, act_word_clk;
357510b2814SMika Kahola 	u32 esc_clk_div_m, esc_clk_div_m_phy;
358379bc100SJani Nikula 
35904865139SJani Nikula 	afe_clk_khz = afe_clk(encoder, crtc_state);
360510b2814SMika Kahola 
361510b2814SMika Kahola 	if (IS_ALDERLAKE_S(dev_priv) || IS_ALDERLAKE_P(dev_priv)) {
362510b2814SMika Kahola 		theo_word_clk = DIV_ROUND_UP(afe_clk_khz, 8 * DSI_MAX_ESC_CLK);
363510b2814SMika Kahola 		act_word_clk = max(3, theo_word_clk + (theo_word_clk + 1) % 2);
364510b2814SMika Kahola 		esc_clk_div_m = act_word_clk * 8;
365510b2814SMika Kahola 		esc_clk_div_m_phy = (act_word_clk - 1) / 2;
366510b2814SMika Kahola 	} else {
367379bc100SJani Nikula 		esc_clk_div_m = DIV_ROUND_UP(afe_clk_khz, DSI_MAX_ESC_CLK);
368510b2814SMika Kahola 	}
369379bc100SJani Nikula 
370379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
3711c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_DSI_ESC_CLK_DIV(port),
372379bc100SJani Nikula 			       esc_clk_div_m & ICL_ESC_CLK_DIV_MASK);
3731c63f6dfSJani Nikula 		intel_de_posting_read(dev_priv, ICL_DSI_ESC_CLK_DIV(port));
374379bc100SJani Nikula 	}
375379bc100SJani Nikula 
376379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
3771c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_DPHY_ESC_CLK_DIV(port),
378379bc100SJani Nikula 			       esc_clk_div_m & ICL_ESC_CLK_DIV_MASK);
3791c63f6dfSJani Nikula 		intel_de_posting_read(dev_priv, ICL_DPHY_ESC_CLK_DIV(port));
380379bc100SJani Nikula 	}
381510b2814SMika Kahola 
382510b2814SMika Kahola 	if (IS_ALDERLAKE_S(dev_priv) || IS_ALDERLAKE_P(dev_priv)) {
383510b2814SMika Kahola 		for_each_dsi_port(port, intel_dsi->ports) {
384510b2814SMika Kahola 			intel_de_write(dev_priv, ADL_MIPIO_DW(port, 8),
385510b2814SMika Kahola 				       esc_clk_div_m_phy & TX_ESC_CLK_DIV_PHY);
386510b2814SMika Kahola 			intel_de_posting_read(dev_priv, ADL_MIPIO_DW(port, 8));
387510b2814SMika Kahola 		}
388510b2814SMika Kahola 	}
389379bc100SJani Nikula }
390379bc100SJani Nikula 
391379bc100SJani Nikula static void get_dsi_io_power_domains(struct drm_i915_private *dev_priv,
392379bc100SJani Nikula 				     struct intel_dsi *intel_dsi)
393379bc100SJani Nikula {
394379bc100SJani Nikula 	enum port port;
395379bc100SJani Nikula 
396379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
3973dbe5e11SPankaj Bharadiya 		drm_WARN_ON(&dev_priv->drm, intel_dsi->io_wakeref[port]);
398379bc100SJani Nikula 		intel_dsi->io_wakeref[port] =
399379bc100SJani Nikula 			intel_display_power_get(dev_priv,
400379bc100SJani Nikula 						port == PORT_A ?
401379bc100SJani Nikula 						POWER_DOMAIN_PORT_DDI_A_IO :
402379bc100SJani Nikula 						POWER_DOMAIN_PORT_DDI_B_IO);
403379bc100SJani Nikula 	}
404379bc100SJani Nikula }
405379bc100SJani Nikula 
406379bc100SJani Nikula static void gen11_dsi_enable_io_power(struct intel_encoder *encoder)
407379bc100SJani Nikula {
408379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
409b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
410379bc100SJani Nikula 	enum port port;
411379bc100SJani Nikula 	u32 tmp;
412379bc100SJani Nikula 
413379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
4141c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, ICL_DSI_IO_MODECTL(port));
415379bc100SJani Nikula 		tmp |= COMBO_PHY_MODE_DSI;
4161c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_DSI_IO_MODECTL(port), tmp);
417379bc100SJani Nikula 	}
418379bc100SJani Nikula 
419379bc100SJani Nikula 	get_dsi_io_power_domains(dev_priv, intel_dsi);
420379bc100SJani Nikula }
421379bc100SJani Nikula 
422379bc100SJani Nikula static void gen11_dsi_power_up_lanes(struct intel_encoder *encoder)
423379bc100SJani Nikula {
424379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
425b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
426dc867bc7SMatt Roper 	enum phy phy;
427379bc100SJani Nikula 
428dc867bc7SMatt Roper 	for_each_dsi_phy(phy, intel_dsi->phys)
429dc867bc7SMatt Roper 		intel_combo_phy_power_up_lanes(dev_priv, phy, true,
430379bc100SJani Nikula 					       intel_dsi->lane_count, false);
431379bc100SJani Nikula }
432379bc100SJani Nikula 
433379bc100SJani Nikula static void gen11_dsi_config_phy_lanes_sequence(struct intel_encoder *encoder)
434379bc100SJani Nikula {
435379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
436b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
437dc867bc7SMatt Roper 	enum phy phy;
438379bc100SJani Nikula 	u32 tmp;
439379bc100SJani Nikula 	int lane;
440379bc100SJani Nikula 
441379bc100SJani Nikula 	/* Step 4b(i) set loadgen select for transmit and aux lanes */
442dc867bc7SMatt Roper 	for_each_dsi_phy(phy, intel_dsi->phys) {
4431c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW4_AUX(phy));
444379bc100SJani Nikula 		tmp &= ~LOADGEN_SELECT;
4451c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW4_AUX(phy), tmp);
446379bc100SJani Nikula 		for (lane = 0; lane <= 3; lane++) {
4471c63f6dfSJani Nikula 			tmp = intel_de_read(dev_priv,
4481c63f6dfSJani Nikula 					    ICL_PORT_TX_DW4_LN(lane, phy));
449379bc100SJani Nikula 			tmp &= ~LOADGEN_SELECT;
450379bc100SJani Nikula 			if (lane != 2)
451379bc100SJani Nikula 				tmp |= LOADGEN_SELECT;
4521c63f6dfSJani Nikula 			intel_de_write(dev_priv,
4531c63f6dfSJani Nikula 				       ICL_PORT_TX_DW4_LN(lane, phy), tmp);
454379bc100SJani Nikula 		}
455379bc100SJani Nikula 	}
456379bc100SJani Nikula 
457379bc100SJani Nikula 	/* Step 4b(ii) set latency optimization for transmit and aux lanes */
458dc867bc7SMatt Roper 	for_each_dsi_phy(phy, intel_dsi->phys) {
4591c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW2_AUX(phy));
460379bc100SJani Nikula 		tmp &= ~FRC_LATENCY_OPTIM_MASK;
461379bc100SJani Nikula 		tmp |= FRC_LATENCY_OPTIM_VAL(0x5);
4621c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW2_AUX(phy), tmp);
463e6908588SVille Syrjälä 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW2_LN(0, phy));
464379bc100SJani Nikula 		tmp &= ~FRC_LATENCY_OPTIM_MASK;
465379bc100SJani Nikula 		tmp |= FRC_LATENCY_OPTIM_VAL(0x5);
4661c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW2_GRP(phy), tmp);
4676a7bafe8SVandita Kulkarni 
468960e9836SVandita Kulkarni 		/* For EHL, TGL, set latency optimization for PCS_DW1 lanes */
469005e9537SMatt Roper 		if (IS_JSL_EHL(dev_priv) || (DISPLAY_VER(dev_priv) >= 12)) {
4701c63f6dfSJani Nikula 			tmp = intel_de_read(dev_priv,
4711c63f6dfSJani Nikula 					    ICL_PORT_PCS_DW1_AUX(phy));
4726a7bafe8SVandita Kulkarni 			tmp &= ~LATENCY_OPTIM_MASK;
4736a7bafe8SVandita Kulkarni 			tmp |= LATENCY_OPTIM_VAL(0);
4741c63f6dfSJani Nikula 			intel_de_write(dev_priv, ICL_PORT_PCS_DW1_AUX(phy),
4751c63f6dfSJani Nikula 				       tmp);
4766a7bafe8SVandita Kulkarni 
4771c63f6dfSJani Nikula 			tmp = intel_de_read(dev_priv,
478e6908588SVille Syrjälä 					    ICL_PORT_PCS_DW1_LN(0, phy));
4796a7bafe8SVandita Kulkarni 			tmp &= ~LATENCY_OPTIM_MASK;
4806a7bafe8SVandita Kulkarni 			tmp |= LATENCY_OPTIM_VAL(0x1);
4811c63f6dfSJani Nikula 			intel_de_write(dev_priv, ICL_PORT_PCS_DW1_GRP(phy),
4821c63f6dfSJani Nikula 				       tmp);
4836a7bafe8SVandita Kulkarni 		}
484379bc100SJani Nikula 	}
485379bc100SJani Nikula 
486379bc100SJani Nikula }
487379bc100SJani Nikula 
488379bc100SJani Nikula static void gen11_dsi_voltage_swing_program_seq(struct intel_encoder *encoder)
489379bc100SJani Nikula {
490379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
491b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
492379bc100SJani Nikula 	u32 tmp;
493dc867bc7SMatt Roper 	enum phy phy;
494379bc100SJani Nikula 
495379bc100SJani Nikula 	/* clear common keeper enable bit */
496dc867bc7SMatt Roper 	for_each_dsi_phy(phy, intel_dsi->phys) {
497e6908588SVille Syrjälä 		tmp = intel_de_read(dev_priv, ICL_PORT_PCS_DW1_LN(0, phy));
498379bc100SJani Nikula 		tmp &= ~COMMON_KEEPER_EN;
4991c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_PCS_DW1_GRP(phy), tmp);
5001c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, ICL_PORT_PCS_DW1_AUX(phy));
501379bc100SJani Nikula 		tmp &= ~COMMON_KEEPER_EN;
5021c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_PCS_DW1_AUX(phy), tmp);
503379bc100SJani Nikula 	}
504379bc100SJani Nikula 
505379bc100SJani Nikula 	/*
506379bc100SJani Nikula 	 * Set SUS Clock Config bitfield to 11b
507379bc100SJani Nikula 	 * Note: loadgen select program is done
508379bc100SJani Nikula 	 * as part of lane phy sequence configuration
509379bc100SJani Nikula 	 */
510dc867bc7SMatt Roper 	for_each_dsi_phy(phy, intel_dsi->phys) {
5111c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, ICL_PORT_CL_DW5(phy));
512379bc100SJani Nikula 		tmp |= SUS_CLOCK_CONFIG;
5131c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_CL_DW5(phy), tmp);
514379bc100SJani Nikula 	}
515379bc100SJani Nikula 
516379bc100SJani Nikula 	/* Clear training enable to change swing values */
517dc867bc7SMatt Roper 	for_each_dsi_phy(phy, intel_dsi->phys) {
518e6908588SVille Syrjälä 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
519379bc100SJani Nikula 		tmp &= ~TX_TRAINING_EN;
5201c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), tmp);
5211c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW5_AUX(phy));
522379bc100SJani Nikula 		tmp &= ~TX_TRAINING_EN;
5231c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW5_AUX(phy), tmp);
524379bc100SJani Nikula 	}
525379bc100SJani Nikula 
526379bc100SJani Nikula 	/* Program swing and de-emphasis */
527379bc100SJani Nikula 	dsi_program_swing_and_deemphasis(encoder);
528379bc100SJani Nikula 
529379bc100SJani Nikula 	/* Set training enable to trigger update */
530dc867bc7SMatt Roper 	for_each_dsi_phy(phy, intel_dsi->phys) {
531e6908588SVille Syrjälä 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW5_LN(0, phy));
532379bc100SJani Nikula 		tmp |= TX_TRAINING_EN;
5331c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW5_GRP(phy), tmp);
5341c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, ICL_PORT_TX_DW5_AUX(phy));
535379bc100SJani Nikula 		tmp |= TX_TRAINING_EN;
5361c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_PORT_TX_DW5_AUX(phy), tmp);
537379bc100SJani Nikula 	}
538379bc100SJani Nikula }
539379bc100SJani Nikula 
540379bc100SJani Nikula static void gen11_dsi_enable_ddi_buffer(struct intel_encoder *encoder)
541379bc100SJani Nikula {
542379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
543b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
544379bc100SJani Nikula 	u32 tmp;
545379bc100SJani Nikula 	enum port port;
546379bc100SJani Nikula 
547379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
5481c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, DDI_BUF_CTL(port));
549379bc100SJani Nikula 		tmp |= DDI_BUF_CTL_ENABLE;
5501c63f6dfSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_CTL(port), tmp);
551379bc100SJani Nikula 
5521c63f6dfSJani Nikula 		if (wait_for_us(!(intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
553379bc100SJani Nikula 				  DDI_BUF_IS_IDLE),
554379bc100SJani Nikula 				  500))
555b5280cd0SWambui Karuga 			drm_err(&dev_priv->drm, "DDI port:%c buffer idle\n",
556b5280cd0SWambui Karuga 				port_name(port));
557379bc100SJani Nikula 	}
558379bc100SJani Nikula }
559379bc100SJani Nikula 
56004865139SJani Nikula static void
56104865139SJani Nikula gen11_dsi_setup_dphy_timings(struct intel_encoder *encoder,
56204865139SJani Nikula 			     const struct intel_crtc_state *crtc_state)
563379bc100SJani Nikula {
564379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
565b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
566379bc100SJani Nikula 	u32 tmp;
567379bc100SJani Nikula 	enum port port;
568dc867bc7SMatt Roper 	enum phy phy;
569379bc100SJani Nikula 
570379bc100SJani Nikula 	/* Program T-INIT master registers */
571379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
5721c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, ICL_DSI_T_INIT_MASTER(port));
573379bc100SJani Nikula 		tmp &= ~MASTER_INIT_TIMER_MASK;
574379bc100SJani Nikula 		tmp |= intel_dsi->init_count;
5751c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_DSI_T_INIT_MASTER(port), tmp);
576379bc100SJani Nikula 	}
577379bc100SJani Nikula 
578379bc100SJani Nikula 	/* Program DPHY clock lanes timings */
579379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
5801c63f6dfSJani Nikula 		intel_de_write(dev_priv, DPHY_CLK_TIMING_PARAM(port),
5811c63f6dfSJani Nikula 			       intel_dsi->dphy_reg);
582379bc100SJani Nikula 
583379bc100SJani Nikula 		/* shadow register inside display core */
5841c63f6dfSJani Nikula 		intel_de_write(dev_priv, DSI_CLK_TIMING_PARAM(port),
5851c63f6dfSJani Nikula 			       intel_dsi->dphy_reg);
586379bc100SJani Nikula 	}
587379bc100SJani Nikula 
588379bc100SJani Nikula 	/* Program DPHY data lanes timings */
589379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
5901c63f6dfSJani Nikula 		intel_de_write(dev_priv, DPHY_DATA_TIMING_PARAM(port),
591379bc100SJani Nikula 			       intel_dsi->dphy_data_lane_reg);
592379bc100SJani Nikula 
593379bc100SJani Nikula 		/* shadow register inside display core */
5941c63f6dfSJani Nikula 		intel_de_write(dev_priv, DSI_DATA_TIMING_PARAM(port),
595379bc100SJani Nikula 			       intel_dsi->dphy_data_lane_reg);
596379bc100SJani Nikula 	}
597379bc100SJani Nikula 
598379bc100SJani Nikula 	/*
599379bc100SJani Nikula 	 * If DSI link operating at or below an 800 MHz,
600379bc100SJani Nikula 	 * TA_SURE should be override and programmed to
601379bc100SJani Nikula 	 * a value '0' inside TA_PARAM_REGISTERS otherwise
602379bc100SJani Nikula 	 * leave all fields at HW default values.
603379bc100SJani Nikula 	 */
60493e7e61eSLucas De Marchi 	if (DISPLAY_VER(dev_priv) == 11) {
60504865139SJani Nikula 		if (afe_clk(encoder, crtc_state) <= 800000) {
606379bc100SJani Nikula 			for_each_dsi_port(port, intel_dsi->ports) {
6071c63f6dfSJani Nikula 				tmp = intel_de_read(dev_priv,
6081c63f6dfSJani Nikula 						    DPHY_TA_TIMING_PARAM(port));
609379bc100SJani Nikula 				tmp &= ~TA_SURE_MASK;
610379bc100SJani Nikula 				tmp |= TA_SURE_OVERRIDE | TA_SURE(0);
6111c63f6dfSJani Nikula 				intel_de_write(dev_priv,
6121c63f6dfSJani Nikula 					       DPHY_TA_TIMING_PARAM(port),
6131c63f6dfSJani Nikula 					       tmp);
614379bc100SJani Nikula 
615379bc100SJani Nikula 				/* shadow register inside display core */
6161c63f6dfSJani Nikula 				tmp = intel_de_read(dev_priv,
6171c63f6dfSJani Nikula 						    DSI_TA_TIMING_PARAM(port));
618379bc100SJani Nikula 				tmp &= ~TA_SURE_MASK;
619379bc100SJani Nikula 				tmp |= TA_SURE_OVERRIDE | TA_SURE(0);
6201c63f6dfSJani Nikula 				intel_de_write(dev_priv,
6211c63f6dfSJani Nikula 					       DSI_TA_TIMING_PARAM(port), tmp);
622379bc100SJani Nikula 			}
623379bc100SJani Nikula 		}
6247b864f95SVandita Kulkarni 	}
625683d672cSJosé Roberto de Souza 
62624ea098bSTejas Upadhyay 	if (IS_JSL_EHL(dev_priv)) {
627dc867bc7SMatt Roper 		for_each_dsi_phy(phy, intel_dsi->phys) {
6281c63f6dfSJani Nikula 			tmp = intel_de_read(dev_priv, ICL_DPHY_CHKN(phy));
629683d672cSJosé Roberto de Souza 			tmp |= ICL_DPHY_CHKN_AFE_OVER_PPI_STRAP;
6301c63f6dfSJani Nikula 			intel_de_write(dev_priv, ICL_DPHY_CHKN(phy), tmp);
631683d672cSJosé Roberto de Souza 		}
632683d672cSJosé Roberto de Souza 	}
633379bc100SJani Nikula }
634379bc100SJani Nikula 
635379bc100SJani Nikula static void gen11_dsi_gate_clocks(struct intel_encoder *encoder)
636379bc100SJani Nikula {
637379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
638b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
639379bc100SJani Nikula 	u32 tmp;
640befa372bSMatt Roper 	enum phy phy;
641379bc100SJani Nikula 
642353ad959SImre Deak 	mutex_lock(&dev_priv->dpll.lock);
6431c63f6dfSJani Nikula 	tmp = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
644dc867bc7SMatt Roper 	for_each_dsi_phy(phy, intel_dsi->phys)
645befa372bSMatt Roper 		tmp |= ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy);
646379bc100SJani Nikula 
6471c63f6dfSJani Nikula 	intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, tmp);
648353ad959SImre Deak 	mutex_unlock(&dev_priv->dpll.lock);
649379bc100SJani Nikula }
650379bc100SJani Nikula 
651379bc100SJani Nikula static void gen11_dsi_ungate_clocks(struct intel_encoder *encoder)
652379bc100SJani Nikula {
653379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
654b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
655379bc100SJani Nikula 	u32 tmp;
656befa372bSMatt Roper 	enum phy phy;
657379bc100SJani Nikula 
658353ad959SImre Deak 	mutex_lock(&dev_priv->dpll.lock);
6591c63f6dfSJani Nikula 	tmp = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
660dc867bc7SMatt Roper 	for_each_dsi_phy(phy, intel_dsi->phys)
661befa372bSMatt Roper 		tmp &= ~ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy);
662379bc100SJani Nikula 
6631c63f6dfSJani Nikula 	intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, tmp);
664353ad959SImre Deak 	mutex_unlock(&dev_priv->dpll.lock);
665379bc100SJani Nikula }
666379bc100SJani Nikula 
6670fbd8694SVille Syrjälä static bool gen11_dsi_is_clock_enabled(struct intel_encoder *encoder)
6680fbd8694SVille Syrjälä {
6690fbd8694SVille Syrjälä 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
6700fbd8694SVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
6710fbd8694SVille Syrjälä 	bool clock_enabled = false;
6720fbd8694SVille Syrjälä 	enum phy phy;
6730fbd8694SVille Syrjälä 	u32 tmp;
6740fbd8694SVille Syrjälä 
6750fbd8694SVille Syrjälä 	tmp = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
6760fbd8694SVille Syrjälä 
6770fbd8694SVille Syrjälä 	for_each_dsi_phy(phy, intel_dsi->phys) {
6780fbd8694SVille Syrjälä 		if (!(tmp & ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy)))
6790fbd8694SVille Syrjälä 			clock_enabled = true;
6800fbd8694SVille Syrjälä 	}
6810fbd8694SVille Syrjälä 
6820fbd8694SVille Syrjälä 	return clock_enabled;
6830fbd8694SVille Syrjälä }
6840fbd8694SVille Syrjälä 
685379bc100SJani Nikula static void gen11_dsi_map_pll(struct intel_encoder *encoder,
686379bc100SJani Nikula 			      const struct intel_crtc_state *crtc_state)
687379bc100SJani Nikula {
688379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
689b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
690379bc100SJani Nikula 	struct intel_shared_dpll *pll = crtc_state->shared_dpll;
691befa372bSMatt Roper 	enum phy phy;
692379bc100SJani Nikula 	u32 val;
693379bc100SJani Nikula 
694353ad959SImre Deak 	mutex_lock(&dev_priv->dpll.lock);
695379bc100SJani Nikula 
6961c63f6dfSJani Nikula 	val = intel_de_read(dev_priv, ICL_DPCLKA_CFGCR0);
697dc867bc7SMatt Roper 	for_each_dsi_phy(phy, intel_dsi->phys) {
698befa372bSMatt Roper 		val &= ~ICL_DPCLKA_CFGCR0_DDI_CLK_SEL_MASK(phy);
699befa372bSMatt Roper 		val |= ICL_DPCLKA_CFGCR0_DDI_CLK_SEL(pll->info->id, phy);
700379bc100SJani Nikula 	}
7011c63f6dfSJani Nikula 	intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, val);
702379bc100SJani Nikula 
703dc867bc7SMatt Roper 	for_each_dsi_phy(phy, intel_dsi->phys) {
704befa372bSMatt Roper 		val &= ~ICL_DPCLKA_CFGCR0_DDI_CLK_OFF(phy);
705379bc100SJani Nikula 	}
7061c63f6dfSJani Nikula 	intel_de_write(dev_priv, ICL_DPCLKA_CFGCR0, val);
707379bc100SJani Nikula 
7081c63f6dfSJani Nikula 	intel_de_posting_read(dev_priv, ICL_DPCLKA_CFGCR0);
709379bc100SJani Nikula 
710353ad959SImre Deak 	mutex_unlock(&dev_priv->dpll.lock);
711379bc100SJani Nikula }
712379bc100SJani Nikula 
713379bc100SJani Nikula static void
714379bc100SJani Nikula gen11_dsi_configure_transcoder(struct intel_encoder *encoder,
715379bc100SJani Nikula 			       const struct intel_crtc_state *pipe_config)
716379bc100SJani Nikula {
717379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
718b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
719f15f01a7SVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
720f15f01a7SVille Syrjälä 	enum pipe pipe = crtc->pipe;
721379bc100SJani Nikula 	u32 tmp;
722379bc100SJani Nikula 	enum port port;
723379bc100SJani Nikula 	enum transcoder dsi_trans;
724379bc100SJani Nikula 
725379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
726379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
7271c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, DSI_TRANS_FUNC_CONF(dsi_trans));
728379bc100SJani Nikula 
729379bc100SJani Nikula 		if (intel_dsi->eotp_pkt)
730379bc100SJani Nikula 			tmp &= ~EOTP_DISABLED;
731379bc100SJani Nikula 		else
732379bc100SJani Nikula 			tmp |= EOTP_DISABLED;
733379bc100SJani Nikula 
734379bc100SJani Nikula 		/* enable link calibration if freq > 1.5Gbps */
73504865139SJani Nikula 		if (afe_clk(encoder, pipe_config) >= 1500 * 1000) {
736379bc100SJani Nikula 			tmp &= ~LINK_CALIBRATION_MASK;
737379bc100SJani Nikula 			tmp |= CALIBRATION_ENABLED_INITIAL_ONLY;
738379bc100SJani Nikula 		}
739379bc100SJani Nikula 
740379bc100SJani Nikula 		/* configure continuous clock */
741379bc100SJani Nikula 		tmp &= ~CONTINUOUS_CLK_MASK;
742379bc100SJani Nikula 		if (intel_dsi->clock_stop)
743379bc100SJani Nikula 			tmp |= CLK_ENTER_LP_AFTER_DATA;
744379bc100SJani Nikula 		else
745379bc100SJani Nikula 			tmp |= CLK_HS_CONTINUOUS;
746379bc100SJani Nikula 
747379bc100SJani Nikula 		/* configure buffer threshold limit to minimum */
748379bc100SJani Nikula 		tmp &= ~PIX_BUF_THRESHOLD_MASK;
749379bc100SJani Nikula 		tmp |= PIX_BUF_THRESHOLD_1_4;
750379bc100SJani Nikula 
751379bc100SJani Nikula 		/* set virtual channel to '0' */
752379bc100SJani Nikula 		tmp &= ~PIX_VIRT_CHAN_MASK;
753379bc100SJani Nikula 		tmp |= PIX_VIRT_CHAN(0);
754379bc100SJani Nikula 
755379bc100SJani Nikula 		/* program BGR transmission */
756379bc100SJani Nikula 		if (intel_dsi->bgr_enabled)
757379bc100SJani Nikula 			tmp |= BGR_TRANSMISSION;
758379bc100SJani Nikula 
759379bc100SJani Nikula 		/* select pixel format */
760379bc100SJani Nikula 		tmp &= ~PIX_FMT_MASK;
76138b89881SJani Nikula 		if (pipe_config->dsc.compression_enable) {
76238b89881SJani Nikula 			tmp |= PIX_FMT_COMPRESSED;
76338b89881SJani Nikula 		} else {
764379bc100SJani Nikula 			switch (intel_dsi->pixel_format) {
765379bc100SJani Nikula 			default:
766379bc100SJani Nikula 				MISSING_CASE(intel_dsi->pixel_format);
767df561f66SGustavo A. R. Silva 				fallthrough;
768379bc100SJani Nikula 			case MIPI_DSI_FMT_RGB565:
769379bc100SJani Nikula 				tmp |= PIX_FMT_RGB565;
770379bc100SJani Nikula 				break;
771379bc100SJani Nikula 			case MIPI_DSI_FMT_RGB666_PACKED:
772379bc100SJani Nikula 				tmp |= PIX_FMT_RGB666_PACKED;
773379bc100SJani Nikula 				break;
774379bc100SJani Nikula 			case MIPI_DSI_FMT_RGB666:
775379bc100SJani Nikula 				tmp |= PIX_FMT_RGB666_LOOSE;
776379bc100SJani Nikula 				break;
777379bc100SJani Nikula 			case MIPI_DSI_FMT_RGB888:
778379bc100SJani Nikula 				tmp |= PIX_FMT_RGB888;
779379bc100SJani Nikula 				break;
780379bc100SJani Nikula 			}
78138b89881SJani Nikula 		}
782379bc100SJani Nikula 
783005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 12) {
78432d38e6cSVandita Kulkarni 			if (is_vid_mode(intel_dsi))
78532d38e6cSVandita Kulkarni 				tmp |= BLANKING_PACKET_ENABLE;
78632d38e6cSVandita Kulkarni 		}
78732d38e6cSVandita Kulkarni 
788379bc100SJani Nikula 		/* program DSI operation mode */
789379bc100SJani Nikula 		if (is_vid_mode(intel_dsi)) {
790379bc100SJani Nikula 			tmp &= ~OP_MODE_MASK;
791379bc100SJani Nikula 			switch (intel_dsi->video_mode_format) {
792379bc100SJani Nikula 			default:
793379bc100SJani Nikula 				MISSING_CASE(intel_dsi->video_mode_format);
794df561f66SGustavo A. R. Silva 				fallthrough;
795379bc100SJani Nikula 			case VIDEO_MODE_NON_BURST_WITH_SYNC_EVENTS:
796379bc100SJani Nikula 				tmp |= VIDEO_MODE_SYNC_EVENT;
797379bc100SJani Nikula 				break;
798379bc100SJani Nikula 			case VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE:
799379bc100SJani Nikula 				tmp |= VIDEO_MODE_SYNC_PULSE;
800379bc100SJani Nikula 				break;
801379bc100SJani Nikula 			}
802b4b95b05SVandita Kulkarni 		} else {
803b4b95b05SVandita Kulkarni 			/*
804b4b95b05SVandita Kulkarni 			 * FIXME: Retrieve this info from VBT.
805b4b95b05SVandita Kulkarni 			 * As per the spec when dsi transcoder is operating
806b4b95b05SVandita Kulkarni 			 * in TE GATE mode, TE comes from GPIO
807b4b95b05SVandita Kulkarni 			 * which is UTIL PIN for DSI 0.
808b4b95b05SVandita Kulkarni 			 * Also this GPIO would not be used for other
809b4b95b05SVandita Kulkarni 			 * purposes is an assumption.
810b4b95b05SVandita Kulkarni 			 */
811b4b95b05SVandita Kulkarni 			tmp &= ~OP_MODE_MASK;
812b4b95b05SVandita Kulkarni 			tmp |= CMD_MODE_TE_GATE;
813b4b95b05SVandita Kulkarni 			tmp |= TE_SOURCE_GPIO;
814379bc100SJani Nikula 		}
815379bc100SJani Nikula 
8161c63f6dfSJani Nikula 		intel_de_write(dev_priv, DSI_TRANS_FUNC_CONF(dsi_trans), tmp);
817379bc100SJani Nikula 	}
818379bc100SJani Nikula 
819379bc100SJani Nikula 	/* enable port sync mode if dual link */
820379bc100SJani Nikula 	if (intel_dsi->dual_link) {
821379bc100SJani Nikula 		for_each_dsi_port(port, intel_dsi->ports) {
822379bc100SJani Nikula 			dsi_trans = dsi_port_to_transcoder(port);
8231c63f6dfSJani Nikula 			tmp = intel_de_read(dev_priv,
8241c63f6dfSJani Nikula 					    TRANS_DDI_FUNC_CTL2(dsi_trans));
825379bc100SJani Nikula 			tmp |= PORT_SYNC_MODE_ENABLE;
8261c63f6dfSJani Nikula 			intel_de_write(dev_priv,
8271c63f6dfSJani Nikula 				       TRANS_DDI_FUNC_CTL2(dsi_trans), tmp);
828379bc100SJani Nikula 		}
829379bc100SJani Nikula 
830379bc100SJani Nikula 		/* configure stream splitting */
831379bc100SJani Nikula 		configure_dual_link_mode(encoder, pipe_config);
832379bc100SJani Nikula 	}
833379bc100SJani Nikula 
834379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
835379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
836379bc100SJani Nikula 
837379bc100SJani Nikula 		/* select data lane width */
8381c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(dsi_trans));
839379bc100SJani Nikula 		tmp &= ~DDI_PORT_WIDTH_MASK;
840379bc100SJani Nikula 		tmp |= DDI_PORT_WIDTH(intel_dsi->lane_count);
841379bc100SJani Nikula 
842379bc100SJani Nikula 		/* select input pipe */
843379bc100SJani Nikula 		tmp &= ~TRANS_DDI_EDP_INPUT_MASK;
844379bc100SJani Nikula 		switch (pipe) {
845379bc100SJani Nikula 		default:
846379bc100SJani Nikula 			MISSING_CASE(pipe);
847df561f66SGustavo A. R. Silva 			fallthrough;
848379bc100SJani Nikula 		case PIPE_A:
849379bc100SJani Nikula 			tmp |= TRANS_DDI_EDP_INPUT_A_ON;
850379bc100SJani Nikula 			break;
851379bc100SJani Nikula 		case PIPE_B:
852379bc100SJani Nikula 			tmp |= TRANS_DDI_EDP_INPUT_B_ONOFF;
853379bc100SJani Nikula 			break;
854379bc100SJani Nikula 		case PIPE_C:
855379bc100SJani Nikula 			tmp |= TRANS_DDI_EDP_INPUT_C_ONOFF;
856379bc100SJani Nikula 			break;
8574d89adc7SJosé Roberto de Souza 		case PIPE_D:
8584d89adc7SJosé Roberto de Souza 			tmp |= TRANS_DDI_EDP_INPUT_D_ONOFF;
8594d89adc7SJosé Roberto de Souza 			break;
860379bc100SJani Nikula 		}
861379bc100SJani Nikula 
862379bc100SJani Nikula 		/* enable DDI buffer */
863379bc100SJani Nikula 		tmp |= TRANS_DDI_FUNC_ENABLE;
8641c63f6dfSJani Nikula 		intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(dsi_trans), tmp);
865379bc100SJani Nikula 	}
866379bc100SJani Nikula 
867379bc100SJani Nikula 	/* wait for link ready */
868379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
869379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
8701c63f6dfSJani Nikula 		if (wait_for_us((intel_de_read(dev_priv, DSI_TRANS_FUNC_CONF(dsi_trans)) &
871379bc100SJani Nikula 				 LINK_READY), 2500))
872b5280cd0SWambui Karuga 			drm_err(&dev_priv->drm, "DSI link not ready\n");
873379bc100SJani Nikula 	}
874379bc100SJani Nikula }
875379bc100SJani Nikula 
876379bc100SJani Nikula static void
877379bc100SJani Nikula gen11_dsi_set_transcoder_timings(struct intel_encoder *encoder,
87853693f02SJani Nikula 				 const struct intel_crtc_state *crtc_state)
879379bc100SJani Nikula {
880379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
881b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
882379bc100SJani Nikula 	const struct drm_display_mode *adjusted_mode =
88353693f02SJani Nikula 		&crtc_state->hw.adjusted_mode;
884379bc100SJani Nikula 	enum port port;
885379bc100SJani Nikula 	enum transcoder dsi_trans;
886379bc100SJani Nikula 	/* horizontal timings */
887379bc100SJani Nikula 	u16 htotal, hactive, hsync_start, hsync_end, hsync_size;
8880cc35a9cSYueHaibing 	u16 hback_porch;
889379bc100SJani Nikula 	/* vertical timings */
890379bc100SJani Nikula 	u16 vtotal, vactive, vsync_start, vsync_end, vsync_shift;
89153693f02SJani Nikula 	int mul = 1, div = 1;
89253693f02SJani Nikula 
89353693f02SJani Nikula 	/*
89453693f02SJani Nikula 	 * Adjust horizontal timings (htotal, hsync_start, hsync_end) to account
89553693f02SJani Nikula 	 * for slower link speed if DSC is enabled.
89653693f02SJani Nikula 	 *
89753693f02SJani Nikula 	 * The compression frequency ratio is the ratio between compressed and
89853693f02SJani Nikula 	 * non-compressed link speeds, and simplifies down to the ratio between
89953693f02SJani Nikula 	 * compressed and non-compressed bpp.
90053693f02SJani Nikula 	 */
90153693f02SJani Nikula 	if (crtc_state->dsc.compression_enable) {
90253693f02SJani Nikula 		mul = crtc_state->dsc.compressed_bpp;
90353693f02SJani Nikula 		div = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
90453693f02SJani Nikula 	}
905379bc100SJani Nikula 
906379bc100SJani Nikula 	hactive = adjusted_mode->crtc_hdisplay;
907b9277832SVandita Kulkarni 
908b9277832SVandita Kulkarni 	if (is_vid_mode(intel_dsi))
90953693f02SJani Nikula 		htotal = DIV_ROUND_UP(adjusted_mode->crtc_htotal * mul, div);
910b9277832SVandita Kulkarni 	else
911b9277832SVandita Kulkarni 		htotal = DIV_ROUND_UP((hactive + 160) * mul, div);
912b9277832SVandita Kulkarni 
91353693f02SJani Nikula 	hsync_start = DIV_ROUND_UP(adjusted_mode->crtc_hsync_start * mul, div);
91453693f02SJani Nikula 	hsync_end = DIV_ROUND_UP(adjusted_mode->crtc_hsync_end * mul, div);
915379bc100SJani Nikula 	hsync_size  = hsync_end - hsync_start;
916379bc100SJani Nikula 	hback_porch = (adjusted_mode->crtc_htotal -
917379bc100SJani Nikula 		       adjusted_mode->crtc_hsync_end);
918379bc100SJani Nikula 	vactive = adjusted_mode->crtc_vdisplay;
919b9277832SVandita Kulkarni 
920b9277832SVandita Kulkarni 	if (is_vid_mode(intel_dsi)) {
921379bc100SJani Nikula 		vtotal = adjusted_mode->crtc_vtotal;
922b9277832SVandita Kulkarni 	} else {
923b9277832SVandita Kulkarni 		int bpp, line_time_us, byte_clk_period_ns;
924b9277832SVandita Kulkarni 
925b9277832SVandita Kulkarni 		if (crtc_state->dsc.compression_enable)
926b9277832SVandita Kulkarni 			bpp = crtc_state->dsc.compressed_bpp;
927b9277832SVandita Kulkarni 		else
928b9277832SVandita Kulkarni 			bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
929b9277832SVandita Kulkarni 
930b9277832SVandita Kulkarni 		byte_clk_period_ns = 1000000 / afe_clk(encoder, crtc_state);
931b9277832SVandita Kulkarni 		line_time_us = (htotal * (bpp / 8) * byte_clk_period_ns) / (1000 * intel_dsi->lane_count);
932b9277832SVandita Kulkarni 		vtotal = vactive + DIV_ROUND_UP(400, line_time_us);
933b9277832SVandita Kulkarni 	}
934379bc100SJani Nikula 	vsync_start = adjusted_mode->crtc_vsync_start;
935379bc100SJani Nikula 	vsync_end = adjusted_mode->crtc_vsync_end;
936379bc100SJani Nikula 	vsync_shift = hsync_start - htotal / 2;
937379bc100SJani Nikula 
938379bc100SJani Nikula 	if (intel_dsi->dual_link) {
939379bc100SJani Nikula 		hactive /= 2;
940379bc100SJani Nikula 		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
941379bc100SJani Nikula 			hactive += intel_dsi->pixel_overlap;
942379bc100SJani Nikula 		htotal /= 2;
943379bc100SJani Nikula 	}
944379bc100SJani Nikula 
945379bc100SJani Nikula 	/* minimum hactive as per bspec: 256 pixels */
946379bc100SJani Nikula 	if (adjusted_mode->crtc_hdisplay < 256)
947b5280cd0SWambui Karuga 		drm_err(&dev_priv->drm, "hactive is less then 256 pixels\n");
948379bc100SJani Nikula 
949379bc100SJani Nikula 	/* if RGB666 format, then hactive must be multiple of 4 pixels */
950379bc100SJani Nikula 	if (intel_dsi->pixel_format == MIPI_DSI_FMT_RGB666 && hactive % 4 != 0)
951b5280cd0SWambui Karuga 		drm_err(&dev_priv->drm,
952b5280cd0SWambui Karuga 			"hactive pixels are not multiple of 4\n");
953379bc100SJani Nikula 
954379bc100SJani Nikula 	/* program TRANS_HTOTAL register */
955379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
956379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
9571c63f6dfSJani Nikula 		intel_de_write(dev_priv, HTOTAL(dsi_trans),
958379bc100SJani Nikula 			       (hactive - 1) | ((htotal - 1) << 16));
959379bc100SJani Nikula 	}
960379bc100SJani Nikula 
961379bc100SJani Nikula 	/* TRANS_HSYNC register to be programmed only for video mode */
962b9277832SVandita Kulkarni 	if (is_vid_mode(intel_dsi)) {
963379bc100SJani Nikula 		if (intel_dsi->video_mode_format ==
964379bc100SJani Nikula 		    VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE) {
965379bc100SJani Nikula 			/* BSPEC: hsync size should be atleast 16 pixels */
966379bc100SJani Nikula 			if (hsync_size < 16)
967b5280cd0SWambui Karuga 				drm_err(&dev_priv->drm,
968b5280cd0SWambui Karuga 					"hsync size < 16 pixels\n");
969379bc100SJani Nikula 		}
970379bc100SJani Nikula 
971379bc100SJani Nikula 		if (hback_porch < 16)
972b5280cd0SWambui Karuga 			drm_err(&dev_priv->drm, "hback porch < 16 pixels\n");
973379bc100SJani Nikula 
974379bc100SJani Nikula 		if (intel_dsi->dual_link) {
975379bc100SJani Nikula 			hsync_start /= 2;
976379bc100SJani Nikula 			hsync_end /= 2;
977379bc100SJani Nikula 		}
978379bc100SJani Nikula 
979379bc100SJani Nikula 		for_each_dsi_port(port, intel_dsi->ports) {
980379bc100SJani Nikula 			dsi_trans = dsi_port_to_transcoder(port);
9811c63f6dfSJani Nikula 			intel_de_write(dev_priv, HSYNC(dsi_trans),
982379bc100SJani Nikula 				       (hsync_start - 1) | ((hsync_end - 1) << 16));
983379bc100SJani Nikula 		}
984379bc100SJani Nikula 	}
985379bc100SJani Nikula 
986379bc100SJani Nikula 	/* program TRANS_VTOTAL register */
987379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
988379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
989379bc100SJani Nikula 		/*
990379bc100SJani Nikula 		 * FIXME: Programing this by assuming progressive mode, since
991379bc100SJani Nikula 		 * non-interlaced info from VBT is not saved inside
992379bc100SJani Nikula 		 * struct drm_display_mode.
993379bc100SJani Nikula 		 * For interlace mode: program required pixel minus 2
994379bc100SJani Nikula 		 */
9951c63f6dfSJani Nikula 		intel_de_write(dev_priv, VTOTAL(dsi_trans),
996379bc100SJani Nikula 			       (vactive - 1) | ((vtotal - 1) << 16));
997379bc100SJani Nikula 	}
998379bc100SJani Nikula 
999379bc100SJani Nikula 	if (vsync_end < vsync_start || vsync_end > vtotal)
1000b5280cd0SWambui Karuga 		drm_err(&dev_priv->drm, "Invalid vsync_end value\n");
1001379bc100SJani Nikula 
1002379bc100SJani Nikula 	if (vsync_start < vactive)
1003b5280cd0SWambui Karuga 		drm_err(&dev_priv->drm, "vsync_start less than vactive\n");
1004379bc100SJani Nikula 
1005b9277832SVandita Kulkarni 	/* program TRANS_VSYNC register for video mode only */
1006b9277832SVandita Kulkarni 	if (is_vid_mode(intel_dsi)) {
1007379bc100SJani Nikula 		for_each_dsi_port(port, intel_dsi->ports) {
1008379bc100SJani Nikula 			dsi_trans = dsi_port_to_transcoder(port);
10091c63f6dfSJani Nikula 			intel_de_write(dev_priv, VSYNC(dsi_trans),
1010379bc100SJani Nikula 				       (vsync_start - 1) | ((vsync_end - 1) << 16));
1011379bc100SJani Nikula 		}
1012b9277832SVandita Kulkarni 	}
1013379bc100SJani Nikula 
1014379bc100SJani Nikula 	/*
1015b9277832SVandita Kulkarni 	 * FIXME: It has to be programmed only for video modes and interlaced
1016379bc100SJani Nikula 	 * modes. Put the check condition here once interlaced
1017379bc100SJani Nikula 	 * info available as described above.
1018379bc100SJani Nikula 	 * program TRANS_VSYNCSHIFT register
1019379bc100SJani Nikula 	 */
1020b9277832SVandita Kulkarni 	if (is_vid_mode(intel_dsi)) {
1021379bc100SJani Nikula 		for_each_dsi_port(port, intel_dsi->ports) {
1022379bc100SJani Nikula 			dsi_trans = dsi_port_to_transcoder(port);
1023b9277832SVandita Kulkarni 			intel_de_write(dev_priv, VSYNCSHIFT(dsi_trans),
1024b9277832SVandita Kulkarni 				       vsync_shift);
1025b9277832SVandita Kulkarni 		}
1026379bc100SJani Nikula 	}
10273522a33aSVandita Kulkarni 
10283522a33aSVandita Kulkarni 	/* program TRANS_VBLANK register, should be same as vtotal programmed */
1029005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 12) {
10303522a33aSVandita Kulkarni 		for_each_dsi_port(port, intel_dsi->ports) {
10313522a33aSVandita Kulkarni 			dsi_trans = dsi_port_to_transcoder(port);
10321c63f6dfSJani Nikula 			intel_de_write(dev_priv, VBLANK(dsi_trans),
10333522a33aSVandita Kulkarni 				       (vactive - 1) | ((vtotal - 1) << 16));
10343522a33aSVandita Kulkarni 		}
10353522a33aSVandita Kulkarni 	}
1036379bc100SJani Nikula }
1037379bc100SJani Nikula 
1038379bc100SJani Nikula static void gen11_dsi_enable_transcoder(struct intel_encoder *encoder)
1039379bc100SJani Nikula {
1040379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1041b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1042379bc100SJani Nikula 	enum port port;
1043379bc100SJani Nikula 	enum transcoder dsi_trans;
1044379bc100SJani Nikula 	u32 tmp;
1045379bc100SJani Nikula 
1046379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1047379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
10481c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, PIPECONF(dsi_trans));
1049379bc100SJani Nikula 		tmp |= PIPECONF_ENABLE;
10501c63f6dfSJani Nikula 		intel_de_write(dev_priv, PIPECONF(dsi_trans), tmp);
1051379bc100SJani Nikula 
1052379bc100SJani Nikula 		/* wait for transcoder to be enabled */
10534cb3b44dSDaniele Ceraolo Spurio 		if (intel_de_wait_for_set(dev_priv, PIPECONF(dsi_trans),
1054*6a6d914dSVille Syrjälä 					  PIPECONF_STATE_ENABLE, 10))
1055b5280cd0SWambui Karuga 			drm_err(&dev_priv->drm,
1056b5280cd0SWambui Karuga 				"DSI transcoder not enabled\n");
1057379bc100SJani Nikula 	}
1058379bc100SJani Nikula }
1059379bc100SJani Nikula 
106004865139SJani Nikula static void gen11_dsi_setup_timeouts(struct intel_encoder *encoder,
106104865139SJani Nikula 				     const struct intel_crtc_state *crtc_state)
1062379bc100SJani Nikula {
1063379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1064b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1065379bc100SJani Nikula 	enum port port;
1066379bc100SJani Nikula 	enum transcoder dsi_trans;
1067379bc100SJani Nikula 	u32 tmp, hs_tx_timeout, lp_rx_timeout, ta_timeout, divisor, mul;
1068379bc100SJani Nikula 
1069379bc100SJani Nikula 	/*
1070379bc100SJani Nikula 	 * escape clock count calculation:
1071379bc100SJani Nikula 	 * BYTE_CLK_COUNT = TIME_NS/(8 * UI)
1072379bc100SJani Nikula 	 * UI (nsec) = (10^6)/Bitrate
1073379bc100SJani Nikula 	 * TIME_NS = (BYTE_CLK_COUNT * 8 * 10^6)/ Bitrate
1074379bc100SJani Nikula 	 * ESCAPE_CLK_COUNT  = TIME_NS/ESC_CLK_NS
1075379bc100SJani Nikula 	 */
107604865139SJani Nikula 	divisor = intel_dsi_tlpx_ns(intel_dsi) * afe_clk(encoder, crtc_state) * 1000;
1077379bc100SJani Nikula 	mul = 8 * 1000000;
1078379bc100SJani Nikula 	hs_tx_timeout = DIV_ROUND_UP(intel_dsi->hs_tx_timeout * mul,
1079379bc100SJani Nikula 				     divisor);
1080379bc100SJani Nikula 	lp_rx_timeout = DIV_ROUND_UP(intel_dsi->lp_rx_timeout * mul, divisor);
1081379bc100SJani Nikula 	ta_timeout = DIV_ROUND_UP(intel_dsi->turn_arnd_val * mul, divisor);
1082379bc100SJani Nikula 
1083379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1084379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
1085379bc100SJani Nikula 
1086379bc100SJani Nikula 		/* program hst_tx_timeout */
10871c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, DSI_HSTX_TO(dsi_trans));
1088379bc100SJani Nikula 		tmp &= ~HSTX_TIMEOUT_VALUE_MASK;
1089379bc100SJani Nikula 		tmp |= HSTX_TIMEOUT_VALUE(hs_tx_timeout);
10901c63f6dfSJani Nikula 		intel_de_write(dev_priv, DSI_HSTX_TO(dsi_trans), tmp);
1091379bc100SJani Nikula 
1092379bc100SJani Nikula 		/* FIXME: DSI_CALIB_TO */
1093379bc100SJani Nikula 
1094379bc100SJani Nikula 		/* program lp_rx_host timeout */
10951c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, DSI_LPRX_HOST_TO(dsi_trans));
1096379bc100SJani Nikula 		tmp &= ~LPRX_TIMEOUT_VALUE_MASK;
1097379bc100SJani Nikula 		tmp |= LPRX_TIMEOUT_VALUE(lp_rx_timeout);
10981c63f6dfSJani Nikula 		intel_de_write(dev_priv, DSI_LPRX_HOST_TO(dsi_trans), tmp);
1099379bc100SJani Nikula 
1100379bc100SJani Nikula 		/* FIXME: DSI_PWAIT_TO */
1101379bc100SJani Nikula 
1102379bc100SJani Nikula 		/* program turn around timeout */
11031c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, DSI_TA_TO(dsi_trans));
1104379bc100SJani Nikula 		tmp &= ~TA_TIMEOUT_VALUE_MASK;
1105379bc100SJani Nikula 		tmp |= TA_TIMEOUT_VALUE(ta_timeout);
11061c63f6dfSJani Nikula 		intel_de_write(dev_priv, DSI_TA_TO(dsi_trans), tmp);
1107379bc100SJani Nikula 	}
1108379bc100SJani Nikula }
1109379bc100SJani Nikula 
1110b4b95b05SVandita Kulkarni static void gen11_dsi_config_util_pin(struct intel_encoder *encoder,
1111b4b95b05SVandita Kulkarni 				      bool enable)
1112b4b95b05SVandita Kulkarni {
1113b4b95b05SVandita Kulkarni 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1114b4b95b05SVandita Kulkarni 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1115b4b95b05SVandita Kulkarni 	u32 tmp;
1116b4b95b05SVandita Kulkarni 
1117b4b95b05SVandita Kulkarni 	/*
1118b4b95b05SVandita Kulkarni 	 * used as TE i/p for DSI0,
1119b4b95b05SVandita Kulkarni 	 * for dual link/DSI1 TE is from slave DSI1
1120b4b95b05SVandita Kulkarni 	 * through GPIO.
1121b4b95b05SVandita Kulkarni 	 */
1122b4b95b05SVandita Kulkarni 	if (is_vid_mode(intel_dsi) || (intel_dsi->ports & BIT(PORT_B)))
1123b4b95b05SVandita Kulkarni 		return;
1124b4b95b05SVandita Kulkarni 
1125b4b95b05SVandita Kulkarni 	tmp = intel_de_read(dev_priv, UTIL_PIN_CTL);
1126b4b95b05SVandita Kulkarni 
1127b4b95b05SVandita Kulkarni 	if (enable) {
1128b4b95b05SVandita Kulkarni 		tmp |= UTIL_PIN_DIRECTION_INPUT;
1129b4b95b05SVandita Kulkarni 		tmp |= UTIL_PIN_ENABLE;
1130b4b95b05SVandita Kulkarni 	} else {
1131b4b95b05SVandita Kulkarni 		tmp &= ~UTIL_PIN_ENABLE;
1132b4b95b05SVandita Kulkarni 	}
1133b4b95b05SVandita Kulkarni 	intel_de_write(dev_priv, UTIL_PIN_CTL, tmp);
1134b4b95b05SVandita Kulkarni }
1135b4b95b05SVandita Kulkarni 
1136379bc100SJani Nikula static void
1137379bc100SJani Nikula gen11_dsi_enable_port_and_phy(struct intel_encoder *encoder,
113804865139SJani Nikula 			      const struct intel_crtc_state *crtc_state)
1139379bc100SJani Nikula {
1140379bc100SJani Nikula 	/* step 4a: power up all lanes of the DDI used by DSI */
1141379bc100SJani Nikula 	gen11_dsi_power_up_lanes(encoder);
1142379bc100SJani Nikula 
1143379bc100SJani Nikula 	/* step 4b: configure lane sequencing of the Combo-PHY transmitters */
1144379bc100SJani Nikula 	gen11_dsi_config_phy_lanes_sequence(encoder);
1145379bc100SJani Nikula 
1146379bc100SJani Nikula 	/* step 4c: configure voltage swing and skew */
1147379bc100SJani Nikula 	gen11_dsi_voltage_swing_program_seq(encoder);
1148379bc100SJani Nikula 
1149379bc100SJani Nikula 	/* enable DDI buffer */
1150379bc100SJani Nikula 	gen11_dsi_enable_ddi_buffer(encoder);
1151379bc100SJani Nikula 
1152379bc100SJani Nikula 	/* setup D-PHY timings */
115304865139SJani Nikula 	gen11_dsi_setup_dphy_timings(encoder, crtc_state);
1154379bc100SJani Nikula 
1155b4b95b05SVandita Kulkarni 	/* Since transcoder is configured to take events from GPIO */
1156b4b95b05SVandita Kulkarni 	gen11_dsi_config_util_pin(encoder, true);
1157b4b95b05SVandita Kulkarni 
1158379bc100SJani Nikula 	/* step 4h: setup DSI protocol timeouts */
115904865139SJani Nikula 	gen11_dsi_setup_timeouts(encoder, crtc_state);
1160379bc100SJani Nikula 
1161379bc100SJani Nikula 	/* Step (4h, 4i, 4j, 4k): Configure transcoder */
116204865139SJani Nikula 	gen11_dsi_configure_transcoder(encoder, crtc_state);
1163379bc100SJani Nikula 
1164379bc100SJani Nikula 	/* Step 4l: Gate DDI clocks */
1165379bc100SJani Nikula 	gen11_dsi_gate_clocks(encoder);
1166379bc100SJani Nikula }
1167379bc100SJani Nikula 
1168379bc100SJani Nikula static void gen11_dsi_powerup_panel(struct intel_encoder *encoder)
1169379bc100SJani Nikula {
1170379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1171b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1172379bc100SJani Nikula 	struct mipi_dsi_device *dsi;
1173379bc100SJani Nikula 	enum port port;
1174379bc100SJani Nikula 	enum transcoder dsi_trans;
1175379bc100SJani Nikula 	u32 tmp;
1176379bc100SJani Nikula 	int ret;
1177379bc100SJani Nikula 
1178379bc100SJani Nikula 	/* set maximum return packet size */
1179379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1180379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
1181379bc100SJani Nikula 
1182379bc100SJani Nikula 		/*
1183379bc100SJani Nikula 		 * FIXME: This uses the number of DW's currently in the payload
1184379bc100SJani Nikula 		 * receive queue. This is probably not what we want here.
1185379bc100SJani Nikula 		 */
11861c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, DSI_CMD_RXCTL(dsi_trans));
1187379bc100SJani Nikula 		tmp &= NUMBER_RX_PLOAD_DW_MASK;
1188379bc100SJani Nikula 		/* multiply "Number Rx Payload DW" by 4 to get max value */
1189379bc100SJani Nikula 		tmp = tmp * 4;
1190379bc100SJani Nikula 		dsi = intel_dsi->dsi_hosts[port]->device;
1191379bc100SJani Nikula 		ret = mipi_dsi_set_maximum_return_packet_size(dsi, tmp);
1192379bc100SJani Nikula 		if (ret < 0)
1193b5280cd0SWambui Karuga 			drm_err(&dev_priv->drm,
1194b5280cd0SWambui Karuga 				"error setting max return pkt size%d\n", tmp);
1195379bc100SJani Nikula 	}
1196379bc100SJani Nikula 
1197379bc100SJani Nikula 	/* panel power on related mipi dsi vbt sequences */
1198379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_ON);
1199379bc100SJani Nikula 	intel_dsi_msleep(intel_dsi, intel_dsi->panel_on_delay);
1200379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DEASSERT_RESET);
1201379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_INIT_OTP);
1202379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
1203379bc100SJani Nikula 
1204379bc100SJani Nikula 	/* ensure all panel commands dispatched before enabling transcoder */
1205379bc100SJani Nikula 	wait_for_cmds_dispatched_to_panel(encoder);
1206379bc100SJani Nikula }
1207379bc100SJani Nikula 
1208ede9771dSVille Syrjälä static void gen11_dsi_pre_pll_enable(struct intel_atomic_state *state,
1209ede9771dSVille Syrjälä 				     struct intel_encoder *encoder,
121004865139SJani Nikula 				     const struct intel_crtc_state *crtc_state,
1211379bc100SJani Nikula 				     const struct drm_connector_state *conn_state)
1212379bc100SJani Nikula {
1213379bc100SJani Nikula 	/* step2: enable IO power */
1214379bc100SJani Nikula 	gen11_dsi_enable_io_power(encoder);
1215379bc100SJani Nikula 
1216379bc100SJani Nikula 	/* step3: enable DSI PLL */
121704865139SJani Nikula 	gen11_dsi_program_esc_clk_div(encoder, crtc_state);
1218379bc100SJani Nikula }
1219379bc100SJani Nikula 
1220ede9771dSVille Syrjälä static void gen11_dsi_pre_enable(struct intel_atomic_state *state,
1221ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
1222379bc100SJani Nikula 				 const struct intel_crtc_state *pipe_config,
1223379bc100SJani Nikula 				 const struct drm_connector_state *conn_state)
1224379bc100SJani Nikula {
1225379bc100SJani Nikula 	/* step3b */
1226379bc100SJani Nikula 	gen11_dsi_map_pll(encoder, pipe_config);
1227379bc100SJani Nikula 
1228379bc100SJani Nikula 	/* step4: enable DSI port and DPHY */
1229379bc100SJani Nikula 	gen11_dsi_enable_port_and_phy(encoder, pipe_config);
1230379bc100SJani Nikula 
1231379bc100SJani Nikula 	/* step5: program and powerup panel */
1232379bc100SJani Nikula 	gen11_dsi_powerup_panel(encoder);
1233379bc100SJani Nikula 
12343126977dSVille Syrjälä 	intel_dsc_dsi_pps_write(encoder, pipe_config);
12353126977dSVille Syrjälä 
1236379bc100SJani Nikula 	/* step6c: configure transcoder timings */
1237379bc100SJani Nikula 	gen11_dsi_set_transcoder_timings(encoder, pipe_config);
1238379bc100SJani Nikula }
1239379bc100SJani Nikula 
1240544021e3STejas Upadhyay /*
1241544021e3STejas Upadhyay  * Wa_1409054076:icl,jsl,ehl
1242544021e3STejas Upadhyay  * When pipe A is disabled and MIPI DSI is enabled on pipe B,
1243544021e3STejas Upadhyay  * the AMT KVMR feature will incorrectly see pipe A as enabled.
1244544021e3STejas Upadhyay  * Set 0x42080 bit 23=1 before enabling DSI on pipe B and leave
1245544021e3STejas Upadhyay  * it set while DSI is enabled on pipe B
1246544021e3STejas Upadhyay  */
1247544021e3STejas Upadhyay static void icl_apply_kvmr_pipe_a_wa(struct intel_encoder *encoder,
1248544021e3STejas Upadhyay 				     enum pipe pipe, bool enable)
1249544021e3STejas Upadhyay {
1250544021e3STejas Upadhyay 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1251544021e3STejas Upadhyay 
1252544021e3STejas Upadhyay 	if (DISPLAY_VER(dev_priv) == 11 && pipe == PIPE_B)
1253544021e3STejas Upadhyay 		intel_de_rmw(dev_priv, CHICKEN_PAR1_1,
1254544021e3STejas Upadhyay 			     IGNORE_KVMR_PIPE_A,
1255544021e3STejas Upadhyay 			     enable ? IGNORE_KVMR_PIPE_A : 0);
1256544021e3STejas Upadhyay }
1257f87c46c4SVandita Kulkarni 
1258f87c46c4SVandita Kulkarni /*
1259f87c46c4SVandita Kulkarni  * Wa_16012360555:adl-p
1260f87c46c4SVandita Kulkarni  * SW will have to program the "LP to HS Wakeup Guardband"
1261f87c46c4SVandita Kulkarni  * to account for the repeaters on the HS Request/Ready
1262f87c46c4SVandita Kulkarni  * PPI signaling between the Display engine and the DPHY.
1263f87c46c4SVandita Kulkarni  */
1264f87c46c4SVandita Kulkarni static void adlp_set_lp_hs_wakeup_gb(struct intel_encoder *encoder)
1265f87c46c4SVandita Kulkarni {
1266f87c46c4SVandita Kulkarni 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1267f87c46c4SVandita Kulkarni 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1268f87c46c4SVandita Kulkarni 	enum port port;
1269f87c46c4SVandita Kulkarni 
1270f87c46c4SVandita Kulkarni 	if (DISPLAY_VER(i915) == 13) {
1271f87c46c4SVandita Kulkarni 		for_each_dsi_port(port, intel_dsi->ports)
1272f87c46c4SVandita Kulkarni 			intel_de_rmw(i915, TGL_DSI_CHKN_REG(port),
12736f07707fSVandita Kulkarni 				     TGL_DSI_CHKN_LSHS_GB_MASK,
12746f07707fSVandita Kulkarni 				     TGL_DSI_CHKN_LSHS_GB(4));
1275f87c46c4SVandita Kulkarni 	}
1276f87c46c4SVandita Kulkarni }
1277f87c46c4SVandita Kulkarni 
1278ede9771dSVille Syrjälä static void gen11_dsi_enable(struct intel_atomic_state *state,
1279ede9771dSVille Syrjälä 			     struct intel_encoder *encoder,
128021fd23acSJani Nikula 			     const struct intel_crtc_state *crtc_state,
128121fd23acSJani Nikula 			     const struct drm_connector_state *conn_state)
128221fd23acSJani Nikula {
128387e9bb49SVandita Kulkarni 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1284544021e3STejas Upadhyay 	struct intel_crtc *crtc = to_intel_crtc(conn_state->crtc);
128587e9bb49SVandita Kulkarni 
128661198fe1SPankaj Bharadiya 	drm_WARN_ON(state->base.dev, crtc_state->has_pch_encoder);
128721fd23acSJani Nikula 
1288544021e3STejas Upadhyay 	/* Wa_1409054076:icl,jsl,ehl */
1289544021e3STejas Upadhyay 	icl_apply_kvmr_pipe_a_wa(encoder, crtc->pipe, true);
1290544021e3STejas Upadhyay 
1291f87c46c4SVandita Kulkarni 	/* Wa_16012360555:adl-p */
1292f87c46c4SVandita Kulkarni 	adlp_set_lp_hs_wakeup_gb(encoder);
1293f87c46c4SVandita Kulkarni 
129487e9bb49SVandita Kulkarni 	/* step6d: enable dsi transcoder */
129587e9bb49SVandita Kulkarni 	gen11_dsi_enable_transcoder(encoder);
129687e9bb49SVandita Kulkarni 
129787e9bb49SVandita Kulkarni 	/* step7: enable backlight */
1298c0a52f8bSJani Nikula 	intel_backlight_enable(crtc_state, conn_state);
129987e9bb49SVandita Kulkarni 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_ON);
130087e9bb49SVandita Kulkarni 
130121fd23acSJani Nikula 	intel_crtc_vblank_on(crtc_state);
130221fd23acSJani Nikula }
130321fd23acSJani Nikula 
1304379bc100SJani Nikula static void gen11_dsi_disable_transcoder(struct intel_encoder *encoder)
1305379bc100SJani Nikula {
1306379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1307b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1308379bc100SJani Nikula 	enum port port;
1309379bc100SJani Nikula 	enum transcoder dsi_trans;
1310379bc100SJani Nikula 	u32 tmp;
1311379bc100SJani Nikula 
1312379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1313379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
1314379bc100SJani Nikula 
1315379bc100SJani Nikula 		/* disable transcoder */
13161c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, PIPECONF(dsi_trans));
1317379bc100SJani Nikula 		tmp &= ~PIPECONF_ENABLE;
13181c63f6dfSJani Nikula 		intel_de_write(dev_priv, PIPECONF(dsi_trans), tmp);
1319379bc100SJani Nikula 
1320379bc100SJani Nikula 		/* wait for transcoder to be disabled */
13214cb3b44dSDaniele Ceraolo Spurio 		if (intel_de_wait_for_clear(dev_priv, PIPECONF(dsi_trans),
1322*6a6d914dSVille Syrjälä 					    PIPECONF_STATE_ENABLE, 50))
1323b5280cd0SWambui Karuga 			drm_err(&dev_priv->drm,
1324b5280cd0SWambui Karuga 				"DSI trancoder not disabled\n");
1325379bc100SJani Nikula 	}
1326379bc100SJani Nikula }
1327379bc100SJani Nikula 
1328379bc100SJani Nikula static void gen11_dsi_powerdown_panel(struct intel_encoder *encoder)
1329379bc100SJani Nikula {
1330b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1331379bc100SJani Nikula 
1332379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_OFF);
1333379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_ASSERT_RESET);
1334379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_OFF);
1335379bc100SJani Nikula 
1336379bc100SJani Nikula 	/* ensure cmds dispatched to panel */
1337379bc100SJani Nikula 	wait_for_cmds_dispatched_to_panel(encoder);
1338379bc100SJani Nikula }
1339379bc100SJani Nikula 
1340379bc100SJani Nikula static void gen11_dsi_deconfigure_trancoder(struct intel_encoder *encoder)
1341379bc100SJani Nikula {
1342379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1343b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1344379bc100SJani Nikula 	enum port port;
1345379bc100SJani Nikula 	enum transcoder dsi_trans;
1346379bc100SJani Nikula 	u32 tmp;
1347379bc100SJani Nikula 
1348b4b95b05SVandita Kulkarni 	/* disable periodic update mode */
1349b4b95b05SVandita Kulkarni 	if (is_cmd_mode(intel_dsi)) {
1350b4b95b05SVandita Kulkarni 		for_each_dsi_port(port, intel_dsi->ports) {
1351b4b95b05SVandita Kulkarni 			tmp = intel_de_read(dev_priv, DSI_CMD_FRMCTL(port));
1352b4b95b05SVandita Kulkarni 			tmp &= ~DSI_PERIODIC_FRAME_UPDATE_ENABLE;
1353b4b95b05SVandita Kulkarni 			intel_de_write(dev_priv, DSI_CMD_FRMCTL(port), tmp);
1354b4b95b05SVandita Kulkarni 		}
1355b4b95b05SVandita Kulkarni 	}
1356b4b95b05SVandita Kulkarni 
1357379bc100SJani Nikula 	/* put dsi link in ULPS */
1358379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1359379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
13601c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, DSI_LP_MSG(dsi_trans));
1361379bc100SJani Nikula 		tmp |= LINK_ENTER_ULPS;
1362379bc100SJani Nikula 		tmp &= ~LINK_ULPS_TYPE_LP11;
13631c63f6dfSJani Nikula 		intel_de_write(dev_priv, DSI_LP_MSG(dsi_trans), tmp);
1364379bc100SJani Nikula 
13651c63f6dfSJani Nikula 		if (wait_for_us((intel_de_read(dev_priv, DSI_LP_MSG(dsi_trans)) &
1366379bc100SJani Nikula 				 LINK_IN_ULPS),
1367379bc100SJani Nikula 				10))
1368b5280cd0SWambui Karuga 			drm_err(&dev_priv->drm, "DSI link not in ULPS\n");
1369379bc100SJani Nikula 	}
1370379bc100SJani Nikula 
1371379bc100SJani Nikula 	/* disable ddi function */
1372379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1373379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
13741c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(dsi_trans));
1375379bc100SJani Nikula 		tmp &= ~TRANS_DDI_FUNC_ENABLE;
13761c63f6dfSJani Nikula 		intel_de_write(dev_priv, TRANS_DDI_FUNC_CTL(dsi_trans), tmp);
1377379bc100SJani Nikula 	}
1378379bc100SJani Nikula 
1379379bc100SJani Nikula 	/* disable port sync mode if dual link */
1380379bc100SJani Nikula 	if (intel_dsi->dual_link) {
1381379bc100SJani Nikula 		for_each_dsi_port(port, intel_dsi->ports) {
1382379bc100SJani Nikula 			dsi_trans = dsi_port_to_transcoder(port);
13831c63f6dfSJani Nikula 			tmp = intel_de_read(dev_priv,
13841c63f6dfSJani Nikula 					    TRANS_DDI_FUNC_CTL2(dsi_trans));
1385379bc100SJani Nikula 			tmp &= ~PORT_SYNC_MODE_ENABLE;
13861c63f6dfSJani Nikula 			intel_de_write(dev_priv,
13871c63f6dfSJani Nikula 				       TRANS_DDI_FUNC_CTL2(dsi_trans), tmp);
1388379bc100SJani Nikula 		}
1389379bc100SJani Nikula 	}
1390379bc100SJani Nikula }
1391379bc100SJani Nikula 
1392379bc100SJani Nikula static void gen11_dsi_disable_port(struct intel_encoder *encoder)
1393379bc100SJani Nikula {
1394379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1395b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1396379bc100SJani Nikula 	u32 tmp;
1397379bc100SJani Nikula 	enum port port;
1398379bc100SJani Nikula 
1399379bc100SJani Nikula 	gen11_dsi_ungate_clocks(encoder);
1400379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
14011c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, DDI_BUF_CTL(port));
1402379bc100SJani Nikula 		tmp &= ~DDI_BUF_CTL_ENABLE;
14031c63f6dfSJani Nikula 		intel_de_write(dev_priv, DDI_BUF_CTL(port), tmp);
1404379bc100SJani Nikula 
14051c63f6dfSJani Nikula 		if (wait_for_us((intel_de_read(dev_priv, DDI_BUF_CTL(port)) &
1406379bc100SJani Nikula 				 DDI_BUF_IS_IDLE),
1407379bc100SJani Nikula 				 8))
1408b5280cd0SWambui Karuga 			drm_err(&dev_priv->drm,
1409b5280cd0SWambui Karuga 				"DDI port:%c buffer not idle\n",
1410379bc100SJani Nikula 				port_name(port));
1411379bc100SJani Nikula 	}
1412379bc100SJani Nikula 	gen11_dsi_gate_clocks(encoder);
1413379bc100SJani Nikula }
1414379bc100SJani Nikula 
1415379bc100SJani Nikula static void gen11_dsi_disable_io_power(struct intel_encoder *encoder)
1416379bc100SJani Nikula {
1417379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1418b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1419379bc100SJani Nikula 	enum port port;
1420379bc100SJani Nikula 	u32 tmp;
1421379bc100SJani Nikula 
1422379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1423379bc100SJani Nikula 		intel_wakeref_t wakeref;
1424379bc100SJani Nikula 
1425379bc100SJani Nikula 		wakeref = fetch_and_zero(&intel_dsi->io_wakeref[port]);
1426379bc100SJani Nikula 		intel_display_power_put(dev_priv,
1427379bc100SJani Nikula 					port == PORT_A ?
1428379bc100SJani Nikula 					POWER_DOMAIN_PORT_DDI_A_IO :
1429379bc100SJani Nikula 					POWER_DOMAIN_PORT_DDI_B_IO,
1430379bc100SJani Nikula 					wakeref);
1431379bc100SJani Nikula 	}
1432379bc100SJani Nikula 
1433379bc100SJani Nikula 	/* set mode to DDI */
1434379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
14351c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, ICL_DSI_IO_MODECTL(port));
1436379bc100SJani Nikula 		tmp &= ~COMBO_PHY_MODE_DSI;
14371c63f6dfSJani Nikula 		intel_de_write(dev_priv, ICL_DSI_IO_MODECTL(port), tmp);
1438379bc100SJani Nikula 	}
1439379bc100SJani Nikula }
1440379bc100SJani Nikula 
1441ede9771dSVille Syrjälä static void gen11_dsi_disable(struct intel_atomic_state *state,
1442ede9771dSVille Syrjälä 			      struct intel_encoder *encoder,
1443379bc100SJani Nikula 			      const struct intel_crtc_state *old_crtc_state,
1444379bc100SJani Nikula 			      const struct drm_connector_state *old_conn_state)
1445379bc100SJani Nikula {
1446b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1447544021e3STejas Upadhyay 	struct intel_crtc *crtc = to_intel_crtc(old_conn_state->crtc);
1448379bc100SJani Nikula 
1449379bc100SJani Nikula 	/* step1: turn off backlight */
1450379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_OFF);
1451c0a52f8bSJani Nikula 	intel_backlight_disable(old_conn_state);
1452379bc100SJani Nikula 
1453379bc100SJani Nikula 	/* step2d,e: disable transcoder and wait */
1454379bc100SJani Nikula 	gen11_dsi_disable_transcoder(encoder);
1455379bc100SJani Nikula 
1456544021e3STejas Upadhyay 	/* Wa_1409054076:icl,jsl,ehl */
1457544021e3STejas Upadhyay 	icl_apply_kvmr_pipe_a_wa(encoder, crtc->pipe, false);
1458544021e3STejas Upadhyay 
1459379bc100SJani Nikula 	/* step2f,g: powerdown panel */
1460379bc100SJani Nikula 	gen11_dsi_powerdown_panel(encoder);
1461379bc100SJani Nikula 
1462379bc100SJani Nikula 	/* step2h,i,j: deconfig trancoder */
1463379bc100SJani Nikula 	gen11_dsi_deconfigure_trancoder(encoder);
1464379bc100SJani Nikula 
1465379bc100SJani Nikula 	/* step3: disable port */
1466379bc100SJani Nikula 	gen11_dsi_disable_port(encoder);
1467379bc100SJani Nikula 
1468b4b95b05SVandita Kulkarni 	gen11_dsi_config_util_pin(encoder, false);
1469b4b95b05SVandita Kulkarni 
1470379bc100SJani Nikula 	/* step4: disable IO power */
1471379bc100SJani Nikula 	gen11_dsi_disable_io_power(encoder);
1472379bc100SJani Nikula }
1473379bc100SJani Nikula 
1474ede9771dSVille Syrjälä static void gen11_dsi_post_disable(struct intel_atomic_state *state,
1475ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
1476773b4b54SVille Syrjälä 				   const struct intel_crtc_state *old_crtc_state,
1477773b4b54SVille Syrjälä 				   const struct drm_connector_state *old_conn_state)
1478773b4b54SVille Syrjälä {
1479773b4b54SVille Syrjälä 	intel_crtc_vblank_off(old_crtc_state);
1480773b4b54SVille Syrjälä 
1481773b4b54SVille Syrjälä 	intel_dsc_disable(old_crtc_state);
1482773b4b54SVille Syrjälä 
1483f6df4d46SLucas De Marchi 	skl_scaler_disable(old_crtc_state);
1484773b4b54SVille Syrjälä }
1485773b4b54SVille Syrjälä 
14862b68392eSJani Nikula static enum drm_mode_status gen11_dsi_mode_valid(struct drm_connector *connector,
14872b68392eSJani Nikula 						 struct drm_display_mode *mode)
14882b68392eSJani Nikula {
14892b68392eSJani Nikula 	/* FIXME: DSC? */
14902b68392eSJani Nikula 	return intel_dsi_mode_valid(connector, mode);
14912b68392eSJani Nikula }
14922b68392eSJani Nikula 
1493379bc100SJani Nikula static void gen11_dsi_get_timings(struct intel_encoder *encoder,
1494379bc100SJani Nikula 				  struct intel_crtc_state *pipe_config)
1495379bc100SJani Nikula {
1496b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1497379bc100SJani Nikula 	struct drm_display_mode *adjusted_mode =
14981326a92cSMaarten Lankhorst 					&pipe_config->hw.adjusted_mode;
1499379bc100SJani Nikula 
1500c2bb35e9SVandita Kulkarni 	if (pipe_config->dsc.compressed_bpp) {
1501c2bb35e9SVandita Kulkarni 		int div = pipe_config->dsc.compressed_bpp;
1502c2bb35e9SVandita Kulkarni 		int mul = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
1503c2bb35e9SVandita Kulkarni 
1504c2bb35e9SVandita Kulkarni 		adjusted_mode->crtc_htotal =
1505c2bb35e9SVandita Kulkarni 			DIV_ROUND_UP(adjusted_mode->crtc_htotal * mul, div);
1506c2bb35e9SVandita Kulkarni 		adjusted_mode->crtc_hsync_start =
1507c2bb35e9SVandita Kulkarni 			DIV_ROUND_UP(adjusted_mode->crtc_hsync_start * mul, div);
1508c2bb35e9SVandita Kulkarni 		adjusted_mode->crtc_hsync_end =
1509c2bb35e9SVandita Kulkarni 			DIV_ROUND_UP(adjusted_mode->crtc_hsync_end * mul, div);
1510c2bb35e9SVandita Kulkarni 	}
1511c2bb35e9SVandita Kulkarni 
1512379bc100SJani Nikula 	if (intel_dsi->dual_link) {
1513379bc100SJani Nikula 		adjusted_mode->crtc_hdisplay *= 2;
1514379bc100SJani Nikula 		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
1515379bc100SJani Nikula 			adjusted_mode->crtc_hdisplay -=
1516379bc100SJani Nikula 						intel_dsi->pixel_overlap;
1517379bc100SJani Nikula 		adjusted_mode->crtc_htotal *= 2;
1518379bc100SJani Nikula 	}
1519379bc100SJani Nikula 	adjusted_mode->crtc_hblank_start = adjusted_mode->crtc_hdisplay;
1520379bc100SJani Nikula 	adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_htotal;
1521379bc100SJani Nikula 
1522379bc100SJani Nikula 	if (intel_dsi->operation_mode == INTEL_DSI_VIDEO_MODE) {
1523379bc100SJani Nikula 		if (intel_dsi->dual_link) {
1524379bc100SJani Nikula 			adjusted_mode->crtc_hsync_start *= 2;
1525379bc100SJani Nikula 			adjusted_mode->crtc_hsync_end *= 2;
1526379bc100SJani Nikula 		}
1527379bc100SJani Nikula 	}
1528379bc100SJani Nikula 	adjusted_mode->crtc_vblank_start = adjusted_mode->crtc_vdisplay;
1529379bc100SJani Nikula 	adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vtotal;
1530379bc100SJani Nikula }
1531379bc100SJani Nikula 
1532cebb28acSVandita Kulkarni static bool gen11_dsi_is_periodic_cmd_mode(struct intel_dsi *intel_dsi)
1533cebb28acSVandita Kulkarni {
1534cebb28acSVandita Kulkarni 	struct drm_device *dev = intel_dsi->base.base.dev;
1535cebb28acSVandita Kulkarni 	struct drm_i915_private *dev_priv = to_i915(dev);
1536cebb28acSVandita Kulkarni 	enum transcoder dsi_trans;
1537cebb28acSVandita Kulkarni 	u32 val;
1538cebb28acSVandita Kulkarni 
1539cebb28acSVandita Kulkarni 	if (intel_dsi->ports == BIT(PORT_B))
1540cebb28acSVandita Kulkarni 		dsi_trans = TRANSCODER_DSI_1;
1541cebb28acSVandita Kulkarni 	else
1542cebb28acSVandita Kulkarni 		dsi_trans = TRANSCODER_DSI_0;
1543cebb28acSVandita Kulkarni 
1544cebb28acSVandita Kulkarni 	val = intel_de_read(dev_priv, DSI_TRANS_FUNC_CONF(dsi_trans));
1545cebb28acSVandita Kulkarni 	return (val & DSI_PERIODIC_FRAME_UPDATE_ENABLE);
1546cebb28acSVandita Kulkarni }
1547cebb28acSVandita Kulkarni 
15485682a41fSVandita Kulkarni static void gen11_dsi_get_cmd_mode_config(struct intel_dsi *intel_dsi,
15495682a41fSVandita Kulkarni 					  struct intel_crtc_state *pipe_config)
15505682a41fSVandita Kulkarni {
15515682a41fSVandita Kulkarni 	if (intel_dsi->ports == (BIT(PORT_B) | BIT(PORT_A)))
15525682a41fSVandita Kulkarni 		pipe_config->mode_flags |= I915_MODE_FLAG_DSI_USE_TE1 |
15535682a41fSVandita Kulkarni 					    I915_MODE_FLAG_DSI_USE_TE0;
15545682a41fSVandita Kulkarni 	else if (intel_dsi->ports == BIT(PORT_B))
15555682a41fSVandita Kulkarni 		pipe_config->mode_flags |= I915_MODE_FLAG_DSI_USE_TE1;
15565682a41fSVandita Kulkarni 	else
15575682a41fSVandita Kulkarni 		pipe_config->mode_flags |= I915_MODE_FLAG_DSI_USE_TE0;
15585682a41fSVandita Kulkarni }
15595682a41fSVandita Kulkarni 
1560379bc100SJani Nikula static void gen11_dsi_get_config(struct intel_encoder *encoder,
1561379bc100SJani Nikula 				 struct intel_crtc_state *pipe_config)
1562379bc100SJani Nikula {
15632225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
1564b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1565379bc100SJani Nikula 
1566351221ffSVille Syrjälä 	intel_ddi_get_clock(encoder, pipe_config, icl_ddi_combo_get_pll(encoder));
1567379bc100SJani Nikula 
15681326a92cSMaarten Lankhorst 	pipe_config->hw.adjusted_mode.crtc_clock = intel_dsi->pclk;
1569379bc100SJani Nikula 	if (intel_dsi->dual_link)
15701326a92cSMaarten Lankhorst 		pipe_config->hw.adjusted_mode.crtc_clock *= 2;
1571379bc100SJani Nikula 
1572379bc100SJani Nikula 	gen11_dsi_get_timings(encoder, pipe_config);
1573379bc100SJani Nikula 	pipe_config->output_types |= BIT(INTEL_OUTPUT_DSI);
1574379bc100SJani Nikula 	pipe_config->pipe_bpp = bdw_get_pipemisc_bpp(crtc);
1575cebb28acSVandita Kulkarni 
15765682a41fSVandita Kulkarni 	/* Get the details on which TE should be enabled */
15775682a41fSVandita Kulkarni 	if (is_cmd_mode(intel_dsi))
15785682a41fSVandita Kulkarni 		gen11_dsi_get_cmd_mode_config(intel_dsi, pipe_config);
15795682a41fSVandita Kulkarni 
1580cebb28acSVandita Kulkarni 	if (gen11_dsi_is_periodic_cmd_mode(intel_dsi))
1581af157b76SVille Syrjälä 		pipe_config->mode_flags |= I915_MODE_FLAG_DSI_PERIODIC_CMD_MODE;
1582379bc100SJani Nikula }
1583379bc100SJani Nikula 
1584544021e3STejas Upadhyay static void gen11_dsi_sync_state(struct intel_encoder *encoder,
1585544021e3STejas Upadhyay 				 const struct intel_crtc_state *crtc_state)
1586544021e3STejas Upadhyay {
1587544021e3STejas Upadhyay 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
15887194dc99SImre Deak 	struct intel_crtc *intel_crtc;
15897194dc99SImre Deak 	enum pipe pipe;
15907194dc99SImre Deak 
15917194dc99SImre Deak 	if (!crtc_state)
15927194dc99SImre Deak 		return;
15937194dc99SImre Deak 
15947194dc99SImre Deak 	intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
15957194dc99SImre Deak 	pipe = intel_crtc->pipe;
1596544021e3STejas Upadhyay 
1597544021e3STejas Upadhyay 	/* wa verify 1409054076:icl,jsl,ehl */
1598544021e3STejas Upadhyay 	if (DISPLAY_VER(dev_priv) == 11 && pipe == PIPE_B &&
1599544021e3STejas Upadhyay 	    !(intel_de_read(dev_priv, CHICKEN_PAR1_1) & IGNORE_KVMR_PIPE_A))
1600544021e3STejas Upadhyay 		drm_dbg_kms(&dev_priv->drm,
1601544021e3STejas Upadhyay 			    "[ENCODER:%d:%s] BIOS left IGNORE_KVMR_PIPE_A cleared with pipe B enabled\n",
1602544021e3STejas Upadhyay 			    encoder->base.base.id,
1603544021e3STejas Upadhyay 			    encoder->base.name);
1604544021e3STejas Upadhyay }
1605544021e3STejas Upadhyay 
16062b68392eSJani Nikula static int gen11_dsi_dsc_compute_config(struct intel_encoder *encoder,
16072b68392eSJani Nikula 					struct intel_crtc_state *crtc_state)
16082b68392eSJani Nikula {
16092b68392eSJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
16102b68392eSJani Nikula 	struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
1611005e9537SMatt Roper 	int dsc_max_bpc = DISPLAY_VER(dev_priv) >= 12 ? 12 : 10;
16122b68392eSJani Nikula 	bool use_dsc;
16132b68392eSJani Nikula 	int ret;
16142b68392eSJani Nikula 
16152b68392eSJani Nikula 	use_dsc = intel_bios_get_dsc_params(encoder, crtc_state, dsc_max_bpc);
16162b68392eSJani Nikula 	if (!use_dsc)
16172b68392eSJani Nikula 		return 0;
16182b68392eSJani Nikula 
16192b68392eSJani Nikula 	if (crtc_state->pipe_bpp < 8 * 3)
16202b68392eSJani Nikula 		return -EINVAL;
16212b68392eSJani Nikula 
16222b68392eSJani Nikula 	/* FIXME: split only when necessary */
16232b68392eSJani Nikula 	if (crtc_state->dsc.slice_count > 1)
16242b68392eSJani Nikula 		crtc_state->dsc.dsc_split = true;
16252b68392eSJani Nikula 
16262b68392eSJani Nikula 	vdsc_cfg->convert_rgb = true;
16272b68392eSJani Nikula 
1628420798a0SJani Nikula 	/* FIXME: initialize from VBT */
1629420798a0SJani Nikula 	vdsc_cfg->rc_model_size = DSC_RC_MODEL_SIZE_CONST;
1630420798a0SJani Nikula 
16313126977dSVille Syrjälä 	ret = intel_dsc_compute_params(crtc_state);
16322b68392eSJani Nikula 	if (ret)
16332b68392eSJani Nikula 		return ret;
16342b68392eSJani Nikula 
16352b68392eSJani Nikula 	/* DSI specific sanity checks on the common code */
16363dbe5e11SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, vdsc_cfg->vbr_enable);
16373dbe5e11SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, vdsc_cfg->simple_422);
16383dbe5e11SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm,
16393dbe5e11SPankaj Bharadiya 		    vdsc_cfg->pic_width % vdsc_cfg->slice_width);
16403dbe5e11SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm, vdsc_cfg->slice_height < 8);
16413dbe5e11SPankaj Bharadiya 	drm_WARN_ON(&dev_priv->drm,
16423dbe5e11SPankaj Bharadiya 		    vdsc_cfg->pic_height % vdsc_cfg->slice_height);
16432b68392eSJani Nikula 
16442b68392eSJani Nikula 	ret = drm_dsc_compute_rc_parameters(vdsc_cfg);
16452b68392eSJani Nikula 	if (ret)
16462b68392eSJani Nikula 		return ret;
16472b68392eSJani Nikula 
16482b68392eSJani Nikula 	crtc_state->dsc.compression_enable = true;
16492b68392eSJani Nikula 
16502b68392eSJani Nikula 	return 0;
16512b68392eSJani Nikula }
16522b68392eSJani Nikula 
1653379bc100SJani Nikula static int gen11_dsi_compute_config(struct intel_encoder *encoder,
1654379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config,
1655379bc100SJani Nikula 				    struct drm_connector_state *conn_state)
1656379bc100SJani Nikula {
1657dd10a80fSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1658379bc100SJani Nikula 	struct intel_dsi *intel_dsi = container_of(encoder, struct intel_dsi,
1659379bc100SJani Nikula 						   base);
1660379bc100SJani Nikula 	struct intel_connector *intel_connector = intel_dsi->attached_connector;
1661379bc100SJani Nikula 	struct drm_display_mode *adjusted_mode =
16621326a92cSMaarten Lankhorst 		&pipe_config->hw.adjusted_mode;
1663d7ff281cSVille Syrjälä 	int ret;
1664379bc100SJani Nikula 
1665379bc100SJani Nikula 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
1666cff4c2c6SVille Syrjälä 
1667cff4c2c6SVille Syrjälä 	ret = intel_panel_compute_config(intel_connector, adjusted_mode);
1668cff4c2c6SVille Syrjälä 	if (ret)
1669cff4c2c6SVille Syrjälä 		return ret;
1670d7ff281cSVille Syrjälä 
16714b93f49dSJani Nikula 	ret = intel_panel_fitting(pipe_config, conn_state);
1672d7ff281cSVille Syrjälä 	if (ret)
1673d7ff281cSVille Syrjälä 		return ret;
1674379bc100SJani Nikula 
1675379bc100SJani Nikula 	adjusted_mode->flags = 0;
1676379bc100SJani Nikula 
1677379bc100SJani Nikula 	/* Dual link goes to trancoder DSI'0' */
1678379bc100SJani Nikula 	if (intel_dsi->ports == BIT(PORT_B))
1679379bc100SJani Nikula 		pipe_config->cpu_transcoder = TRANSCODER_DSI_1;
1680379bc100SJani Nikula 	else
1681379bc100SJani Nikula 		pipe_config->cpu_transcoder = TRANSCODER_DSI_0;
1682379bc100SJani Nikula 
168350003bf5SJani Nikula 	if (intel_dsi->pixel_format == MIPI_DSI_FMT_RGB888)
168450003bf5SJani Nikula 		pipe_config->pipe_bpp = 24;
168550003bf5SJani Nikula 	else
168650003bf5SJani Nikula 		pipe_config->pipe_bpp = 18;
168750003bf5SJani Nikula 
1688379bc100SJani Nikula 	pipe_config->clock_set = true;
16892b68392eSJani Nikula 
16902b68392eSJani Nikula 	if (gen11_dsi_dsc_compute_config(encoder, pipe_config))
1691dd10a80fSJani Nikula 		drm_dbg_kms(&i915->drm, "Attempting to use DSC failed\n");
16922b68392eSJani Nikula 
169304865139SJani Nikula 	pipe_config->port_clock = afe_clk(encoder, pipe_config) / 5;
1694379bc100SJani Nikula 
1695f78a862dSVandita Kulkarni 	/*
1696f78a862dSVandita Kulkarni 	 * In case of TE GATE cmd mode, we
1697f78a862dSVandita Kulkarni 	 * receive TE from the slave if
1698f78a862dSVandita Kulkarni 	 * dual link is enabled
1699f78a862dSVandita Kulkarni 	 */
17005682a41fSVandita Kulkarni 	if (is_cmd_mode(intel_dsi))
17015682a41fSVandita Kulkarni 		gen11_dsi_get_cmd_mode_config(intel_dsi, pipe_config);
1702f78a862dSVandita Kulkarni 
1703379bc100SJani Nikula 	return 0;
1704379bc100SJani Nikula }
1705379bc100SJani Nikula 
1706379bc100SJani Nikula static void gen11_dsi_get_power_domains(struct intel_encoder *encoder,
1707379bc100SJani Nikula 					struct intel_crtc_state *crtc_state)
1708379bc100SJani Nikula {
17092b68392eSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
17102b68392eSJani Nikula 
1711b7d02c3aSVille Syrjälä 	get_dsi_io_power_domains(i915,
1712b7d02c3aSVille Syrjälä 				 enc_to_intel_dsi(encoder));
1713379bc100SJani Nikula }
1714379bc100SJani Nikula 
1715379bc100SJani Nikula static bool gen11_dsi_get_hw_state(struct intel_encoder *encoder,
1716379bc100SJani Nikula 				   enum pipe *pipe)
1717379bc100SJani Nikula {
1718379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1719b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1720379bc100SJani Nikula 	enum transcoder dsi_trans;
1721379bc100SJani Nikula 	intel_wakeref_t wakeref;
1722379bc100SJani Nikula 	enum port port;
1723379bc100SJani Nikula 	bool ret = false;
1724379bc100SJani Nikula 	u32 tmp;
1725379bc100SJani Nikula 
1726379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1727379bc100SJani Nikula 						     encoder->power_domain);
1728379bc100SJani Nikula 	if (!wakeref)
1729379bc100SJani Nikula 		return false;
1730379bc100SJani Nikula 
1731379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1732379bc100SJani Nikula 		dsi_trans = dsi_port_to_transcoder(port);
17331c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(dsi_trans));
1734379bc100SJani Nikula 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
1735379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_A_ON:
1736379bc100SJani Nikula 			*pipe = PIPE_A;
1737379bc100SJani Nikula 			break;
1738379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
1739379bc100SJani Nikula 			*pipe = PIPE_B;
1740379bc100SJani Nikula 			break;
1741379bc100SJani Nikula 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
1742379bc100SJani Nikula 			*pipe = PIPE_C;
1743379bc100SJani Nikula 			break;
17444d89adc7SJosé Roberto de Souza 		case TRANS_DDI_EDP_INPUT_D_ONOFF:
17454d89adc7SJosé Roberto de Souza 			*pipe = PIPE_D;
17464d89adc7SJosé Roberto de Souza 			break;
1747379bc100SJani Nikula 		default:
1748b5280cd0SWambui Karuga 			drm_err(&dev_priv->drm, "Invalid PIPE input\n");
1749379bc100SJani Nikula 			goto out;
1750379bc100SJani Nikula 		}
1751379bc100SJani Nikula 
17521c63f6dfSJani Nikula 		tmp = intel_de_read(dev_priv, PIPECONF(dsi_trans));
1753379bc100SJani Nikula 		ret = tmp & PIPECONF_ENABLE;
1754379bc100SJani Nikula 	}
1755379bc100SJani Nikula out:
1756379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
1757379bc100SJani Nikula 	return ret;
1758379bc100SJani Nikula }
1759379bc100SJani Nikula 
1760b671d6efSImre Deak static bool gen11_dsi_initial_fastset_check(struct intel_encoder *encoder,
1761b671d6efSImre Deak 					    struct intel_crtc_state *crtc_state)
1762b671d6efSImre Deak {
1763b671d6efSImre Deak 	if (crtc_state->dsc.compression_enable) {
1764b671d6efSImre Deak 		drm_dbg_kms(encoder->base.dev, "Forcing full modeset due to DSC being enabled\n");
1765b671d6efSImre Deak 		crtc_state->uapi.mode_changed = true;
1766b671d6efSImre Deak 
1767b671d6efSImre Deak 		return false;
1768b671d6efSImre Deak 	}
1769b671d6efSImre Deak 
1770b671d6efSImre Deak 	return true;
1771b671d6efSImre Deak }
1772b671d6efSImre Deak 
1773379bc100SJani Nikula static void gen11_dsi_encoder_destroy(struct drm_encoder *encoder)
1774379bc100SJani Nikula {
1775379bc100SJani Nikula 	intel_encoder_destroy(encoder);
1776379bc100SJani Nikula }
1777379bc100SJani Nikula 
1778379bc100SJani Nikula static const struct drm_encoder_funcs gen11_dsi_encoder_funcs = {
1779379bc100SJani Nikula 	.destroy = gen11_dsi_encoder_destroy,
1780379bc100SJani Nikula };
1781379bc100SJani Nikula 
1782379bc100SJani Nikula static const struct drm_connector_funcs gen11_dsi_connector_funcs = {
1783b81dddb9SVille Syrjälä 	.detect = intel_panel_detect,
1784379bc100SJani Nikula 	.late_register = intel_connector_register,
1785379bc100SJani Nikula 	.early_unregister = intel_connector_unregister,
1786379bc100SJani Nikula 	.destroy = intel_connector_destroy,
1787379bc100SJani Nikula 	.fill_modes = drm_helper_probe_single_connector_modes,
1788379bc100SJani Nikula 	.atomic_get_property = intel_digital_connector_atomic_get_property,
1789379bc100SJani Nikula 	.atomic_set_property = intel_digital_connector_atomic_set_property,
1790379bc100SJani Nikula 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1791379bc100SJani Nikula 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
1792379bc100SJani Nikula };
1793379bc100SJani Nikula 
1794379bc100SJani Nikula static const struct drm_connector_helper_funcs gen11_dsi_connector_helper_funcs = {
1795379bc100SJani Nikula 	.get_modes = intel_dsi_get_modes,
17962b68392eSJani Nikula 	.mode_valid = gen11_dsi_mode_valid,
1797379bc100SJani Nikula 	.atomic_check = intel_digital_connector_atomic_check,
1798379bc100SJani Nikula };
1799379bc100SJani Nikula 
1800379bc100SJani Nikula static int gen11_dsi_host_attach(struct mipi_dsi_host *host,
1801379bc100SJani Nikula 				 struct mipi_dsi_device *dsi)
1802379bc100SJani Nikula {
1803379bc100SJani Nikula 	return 0;
1804379bc100SJani Nikula }
1805379bc100SJani Nikula 
1806379bc100SJani Nikula static int gen11_dsi_host_detach(struct mipi_dsi_host *host,
1807379bc100SJani Nikula 				 struct mipi_dsi_device *dsi)
1808379bc100SJani Nikula {
1809379bc100SJani Nikula 	return 0;
1810379bc100SJani Nikula }
1811379bc100SJani Nikula 
1812379bc100SJani Nikula static ssize_t gen11_dsi_host_transfer(struct mipi_dsi_host *host,
1813379bc100SJani Nikula 				       const struct mipi_dsi_msg *msg)
1814379bc100SJani Nikula {
1815379bc100SJani Nikula 	struct intel_dsi_host *intel_dsi_host = to_intel_dsi_host(host);
1816379bc100SJani Nikula 	struct mipi_dsi_packet dsi_pkt;
1817379bc100SJani Nikula 	ssize_t ret;
1818379bc100SJani Nikula 	bool enable_lpdt = false;
1819379bc100SJani Nikula 
1820379bc100SJani Nikula 	ret = mipi_dsi_create_packet(&dsi_pkt, msg);
1821379bc100SJani Nikula 	if (ret < 0)
1822379bc100SJani Nikula 		return ret;
1823379bc100SJani Nikula 
1824379bc100SJani Nikula 	if (msg->flags & MIPI_DSI_MSG_USE_LPM)
1825379bc100SJani Nikula 		enable_lpdt = true;
1826379bc100SJani Nikula 
1827379bc100SJani Nikula 	/* only long packet contains payload */
1828379bc100SJani Nikula 	if (mipi_dsi_packet_format_is_long(msg->type)) {
18293e2947cdSJani Nikula 		ret = dsi_send_pkt_payld(intel_dsi_host, &dsi_pkt);
1830379bc100SJani Nikula 		if (ret < 0)
1831379bc100SJani Nikula 			return ret;
1832379bc100SJani Nikula 	}
1833379bc100SJani Nikula 
18345ebd50d3SLee Shawn C 	/* send packet header */
18353e2947cdSJani Nikula 	ret  = dsi_send_pkt_hdr(intel_dsi_host, &dsi_pkt, enable_lpdt);
18365ebd50d3SLee Shawn C 	if (ret < 0)
18375ebd50d3SLee Shawn C 		return ret;
18385ebd50d3SLee Shawn C 
1839379bc100SJani Nikula 	//TODO: add payload receive code if needed
1840379bc100SJani Nikula 
1841379bc100SJani Nikula 	ret = sizeof(dsi_pkt.header) + dsi_pkt.payload_length;
1842379bc100SJani Nikula 
1843379bc100SJani Nikula 	return ret;
1844379bc100SJani Nikula }
1845379bc100SJani Nikula 
1846379bc100SJani Nikula static const struct mipi_dsi_host_ops gen11_dsi_host_ops = {
1847379bc100SJani Nikula 	.attach = gen11_dsi_host_attach,
1848379bc100SJani Nikula 	.detach = gen11_dsi_host_detach,
1849379bc100SJani Nikula 	.transfer = gen11_dsi_host_transfer,
1850379bc100SJani Nikula };
1851379bc100SJani Nikula 
1852379bc100SJani Nikula #define ICL_PREPARE_CNT_MAX	0x7
1853379bc100SJani Nikula #define ICL_CLK_ZERO_CNT_MAX	0xf
1854379bc100SJani Nikula #define ICL_TRAIL_CNT_MAX	0x7
1855379bc100SJani Nikula #define ICL_TCLK_PRE_CNT_MAX	0x3
1856379bc100SJani Nikula #define ICL_TCLK_POST_CNT_MAX	0x7
1857379bc100SJani Nikula #define ICL_HS_ZERO_CNT_MAX	0xf
1858379bc100SJani Nikula #define ICL_EXIT_ZERO_CNT_MAX	0x7
1859379bc100SJani Nikula 
1860379bc100SJani Nikula static void icl_dphy_param_init(struct intel_dsi *intel_dsi)
1861379bc100SJani Nikula {
1862379bc100SJani Nikula 	struct drm_device *dev = intel_dsi->base.base.dev;
1863379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1864379bc100SJani Nikula 	struct mipi_config *mipi_config = dev_priv->vbt.dsi.config;
1865379bc100SJani Nikula 	u32 tlpx_ns;
1866379bc100SJani Nikula 	u32 prepare_cnt, exit_zero_cnt, clk_zero_cnt, trail_cnt;
1867379bc100SJani Nikula 	u32 ths_prepare_ns, tclk_trail_ns;
1868379bc100SJani Nikula 	u32 hs_zero_cnt;
1869379bc100SJani Nikula 	u32 tclk_pre_cnt, tclk_post_cnt;
1870379bc100SJani Nikula 
1871379bc100SJani Nikula 	tlpx_ns = intel_dsi_tlpx_ns(intel_dsi);
1872379bc100SJani Nikula 
1873379bc100SJani Nikula 	tclk_trail_ns = max(mipi_config->tclk_trail, mipi_config->ths_trail);
1874379bc100SJani Nikula 	ths_prepare_ns = max(mipi_config->ths_prepare,
1875379bc100SJani Nikula 			     mipi_config->tclk_prepare);
1876379bc100SJani Nikula 
1877379bc100SJani Nikula 	/*
1878379bc100SJani Nikula 	 * prepare cnt in escape clocks
1879379bc100SJani Nikula 	 * this field represents a hexadecimal value with a precision
1880379bc100SJani Nikula 	 * of 1.2 – i.e. the most significant bit is the integer
1881379bc100SJani Nikula 	 * and the least significant 2 bits are fraction bits.
1882379bc100SJani Nikula 	 * so, the field can represent a range of 0.25 to 1.75
1883379bc100SJani Nikula 	 */
1884379bc100SJani Nikula 	prepare_cnt = DIV_ROUND_UP(ths_prepare_ns * 4, tlpx_ns);
1885379bc100SJani Nikula 	if (prepare_cnt > ICL_PREPARE_CNT_MAX) {
1886b5280cd0SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "prepare_cnt out of range (%d)\n",
1887b5280cd0SWambui Karuga 			    prepare_cnt);
1888379bc100SJani Nikula 		prepare_cnt = ICL_PREPARE_CNT_MAX;
1889379bc100SJani Nikula 	}
1890379bc100SJani Nikula 
1891379bc100SJani Nikula 	/* clk zero count in escape clocks */
1892379bc100SJani Nikula 	clk_zero_cnt = DIV_ROUND_UP(mipi_config->tclk_prepare_clkzero -
1893379bc100SJani Nikula 				    ths_prepare_ns, tlpx_ns);
1894379bc100SJani Nikula 	if (clk_zero_cnt > ICL_CLK_ZERO_CNT_MAX) {
1895b5280cd0SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
1896b5280cd0SWambui Karuga 			    "clk_zero_cnt out of range (%d)\n", clk_zero_cnt);
1897379bc100SJani Nikula 		clk_zero_cnt = ICL_CLK_ZERO_CNT_MAX;
1898379bc100SJani Nikula 	}
1899379bc100SJani Nikula 
1900379bc100SJani Nikula 	/* trail cnt in escape clocks*/
1901379bc100SJani Nikula 	trail_cnt = DIV_ROUND_UP(tclk_trail_ns, tlpx_ns);
1902379bc100SJani Nikula 	if (trail_cnt > ICL_TRAIL_CNT_MAX) {
1903b5280cd0SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "trail_cnt out of range (%d)\n",
1904b5280cd0SWambui Karuga 			    trail_cnt);
1905379bc100SJani Nikula 		trail_cnt = ICL_TRAIL_CNT_MAX;
1906379bc100SJani Nikula 	}
1907379bc100SJani Nikula 
1908379bc100SJani Nikula 	/* tclk pre count in escape clocks */
1909379bc100SJani Nikula 	tclk_pre_cnt = DIV_ROUND_UP(mipi_config->tclk_pre, tlpx_ns);
1910379bc100SJani Nikula 	if (tclk_pre_cnt > ICL_TCLK_PRE_CNT_MAX) {
1911b5280cd0SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
1912b5280cd0SWambui Karuga 			    "tclk_pre_cnt out of range (%d)\n", tclk_pre_cnt);
1913379bc100SJani Nikula 		tclk_pre_cnt = ICL_TCLK_PRE_CNT_MAX;
1914379bc100SJani Nikula 	}
1915379bc100SJani Nikula 
1916379bc100SJani Nikula 	/* tclk post count in escape clocks */
1917379bc100SJani Nikula 	tclk_post_cnt = DIV_ROUND_UP(mipi_config->tclk_post, tlpx_ns);
1918379bc100SJani Nikula 	if (tclk_post_cnt > ICL_TCLK_POST_CNT_MAX) {
1919b5280cd0SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
1920b5280cd0SWambui Karuga 			    "tclk_post_cnt out of range (%d)\n",
1921b5280cd0SWambui Karuga 			    tclk_post_cnt);
1922379bc100SJani Nikula 		tclk_post_cnt = ICL_TCLK_POST_CNT_MAX;
1923379bc100SJani Nikula 	}
1924379bc100SJani Nikula 
1925379bc100SJani Nikula 	/* hs zero cnt in escape clocks */
1926379bc100SJani Nikula 	hs_zero_cnt = DIV_ROUND_UP(mipi_config->ths_prepare_hszero -
1927379bc100SJani Nikula 				   ths_prepare_ns, tlpx_ns);
1928379bc100SJani Nikula 	if (hs_zero_cnt > ICL_HS_ZERO_CNT_MAX) {
1929b5280cd0SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "hs_zero_cnt out of range (%d)\n",
1930b5280cd0SWambui Karuga 			    hs_zero_cnt);
1931379bc100SJani Nikula 		hs_zero_cnt = ICL_HS_ZERO_CNT_MAX;
1932379bc100SJani Nikula 	}
1933379bc100SJani Nikula 
1934379bc100SJani Nikula 	/* hs exit zero cnt in escape clocks */
1935379bc100SJani Nikula 	exit_zero_cnt = DIV_ROUND_UP(mipi_config->ths_exit, tlpx_ns);
1936379bc100SJani Nikula 	if (exit_zero_cnt > ICL_EXIT_ZERO_CNT_MAX) {
1937b5280cd0SWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
1938b5280cd0SWambui Karuga 			    "exit_zero_cnt out of range (%d)\n",
1939b5280cd0SWambui Karuga 			    exit_zero_cnt);
1940379bc100SJani Nikula 		exit_zero_cnt = ICL_EXIT_ZERO_CNT_MAX;
1941379bc100SJani Nikula 	}
1942379bc100SJani Nikula 
1943379bc100SJani Nikula 	/* clock lane dphy timings */
1944379bc100SJani Nikula 	intel_dsi->dphy_reg = (CLK_PREPARE_OVERRIDE |
1945379bc100SJani Nikula 			       CLK_PREPARE(prepare_cnt) |
1946379bc100SJani Nikula 			       CLK_ZERO_OVERRIDE |
1947379bc100SJani Nikula 			       CLK_ZERO(clk_zero_cnt) |
1948379bc100SJani Nikula 			       CLK_PRE_OVERRIDE |
1949379bc100SJani Nikula 			       CLK_PRE(tclk_pre_cnt) |
1950379bc100SJani Nikula 			       CLK_POST_OVERRIDE |
1951379bc100SJani Nikula 			       CLK_POST(tclk_post_cnt) |
1952379bc100SJani Nikula 			       CLK_TRAIL_OVERRIDE |
1953379bc100SJani Nikula 			       CLK_TRAIL(trail_cnt));
1954379bc100SJani Nikula 
1955379bc100SJani Nikula 	/* data lanes dphy timings */
1956379bc100SJani Nikula 	intel_dsi->dphy_data_lane_reg = (HS_PREPARE_OVERRIDE |
1957379bc100SJani Nikula 					 HS_PREPARE(prepare_cnt) |
1958379bc100SJani Nikula 					 HS_ZERO_OVERRIDE |
1959379bc100SJani Nikula 					 HS_ZERO(hs_zero_cnt) |
1960379bc100SJani Nikula 					 HS_TRAIL_OVERRIDE |
1961379bc100SJani Nikula 					 HS_TRAIL(trail_cnt) |
1962379bc100SJani Nikula 					 HS_EXIT_OVERRIDE |
1963379bc100SJani Nikula 					 HS_EXIT(exit_zero_cnt));
1964379bc100SJani Nikula 
1965379bc100SJani Nikula 	intel_dsi_log_params(intel_dsi);
1966379bc100SJani Nikula }
1967379bc100SJani Nikula 
1968f384e48dSVandita Kulkarni static void icl_dsi_add_properties(struct intel_connector *connector)
1969f384e48dSVandita Kulkarni {
1970f384e48dSVandita Kulkarni 	u32 allowed_scalers;
1971f384e48dSVandita Kulkarni 
1972f384e48dSVandita Kulkarni 	allowed_scalers = BIT(DRM_MODE_SCALE_ASPECT) |
1973f384e48dSVandita Kulkarni 			   BIT(DRM_MODE_SCALE_FULLSCREEN) |
1974f384e48dSVandita Kulkarni 			   BIT(DRM_MODE_SCALE_CENTER);
1975f384e48dSVandita Kulkarni 
1976f384e48dSVandita Kulkarni 	drm_connector_attach_scaling_mode_property(&connector->base,
1977f384e48dSVandita Kulkarni 						   allowed_scalers);
1978f384e48dSVandita Kulkarni 
1979f384e48dSVandita Kulkarni 	connector->base.state->scaling_mode = DRM_MODE_SCALE_ASPECT;
1980f384e48dSVandita Kulkarni 
198169654c63SDerek Basehore 	drm_connector_set_panel_orientation_with_quirk(&connector->base,
198269654c63SDerek Basehore 				intel_dsi_get_panel_orientation(connector),
1983f384e48dSVandita Kulkarni 				connector->panel.fixed_mode->hdisplay,
1984f384e48dSVandita Kulkarni 				connector->panel.fixed_mode->vdisplay);
1985f384e48dSVandita Kulkarni }
1986f384e48dSVandita Kulkarni 
1987379bc100SJani Nikula void icl_dsi_init(struct drm_i915_private *dev_priv)
1988379bc100SJani Nikula {
1989379bc100SJani Nikula 	struct drm_device *dev = &dev_priv->drm;
1990379bc100SJani Nikula 	struct intel_dsi *intel_dsi;
1991379bc100SJani Nikula 	struct intel_encoder *encoder;
1992379bc100SJani Nikula 	struct intel_connector *intel_connector;
1993379bc100SJani Nikula 	struct drm_connector *connector;
1994379bc100SJani Nikula 	struct drm_display_mode *fixed_mode;
1995379bc100SJani Nikula 	enum port port;
1996379bc100SJani Nikula 
1997379bc100SJani Nikula 	if (!intel_bios_is_dsi_present(dev_priv, &port))
1998379bc100SJani Nikula 		return;
1999379bc100SJani Nikula 
2000379bc100SJani Nikula 	intel_dsi = kzalloc(sizeof(*intel_dsi), GFP_KERNEL);
2001379bc100SJani Nikula 	if (!intel_dsi)
2002379bc100SJani Nikula 		return;
2003379bc100SJani Nikula 
2004379bc100SJani Nikula 	intel_connector = intel_connector_alloc();
2005379bc100SJani Nikula 	if (!intel_connector) {
2006379bc100SJani Nikula 		kfree(intel_dsi);
2007379bc100SJani Nikula 		return;
2008379bc100SJani Nikula 	}
2009379bc100SJani Nikula 
2010379bc100SJani Nikula 	encoder = &intel_dsi->base;
2011379bc100SJani Nikula 	intel_dsi->attached_connector = intel_connector;
2012379bc100SJani Nikula 	connector = &intel_connector->base;
2013379bc100SJani Nikula 
2014379bc100SJani Nikula 	/* register DSI encoder with DRM subsystem */
2015379bc100SJani Nikula 	drm_encoder_init(dev, &encoder->base, &gen11_dsi_encoder_funcs,
2016379bc100SJani Nikula 			 DRM_MODE_ENCODER_DSI, "DSI %c", port_name(port));
2017379bc100SJani Nikula 
2018379bc100SJani Nikula 	encoder->pre_pll_enable = gen11_dsi_pre_pll_enable;
2019379bc100SJani Nikula 	encoder->pre_enable = gen11_dsi_pre_enable;
202021fd23acSJani Nikula 	encoder->enable = gen11_dsi_enable;
2021379bc100SJani Nikula 	encoder->disable = gen11_dsi_disable;
2022773b4b54SVille Syrjälä 	encoder->post_disable = gen11_dsi_post_disable;
2023379bc100SJani Nikula 	encoder->port = port;
2024379bc100SJani Nikula 	encoder->get_config = gen11_dsi_get_config;
2025544021e3STejas Upadhyay 	encoder->sync_state = gen11_dsi_sync_state;
2026c0a52f8bSJani Nikula 	encoder->update_pipe = intel_backlight_update;
2027379bc100SJani Nikula 	encoder->compute_config = gen11_dsi_compute_config;
2028379bc100SJani Nikula 	encoder->get_hw_state = gen11_dsi_get_hw_state;
2029b671d6efSImre Deak 	encoder->initial_fastset_check = gen11_dsi_initial_fastset_check;
2030379bc100SJani Nikula 	encoder->type = INTEL_OUTPUT_DSI;
2031379bc100SJani Nikula 	encoder->cloneable = 0;
203234053ee1SVille Syrjälä 	encoder->pipe_mask = ~0;
2033379bc100SJani Nikula 	encoder->power_domain = POWER_DOMAIN_PORT_DSI;
2034379bc100SJani Nikula 	encoder->get_power_domains = gen11_dsi_get_power_domains;
203587bd8498SVille Syrjälä 	encoder->disable_clock = gen11_dsi_gate_clocks;
20360fbd8694SVille Syrjälä 	encoder->is_clock_enabled = gen11_dsi_is_clock_enabled;
2037379bc100SJani Nikula 
2038379bc100SJani Nikula 	/* register DSI connector with DRM subsystem */
2039379bc100SJani Nikula 	drm_connector_init(dev, connector, &gen11_dsi_connector_funcs,
2040379bc100SJani Nikula 			   DRM_MODE_CONNECTOR_DSI);
2041379bc100SJani Nikula 	drm_connector_helper_add(connector, &gen11_dsi_connector_helper_funcs);
2042379bc100SJani Nikula 	connector->display_info.subpixel_order = SubPixelHorizontalRGB;
2043379bc100SJani Nikula 	connector->interlace_allowed = false;
2044379bc100SJani Nikula 	connector->doublescan_allowed = false;
2045379bc100SJani Nikula 	intel_connector->get_hw_state = intel_connector_get_hw_state;
2046379bc100SJani Nikula 
2047379bc100SJani Nikula 	/* attach connector to encoder */
2048379bc100SJani Nikula 	intel_connector_attach_encoder(intel_connector, encoder);
2049379bc100SJani Nikula 
2050379bc100SJani Nikula 	mutex_lock(&dev->mode_config.mutex);
2051379bc100SJani Nikula 	fixed_mode = intel_panel_vbt_fixed_mode(intel_connector);
2052379bc100SJani Nikula 	mutex_unlock(&dev->mode_config.mutex);
2053379bc100SJani Nikula 
2054379bc100SJani Nikula 	if (!fixed_mode) {
2055b5280cd0SWambui Karuga 		drm_err(&dev_priv->drm, "DSI fixed mode info missing\n");
2056379bc100SJani Nikula 		goto err;
2057379bc100SJani Nikula 	}
2058379bc100SJani Nikula 
2059379bc100SJani Nikula 	intel_panel_init(&intel_connector->panel, fixed_mode, NULL);
2060c0a52f8bSJani Nikula 	intel_backlight_setup(intel_connector, INVALID_PIPE);
2061379bc100SJani Nikula 
2062379bc100SJani Nikula 	if (dev_priv->vbt.dsi.config->dual_link)
2063379bc100SJani Nikula 		intel_dsi->ports = BIT(PORT_A) | BIT(PORT_B);
2064379bc100SJani Nikula 	else
2065379bc100SJani Nikula 		intel_dsi->ports = BIT(port);
2066379bc100SJani Nikula 
2067379bc100SJani Nikula 	intel_dsi->dcs_backlight_ports = dev_priv->vbt.dsi.bl_ports;
2068379bc100SJani Nikula 	intel_dsi->dcs_cabc_ports = dev_priv->vbt.dsi.cabc_ports;
2069379bc100SJani Nikula 
2070379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
2071379bc100SJani Nikula 		struct intel_dsi_host *host;
2072379bc100SJani Nikula 
2073379bc100SJani Nikula 		host = intel_dsi_host_init(intel_dsi, &gen11_dsi_host_ops, port);
2074379bc100SJani Nikula 		if (!host)
2075379bc100SJani Nikula 			goto err;
2076379bc100SJani Nikula 
2077379bc100SJani Nikula 		intel_dsi->dsi_hosts[port] = host;
2078379bc100SJani Nikula 	}
2079379bc100SJani Nikula 
2080379bc100SJani Nikula 	if (!intel_dsi_vbt_init(intel_dsi, MIPI_DSI_GENERIC_PANEL_ID)) {
2081b5280cd0SWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "no device found\n");
2082379bc100SJani Nikula 		goto err;
2083379bc100SJani Nikula 	}
2084379bc100SJani Nikula 
2085379bc100SJani Nikula 	icl_dphy_param_init(intel_dsi);
2086f384e48dSVandita Kulkarni 
2087f384e48dSVandita Kulkarni 	icl_dsi_add_properties(intel_connector);
2088379bc100SJani Nikula 	return;
2089379bc100SJani Nikula 
2090379bc100SJani Nikula err:
2091d1613061SVivek Kasireddy 	drm_connector_cleanup(connector);
2092379bc100SJani Nikula 	drm_encoder_cleanup(&encoder->base);
2093379bc100SJani Nikula 	kfree(intel_dsi);
2094379bc100SJani Nikula 	kfree(intel_connector);
2095379bc100SJani Nikula }
2096