xref: /linux/drivers/gpu/drm/i915/display/vlv_dsi.c (revision ede9771d7f84025e64ed5cbd683c5f0e4e7f2187)
1379bc100SJani Nikula /*
2379bc100SJani Nikula  * Copyright © 2013 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  * Author: Jani Nikula <jani.nikula@intel.com>
24379bc100SJani Nikula  */
25379bc100SJani Nikula 
26379bc100SJani Nikula #include <linux/slab.h>
27379bc100SJani Nikula 
28379bc100SJani Nikula #include <drm/drm_atomic_helper.h>
29379bc100SJani Nikula #include <drm/drm_crtc.h>
30379bc100SJani Nikula #include <drm/drm_edid.h>
31379bc100SJani Nikula #include <drm/drm_mipi_dsi.h>
32379bc100SJani Nikula 
33379bc100SJani Nikula #include "i915_drv.h"
34379bc100SJani Nikula #include "intel_atomic.h"
35379bc100SJani Nikula #include "intel_connector.h"
361d455f8dSJani Nikula #include "intel_display_types.h"
37379bc100SJani Nikula #include "intel_dsi.h"
38379bc100SJani Nikula #include "intel_fifo_underrun.h"
39379bc100SJani Nikula #include "intel_panel.h"
40379bc100SJani Nikula #include "intel_sideband.h"
41379bc100SJani Nikula 
42379bc100SJani Nikula /* return pixels in terms of txbyteclkhs */
43379bc100SJani Nikula static u16 txbyteclkhs(u16 pixels, int bpp, int lane_count,
44379bc100SJani Nikula 		       u16 burst_mode_ratio)
45379bc100SJani Nikula {
46379bc100SJani Nikula 	return DIV_ROUND_UP(DIV_ROUND_UP(pixels * bpp * burst_mode_ratio,
47379bc100SJani Nikula 					 8 * 100), lane_count);
48379bc100SJani Nikula }
49379bc100SJani Nikula 
50379bc100SJani Nikula /* return pixels equvalent to txbyteclkhs */
51379bc100SJani Nikula static u16 pixels_from_txbyteclkhs(u16 clk_hs, int bpp, int lane_count,
52379bc100SJani Nikula 			u16 burst_mode_ratio)
53379bc100SJani Nikula {
54379bc100SJani Nikula 	return DIV_ROUND_UP((clk_hs * lane_count * 8 * 100),
55379bc100SJani Nikula 						(bpp * burst_mode_ratio));
56379bc100SJani Nikula }
57379bc100SJani Nikula 
58379bc100SJani Nikula enum mipi_dsi_pixel_format pixel_format_from_register_bits(u32 fmt)
59379bc100SJani Nikula {
60379bc100SJani Nikula 	/* It just so happens the VBT matches register contents. */
61379bc100SJani Nikula 	switch (fmt) {
62379bc100SJani Nikula 	case VID_MODE_FORMAT_RGB888:
63379bc100SJani Nikula 		return MIPI_DSI_FMT_RGB888;
64379bc100SJani Nikula 	case VID_MODE_FORMAT_RGB666:
65379bc100SJani Nikula 		return MIPI_DSI_FMT_RGB666;
66379bc100SJani Nikula 	case VID_MODE_FORMAT_RGB666_PACKED:
67379bc100SJani Nikula 		return MIPI_DSI_FMT_RGB666_PACKED;
68379bc100SJani Nikula 	case VID_MODE_FORMAT_RGB565:
69379bc100SJani Nikula 		return MIPI_DSI_FMT_RGB565;
70379bc100SJani Nikula 	default:
71379bc100SJani Nikula 		MISSING_CASE(fmt);
72379bc100SJani Nikula 		return MIPI_DSI_FMT_RGB666;
73379bc100SJani Nikula 	}
74379bc100SJani Nikula }
75379bc100SJani Nikula 
76379bc100SJani Nikula void vlv_dsi_wait_for_fifo_empty(struct intel_dsi *intel_dsi, enum port port)
77379bc100SJani Nikula {
78379bc100SJani Nikula 	struct drm_encoder *encoder = &intel_dsi->base.base;
79379bc100SJani Nikula 	struct drm_device *dev = encoder->dev;
80379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
81379bc100SJani Nikula 	u32 mask;
82379bc100SJani Nikula 
83379bc100SJani Nikula 	mask = LP_CTRL_FIFO_EMPTY | HS_CTRL_FIFO_EMPTY |
84379bc100SJani Nikula 		LP_DATA_FIFO_EMPTY | HS_DATA_FIFO_EMPTY;
85379bc100SJani Nikula 
864cb3b44dSDaniele Ceraolo Spurio 	if (intel_de_wait_for_set(dev_priv, MIPI_GEN_FIFO_STAT(port),
874cb3b44dSDaniele Ceraolo Spurio 				  mask, 100))
88f1f76d7aSWambui Karuga 		drm_err(&dev_priv->drm, "DPI FIFOs are not empty\n");
89379bc100SJani Nikula }
90379bc100SJani Nikula 
91379bc100SJani Nikula static void write_data(struct drm_i915_private *dev_priv,
92379bc100SJani Nikula 		       i915_reg_t reg,
93379bc100SJani Nikula 		       const u8 *data, u32 len)
94379bc100SJani Nikula {
95379bc100SJani Nikula 	u32 i, j;
96379bc100SJani Nikula 
97379bc100SJani Nikula 	for (i = 0; i < len; i += 4) {
98379bc100SJani Nikula 		u32 val = 0;
99379bc100SJani Nikula 
100379bc100SJani Nikula 		for (j = 0; j < min_t(u32, len - i, 4); j++)
101379bc100SJani Nikula 			val |= *data++ << 8 * j;
102379bc100SJani Nikula 
103992d4694SJani Nikula 		intel_de_write(dev_priv, reg, val);
104379bc100SJani Nikula 	}
105379bc100SJani Nikula }
106379bc100SJani Nikula 
107379bc100SJani Nikula static void read_data(struct drm_i915_private *dev_priv,
108379bc100SJani Nikula 		      i915_reg_t reg,
109379bc100SJani Nikula 		      u8 *data, u32 len)
110379bc100SJani Nikula {
111379bc100SJani Nikula 	u32 i, j;
112379bc100SJani Nikula 
113379bc100SJani Nikula 	for (i = 0; i < len; i += 4) {
114992d4694SJani Nikula 		u32 val = intel_de_read(dev_priv, reg);
115379bc100SJani Nikula 
116379bc100SJani Nikula 		for (j = 0; j < min_t(u32, len - i, 4); j++)
117379bc100SJani Nikula 			*data++ = val >> 8 * j;
118379bc100SJani Nikula 	}
119379bc100SJani Nikula }
120379bc100SJani Nikula 
121379bc100SJani Nikula static ssize_t intel_dsi_host_transfer(struct mipi_dsi_host *host,
122379bc100SJani Nikula 				       const struct mipi_dsi_msg *msg)
123379bc100SJani Nikula {
124379bc100SJani Nikula 	struct intel_dsi_host *intel_dsi_host = to_intel_dsi_host(host);
125379bc100SJani Nikula 	struct drm_device *dev = intel_dsi_host->intel_dsi->base.base.dev;
126379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
127379bc100SJani Nikula 	enum port port = intel_dsi_host->port;
128379bc100SJani Nikula 	struct mipi_dsi_packet packet;
129379bc100SJani Nikula 	ssize_t ret;
130379bc100SJani Nikula 	const u8 *header, *data;
131379bc100SJani Nikula 	i915_reg_t data_reg, ctrl_reg;
132379bc100SJani Nikula 	u32 data_mask, ctrl_mask;
133379bc100SJani Nikula 
134379bc100SJani Nikula 	ret = mipi_dsi_create_packet(&packet, msg);
135379bc100SJani Nikula 	if (ret < 0)
136379bc100SJani Nikula 		return ret;
137379bc100SJani Nikula 
138379bc100SJani Nikula 	header = packet.header;
139379bc100SJani Nikula 	data = packet.payload;
140379bc100SJani Nikula 
141379bc100SJani Nikula 	if (msg->flags & MIPI_DSI_MSG_USE_LPM) {
142379bc100SJani Nikula 		data_reg = MIPI_LP_GEN_DATA(port);
143379bc100SJani Nikula 		data_mask = LP_DATA_FIFO_FULL;
144379bc100SJani Nikula 		ctrl_reg = MIPI_LP_GEN_CTRL(port);
145379bc100SJani Nikula 		ctrl_mask = LP_CTRL_FIFO_FULL;
146379bc100SJani Nikula 	} else {
147379bc100SJani Nikula 		data_reg = MIPI_HS_GEN_DATA(port);
148379bc100SJani Nikula 		data_mask = HS_DATA_FIFO_FULL;
149379bc100SJani Nikula 		ctrl_reg = MIPI_HS_GEN_CTRL(port);
150379bc100SJani Nikula 		ctrl_mask = HS_CTRL_FIFO_FULL;
151379bc100SJani Nikula 	}
152379bc100SJani Nikula 
153379bc100SJani Nikula 	/* note: this is never true for reads */
154379bc100SJani Nikula 	if (packet.payload_length) {
1554cb3b44dSDaniele Ceraolo Spurio 		if (intel_de_wait_for_clear(dev_priv, MIPI_GEN_FIFO_STAT(port),
1564cb3b44dSDaniele Ceraolo Spurio 					    data_mask, 50))
157f1f76d7aSWambui Karuga 			drm_err(&dev_priv->drm,
158f1f76d7aSWambui Karuga 				"Timeout waiting for HS/LP DATA FIFO !full\n");
159379bc100SJani Nikula 
160379bc100SJani Nikula 		write_data(dev_priv, data_reg, packet.payload,
161379bc100SJani Nikula 			   packet.payload_length);
162379bc100SJani Nikula 	}
163379bc100SJani Nikula 
164379bc100SJani Nikula 	if (msg->rx_len) {
165992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_INTR_STAT(port),
166992d4694SJani Nikula 			       GEN_READ_DATA_AVAIL);
167379bc100SJani Nikula 	}
168379bc100SJani Nikula 
1694cb3b44dSDaniele Ceraolo Spurio 	if (intel_de_wait_for_clear(dev_priv, MIPI_GEN_FIFO_STAT(port),
1704cb3b44dSDaniele Ceraolo Spurio 				    ctrl_mask, 50)) {
171f1f76d7aSWambui Karuga 		drm_err(&dev_priv->drm,
172f1f76d7aSWambui Karuga 			"Timeout waiting for HS/LP CTRL FIFO !full\n");
173379bc100SJani Nikula 	}
174379bc100SJani Nikula 
175992d4694SJani Nikula 	intel_de_write(dev_priv, ctrl_reg,
176992d4694SJani Nikula 		       header[2] << 16 | header[1] << 8 | header[0]);
177379bc100SJani Nikula 
178379bc100SJani Nikula 	/* ->rx_len is set only for reads */
179379bc100SJani Nikula 	if (msg->rx_len) {
180379bc100SJani Nikula 		data_mask = GEN_READ_DATA_AVAIL;
1814cb3b44dSDaniele Ceraolo Spurio 		if (intel_de_wait_for_set(dev_priv, MIPI_INTR_STAT(port),
1824cb3b44dSDaniele Ceraolo Spurio 					  data_mask, 50))
183f1f76d7aSWambui Karuga 			drm_err(&dev_priv->drm,
184f1f76d7aSWambui Karuga 				"Timeout waiting for read data.\n");
185379bc100SJani Nikula 
186379bc100SJani Nikula 		read_data(dev_priv, data_reg, msg->rx_buf, msg->rx_len);
187379bc100SJani Nikula 	}
188379bc100SJani Nikula 
189379bc100SJani Nikula 	/* XXX: fix for reads and writes */
190379bc100SJani Nikula 	return 4 + packet.payload_length;
191379bc100SJani Nikula }
192379bc100SJani Nikula 
193379bc100SJani Nikula static int intel_dsi_host_attach(struct mipi_dsi_host *host,
194379bc100SJani Nikula 				 struct mipi_dsi_device *dsi)
195379bc100SJani Nikula {
196379bc100SJani Nikula 	return 0;
197379bc100SJani Nikula }
198379bc100SJani Nikula 
199379bc100SJani Nikula static int intel_dsi_host_detach(struct mipi_dsi_host *host,
200379bc100SJani Nikula 				 struct mipi_dsi_device *dsi)
201379bc100SJani Nikula {
202379bc100SJani Nikula 	return 0;
203379bc100SJani Nikula }
204379bc100SJani Nikula 
205379bc100SJani Nikula static const struct mipi_dsi_host_ops intel_dsi_host_ops = {
206379bc100SJani Nikula 	.attach = intel_dsi_host_attach,
207379bc100SJani Nikula 	.detach = intel_dsi_host_detach,
208379bc100SJani Nikula 	.transfer = intel_dsi_host_transfer,
209379bc100SJani Nikula };
210379bc100SJani Nikula 
211379bc100SJani Nikula /*
212379bc100SJani Nikula  * send a video mode command
213379bc100SJani Nikula  *
214379bc100SJani Nikula  * XXX: commands with data in MIPI_DPI_DATA?
215379bc100SJani Nikula  */
216379bc100SJani Nikula static int dpi_send_cmd(struct intel_dsi *intel_dsi, u32 cmd, bool hs,
217379bc100SJani Nikula 			enum port port)
218379bc100SJani Nikula {
219379bc100SJani Nikula 	struct drm_encoder *encoder = &intel_dsi->base.base;
220379bc100SJani Nikula 	struct drm_device *dev = encoder->dev;
221379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
222379bc100SJani Nikula 	u32 mask;
223379bc100SJani Nikula 
224379bc100SJani Nikula 	/* XXX: pipe, hs */
225379bc100SJani Nikula 	if (hs)
226379bc100SJani Nikula 		cmd &= ~DPI_LP_MODE;
227379bc100SJani Nikula 	else
228379bc100SJani Nikula 		cmd |= DPI_LP_MODE;
229379bc100SJani Nikula 
230379bc100SJani Nikula 	/* clear bit */
231992d4694SJani Nikula 	intel_de_write(dev_priv, MIPI_INTR_STAT(port), SPL_PKT_SENT_INTERRUPT);
232379bc100SJani Nikula 
233379bc100SJani Nikula 	/* XXX: old code skips write if control unchanged */
234992d4694SJani Nikula 	if (cmd == intel_de_read(dev_priv, MIPI_DPI_CONTROL(port)))
235f1f76d7aSWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
236f1f76d7aSWambui Karuga 			    "Same special packet %02x twice in a row.\n", cmd);
237379bc100SJani Nikula 
238992d4694SJani Nikula 	intel_de_write(dev_priv, MIPI_DPI_CONTROL(port), cmd);
239379bc100SJani Nikula 
240379bc100SJani Nikula 	mask = SPL_PKT_SENT_INTERRUPT;
2414cb3b44dSDaniele Ceraolo Spurio 	if (intel_de_wait_for_set(dev_priv, MIPI_INTR_STAT(port), mask, 100))
242f1f76d7aSWambui Karuga 		drm_err(&dev_priv->drm,
243f1f76d7aSWambui Karuga 			"Video mode command 0x%08x send failed.\n", cmd);
244379bc100SJani Nikula 
245379bc100SJani Nikula 	return 0;
246379bc100SJani Nikula }
247379bc100SJani Nikula 
248379bc100SJani Nikula static void band_gap_reset(struct drm_i915_private *dev_priv)
249379bc100SJani Nikula {
250379bc100SJani Nikula 	vlv_flisdsi_get(dev_priv);
251379bc100SJani Nikula 
252379bc100SJani Nikula 	vlv_flisdsi_write(dev_priv, 0x08, 0x0001);
253379bc100SJani Nikula 	vlv_flisdsi_write(dev_priv, 0x0F, 0x0005);
254379bc100SJani Nikula 	vlv_flisdsi_write(dev_priv, 0x0F, 0x0025);
255379bc100SJani Nikula 	udelay(150);
256379bc100SJani Nikula 	vlv_flisdsi_write(dev_priv, 0x0F, 0x0000);
257379bc100SJani Nikula 	vlv_flisdsi_write(dev_priv, 0x08, 0x0000);
258379bc100SJani Nikula 
259379bc100SJani Nikula 	vlv_flisdsi_put(dev_priv);
260379bc100SJani Nikula }
261379bc100SJani Nikula 
262379bc100SJani Nikula static int intel_dsi_compute_config(struct intel_encoder *encoder,
263379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config,
264379bc100SJani Nikula 				    struct drm_connector_state *conn_state)
265379bc100SJani Nikula {
266379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
267379bc100SJani Nikula 	struct intel_dsi *intel_dsi = container_of(encoder, struct intel_dsi,
268379bc100SJani Nikula 						   base);
269379bc100SJani Nikula 	struct intel_connector *intel_connector = intel_dsi->attached_connector;
2702225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
271379bc100SJani Nikula 	const struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
2721326a92cSMaarten Lankhorst 	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
273379bc100SJani Nikula 	int ret;
274379bc100SJani Nikula 
275f1f76d7aSWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "\n");
276379bc100SJani Nikula 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
277379bc100SJani Nikula 
278379bc100SJani Nikula 	if (fixed_mode) {
279379bc100SJani Nikula 		intel_fixed_panel_mode(fixed_mode, adjusted_mode);
280379bc100SJani Nikula 
281379bc100SJani Nikula 		if (HAS_GMCH(dev_priv))
282379bc100SJani Nikula 			intel_gmch_panel_fitting(crtc, pipe_config,
283379bc100SJani Nikula 						 conn_state->scaling_mode);
284379bc100SJani Nikula 		else
285379bc100SJani Nikula 			intel_pch_panel_fitting(crtc, pipe_config,
286379bc100SJani Nikula 						conn_state->scaling_mode);
287379bc100SJani Nikula 	}
288379bc100SJani Nikula 
289379bc100SJani Nikula 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
290379bc100SJani Nikula 		return -EINVAL;
291379bc100SJani Nikula 
292379bc100SJani Nikula 	/* DSI uses short packets for sync events, so clear mode flags for DSI */
293379bc100SJani Nikula 	adjusted_mode->flags = 0;
294379bc100SJani Nikula 
295379bc100SJani Nikula 	if (intel_dsi->pixel_format == MIPI_DSI_FMT_RGB888)
296379bc100SJani Nikula 		pipe_config->pipe_bpp = 24;
297379bc100SJani Nikula 	else
298379bc100SJani Nikula 		pipe_config->pipe_bpp = 18;
299379bc100SJani Nikula 
300379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv)) {
301379bc100SJani Nikula 		/* Enable Frame time stamp based scanline reporting */
302379bc100SJani Nikula 		adjusted_mode->private_flags |=
303379bc100SJani Nikula 			I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP;
304379bc100SJani Nikula 
305379bc100SJani Nikula 		/* Dual link goes to DSI transcoder A. */
306379bc100SJani Nikula 		if (intel_dsi->ports == BIT(PORT_C))
307379bc100SJani Nikula 			pipe_config->cpu_transcoder = TRANSCODER_DSI_C;
308379bc100SJani Nikula 		else
309379bc100SJani Nikula 			pipe_config->cpu_transcoder = TRANSCODER_DSI_A;
310379bc100SJani Nikula 
311379bc100SJani Nikula 		ret = bxt_dsi_pll_compute(encoder, pipe_config);
312379bc100SJani Nikula 		if (ret)
313379bc100SJani Nikula 			return -EINVAL;
314379bc100SJani Nikula 	} else {
315379bc100SJani Nikula 		ret = vlv_dsi_pll_compute(encoder, pipe_config);
316379bc100SJani Nikula 		if (ret)
317379bc100SJani Nikula 			return -EINVAL;
318379bc100SJani Nikula 	}
319379bc100SJani Nikula 
320379bc100SJani Nikula 	pipe_config->clock_set = true;
321379bc100SJani Nikula 
322379bc100SJani Nikula 	return 0;
323379bc100SJani Nikula }
324379bc100SJani Nikula 
325379bc100SJani Nikula static bool glk_dsi_enable_io(struct intel_encoder *encoder)
326379bc100SJani Nikula {
327379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
328b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
329379bc100SJani Nikula 	enum port port;
330379bc100SJani Nikula 	u32 tmp;
331379bc100SJani Nikula 	bool cold_boot = false;
332379bc100SJani Nikula 
333379bc100SJani Nikula 	/* Set the MIPI mode
334379bc100SJani Nikula 	 * If MIPI_Mode is off, then writing to LP_Wake bit is not reflecting.
335379bc100SJani Nikula 	 * Power ON MIPI IO first and then write into IO reset and LP wake bits
336379bc100SJani Nikula 	 */
337379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
338992d4694SJani Nikula 		tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
339992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_CTRL(port),
340992d4694SJani Nikula 			       tmp | GLK_MIPIIO_ENABLE);
341379bc100SJani Nikula 	}
342379bc100SJani Nikula 
343379bc100SJani Nikula 	/* Put the IO into reset */
344992d4694SJani Nikula 	tmp = intel_de_read(dev_priv, MIPI_CTRL(PORT_A));
345379bc100SJani Nikula 	tmp &= ~GLK_MIPIIO_RESET_RELEASED;
346992d4694SJani Nikula 	intel_de_write(dev_priv, MIPI_CTRL(PORT_A), tmp);
347379bc100SJani Nikula 
348379bc100SJani Nikula 	/* Program LP Wake */
349379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
350992d4694SJani Nikula 		tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
351992d4694SJani Nikula 		if (!(intel_de_read(dev_priv, MIPI_DEVICE_READY(port)) & DEVICE_READY))
352379bc100SJani Nikula 			tmp &= ~GLK_LP_WAKE;
353379bc100SJani Nikula 		else
354379bc100SJani Nikula 			tmp |= GLK_LP_WAKE;
355992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_CTRL(port), tmp);
356379bc100SJani Nikula 	}
357379bc100SJani Nikula 
358379bc100SJani Nikula 	/* Wait for Pwr ACK */
359379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
3604cb3b44dSDaniele Ceraolo Spurio 		if (intel_de_wait_for_set(dev_priv, MIPI_CTRL(port),
3614cb3b44dSDaniele Ceraolo Spurio 					  GLK_MIPIIO_PORT_POWERED, 20))
362f1f76d7aSWambui Karuga 			drm_err(&dev_priv->drm, "MIPIO port is powergated\n");
363379bc100SJani Nikula 	}
364379bc100SJani Nikula 
365379bc100SJani Nikula 	/* Check for cold boot scenario */
366379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
367379bc100SJani Nikula 		cold_boot |=
368992d4694SJani Nikula 			!(intel_de_read(dev_priv, MIPI_DEVICE_READY(port)) & DEVICE_READY);
369379bc100SJani Nikula 	}
370379bc100SJani Nikula 
371379bc100SJani Nikula 	return cold_boot;
372379bc100SJani Nikula }
373379bc100SJani Nikula 
374379bc100SJani Nikula static void glk_dsi_device_ready(struct intel_encoder *encoder)
375379bc100SJani Nikula {
376379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
377b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
378379bc100SJani Nikula 	enum port port;
379379bc100SJani Nikula 	u32 val;
380379bc100SJani Nikula 
381379bc100SJani Nikula 	/* Wait for MIPI PHY status bit to set */
382379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
3834cb3b44dSDaniele Ceraolo Spurio 		if (intel_de_wait_for_set(dev_priv, MIPI_CTRL(port),
3844cb3b44dSDaniele Ceraolo Spurio 					  GLK_PHY_STATUS_PORT_READY, 20))
385f1f76d7aSWambui Karuga 			drm_err(&dev_priv->drm, "PHY is not ON\n");
386379bc100SJani Nikula 	}
387379bc100SJani Nikula 
388379bc100SJani Nikula 	/* Get IO out of reset */
389992d4694SJani Nikula 	val = intel_de_read(dev_priv, MIPI_CTRL(PORT_A));
390992d4694SJani Nikula 	intel_de_write(dev_priv, MIPI_CTRL(PORT_A),
391992d4694SJani Nikula 		       val | GLK_MIPIIO_RESET_RELEASED);
392379bc100SJani Nikula 
393379bc100SJani Nikula 	/* Get IO out of Low power state*/
394379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
395992d4694SJani Nikula 		if (!(intel_de_read(dev_priv, MIPI_DEVICE_READY(port)) & DEVICE_READY)) {
396992d4694SJani Nikula 			val = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
397379bc100SJani Nikula 			val &= ~ULPS_STATE_MASK;
398379bc100SJani Nikula 			val |= DEVICE_READY;
399992d4694SJani Nikula 			intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
400379bc100SJani Nikula 			usleep_range(10, 15);
401379bc100SJani Nikula 		} else {
402379bc100SJani Nikula 			/* Enter ULPS */
403992d4694SJani Nikula 			val = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
404379bc100SJani Nikula 			val &= ~ULPS_STATE_MASK;
405379bc100SJani Nikula 			val |= (ULPS_STATE_ENTER | DEVICE_READY);
406992d4694SJani Nikula 			intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
407379bc100SJani Nikula 
408379bc100SJani Nikula 			/* Wait for ULPS active */
4094cb3b44dSDaniele Ceraolo Spurio 			if (intel_de_wait_for_clear(dev_priv, MIPI_CTRL(port),
4104cb3b44dSDaniele Ceraolo Spurio 						    GLK_ULPS_NOT_ACTIVE, 20))
411f1f76d7aSWambui Karuga 				drm_err(&dev_priv->drm, "ULPS not active\n");
412379bc100SJani Nikula 
413379bc100SJani Nikula 			/* Exit ULPS */
414992d4694SJani Nikula 			val = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
415379bc100SJani Nikula 			val &= ~ULPS_STATE_MASK;
416379bc100SJani Nikula 			val |= (ULPS_STATE_EXIT | DEVICE_READY);
417992d4694SJani Nikula 			intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
418379bc100SJani Nikula 
419379bc100SJani Nikula 			/* Enter Normal Mode */
420992d4694SJani Nikula 			val = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
421379bc100SJani Nikula 			val &= ~ULPS_STATE_MASK;
422379bc100SJani Nikula 			val |= (ULPS_STATE_NORMAL_OPERATION | DEVICE_READY);
423992d4694SJani Nikula 			intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
424379bc100SJani Nikula 
425992d4694SJani Nikula 			val = intel_de_read(dev_priv, MIPI_CTRL(port));
426379bc100SJani Nikula 			val &= ~GLK_LP_WAKE;
427992d4694SJani Nikula 			intel_de_write(dev_priv, MIPI_CTRL(port), val);
428379bc100SJani Nikula 		}
429379bc100SJani Nikula 	}
430379bc100SJani Nikula 
431379bc100SJani Nikula 	/* Wait for Stop state */
432379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
4334cb3b44dSDaniele Ceraolo Spurio 		if (intel_de_wait_for_set(dev_priv, MIPI_CTRL(port),
4344cb3b44dSDaniele Ceraolo Spurio 					  GLK_DATA_LANE_STOP_STATE, 20))
435f1f76d7aSWambui Karuga 			drm_err(&dev_priv->drm,
436f1f76d7aSWambui Karuga 				"Date lane not in STOP state\n");
437379bc100SJani Nikula 	}
438379bc100SJani Nikula 
439379bc100SJani Nikula 	/* Wait for AFE LATCH */
440379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
4414cb3b44dSDaniele Ceraolo Spurio 		if (intel_de_wait_for_set(dev_priv, BXT_MIPI_PORT_CTRL(port),
4424cb3b44dSDaniele Ceraolo Spurio 					  AFE_LATCHOUT, 20))
443f1f76d7aSWambui Karuga 			drm_err(&dev_priv->drm,
444f1f76d7aSWambui Karuga 				"D-PHY not entering LP-11 state\n");
445379bc100SJani Nikula 	}
446379bc100SJani Nikula }
447379bc100SJani Nikula 
448379bc100SJani Nikula static void bxt_dsi_device_ready(struct intel_encoder *encoder)
449379bc100SJani Nikula {
450379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
451b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
452379bc100SJani Nikula 	enum port port;
453379bc100SJani Nikula 	u32 val;
454379bc100SJani Nikula 
455f1f76d7aSWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "\n");
456379bc100SJani Nikula 
457379bc100SJani Nikula 	/* Enable MIPI PHY transparent latch */
458379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
459992d4694SJani Nikula 		val = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
460992d4694SJani Nikula 		intel_de_write(dev_priv, BXT_MIPI_PORT_CTRL(port),
461992d4694SJani Nikula 			       val | LP_OUTPUT_HOLD);
462379bc100SJani Nikula 		usleep_range(2000, 2500);
463379bc100SJani Nikula 	}
464379bc100SJani Nikula 
465379bc100SJani Nikula 	/* Clear ULPS and set device ready */
466379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
467992d4694SJani Nikula 		val = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
468379bc100SJani Nikula 		val &= ~ULPS_STATE_MASK;
469992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
470379bc100SJani Nikula 		usleep_range(2000, 2500);
471379bc100SJani Nikula 		val |= DEVICE_READY;
472992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
473379bc100SJani Nikula 	}
474379bc100SJani Nikula }
475379bc100SJani Nikula 
476379bc100SJani Nikula static void vlv_dsi_device_ready(struct intel_encoder *encoder)
477379bc100SJani Nikula {
478379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
479b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
480379bc100SJani Nikula 	enum port port;
481379bc100SJani Nikula 	u32 val;
482379bc100SJani Nikula 
483f1f76d7aSWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "\n");
484379bc100SJani Nikula 
485379bc100SJani Nikula 	vlv_flisdsi_get(dev_priv);
486379bc100SJani Nikula 	/* program rcomp for compliance, reduce from 50 ohms to 45 ohms
487379bc100SJani Nikula 	 * needed everytime after power gate */
488379bc100SJani Nikula 	vlv_flisdsi_write(dev_priv, 0x04, 0x0004);
489379bc100SJani Nikula 	vlv_flisdsi_put(dev_priv);
490379bc100SJani Nikula 
491379bc100SJani Nikula 	/* bandgap reset is needed after everytime we do power gate */
492379bc100SJani Nikula 	band_gap_reset(dev_priv);
493379bc100SJani Nikula 
494379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
495379bc100SJani Nikula 
496992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
497992d4694SJani Nikula 			       ULPS_STATE_ENTER);
498379bc100SJani Nikula 		usleep_range(2500, 3000);
499379bc100SJani Nikula 
500379bc100SJani Nikula 		/* Enable MIPI PHY transparent latch
501379bc100SJani Nikula 		 * Common bit for both MIPI Port A & MIPI Port C
502379bc100SJani Nikula 		 * No similar bit in MIPI Port C reg
503379bc100SJani Nikula 		 */
504992d4694SJani Nikula 		val = intel_de_read(dev_priv, MIPI_PORT_CTRL(PORT_A));
505992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_PORT_CTRL(PORT_A),
506992d4694SJani Nikula 			       val | LP_OUTPUT_HOLD);
507379bc100SJani Nikula 		usleep_range(1000, 1500);
508379bc100SJani Nikula 
509992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
510992d4694SJani Nikula 			       ULPS_STATE_EXIT);
511379bc100SJani Nikula 		usleep_range(2500, 3000);
512379bc100SJani Nikula 
513992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
514992d4694SJani Nikula 			       DEVICE_READY);
515379bc100SJani Nikula 		usleep_range(2500, 3000);
516379bc100SJani Nikula 	}
517379bc100SJani Nikula }
518379bc100SJani Nikula 
519379bc100SJani Nikula static void intel_dsi_device_ready(struct intel_encoder *encoder)
520379bc100SJani Nikula {
521379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
522379bc100SJani Nikula 
523379bc100SJani Nikula 	if (IS_GEMINILAKE(dev_priv))
524379bc100SJani Nikula 		glk_dsi_device_ready(encoder);
525379bc100SJani Nikula 	else if (IS_GEN9_LP(dev_priv))
526379bc100SJani Nikula 		bxt_dsi_device_ready(encoder);
527379bc100SJani Nikula 	else
528379bc100SJani Nikula 		vlv_dsi_device_ready(encoder);
529379bc100SJani Nikula }
530379bc100SJani Nikula 
531379bc100SJani Nikula static void glk_dsi_enter_low_power_mode(struct intel_encoder *encoder)
532379bc100SJani Nikula {
533379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
534b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
535379bc100SJani Nikula 	enum port port;
536379bc100SJani Nikula 	u32 val;
537379bc100SJani Nikula 
538379bc100SJani Nikula 	/* Enter ULPS */
539379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
540992d4694SJani Nikula 		val = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
541379bc100SJani Nikula 		val &= ~ULPS_STATE_MASK;
542379bc100SJani Nikula 		val |= (ULPS_STATE_ENTER | DEVICE_READY);
543992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
544379bc100SJani Nikula 	}
545379bc100SJani Nikula 
546379bc100SJani Nikula 	/* Wait for MIPI PHY status bit to unset */
547379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
5484cb3b44dSDaniele Ceraolo Spurio 		if (intel_de_wait_for_clear(dev_priv, MIPI_CTRL(port),
5494cb3b44dSDaniele Ceraolo Spurio 					    GLK_PHY_STATUS_PORT_READY, 20))
550f1f76d7aSWambui Karuga 			drm_err(&dev_priv->drm, "PHY is not turning OFF\n");
551379bc100SJani Nikula 	}
552379bc100SJani Nikula 
553379bc100SJani Nikula 	/* Wait for Pwr ACK bit to unset */
554379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
5554cb3b44dSDaniele Ceraolo Spurio 		if (intel_de_wait_for_clear(dev_priv, MIPI_CTRL(port),
5564cb3b44dSDaniele Ceraolo Spurio 					    GLK_MIPIIO_PORT_POWERED, 20))
557f1f76d7aSWambui Karuga 			drm_err(&dev_priv->drm,
558f1f76d7aSWambui Karuga 				"MIPI IO Port is not powergated\n");
559379bc100SJani Nikula 	}
560379bc100SJani Nikula }
561379bc100SJani Nikula 
562379bc100SJani Nikula static void glk_dsi_disable_mipi_io(struct intel_encoder *encoder)
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 	enum port port;
567379bc100SJani Nikula 	u32 tmp;
568379bc100SJani Nikula 
569379bc100SJani Nikula 	/* Put the IO into reset */
570992d4694SJani Nikula 	tmp = intel_de_read(dev_priv, MIPI_CTRL(PORT_A));
571379bc100SJani Nikula 	tmp &= ~GLK_MIPIIO_RESET_RELEASED;
572992d4694SJani Nikula 	intel_de_write(dev_priv, MIPI_CTRL(PORT_A), tmp);
573379bc100SJani Nikula 
574379bc100SJani Nikula 	/* Wait for MIPI PHY status bit to unset */
575379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
5764cb3b44dSDaniele Ceraolo Spurio 		if (intel_de_wait_for_clear(dev_priv, MIPI_CTRL(port),
5774cb3b44dSDaniele Ceraolo Spurio 					    GLK_PHY_STATUS_PORT_READY, 20))
578f1f76d7aSWambui Karuga 			drm_err(&dev_priv->drm, "PHY is not turning OFF\n");
579379bc100SJani Nikula 	}
580379bc100SJani Nikula 
581379bc100SJani Nikula 	/* Clear MIPI mode */
582379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
583992d4694SJani Nikula 		tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
584379bc100SJani Nikula 		tmp &= ~GLK_MIPIIO_ENABLE;
585992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_CTRL(port), tmp);
586379bc100SJani Nikula 	}
587379bc100SJani Nikula }
588379bc100SJani Nikula 
589379bc100SJani Nikula static void glk_dsi_clear_device_ready(struct intel_encoder *encoder)
590379bc100SJani Nikula {
591379bc100SJani Nikula 	glk_dsi_enter_low_power_mode(encoder);
592379bc100SJani Nikula 	glk_dsi_disable_mipi_io(encoder);
593379bc100SJani Nikula }
594379bc100SJani Nikula 
595379bc100SJani Nikula static void vlv_dsi_clear_device_ready(struct intel_encoder *encoder)
596379bc100SJani Nikula {
597379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
598b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
599379bc100SJani Nikula 	enum port port;
600379bc100SJani Nikula 
601f1f76d7aSWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "\n");
602379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
603379bc100SJani Nikula 		/* Common bit for both MIPI Port A & MIPI Port C on VLV/CHV */
604379bc100SJani Nikula 		i915_reg_t port_ctrl = IS_GEN9_LP(dev_priv) ?
605379bc100SJani Nikula 			BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(PORT_A);
606379bc100SJani Nikula 		u32 val;
607379bc100SJani Nikula 
608992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
609992d4694SJani Nikula 			       DEVICE_READY | ULPS_STATE_ENTER);
610379bc100SJani Nikula 		usleep_range(2000, 2500);
611379bc100SJani Nikula 
612992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
613992d4694SJani Nikula 			       DEVICE_READY | ULPS_STATE_EXIT);
614379bc100SJani Nikula 		usleep_range(2000, 2500);
615379bc100SJani Nikula 
616992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
617992d4694SJani Nikula 			       DEVICE_READY | ULPS_STATE_ENTER);
618379bc100SJani Nikula 		usleep_range(2000, 2500);
619379bc100SJani Nikula 
620379bc100SJani Nikula 		/*
621379bc100SJani Nikula 		 * On VLV/CHV, wait till Clock lanes are in LP-00 state for MIPI
622379bc100SJani Nikula 		 * Port A only. MIPI Port C has no similar bit for checking.
623379bc100SJani Nikula 		 */
624379bc100SJani Nikula 		if ((IS_GEN9_LP(dev_priv) || port == PORT_A) &&
6254cb3b44dSDaniele Ceraolo Spurio 		    intel_de_wait_for_clear(dev_priv, port_ctrl,
6264cb3b44dSDaniele Ceraolo Spurio 					    AFE_LATCHOUT, 30))
627f1f76d7aSWambui Karuga 			drm_err(&dev_priv->drm, "DSI LP not going Low\n");
628379bc100SJani Nikula 
629379bc100SJani Nikula 		/* Disable MIPI PHY transparent latch */
630992d4694SJani Nikula 		val = intel_de_read(dev_priv, port_ctrl);
631992d4694SJani Nikula 		intel_de_write(dev_priv, port_ctrl, val & ~LP_OUTPUT_HOLD);
632379bc100SJani Nikula 		usleep_range(1000, 1500);
633379bc100SJani Nikula 
634992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), 0x00);
635379bc100SJani Nikula 		usleep_range(2000, 2500);
636379bc100SJani Nikula 	}
637379bc100SJani Nikula }
638379bc100SJani Nikula 
639379bc100SJani Nikula static void intel_dsi_port_enable(struct intel_encoder *encoder,
640379bc100SJani Nikula 				  const struct intel_crtc_state *crtc_state)
641379bc100SJani Nikula {
642379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
6432225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
644b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
645379bc100SJani Nikula 	enum port port;
646379bc100SJani Nikula 
647379bc100SJani Nikula 	if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
648379bc100SJani Nikula 		u32 temp;
649379bc100SJani Nikula 		if (IS_GEN9_LP(dev_priv)) {
650379bc100SJani Nikula 			for_each_dsi_port(port, intel_dsi->ports) {
651992d4694SJani Nikula 				temp = intel_de_read(dev_priv,
652992d4694SJani Nikula 						     MIPI_CTRL(port));
653379bc100SJani Nikula 				temp &= ~BXT_PIXEL_OVERLAP_CNT_MASK |
654379bc100SJani Nikula 					intel_dsi->pixel_overlap <<
655379bc100SJani Nikula 					BXT_PIXEL_OVERLAP_CNT_SHIFT;
656992d4694SJani Nikula 				intel_de_write(dev_priv, MIPI_CTRL(port),
657992d4694SJani Nikula 					       temp);
658379bc100SJani Nikula 			}
659379bc100SJani Nikula 		} else {
660992d4694SJani Nikula 			temp = intel_de_read(dev_priv, VLV_CHICKEN_3);
661379bc100SJani Nikula 			temp &= ~PIXEL_OVERLAP_CNT_MASK |
662379bc100SJani Nikula 					intel_dsi->pixel_overlap <<
663379bc100SJani Nikula 					PIXEL_OVERLAP_CNT_SHIFT;
664992d4694SJani Nikula 			intel_de_write(dev_priv, VLV_CHICKEN_3, temp);
665379bc100SJani Nikula 		}
666379bc100SJani Nikula 	}
667379bc100SJani Nikula 
668379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
669379bc100SJani Nikula 		i915_reg_t port_ctrl = IS_GEN9_LP(dev_priv) ?
670379bc100SJani Nikula 			BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
671379bc100SJani Nikula 		u32 temp;
672379bc100SJani Nikula 
673992d4694SJani Nikula 		temp = intel_de_read(dev_priv, port_ctrl);
674379bc100SJani Nikula 
675379bc100SJani Nikula 		temp &= ~LANE_CONFIGURATION_MASK;
676379bc100SJani Nikula 		temp &= ~DUAL_LINK_MODE_MASK;
677379bc100SJani Nikula 
678379bc100SJani Nikula 		if (intel_dsi->ports == (BIT(PORT_A) | BIT(PORT_C))) {
679379bc100SJani Nikula 			temp |= (intel_dsi->dual_link - 1)
680379bc100SJani Nikula 						<< DUAL_LINK_MODE_SHIFT;
681379bc100SJani Nikula 			if (IS_BROXTON(dev_priv))
682379bc100SJani Nikula 				temp |= LANE_CONFIGURATION_DUAL_LINK_A;
683379bc100SJani Nikula 			else
684379bc100SJani Nikula 				temp |= crtc->pipe ?
685379bc100SJani Nikula 					LANE_CONFIGURATION_DUAL_LINK_B :
686379bc100SJani Nikula 					LANE_CONFIGURATION_DUAL_LINK_A;
687379bc100SJani Nikula 		}
688379bc100SJani Nikula 
689379bc100SJani Nikula 		if (intel_dsi->pixel_format != MIPI_DSI_FMT_RGB888)
690379bc100SJani Nikula 			temp |= DITHERING_ENABLE;
691379bc100SJani Nikula 
692379bc100SJani Nikula 		/* assert ip_tg_enable signal */
693992d4694SJani Nikula 		intel_de_write(dev_priv, port_ctrl, temp | DPI_ENABLE);
694992d4694SJani Nikula 		intel_de_posting_read(dev_priv, port_ctrl);
695379bc100SJani Nikula 	}
696379bc100SJani Nikula }
697379bc100SJani Nikula 
698379bc100SJani Nikula static void intel_dsi_port_disable(struct intel_encoder *encoder)
699379bc100SJani Nikula {
700379bc100SJani Nikula 	struct drm_device *dev = encoder->base.dev;
701379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
702b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
703379bc100SJani Nikula 	enum port port;
704379bc100SJani Nikula 
705379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
706379bc100SJani Nikula 		i915_reg_t port_ctrl = IS_GEN9_LP(dev_priv) ?
707379bc100SJani Nikula 			BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
708379bc100SJani Nikula 		u32 temp;
709379bc100SJani Nikula 
710379bc100SJani Nikula 		/* de-assert ip_tg_enable signal */
711992d4694SJani Nikula 		temp = intel_de_read(dev_priv, port_ctrl);
712992d4694SJani Nikula 		intel_de_write(dev_priv, port_ctrl, temp & ~DPI_ENABLE);
713992d4694SJani Nikula 		intel_de_posting_read(dev_priv, port_ctrl);
714379bc100SJani Nikula 	}
715379bc100SJani Nikula }
716379bc100SJani Nikula 
717379bc100SJani Nikula static void intel_dsi_prepare(struct intel_encoder *intel_encoder,
718379bc100SJani Nikula 			      const struct intel_crtc_state *pipe_config);
719379bc100SJani Nikula static void intel_dsi_unprepare(struct intel_encoder *encoder);
720379bc100SJani Nikula 
721379bc100SJani Nikula /*
722379bc100SJani Nikula  * Panel enable/disable sequences from the VBT spec.
723379bc100SJani Nikula  *
724379bc100SJani Nikula  * Note the spec has AssertReset / DeassertReset swapped from their
725379bc100SJani Nikula  * usual naming. We use the normal names to avoid confusion (so below
726379bc100SJani Nikula  * they are swapped compared to the spec).
727379bc100SJani Nikula  *
728379bc100SJani Nikula  * Steps starting with MIPI refer to VBT sequences, note that for v2
729379bc100SJani Nikula  * VBTs several steps which have a VBT in v2 are expected to be handled
730379bc100SJani Nikula  * directly by the driver, by directly driving gpios for example.
731379bc100SJani Nikula  *
732379bc100SJani Nikula  * v2 video mode seq         v3 video mode seq         command mode seq
733379bc100SJani Nikula  * - power on                - MIPIPanelPowerOn        - power on
734379bc100SJani Nikula  * - wait t1+t2                                        - wait t1+t2
735379bc100SJani Nikula  * - MIPIDeassertResetPin    - MIPIDeassertResetPin    - MIPIDeassertResetPin
736379bc100SJani Nikula  * - io lines to lp-11       - io lines to lp-11       - io lines to lp-11
737379bc100SJani Nikula  * - MIPISendInitialDcsCmds  - MIPISendInitialDcsCmds  - MIPISendInitialDcsCmds
738379bc100SJani Nikula  *                                                     - MIPITearOn
739379bc100SJani Nikula  *                                                     - MIPIDisplayOn
740379bc100SJani Nikula  * - turn on DPI             - turn on DPI             - set pipe to dsr mode
741379bc100SJani Nikula  * - MIPIDisplayOn           - MIPIDisplayOn
742379bc100SJani Nikula  * - wait t5                                           - wait t5
743379bc100SJani Nikula  * - backlight on            - MIPIBacklightOn         - backlight on
744379bc100SJani Nikula  * ...                       ...                       ... issue mem cmds ...
745379bc100SJani Nikula  * - backlight off           - MIPIBacklightOff        - backlight off
746379bc100SJani Nikula  * - wait t6                                           - wait t6
747379bc100SJani Nikula  * - MIPIDisplayOff
748379bc100SJani Nikula  * - turn off DPI            - turn off DPI            - disable pipe dsr mode
749379bc100SJani Nikula  *                                                     - MIPITearOff
750379bc100SJani Nikula  *                           - MIPIDisplayOff          - MIPIDisplayOff
751379bc100SJani Nikula  * - io lines to lp-00       - io lines to lp-00       - io lines to lp-00
752379bc100SJani Nikula  * - MIPIAssertResetPin      - MIPIAssertResetPin      - MIPIAssertResetPin
753379bc100SJani Nikula  * - wait t3                                           - wait t3
754379bc100SJani Nikula  * - power off               - MIPIPanelPowerOff       - power off
755379bc100SJani Nikula  * - wait t4                                           - wait t4
756379bc100SJani Nikula  */
757379bc100SJani Nikula 
758379bc100SJani Nikula /*
759379bc100SJani Nikula  * DSI port enable has to be done before pipe and plane enable, so we do it in
760379bc100SJani Nikula  * the pre_enable hook instead of the enable hook.
761379bc100SJani Nikula  */
762*ede9771dSVille Syrjälä static void intel_dsi_pre_enable(struct intel_atomic_state *state,
763*ede9771dSVille Syrjälä 				 struct intel_encoder *encoder,
764379bc100SJani Nikula 				 const struct intel_crtc_state *pipe_config,
765379bc100SJani Nikula 				 const struct drm_connector_state *conn_state)
766379bc100SJani Nikula {
767b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
7682225f3c6SMaarten Lankhorst 	struct drm_crtc *crtc = pipe_config->uapi.crtc;
769379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
770379bc100SJani Nikula 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
771d048a268SVille Syrjälä 	enum pipe pipe = intel_crtc->pipe;
772379bc100SJani Nikula 	enum port port;
773379bc100SJani Nikula 	u32 val;
774379bc100SJani Nikula 	bool glk_cold_boot = false;
775379bc100SJani Nikula 
776f1f76d7aSWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "\n");
777379bc100SJani Nikula 
778379bc100SJani Nikula 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
779379bc100SJani Nikula 
780379bc100SJani Nikula 	/*
781379bc100SJani Nikula 	 * The BIOS may leave the PLL in a wonky state where it doesn't
782379bc100SJani Nikula 	 * lock. It needs to be fully powered down to fix it.
783379bc100SJani Nikula 	 */
784379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv)) {
785379bc100SJani Nikula 		bxt_dsi_pll_disable(encoder);
786379bc100SJani Nikula 		bxt_dsi_pll_enable(encoder, pipe_config);
787379bc100SJani Nikula 	} else {
788379bc100SJani Nikula 		vlv_dsi_pll_disable(encoder);
789379bc100SJani Nikula 		vlv_dsi_pll_enable(encoder, pipe_config);
790379bc100SJani Nikula 	}
791379bc100SJani Nikula 
792379bc100SJani Nikula 	if (IS_BROXTON(dev_priv)) {
793379bc100SJani Nikula 		/* Add MIPI IO reset programming for modeset */
794992d4694SJani Nikula 		val = intel_de_read(dev_priv, BXT_P_CR_GT_DISP_PWRON);
795992d4694SJani Nikula 		intel_de_write(dev_priv, BXT_P_CR_GT_DISP_PWRON,
796379bc100SJani Nikula 			       val | MIPIO_RST_CTRL);
797379bc100SJani Nikula 
798379bc100SJani Nikula 		/* Power up DSI regulator */
799992d4694SJani Nikula 		intel_de_write(dev_priv, BXT_P_DSI_REGULATOR_CFG, STAP_SELECT);
800992d4694SJani Nikula 		intel_de_write(dev_priv, BXT_P_DSI_REGULATOR_TX_CTRL, 0);
801379bc100SJani Nikula 	}
802379bc100SJani Nikula 
803379bc100SJani Nikula 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
804379bc100SJani Nikula 		u32 val;
805379bc100SJani Nikula 
806379bc100SJani Nikula 		/* Disable DPOunit clock gating, can stall pipe */
807992d4694SJani Nikula 		val = intel_de_read(dev_priv, DSPCLK_GATE_D);
808379bc100SJani Nikula 		val |= DPOUNIT_CLOCK_GATE_DISABLE;
809992d4694SJani Nikula 		intel_de_write(dev_priv, DSPCLK_GATE_D, val);
810379bc100SJani Nikula 	}
811379bc100SJani Nikula 
812379bc100SJani Nikula 	if (!IS_GEMINILAKE(dev_priv))
813379bc100SJani Nikula 		intel_dsi_prepare(encoder, pipe_config);
814379bc100SJani Nikula 
815379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_ON);
816379bc100SJani Nikula 	intel_dsi_msleep(intel_dsi, intel_dsi->panel_on_delay);
817379bc100SJani Nikula 
818379bc100SJani Nikula 	/* Deassert reset */
819379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DEASSERT_RESET);
820379bc100SJani Nikula 
821379bc100SJani Nikula 	if (IS_GEMINILAKE(dev_priv)) {
822379bc100SJani Nikula 		glk_cold_boot = glk_dsi_enable_io(encoder);
823379bc100SJani Nikula 
824379bc100SJani Nikula 		/* Prepare port in cold boot(s3/s4) scenario */
825379bc100SJani Nikula 		if (glk_cold_boot)
826379bc100SJani Nikula 			intel_dsi_prepare(encoder, pipe_config);
827379bc100SJani Nikula 	}
828379bc100SJani Nikula 
829379bc100SJani Nikula 	/* Put device in ready state (LP-11) */
830379bc100SJani Nikula 	intel_dsi_device_ready(encoder);
831379bc100SJani Nikula 
832379bc100SJani Nikula 	/* Prepare port in normal boot scenario */
833379bc100SJani Nikula 	if (IS_GEMINILAKE(dev_priv) && !glk_cold_boot)
834379bc100SJani Nikula 		intel_dsi_prepare(encoder, pipe_config);
835379bc100SJani Nikula 
836379bc100SJani Nikula 	/* Send initialization commands in LP mode */
837379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_INIT_OTP);
838379bc100SJani Nikula 
839379bc100SJani Nikula 	/* Enable port in pre-enable phase itself because as per hw team
840379bc100SJani Nikula 	 * recommendation, port should be enabled befor plane & pipe */
841379bc100SJani Nikula 	if (is_cmd_mode(intel_dsi)) {
842379bc100SJani Nikula 		for_each_dsi_port(port, intel_dsi->ports)
843992d4694SJani Nikula 			intel_de_write(dev_priv,
844992d4694SJani Nikula 				       MIPI_MAX_RETURN_PKT_SIZE(port), 8 * 4);
845379bc100SJani Nikula 		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_TEAR_ON);
846379bc100SJani Nikula 		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
847379bc100SJani Nikula 	} else {
848379bc100SJani Nikula 		msleep(20); /* XXX */
849379bc100SJani Nikula 		for_each_dsi_port(port, intel_dsi->ports)
850379bc100SJani Nikula 			dpi_send_cmd(intel_dsi, TURN_ON, false, port);
851379bc100SJani Nikula 		intel_dsi_msleep(intel_dsi, 100);
852379bc100SJani Nikula 
853379bc100SJani Nikula 		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
854379bc100SJani Nikula 
855379bc100SJani Nikula 		intel_dsi_port_enable(encoder, pipe_config);
856379bc100SJani Nikula 	}
857379bc100SJani Nikula 
858379bc100SJani Nikula 	intel_panel_enable_backlight(pipe_config, conn_state);
859379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_ON);
860379bc100SJani Nikula }
861379bc100SJani Nikula 
862*ede9771dSVille Syrjälä static void bxt_dsi_enable(struct intel_atomic_state *state,
863*ede9771dSVille Syrjälä 			   struct intel_encoder *encoder,
86421fd23acSJani Nikula 			   const struct intel_crtc_state *crtc_state,
86521fd23acSJani Nikula 			   const struct drm_connector_state *conn_state)
86621fd23acSJani Nikula {
86721fd23acSJani Nikula 	WARN_ON(crtc_state->has_pch_encoder);
86821fd23acSJani Nikula 
86921fd23acSJani Nikula 	intel_crtc_vblank_on(crtc_state);
87021fd23acSJani Nikula }
87121fd23acSJani Nikula 
872379bc100SJani Nikula /*
873379bc100SJani Nikula  * DSI port disable has to be done after pipe and plane disable, so we do it in
874379bc100SJani Nikula  * the post_disable hook.
875379bc100SJani Nikula  */
876*ede9771dSVille Syrjälä static void intel_dsi_disable(struct intel_atomic_state *state,
877*ede9771dSVille Syrjälä 			      struct intel_encoder *encoder,
878379bc100SJani Nikula 			      const struct intel_crtc_state *old_crtc_state,
879379bc100SJani Nikula 			      const struct drm_connector_state *old_conn_state)
880379bc100SJani Nikula {
881dd10a80fSJani Nikula 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
882b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
883379bc100SJani Nikula 	enum port port;
884379bc100SJani Nikula 
885dd10a80fSJani Nikula 	drm_dbg_kms(&i915->drm, "\n");
886379bc100SJani Nikula 
887379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_OFF);
888379bc100SJani Nikula 	intel_panel_disable_backlight(old_conn_state);
889379bc100SJani Nikula 
890379bc100SJani Nikula 	/*
891379bc100SJani Nikula 	 * According to the spec we should send SHUTDOWN before
892379bc100SJani Nikula 	 * MIPI_SEQ_DISPLAY_OFF only for v3+ VBTs, but field testing
893379bc100SJani Nikula 	 * has shown that the v3 sequence works for v2 VBTs too
894379bc100SJani Nikula 	 */
895379bc100SJani Nikula 	if (is_vid_mode(intel_dsi)) {
896379bc100SJani Nikula 		/* Send Shutdown command to the panel in LP mode */
897379bc100SJani Nikula 		for_each_dsi_port(port, intel_dsi->ports)
898379bc100SJani Nikula 			dpi_send_cmd(intel_dsi, SHUTDOWN, false, port);
899379bc100SJani Nikula 		msleep(10);
900379bc100SJani Nikula 	}
901379bc100SJani Nikula }
902379bc100SJani Nikula 
903379bc100SJani Nikula static void intel_dsi_clear_device_ready(struct intel_encoder *encoder)
904379bc100SJani Nikula {
905379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
906379bc100SJani Nikula 
907379bc100SJani Nikula 	if (IS_GEMINILAKE(dev_priv))
908379bc100SJani Nikula 		glk_dsi_clear_device_ready(encoder);
909379bc100SJani Nikula 	else
910379bc100SJani Nikula 		vlv_dsi_clear_device_ready(encoder);
911379bc100SJani Nikula }
912379bc100SJani Nikula 
913*ede9771dSVille Syrjälä static void intel_dsi_post_disable(struct intel_atomic_state *state,
914*ede9771dSVille Syrjälä 				   struct intel_encoder *encoder,
915773b4b54SVille Syrjälä 				   const struct intel_crtc_state *old_crtc_state,
916773b4b54SVille Syrjälä 				   const struct drm_connector_state *old_conn_state)
917379bc100SJani Nikula {
918379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
919b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
920379bc100SJani Nikula 	enum port port;
921379bc100SJani Nikula 	u32 val;
922379bc100SJani Nikula 
923f1f76d7aSWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "\n");
924379bc100SJani Nikula 
925773b4b54SVille Syrjälä 	if (IS_GEN9_LP(dev_priv)) {
926773b4b54SVille Syrjälä 		intel_crtc_vblank_off(old_crtc_state);
927773b4b54SVille Syrjälä 
928f6df4d46SLucas De Marchi 		skl_scaler_disable(old_crtc_state);
929773b4b54SVille Syrjälä 	}
930773b4b54SVille Syrjälä 
931379bc100SJani Nikula 	if (is_vid_mode(intel_dsi)) {
932379bc100SJani Nikula 		for_each_dsi_port(port, intel_dsi->ports)
933379bc100SJani Nikula 			vlv_dsi_wait_for_fifo_empty(intel_dsi, port);
934379bc100SJani Nikula 
935379bc100SJani Nikula 		intel_dsi_port_disable(encoder);
936379bc100SJani Nikula 		usleep_range(2000, 5000);
937379bc100SJani Nikula 	}
938379bc100SJani Nikula 
939379bc100SJani Nikula 	intel_dsi_unprepare(encoder);
940379bc100SJani Nikula 
941379bc100SJani Nikula 	/*
942379bc100SJani Nikula 	 * if disable packets are sent before sending shutdown packet then in
943379bc100SJani Nikula 	 * some next enable sequence send turn on packet error is observed
944379bc100SJani Nikula 	 */
945379bc100SJani Nikula 	if (is_cmd_mode(intel_dsi))
946379bc100SJani Nikula 		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_TEAR_OFF);
947379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_OFF);
948379bc100SJani Nikula 
949379bc100SJani Nikula 	/* Transition to LP-00 */
950379bc100SJani Nikula 	intel_dsi_clear_device_ready(encoder);
951379bc100SJani Nikula 
952379bc100SJani Nikula 	if (IS_BROXTON(dev_priv)) {
953379bc100SJani Nikula 		/* Power down DSI regulator to save power */
954992d4694SJani Nikula 		intel_de_write(dev_priv, BXT_P_DSI_REGULATOR_CFG, STAP_SELECT);
955992d4694SJani Nikula 		intel_de_write(dev_priv, BXT_P_DSI_REGULATOR_TX_CTRL,
956992d4694SJani Nikula 			       HS_IO_CTRL_SELECT);
957379bc100SJani Nikula 
958379bc100SJani Nikula 		/* Add MIPI IO reset programming for modeset */
959992d4694SJani Nikula 		val = intel_de_read(dev_priv, BXT_P_CR_GT_DISP_PWRON);
960992d4694SJani Nikula 		intel_de_write(dev_priv, BXT_P_CR_GT_DISP_PWRON,
961379bc100SJani Nikula 			       val & ~MIPIO_RST_CTRL);
962379bc100SJani Nikula 	}
963379bc100SJani Nikula 
964379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv)) {
965379bc100SJani Nikula 		bxt_dsi_pll_disable(encoder);
966379bc100SJani Nikula 	} else {
967379bc100SJani Nikula 		u32 val;
968379bc100SJani Nikula 
969379bc100SJani Nikula 		vlv_dsi_pll_disable(encoder);
970379bc100SJani Nikula 
971992d4694SJani Nikula 		val = intel_de_read(dev_priv, DSPCLK_GATE_D);
972379bc100SJani Nikula 		val &= ~DPOUNIT_CLOCK_GATE_DISABLE;
973992d4694SJani Nikula 		intel_de_write(dev_priv, DSPCLK_GATE_D, val);
974379bc100SJani Nikula 	}
975379bc100SJani Nikula 
976379bc100SJani Nikula 	/* Assert reset */
977379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_ASSERT_RESET);
978379bc100SJani Nikula 
979379bc100SJani Nikula 	intel_dsi_msleep(intel_dsi, intel_dsi->panel_off_delay);
980379bc100SJani Nikula 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_OFF);
981379bc100SJani Nikula 
982379bc100SJani Nikula 	/*
983379bc100SJani Nikula 	 * FIXME As we do with eDP, just make a note of the time here
984379bc100SJani Nikula 	 * and perform the wait before the next panel power on.
985379bc100SJani Nikula 	 */
986379bc100SJani Nikula 	intel_dsi_msleep(intel_dsi, intel_dsi->panel_pwr_cycle_delay);
987379bc100SJani Nikula }
988379bc100SJani Nikula 
989379bc100SJani Nikula static bool intel_dsi_get_hw_state(struct intel_encoder *encoder,
990379bc100SJani Nikula 				   enum pipe *pipe)
991379bc100SJani Nikula {
992379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
993b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
994379bc100SJani Nikula 	intel_wakeref_t wakeref;
995379bc100SJani Nikula 	enum port port;
996379bc100SJani Nikula 	bool active = false;
997379bc100SJani Nikula 
998f1f76d7aSWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "\n");
999379bc100SJani Nikula 
1000379bc100SJani Nikula 	wakeref = intel_display_power_get_if_enabled(dev_priv,
1001379bc100SJani Nikula 						     encoder->power_domain);
1002379bc100SJani Nikula 	if (!wakeref)
1003379bc100SJani Nikula 		return false;
1004379bc100SJani Nikula 
1005379bc100SJani Nikula 	/*
1006379bc100SJani Nikula 	 * On Broxton the PLL needs to be enabled with a valid divider
1007379bc100SJani Nikula 	 * configuration, otherwise accessing DSI registers will hang the
1008379bc100SJani Nikula 	 * machine. See BSpec North Display Engine registers/MIPI[BXT].
1009379bc100SJani Nikula 	 */
1010379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv) && !bxt_dsi_pll_is_enabled(dev_priv))
1011379bc100SJani Nikula 		goto out_put_power;
1012379bc100SJani Nikula 
1013379bc100SJani Nikula 	/* XXX: this only works for one DSI output */
1014379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1015379bc100SJani Nikula 		i915_reg_t ctrl_reg = IS_GEN9_LP(dev_priv) ?
1016379bc100SJani Nikula 			BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
1017992d4694SJani Nikula 		bool enabled = intel_de_read(dev_priv, ctrl_reg) & DPI_ENABLE;
1018379bc100SJani Nikula 
1019379bc100SJani Nikula 		/*
1020379bc100SJani Nikula 		 * Due to some hardware limitations on VLV/CHV, the DPI enable
1021379bc100SJani Nikula 		 * bit in port C control register does not get set. As a
1022379bc100SJani Nikula 		 * workaround, check pipe B conf instead.
1023379bc100SJani Nikula 		 */
1024379bc100SJani Nikula 		if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
1025379bc100SJani Nikula 		    port == PORT_C)
1026992d4694SJani Nikula 			enabled = intel_de_read(dev_priv, PIPECONF(PIPE_B)) & PIPECONF_ENABLE;
1027379bc100SJani Nikula 
1028379bc100SJani Nikula 		/* Try command mode if video mode not enabled */
1029379bc100SJani Nikula 		if (!enabled) {
1030992d4694SJani Nikula 			u32 tmp = intel_de_read(dev_priv,
1031992d4694SJani Nikula 						MIPI_DSI_FUNC_PRG(port));
1032379bc100SJani Nikula 			enabled = tmp & CMD_MODE_DATA_WIDTH_MASK;
1033379bc100SJani Nikula 		}
1034379bc100SJani Nikula 
1035379bc100SJani Nikula 		if (!enabled)
1036379bc100SJani Nikula 			continue;
1037379bc100SJani Nikula 
1038992d4694SJani Nikula 		if (!(intel_de_read(dev_priv, MIPI_DEVICE_READY(port)) & DEVICE_READY))
1039379bc100SJani Nikula 			continue;
1040379bc100SJani Nikula 
1041379bc100SJani Nikula 		if (IS_GEN9_LP(dev_priv)) {
1042992d4694SJani Nikula 			u32 tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
1043379bc100SJani Nikula 			tmp &= BXT_PIPE_SELECT_MASK;
1044379bc100SJani Nikula 			tmp >>= BXT_PIPE_SELECT_SHIFT;
1045379bc100SJani Nikula 
1046f4224a4cSPankaj Bharadiya 			if (drm_WARN_ON(&dev_priv->drm, tmp > PIPE_C))
1047379bc100SJani Nikula 				continue;
1048379bc100SJani Nikula 
1049379bc100SJani Nikula 			*pipe = tmp;
1050379bc100SJani Nikula 		} else {
1051379bc100SJani Nikula 			*pipe = port == PORT_A ? PIPE_A : PIPE_B;
1052379bc100SJani Nikula 		}
1053379bc100SJani Nikula 
1054379bc100SJani Nikula 		active = true;
1055379bc100SJani Nikula 		break;
1056379bc100SJani Nikula 	}
1057379bc100SJani Nikula 
1058379bc100SJani Nikula out_put_power:
1059379bc100SJani Nikula 	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
1060379bc100SJani Nikula 
1061379bc100SJani Nikula 	return active;
1062379bc100SJani Nikula }
1063379bc100SJani Nikula 
1064379bc100SJani Nikula static void bxt_dsi_get_pipe_config(struct intel_encoder *encoder,
1065379bc100SJani Nikula 				    struct intel_crtc_state *pipe_config)
1066379bc100SJani Nikula {
1067379bc100SJani Nikula 	struct drm_device *dev = encoder->base.dev;
1068379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1069379bc100SJani Nikula 	struct drm_display_mode *adjusted_mode =
10701326a92cSMaarten Lankhorst 					&pipe_config->hw.adjusted_mode;
1071379bc100SJani Nikula 	struct drm_display_mode *adjusted_mode_sw;
10722225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
1073b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1074379bc100SJani Nikula 	unsigned int lane_count = intel_dsi->lane_count;
1075379bc100SJani Nikula 	unsigned int bpp, fmt;
1076379bc100SJani Nikula 	enum port port;
1077379bc100SJani Nikula 	u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
1078379bc100SJani Nikula 	u16 hfp_sw, hsync_sw, hbp_sw;
1079379bc100SJani Nikula 	u16 crtc_htotal_sw, crtc_hsync_start_sw, crtc_hsync_end_sw,
1080379bc100SJani Nikula 				crtc_hblank_start_sw, crtc_hblank_end_sw;
1081379bc100SJani Nikula 
1082379bc100SJani Nikula 	/* FIXME: hw readout should not depend on SW state */
10831326a92cSMaarten Lankhorst 	adjusted_mode_sw = &crtc->config->hw.adjusted_mode;
1084379bc100SJani Nikula 
1085379bc100SJani Nikula 	/*
1086379bc100SJani Nikula 	 * Atleast one port is active as encoder->get_config called only if
1087379bc100SJani Nikula 	 * encoder->get_hw_state() returns true.
1088379bc100SJani Nikula 	 */
1089379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1090992d4694SJani Nikula 		if (intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port)) & DPI_ENABLE)
1091379bc100SJani Nikula 			break;
1092379bc100SJani Nikula 	}
1093379bc100SJani Nikula 
1094992d4694SJani Nikula 	fmt = intel_de_read(dev_priv, MIPI_DSI_FUNC_PRG(port)) & VID_MODE_FORMAT_MASK;
1095379bc100SJani Nikula 	bpp = mipi_dsi_pixel_format_to_bpp(
1096379bc100SJani Nikula 			pixel_format_from_register_bits(fmt));
1097379bc100SJani Nikula 
1098379bc100SJani Nikula 	pipe_config->pipe_bpp = bdw_get_pipemisc_bpp(crtc);
1099379bc100SJani Nikula 
1100379bc100SJani Nikula 	/* Enable Frame time stamo based scanline reporting */
1101379bc100SJani Nikula 	adjusted_mode->private_flags |=
1102379bc100SJani Nikula 			I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP;
1103379bc100SJani Nikula 
1104379bc100SJani Nikula 	/* In terms of pixels */
1105379bc100SJani Nikula 	adjusted_mode->crtc_hdisplay =
1106992d4694SJani Nikula 				intel_de_read(dev_priv,
1107992d4694SJani Nikula 				              BXT_MIPI_TRANS_HACTIVE(port));
1108379bc100SJani Nikula 	adjusted_mode->crtc_vdisplay =
1109992d4694SJani Nikula 				intel_de_read(dev_priv,
1110992d4694SJani Nikula 				              BXT_MIPI_TRANS_VACTIVE(port));
1111379bc100SJani Nikula 	adjusted_mode->crtc_vtotal =
1112992d4694SJani Nikula 				intel_de_read(dev_priv,
1113992d4694SJani Nikula 				              BXT_MIPI_TRANS_VTOTAL(port));
1114379bc100SJani Nikula 
1115379bc100SJani Nikula 	hactive = adjusted_mode->crtc_hdisplay;
1116992d4694SJani Nikula 	hfp = intel_de_read(dev_priv, MIPI_HFP_COUNT(port));
1117379bc100SJani Nikula 
1118379bc100SJani Nikula 	/*
1119379bc100SJani Nikula 	 * Meaningful for video mode non-burst sync pulse mode only,
1120379bc100SJani Nikula 	 * can be zero for non-burst sync events and burst modes
1121379bc100SJani Nikula 	 */
1122992d4694SJani Nikula 	hsync = intel_de_read(dev_priv, MIPI_HSYNC_PADDING_COUNT(port));
1123992d4694SJani Nikula 	hbp = intel_de_read(dev_priv, MIPI_HBP_COUNT(port));
1124379bc100SJani Nikula 
1125379bc100SJani Nikula 	/* harizontal values are in terms of high speed byte clock */
1126379bc100SJani Nikula 	hfp = pixels_from_txbyteclkhs(hfp, bpp, lane_count,
1127379bc100SJani Nikula 						intel_dsi->burst_mode_ratio);
1128379bc100SJani Nikula 	hsync = pixels_from_txbyteclkhs(hsync, bpp, lane_count,
1129379bc100SJani Nikula 						intel_dsi->burst_mode_ratio);
1130379bc100SJani Nikula 	hbp = pixels_from_txbyteclkhs(hbp, bpp, lane_count,
1131379bc100SJani Nikula 						intel_dsi->burst_mode_ratio);
1132379bc100SJani Nikula 
1133379bc100SJani Nikula 	if (intel_dsi->dual_link) {
1134379bc100SJani Nikula 		hfp *= 2;
1135379bc100SJani Nikula 		hsync *= 2;
1136379bc100SJani Nikula 		hbp *= 2;
1137379bc100SJani Nikula 	}
1138379bc100SJani Nikula 
1139379bc100SJani Nikula 	/* vertical values are in terms of lines */
1140992d4694SJani Nikula 	vfp = intel_de_read(dev_priv, MIPI_VFP_COUNT(port));
1141992d4694SJani Nikula 	vsync = intel_de_read(dev_priv, MIPI_VSYNC_PADDING_COUNT(port));
1142992d4694SJani Nikula 	vbp = intel_de_read(dev_priv, MIPI_VBP_COUNT(port));
1143379bc100SJani Nikula 
1144379bc100SJani Nikula 	adjusted_mode->crtc_htotal = hactive + hfp + hsync + hbp;
1145379bc100SJani Nikula 	adjusted_mode->crtc_hsync_start = hfp + adjusted_mode->crtc_hdisplay;
1146379bc100SJani Nikula 	adjusted_mode->crtc_hsync_end = hsync + adjusted_mode->crtc_hsync_start;
1147379bc100SJani Nikula 	adjusted_mode->crtc_hblank_start = adjusted_mode->crtc_hdisplay;
1148379bc100SJani Nikula 	adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_htotal;
1149379bc100SJani Nikula 
1150379bc100SJani Nikula 	adjusted_mode->crtc_vsync_start = vfp + adjusted_mode->crtc_vdisplay;
1151379bc100SJani Nikula 	adjusted_mode->crtc_vsync_end = vsync + adjusted_mode->crtc_vsync_start;
1152379bc100SJani Nikula 	adjusted_mode->crtc_vblank_start = adjusted_mode->crtc_vdisplay;
1153379bc100SJani Nikula 	adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vtotal;
1154379bc100SJani Nikula 
1155379bc100SJani Nikula 	/*
1156379bc100SJani Nikula 	 * In BXT DSI there is no regs programmed with few horizontal timings
1157379bc100SJani Nikula 	 * in Pixels but txbyteclkhs.. So retrieval process adds some
1158379bc100SJani Nikula 	 * ROUND_UP ERRORS in the process of PIXELS<==>txbyteclkhs.
1159379bc100SJani Nikula 	 * Actually here for the given adjusted_mode, we are calculating the
1160379bc100SJani Nikula 	 * value programmed to the port and then back to the horizontal timing
1161379bc100SJani Nikula 	 * param in pixels. This is the expected value, including roundup errors
1162379bc100SJani Nikula 	 * And if that is same as retrieved value from port, then
1163379bc100SJani Nikula 	 * (HW state) adjusted_mode's horizontal timings are corrected to
1164379bc100SJani Nikula 	 * match with SW state to nullify the errors.
1165379bc100SJani Nikula 	 */
1166379bc100SJani Nikula 	/* Calculating the value programmed to the Port register */
1167379bc100SJani Nikula 	hfp_sw = adjusted_mode_sw->crtc_hsync_start -
1168379bc100SJani Nikula 					adjusted_mode_sw->crtc_hdisplay;
1169379bc100SJani Nikula 	hsync_sw = adjusted_mode_sw->crtc_hsync_end -
1170379bc100SJani Nikula 					adjusted_mode_sw->crtc_hsync_start;
1171379bc100SJani Nikula 	hbp_sw = adjusted_mode_sw->crtc_htotal -
1172379bc100SJani Nikula 					adjusted_mode_sw->crtc_hsync_end;
1173379bc100SJani Nikula 
1174379bc100SJani Nikula 	if (intel_dsi->dual_link) {
1175379bc100SJani Nikula 		hfp_sw /= 2;
1176379bc100SJani Nikula 		hsync_sw /= 2;
1177379bc100SJani Nikula 		hbp_sw /= 2;
1178379bc100SJani Nikula 	}
1179379bc100SJani Nikula 
1180379bc100SJani Nikula 	hfp_sw = txbyteclkhs(hfp_sw, bpp, lane_count,
1181379bc100SJani Nikula 						intel_dsi->burst_mode_ratio);
1182379bc100SJani Nikula 	hsync_sw = txbyteclkhs(hsync_sw, bpp, lane_count,
1183379bc100SJani Nikula 			    intel_dsi->burst_mode_ratio);
1184379bc100SJani Nikula 	hbp_sw = txbyteclkhs(hbp_sw, bpp, lane_count,
1185379bc100SJani Nikula 						intel_dsi->burst_mode_ratio);
1186379bc100SJani Nikula 
1187379bc100SJani Nikula 	/* Reverse calculating the adjusted mode parameters from port reg vals*/
1188379bc100SJani Nikula 	hfp_sw = pixels_from_txbyteclkhs(hfp_sw, bpp, lane_count,
1189379bc100SJani Nikula 						intel_dsi->burst_mode_ratio);
1190379bc100SJani Nikula 	hsync_sw = pixels_from_txbyteclkhs(hsync_sw, bpp, lane_count,
1191379bc100SJani Nikula 						intel_dsi->burst_mode_ratio);
1192379bc100SJani Nikula 	hbp_sw = pixels_from_txbyteclkhs(hbp_sw, bpp, lane_count,
1193379bc100SJani Nikula 						intel_dsi->burst_mode_ratio);
1194379bc100SJani Nikula 
1195379bc100SJani Nikula 	if (intel_dsi->dual_link) {
1196379bc100SJani Nikula 		hfp_sw *= 2;
1197379bc100SJani Nikula 		hsync_sw *= 2;
1198379bc100SJani Nikula 		hbp_sw *= 2;
1199379bc100SJani Nikula 	}
1200379bc100SJani Nikula 
1201379bc100SJani Nikula 	crtc_htotal_sw = adjusted_mode_sw->crtc_hdisplay + hfp_sw +
1202379bc100SJani Nikula 							hsync_sw + hbp_sw;
1203379bc100SJani Nikula 	crtc_hsync_start_sw = hfp_sw + adjusted_mode_sw->crtc_hdisplay;
1204379bc100SJani Nikula 	crtc_hsync_end_sw = hsync_sw + crtc_hsync_start_sw;
1205379bc100SJani Nikula 	crtc_hblank_start_sw = adjusted_mode_sw->crtc_hdisplay;
1206379bc100SJani Nikula 	crtc_hblank_end_sw = crtc_htotal_sw;
1207379bc100SJani Nikula 
1208379bc100SJani Nikula 	if (adjusted_mode->crtc_htotal == crtc_htotal_sw)
1209379bc100SJani Nikula 		adjusted_mode->crtc_htotal = adjusted_mode_sw->crtc_htotal;
1210379bc100SJani Nikula 
1211379bc100SJani Nikula 	if (adjusted_mode->crtc_hsync_start == crtc_hsync_start_sw)
1212379bc100SJani Nikula 		adjusted_mode->crtc_hsync_start =
1213379bc100SJani Nikula 					adjusted_mode_sw->crtc_hsync_start;
1214379bc100SJani Nikula 
1215379bc100SJani Nikula 	if (adjusted_mode->crtc_hsync_end == crtc_hsync_end_sw)
1216379bc100SJani Nikula 		adjusted_mode->crtc_hsync_end =
1217379bc100SJani Nikula 					adjusted_mode_sw->crtc_hsync_end;
1218379bc100SJani Nikula 
1219379bc100SJani Nikula 	if (adjusted_mode->crtc_hblank_start == crtc_hblank_start_sw)
1220379bc100SJani Nikula 		adjusted_mode->crtc_hblank_start =
1221379bc100SJani Nikula 					adjusted_mode_sw->crtc_hblank_start;
1222379bc100SJani Nikula 
1223379bc100SJani Nikula 	if (adjusted_mode->crtc_hblank_end == crtc_hblank_end_sw)
1224379bc100SJani Nikula 		adjusted_mode->crtc_hblank_end =
1225379bc100SJani Nikula 					adjusted_mode_sw->crtc_hblank_end;
1226379bc100SJani Nikula }
1227379bc100SJani Nikula 
1228379bc100SJani Nikula static void intel_dsi_get_config(struct intel_encoder *encoder,
1229379bc100SJani Nikula 				 struct intel_crtc_state *pipe_config)
1230379bc100SJani Nikula {
1231379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1232379bc100SJani Nikula 	u32 pclk;
1233f1f76d7aSWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "\n");
1234379bc100SJani Nikula 
1235379bc100SJani Nikula 	pipe_config->output_types |= BIT(INTEL_OUTPUT_DSI);
1236379bc100SJani Nikula 
1237379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv)) {
1238379bc100SJani Nikula 		bxt_dsi_get_pipe_config(encoder, pipe_config);
1239379bc100SJani Nikula 		pclk = bxt_dsi_get_pclk(encoder, pipe_config);
1240379bc100SJani Nikula 	} else {
1241379bc100SJani Nikula 		pclk = vlv_dsi_get_pclk(encoder, pipe_config);
1242379bc100SJani Nikula 	}
1243379bc100SJani Nikula 
1244379bc100SJani Nikula 	if (pclk) {
12451326a92cSMaarten Lankhorst 		pipe_config->hw.adjusted_mode.crtc_clock = pclk;
1246379bc100SJani Nikula 		pipe_config->port_clock = pclk;
1247379bc100SJani Nikula 	}
1248379bc100SJani Nikula }
1249379bc100SJani Nikula 
1250379bc100SJani Nikula /* return txclkesc cycles in terms of divider and duration in us */
1251379bc100SJani Nikula static u16 txclkesc(u32 divider, unsigned int us)
1252379bc100SJani Nikula {
1253379bc100SJani Nikula 	switch (divider) {
1254379bc100SJani Nikula 	case ESCAPE_CLOCK_DIVIDER_1:
1255379bc100SJani Nikula 	default:
1256379bc100SJani Nikula 		return 20 * us;
1257379bc100SJani Nikula 	case ESCAPE_CLOCK_DIVIDER_2:
1258379bc100SJani Nikula 		return 10 * us;
1259379bc100SJani Nikula 	case ESCAPE_CLOCK_DIVIDER_4:
1260379bc100SJani Nikula 		return 5 * us;
1261379bc100SJani Nikula 	}
1262379bc100SJani Nikula }
1263379bc100SJani Nikula 
1264379bc100SJani Nikula static void set_dsi_timings(struct drm_encoder *encoder,
1265379bc100SJani Nikula 			    const struct drm_display_mode *adjusted_mode)
1266379bc100SJani Nikula {
1267379bc100SJani Nikula 	struct drm_device *dev = encoder->dev;
1268379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1269b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(to_intel_encoder(encoder));
1270379bc100SJani Nikula 	enum port port;
1271379bc100SJani Nikula 	unsigned int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
1272379bc100SJani Nikula 	unsigned int lane_count = intel_dsi->lane_count;
1273379bc100SJani Nikula 
1274379bc100SJani Nikula 	u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
1275379bc100SJani Nikula 
1276379bc100SJani Nikula 	hactive = adjusted_mode->crtc_hdisplay;
1277379bc100SJani Nikula 	hfp = adjusted_mode->crtc_hsync_start - adjusted_mode->crtc_hdisplay;
1278379bc100SJani Nikula 	hsync = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
1279379bc100SJani Nikula 	hbp = adjusted_mode->crtc_htotal - adjusted_mode->crtc_hsync_end;
1280379bc100SJani Nikula 
1281379bc100SJani Nikula 	if (intel_dsi->dual_link) {
1282379bc100SJani Nikula 		hactive /= 2;
1283379bc100SJani Nikula 		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
1284379bc100SJani Nikula 			hactive += intel_dsi->pixel_overlap;
1285379bc100SJani Nikula 		hfp /= 2;
1286379bc100SJani Nikula 		hsync /= 2;
1287379bc100SJani Nikula 		hbp /= 2;
1288379bc100SJani Nikula 	}
1289379bc100SJani Nikula 
1290379bc100SJani Nikula 	vfp = adjusted_mode->crtc_vsync_start - adjusted_mode->crtc_vdisplay;
1291379bc100SJani Nikula 	vsync = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
1292379bc100SJani Nikula 	vbp = adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vsync_end;
1293379bc100SJani Nikula 
1294379bc100SJani Nikula 	/* horizontal values are in terms of high speed byte clock */
1295379bc100SJani Nikula 	hactive = txbyteclkhs(hactive, bpp, lane_count,
1296379bc100SJani Nikula 			      intel_dsi->burst_mode_ratio);
1297379bc100SJani Nikula 	hfp = txbyteclkhs(hfp, bpp, lane_count, intel_dsi->burst_mode_ratio);
1298379bc100SJani Nikula 	hsync = txbyteclkhs(hsync, bpp, lane_count,
1299379bc100SJani Nikula 			    intel_dsi->burst_mode_ratio);
1300379bc100SJani Nikula 	hbp = txbyteclkhs(hbp, bpp, lane_count, intel_dsi->burst_mode_ratio);
1301379bc100SJani Nikula 
1302379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1303379bc100SJani Nikula 		if (IS_GEN9_LP(dev_priv)) {
1304379bc100SJani Nikula 			/*
1305379bc100SJani Nikula 			 * Program hdisplay and vdisplay on MIPI transcoder.
1306379bc100SJani Nikula 			 * This is different from calculated hactive and
1307379bc100SJani Nikula 			 * vactive, as they are calculated per channel basis,
1308379bc100SJani Nikula 			 * whereas these values should be based on resolution.
1309379bc100SJani Nikula 			 */
1310992d4694SJani Nikula 			intel_de_write(dev_priv, BXT_MIPI_TRANS_HACTIVE(port),
1311379bc100SJani Nikula 				       adjusted_mode->crtc_hdisplay);
1312992d4694SJani Nikula 			intel_de_write(dev_priv, BXT_MIPI_TRANS_VACTIVE(port),
1313379bc100SJani Nikula 				       adjusted_mode->crtc_vdisplay);
1314992d4694SJani Nikula 			intel_de_write(dev_priv, BXT_MIPI_TRANS_VTOTAL(port),
1315379bc100SJani Nikula 				       adjusted_mode->crtc_vtotal);
1316379bc100SJani Nikula 		}
1317379bc100SJani Nikula 
1318992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_HACTIVE_AREA_COUNT(port),
1319992d4694SJani Nikula 			       hactive);
1320992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_HFP_COUNT(port), hfp);
1321379bc100SJani Nikula 
1322379bc100SJani Nikula 		/* meaningful for video mode non-burst sync pulse mode only,
1323379bc100SJani Nikula 		 * can be zero for non-burst sync events and burst modes */
1324992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_HSYNC_PADDING_COUNT(port),
1325992d4694SJani Nikula 			       hsync);
1326992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_HBP_COUNT(port), hbp);
1327379bc100SJani Nikula 
1328379bc100SJani Nikula 		/* vertical values are in terms of lines */
1329992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_VFP_COUNT(port), vfp);
1330992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_VSYNC_PADDING_COUNT(port),
1331992d4694SJani Nikula 			       vsync);
1332992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_VBP_COUNT(port), vbp);
1333379bc100SJani Nikula 	}
1334379bc100SJani Nikula }
1335379bc100SJani Nikula 
1336379bc100SJani Nikula static u32 pixel_format_to_reg(enum mipi_dsi_pixel_format fmt)
1337379bc100SJani Nikula {
1338379bc100SJani Nikula 	switch (fmt) {
1339379bc100SJani Nikula 	case MIPI_DSI_FMT_RGB888:
1340379bc100SJani Nikula 		return VID_MODE_FORMAT_RGB888;
1341379bc100SJani Nikula 	case MIPI_DSI_FMT_RGB666:
1342379bc100SJani Nikula 		return VID_MODE_FORMAT_RGB666;
1343379bc100SJani Nikula 	case MIPI_DSI_FMT_RGB666_PACKED:
1344379bc100SJani Nikula 		return VID_MODE_FORMAT_RGB666_PACKED;
1345379bc100SJani Nikula 	case MIPI_DSI_FMT_RGB565:
1346379bc100SJani Nikula 		return VID_MODE_FORMAT_RGB565;
1347379bc100SJani Nikula 	default:
1348379bc100SJani Nikula 		MISSING_CASE(fmt);
1349379bc100SJani Nikula 		return VID_MODE_FORMAT_RGB666;
1350379bc100SJani Nikula 	}
1351379bc100SJani Nikula }
1352379bc100SJani Nikula 
1353379bc100SJani Nikula static void intel_dsi_prepare(struct intel_encoder *intel_encoder,
1354379bc100SJani Nikula 			      const struct intel_crtc_state *pipe_config)
1355379bc100SJani Nikula {
1356379bc100SJani Nikula 	struct drm_encoder *encoder = &intel_encoder->base;
1357379bc100SJani Nikula 	struct drm_device *dev = encoder->dev;
1358379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
13592225f3c6SMaarten Lankhorst 	struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->uapi.crtc);
1360b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(to_intel_encoder(encoder));
13611326a92cSMaarten Lankhorst 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
1362379bc100SJani Nikula 	enum port port;
1363379bc100SJani Nikula 	unsigned int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
1364379bc100SJani Nikula 	u32 val, tmp;
1365379bc100SJani Nikula 	u16 mode_hdisplay;
1366379bc100SJani Nikula 
1367f1f76d7aSWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "pipe %c\n", pipe_name(intel_crtc->pipe));
1368379bc100SJani Nikula 
1369379bc100SJani Nikula 	mode_hdisplay = adjusted_mode->crtc_hdisplay;
1370379bc100SJani Nikula 
1371379bc100SJani Nikula 	if (intel_dsi->dual_link) {
1372379bc100SJani Nikula 		mode_hdisplay /= 2;
1373379bc100SJani Nikula 		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
1374379bc100SJani Nikula 			mode_hdisplay += intel_dsi->pixel_overlap;
1375379bc100SJani Nikula 	}
1376379bc100SJani Nikula 
1377379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1378379bc100SJani Nikula 		if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1379379bc100SJani Nikula 			/*
1380379bc100SJani Nikula 			 * escape clock divider, 20MHz, shared for A and C.
1381379bc100SJani Nikula 			 * device ready must be off when doing this! txclkesc?
1382379bc100SJani Nikula 			 */
1383992d4694SJani Nikula 			tmp = intel_de_read(dev_priv, MIPI_CTRL(PORT_A));
1384379bc100SJani Nikula 			tmp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
1385992d4694SJani Nikula 			intel_de_write(dev_priv, MIPI_CTRL(PORT_A),
1386992d4694SJani Nikula 				       tmp | ESCAPE_CLOCK_DIVIDER_1);
1387379bc100SJani Nikula 
1388379bc100SJani Nikula 			/* read request priority is per pipe */
1389992d4694SJani Nikula 			tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
1390379bc100SJani Nikula 			tmp &= ~READ_REQUEST_PRIORITY_MASK;
1391992d4694SJani Nikula 			intel_de_write(dev_priv, MIPI_CTRL(port),
1392992d4694SJani Nikula 				       tmp | READ_REQUEST_PRIORITY_HIGH);
1393379bc100SJani Nikula 		} else if (IS_GEN9_LP(dev_priv)) {
1394379bc100SJani Nikula 			enum pipe pipe = intel_crtc->pipe;
1395379bc100SJani Nikula 
1396992d4694SJani Nikula 			tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
1397379bc100SJani Nikula 			tmp &= ~BXT_PIPE_SELECT_MASK;
1398379bc100SJani Nikula 
1399379bc100SJani Nikula 			tmp |= BXT_PIPE_SELECT(pipe);
1400992d4694SJani Nikula 			intel_de_write(dev_priv, MIPI_CTRL(port), tmp);
1401379bc100SJani Nikula 		}
1402379bc100SJani Nikula 
1403379bc100SJani Nikula 		/* XXX: why here, why like this? handling in irq handler?! */
1404992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_INTR_STAT(port), 0xffffffff);
1405992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_INTR_EN(port), 0xffffffff);
1406379bc100SJani Nikula 
1407992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DPHY_PARAM(port),
1408992d4694SJani Nikula 			       intel_dsi->dphy_reg);
1409379bc100SJani Nikula 
1410992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DPI_RESOLUTION(port),
1411992d4694SJani Nikula 			       adjusted_mode->crtc_vdisplay << VERTICAL_ADDRESS_SHIFT | mode_hdisplay << HORIZONTAL_ADDRESS_SHIFT);
1412379bc100SJani Nikula 	}
1413379bc100SJani Nikula 
1414379bc100SJani Nikula 	set_dsi_timings(encoder, adjusted_mode);
1415379bc100SJani Nikula 
1416379bc100SJani Nikula 	val = intel_dsi->lane_count << DATA_LANES_PRG_REG_SHIFT;
1417379bc100SJani Nikula 	if (is_cmd_mode(intel_dsi)) {
1418379bc100SJani Nikula 		val |= intel_dsi->channel << CMD_MODE_CHANNEL_NUMBER_SHIFT;
1419379bc100SJani Nikula 		val |= CMD_MODE_DATA_WIDTH_8_BIT; /* XXX */
1420379bc100SJani Nikula 	} else {
1421379bc100SJani Nikula 		val |= intel_dsi->channel << VID_MODE_CHANNEL_NUMBER_SHIFT;
1422379bc100SJani Nikula 		val |= pixel_format_to_reg(intel_dsi->pixel_format);
1423379bc100SJani Nikula 	}
1424379bc100SJani Nikula 
1425379bc100SJani Nikula 	tmp = 0;
1426379bc100SJani Nikula 	if (intel_dsi->eotp_pkt == 0)
1427379bc100SJani Nikula 		tmp |= EOT_DISABLE;
1428379bc100SJani Nikula 	if (intel_dsi->clock_stop)
1429379bc100SJani Nikula 		tmp |= CLOCKSTOP;
1430379bc100SJani Nikula 
1431379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv)) {
1432379bc100SJani Nikula 		tmp |= BXT_DPHY_DEFEATURE_EN;
1433379bc100SJani Nikula 		if (!is_cmd_mode(intel_dsi))
1434379bc100SJani Nikula 			tmp |= BXT_DEFEATURE_DPI_FIFO_CTR;
1435379bc100SJani Nikula 	}
1436379bc100SJani Nikula 
1437379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1438992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DSI_FUNC_PRG(port), val);
1439379bc100SJani Nikula 
1440379bc100SJani Nikula 		/* timeouts for recovery. one frame IIUC. if counter expires,
1441379bc100SJani Nikula 		 * EOT and stop state. */
1442379bc100SJani Nikula 
1443379bc100SJani Nikula 		/*
1444379bc100SJani Nikula 		 * In burst mode, value greater than one DPI line Time in byte
1445379bc100SJani Nikula 		 * clock (txbyteclkhs) To timeout this timer 1+ of the above
1446379bc100SJani Nikula 		 * said value is recommended.
1447379bc100SJani Nikula 		 *
1448379bc100SJani Nikula 		 * In non-burst mode, Value greater than one DPI frame time in
1449379bc100SJani Nikula 		 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
1450379bc100SJani Nikula 		 * said value is recommended.
1451379bc100SJani Nikula 		 *
1452379bc100SJani Nikula 		 * In DBI only mode, value greater than one DBI frame time in
1453379bc100SJani Nikula 		 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
1454379bc100SJani Nikula 		 * said value is recommended.
1455379bc100SJani Nikula 		 */
1456379bc100SJani Nikula 
1457379bc100SJani Nikula 		if (is_vid_mode(intel_dsi) &&
1458379bc100SJani Nikula 			intel_dsi->video_mode_format == VIDEO_MODE_BURST) {
1459992d4694SJani Nikula 			intel_de_write(dev_priv, MIPI_HS_TX_TIMEOUT(port),
1460992d4694SJani Nikula 				       txbyteclkhs(adjusted_mode->crtc_htotal, bpp, intel_dsi->lane_count, intel_dsi->burst_mode_ratio) + 1);
1461379bc100SJani Nikula 		} else {
1462992d4694SJani Nikula 			intel_de_write(dev_priv, MIPI_HS_TX_TIMEOUT(port),
1463992d4694SJani Nikula 				       txbyteclkhs(adjusted_mode->crtc_vtotal * adjusted_mode->crtc_htotal, bpp, intel_dsi->lane_count, intel_dsi->burst_mode_ratio) + 1);
1464379bc100SJani Nikula 		}
1465992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_LP_RX_TIMEOUT(port),
1466992d4694SJani Nikula 			       intel_dsi->lp_rx_timeout);
1467992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_TURN_AROUND_TIMEOUT(port),
1468379bc100SJani Nikula 			       intel_dsi->turn_arnd_val);
1469992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DEVICE_RESET_TIMER(port),
1470379bc100SJani Nikula 			       intel_dsi->rst_timer_val);
1471379bc100SJani Nikula 
1472379bc100SJani Nikula 		/* dphy stuff */
1473379bc100SJani Nikula 
1474379bc100SJani Nikula 		/* in terms of low power clock */
1475992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_INIT_COUNT(port),
1476379bc100SJani Nikula 			       txclkesc(intel_dsi->escape_clk_div, 100));
1477379bc100SJani Nikula 
1478379bc100SJani Nikula 		if (IS_GEN9_LP(dev_priv) && (!intel_dsi->dual_link)) {
1479379bc100SJani Nikula 			/*
1480379bc100SJani Nikula 			 * BXT spec says write MIPI_INIT_COUNT for
1481379bc100SJani Nikula 			 * both the ports, even if only one is
1482379bc100SJani Nikula 			 * getting used. So write the other port
1483379bc100SJani Nikula 			 * if not in dual link mode.
1484379bc100SJani Nikula 			 */
1485992d4694SJani Nikula 			intel_de_write(dev_priv,
1486992d4694SJani Nikula 				       MIPI_INIT_COUNT(port == PORT_A ? PORT_C : PORT_A),
1487379bc100SJani Nikula 				       intel_dsi->init_count);
1488379bc100SJani Nikula 		}
1489379bc100SJani Nikula 
1490379bc100SJani Nikula 		/* recovery disables */
1491992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_EOT_DISABLE(port), tmp);
1492379bc100SJani Nikula 
1493379bc100SJani Nikula 		/* in terms of low power clock */
1494992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_INIT_COUNT(port),
1495992d4694SJani Nikula 			       intel_dsi->init_count);
1496379bc100SJani Nikula 
1497379bc100SJani Nikula 		/* in terms of txbyteclkhs. actual high to low switch +
1498379bc100SJani Nikula 		 * MIPI_STOP_STATE_STALL * MIPI_LP_BYTECLK.
1499379bc100SJani Nikula 		 *
1500379bc100SJani Nikula 		 * XXX: write MIPI_STOP_STATE_STALL?
1501379bc100SJani Nikula 		 */
1502992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_HIGH_LOW_SWITCH_COUNT(port),
1503379bc100SJani Nikula 			       intel_dsi->hs_to_lp_count);
1504379bc100SJani Nikula 
1505379bc100SJani Nikula 		/* XXX: low power clock equivalence in terms of byte clock.
1506379bc100SJani Nikula 		 * the number of byte clocks occupied in one low power clock.
1507379bc100SJani Nikula 		 * based on txbyteclkhs and txclkesc.
1508379bc100SJani Nikula 		 * txclkesc time / txbyteclk time * (105 + MIPI_STOP_STATE_STALL
1509379bc100SJani Nikula 		 * ) / 105.???
1510379bc100SJani Nikula 		 */
1511992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_LP_BYTECLK(port),
1512992d4694SJani Nikula 			       intel_dsi->lp_byte_clk);
1513379bc100SJani Nikula 
1514379bc100SJani Nikula 		if (IS_GEMINILAKE(dev_priv)) {
1515992d4694SJani Nikula 			intel_de_write(dev_priv, MIPI_TLPX_TIME_COUNT(port),
1516379bc100SJani Nikula 				       intel_dsi->lp_byte_clk);
1517379bc100SJani Nikula 			/* Shadow of DPHY reg */
1518992d4694SJani Nikula 			intel_de_write(dev_priv, MIPI_CLK_LANE_TIMING(port),
1519379bc100SJani Nikula 				       intel_dsi->dphy_reg);
1520379bc100SJani Nikula 		}
1521379bc100SJani Nikula 
1522379bc100SJani Nikula 		/* the bw essential for transmitting 16 long packets containing
1523379bc100SJani Nikula 		 * 252 bytes meant for dcs write memory command is programmed in
1524379bc100SJani Nikula 		 * this register in terms of byte clocks. based on dsi transfer
1525379bc100SJani Nikula 		 * rate and the number of lanes configured the time taken to
1526379bc100SJani Nikula 		 * transmit 16 long packets in a dsi stream varies. */
1527992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DBI_BW_CTRL(port),
1528992d4694SJani Nikula 			       intel_dsi->bw_timer);
1529379bc100SJani Nikula 
1530992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_CLK_LANE_SWITCH_TIME_CNT(port),
1531992d4694SJani Nikula 			       intel_dsi->clk_lp_to_hs_count << LP_HS_SSW_CNT_SHIFT | intel_dsi->clk_hs_to_lp_count << HS_LP_PWR_SW_CNT_SHIFT);
1532379bc100SJani Nikula 
1533379bc100SJani Nikula 		if (is_vid_mode(intel_dsi))
1534379bc100SJani Nikula 			/* Some panels might have resolution which is not a
1535379bc100SJani Nikula 			 * multiple of 64 like 1366 x 768. Enable RANDOM
1536379bc100SJani Nikula 			 * resolution support for such panels by default */
1537992d4694SJani Nikula 			intel_de_write(dev_priv, MIPI_VIDEO_MODE_FORMAT(port),
1538992d4694SJani Nikula 				       intel_dsi->video_frmt_cfg_bits | intel_dsi->video_mode_format | IP_TG_CONFIG | RANDOM_DPI_DISPLAY_RESOLUTION);
1539379bc100SJani Nikula 	}
1540379bc100SJani Nikula }
1541379bc100SJani Nikula 
1542379bc100SJani Nikula static void intel_dsi_unprepare(struct intel_encoder *encoder)
1543379bc100SJani Nikula {
1544379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1545b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1546379bc100SJani Nikula 	enum port port;
1547379bc100SJani Nikula 	u32 val;
1548379bc100SJani Nikula 
1549379bc100SJani Nikula 	if (IS_GEMINILAKE(dev_priv))
1550379bc100SJani Nikula 		return;
1551379bc100SJani Nikula 
1552379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1553379bc100SJani Nikula 		/* Panel commands can be sent when clock is in LP11 */
1554992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), 0x0);
1555379bc100SJani Nikula 
1556379bc100SJani Nikula 		if (IS_GEN9_LP(dev_priv))
1557379bc100SJani Nikula 			bxt_dsi_reset_clocks(encoder, port);
1558379bc100SJani Nikula 		else
1559379bc100SJani Nikula 			vlv_dsi_reset_clocks(encoder, port);
1560992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_EOT_DISABLE(port), CLOCKSTOP);
1561379bc100SJani Nikula 
1562992d4694SJani Nikula 		val = intel_de_read(dev_priv, MIPI_DSI_FUNC_PRG(port));
1563379bc100SJani Nikula 		val &= ~VID_MODE_FORMAT_MASK;
1564992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DSI_FUNC_PRG(port), val);
1565379bc100SJani Nikula 
1566992d4694SJani Nikula 		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), 0x1);
1567379bc100SJani Nikula 	}
1568379bc100SJani Nikula }
1569379bc100SJani Nikula 
1570379bc100SJani Nikula static void intel_dsi_encoder_destroy(struct drm_encoder *encoder)
1571379bc100SJani Nikula {
1572b7d02c3aSVille Syrjälä 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(to_intel_encoder(encoder));
1573379bc100SJani Nikula 
1574ea0fe672SHans de Goede 	intel_dsi_vbt_gpio_cleanup(intel_dsi);
1575379bc100SJani Nikula 	intel_encoder_destroy(encoder);
1576379bc100SJani Nikula }
1577379bc100SJani Nikula 
1578379bc100SJani Nikula static const struct drm_encoder_funcs intel_dsi_funcs = {
1579379bc100SJani Nikula 	.destroy = intel_dsi_encoder_destroy,
1580379bc100SJani Nikula };
1581379bc100SJani Nikula 
1582379bc100SJani Nikula static const struct drm_connector_helper_funcs intel_dsi_connector_helper_funcs = {
1583379bc100SJani Nikula 	.get_modes = intel_dsi_get_modes,
1584379bc100SJani Nikula 	.mode_valid = intel_dsi_mode_valid,
1585379bc100SJani Nikula 	.atomic_check = intel_digital_connector_atomic_check,
1586379bc100SJani Nikula };
1587379bc100SJani Nikula 
1588379bc100SJani Nikula static const struct drm_connector_funcs intel_dsi_connector_funcs = {
1589379bc100SJani Nikula 	.late_register = intel_connector_register,
1590379bc100SJani Nikula 	.early_unregister = intel_connector_unregister,
1591379bc100SJani Nikula 	.destroy = intel_connector_destroy,
1592379bc100SJani Nikula 	.fill_modes = drm_helper_probe_single_connector_modes,
1593379bc100SJani Nikula 	.atomic_get_property = intel_digital_connector_atomic_get_property,
1594379bc100SJani Nikula 	.atomic_set_property = intel_digital_connector_atomic_set_property,
1595379bc100SJani Nikula 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1596379bc100SJani Nikula 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
1597379bc100SJani Nikula };
1598379bc100SJani Nikula 
1599f384e48dSVandita Kulkarni static void vlv_dsi_add_properties(struct intel_connector *connector)
1600379bc100SJani Nikula {
1601379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1602379bc100SJani Nikula 
1603379bc100SJani Nikula 	if (connector->panel.fixed_mode) {
1604379bc100SJani Nikula 		u32 allowed_scalers;
1605379bc100SJani Nikula 
1606379bc100SJani Nikula 		allowed_scalers = BIT(DRM_MODE_SCALE_ASPECT) | BIT(DRM_MODE_SCALE_FULLSCREEN);
1607379bc100SJani Nikula 		if (!HAS_GMCH(dev_priv))
1608379bc100SJani Nikula 			allowed_scalers |= BIT(DRM_MODE_SCALE_CENTER);
1609379bc100SJani Nikula 
1610379bc100SJani Nikula 		drm_connector_attach_scaling_mode_property(&connector->base,
1611379bc100SJani Nikula 								allowed_scalers);
1612379bc100SJani Nikula 
1613379bc100SJani Nikula 		connector->base.state->scaling_mode = DRM_MODE_SCALE_ASPECT;
1614379bc100SJani Nikula 
161569654c63SDerek Basehore 		drm_connector_set_panel_orientation_with_quirk(
1616379bc100SJani Nikula 				&connector->base,
16171ca002adSHans de Goede 				intel_dsi_get_panel_orientation(connector),
1618379bc100SJani Nikula 				connector->panel.fixed_mode->hdisplay,
1619379bc100SJani Nikula 				connector->panel.fixed_mode->vdisplay);
1620379bc100SJani Nikula 	}
1621379bc100SJani Nikula }
1622379bc100SJani Nikula 
1623379bc100SJani Nikula #define NS_KHZ_RATIO		1000000
1624379bc100SJani Nikula 
1625379bc100SJani Nikula #define PREPARE_CNT_MAX		0x3F
1626379bc100SJani Nikula #define EXIT_ZERO_CNT_MAX	0x3F
1627379bc100SJani Nikula #define CLK_ZERO_CNT_MAX	0xFF
1628379bc100SJani Nikula #define TRAIL_CNT_MAX		0x1F
1629379bc100SJani Nikula 
1630379bc100SJani Nikula static void vlv_dphy_param_init(struct intel_dsi *intel_dsi)
1631379bc100SJani Nikula {
1632379bc100SJani Nikula 	struct drm_device *dev = intel_dsi->base.base.dev;
1633379bc100SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(dev);
1634379bc100SJani Nikula 	struct mipi_config *mipi_config = dev_priv->vbt.dsi.config;
1635379bc100SJani Nikula 	u32 tlpx_ns, extra_byte_count, tlpx_ui;
1636379bc100SJani Nikula 	u32 ui_num, ui_den;
1637379bc100SJani Nikula 	u32 prepare_cnt, exit_zero_cnt, clk_zero_cnt, trail_cnt;
1638379bc100SJani Nikula 	u32 ths_prepare_ns, tclk_trail_ns;
1639379bc100SJani Nikula 	u32 tclk_prepare_clkzero, ths_prepare_hszero;
1640379bc100SJani Nikula 	u32 lp_to_hs_switch, hs_to_lp_switch;
1641379bc100SJani Nikula 	u32 mul;
1642379bc100SJani Nikula 
1643379bc100SJani Nikula 	tlpx_ns = intel_dsi_tlpx_ns(intel_dsi);
1644379bc100SJani Nikula 
1645379bc100SJani Nikula 	switch (intel_dsi->lane_count) {
1646379bc100SJani Nikula 	case 1:
1647379bc100SJani Nikula 	case 2:
1648379bc100SJani Nikula 		extra_byte_count = 2;
1649379bc100SJani Nikula 		break;
1650379bc100SJani Nikula 	case 3:
1651379bc100SJani Nikula 		extra_byte_count = 4;
1652379bc100SJani Nikula 		break;
1653379bc100SJani Nikula 	case 4:
1654379bc100SJani Nikula 	default:
1655379bc100SJani Nikula 		extra_byte_count = 3;
1656379bc100SJani Nikula 		break;
1657379bc100SJani Nikula 	}
1658379bc100SJani Nikula 
1659379bc100SJani Nikula 	/* in Kbps */
1660379bc100SJani Nikula 	ui_num = NS_KHZ_RATIO;
1661379bc100SJani Nikula 	ui_den = intel_dsi_bitrate(intel_dsi);
1662379bc100SJani Nikula 
1663379bc100SJani Nikula 	tclk_prepare_clkzero = mipi_config->tclk_prepare_clkzero;
1664379bc100SJani Nikula 	ths_prepare_hszero = mipi_config->ths_prepare_hszero;
1665379bc100SJani Nikula 
1666379bc100SJani Nikula 	/*
1667379bc100SJani Nikula 	 * B060
1668379bc100SJani Nikula 	 * LP byte clock = TLPX/ (8UI)
1669379bc100SJani Nikula 	 */
1670379bc100SJani Nikula 	intel_dsi->lp_byte_clk = DIV_ROUND_UP(tlpx_ns * ui_den, 8 * ui_num);
1671379bc100SJani Nikula 
1672379bc100SJani Nikula 	/* DDR clock period = 2 * UI
1673379bc100SJani Nikula 	 * UI(sec) = 1/(bitrate * 10^3) (bitrate is in KHZ)
1674379bc100SJani Nikula 	 * UI(nsec) = 10^6 / bitrate
1675379bc100SJani Nikula 	 * DDR clock period (nsec) = 2 * UI = (2 * 10^6)/ bitrate
1676379bc100SJani Nikula 	 * DDR clock count  = ns_value / DDR clock period
1677379bc100SJani Nikula 	 *
1678379bc100SJani Nikula 	 * For GEMINILAKE dphy_param_reg will be programmed in terms of
1679379bc100SJani Nikula 	 * HS byte clock count for other platform in HS ddr clock count
1680379bc100SJani Nikula 	 */
1681379bc100SJani Nikula 	mul = IS_GEMINILAKE(dev_priv) ? 8 : 2;
1682379bc100SJani Nikula 	ths_prepare_ns = max(mipi_config->ths_prepare,
1683379bc100SJani Nikula 			     mipi_config->tclk_prepare);
1684379bc100SJani Nikula 
1685379bc100SJani Nikula 	/* prepare count */
1686379bc100SJani Nikula 	prepare_cnt = DIV_ROUND_UP(ths_prepare_ns * ui_den, ui_num * mul);
1687379bc100SJani Nikula 
1688379bc100SJani Nikula 	if (prepare_cnt > PREPARE_CNT_MAX) {
1689f1f76d7aSWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "prepare count too high %u\n",
1690f1f76d7aSWambui Karuga 			    prepare_cnt);
1691379bc100SJani Nikula 		prepare_cnt = PREPARE_CNT_MAX;
1692379bc100SJani Nikula 	}
1693379bc100SJani Nikula 
1694379bc100SJani Nikula 	/* exit zero count */
1695379bc100SJani Nikula 	exit_zero_cnt = DIV_ROUND_UP(
1696379bc100SJani Nikula 				(ths_prepare_hszero - ths_prepare_ns) * ui_den,
1697379bc100SJani Nikula 				ui_num * mul
1698379bc100SJani Nikula 				);
1699379bc100SJani Nikula 
1700379bc100SJani Nikula 	/*
1701379bc100SJani Nikula 	 * Exit zero is unified val ths_zero and ths_exit
1702379bc100SJani Nikula 	 * minimum value for ths_exit = 110ns
1703379bc100SJani Nikula 	 * min (exit_zero_cnt * 2) = 110/UI
1704379bc100SJani Nikula 	 * exit_zero_cnt = 55/UI
1705379bc100SJani Nikula 	 */
1706379bc100SJani Nikula 	if (exit_zero_cnt < (55 * ui_den / ui_num) && (55 * ui_den) % ui_num)
1707379bc100SJani Nikula 		exit_zero_cnt += 1;
1708379bc100SJani Nikula 
1709379bc100SJani Nikula 	if (exit_zero_cnt > EXIT_ZERO_CNT_MAX) {
1710f1f76d7aSWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "exit zero count too high %u\n",
1711f1f76d7aSWambui Karuga 			    exit_zero_cnt);
1712379bc100SJani Nikula 		exit_zero_cnt = EXIT_ZERO_CNT_MAX;
1713379bc100SJani Nikula 	}
1714379bc100SJani Nikula 
1715379bc100SJani Nikula 	/* clk zero count */
1716379bc100SJani Nikula 	clk_zero_cnt = DIV_ROUND_UP(
1717379bc100SJani Nikula 				(tclk_prepare_clkzero -	ths_prepare_ns)
1718379bc100SJani Nikula 				* ui_den, ui_num * mul);
1719379bc100SJani Nikula 
1720379bc100SJani Nikula 	if (clk_zero_cnt > CLK_ZERO_CNT_MAX) {
1721f1f76d7aSWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "clock zero count too high %u\n",
1722f1f76d7aSWambui Karuga 			    clk_zero_cnt);
1723379bc100SJani Nikula 		clk_zero_cnt = CLK_ZERO_CNT_MAX;
1724379bc100SJani Nikula 	}
1725379bc100SJani Nikula 
1726379bc100SJani Nikula 	/* trail count */
1727379bc100SJani Nikula 	tclk_trail_ns = max(mipi_config->tclk_trail, mipi_config->ths_trail);
1728379bc100SJani Nikula 	trail_cnt = DIV_ROUND_UP(tclk_trail_ns * ui_den, ui_num * mul);
1729379bc100SJani Nikula 
1730379bc100SJani Nikula 	if (trail_cnt > TRAIL_CNT_MAX) {
1731f1f76d7aSWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "trail count too high %u\n",
1732f1f76d7aSWambui Karuga 			    trail_cnt);
1733379bc100SJani Nikula 		trail_cnt = TRAIL_CNT_MAX;
1734379bc100SJani Nikula 	}
1735379bc100SJani Nikula 
1736379bc100SJani Nikula 	/* B080 */
1737379bc100SJani Nikula 	intel_dsi->dphy_reg = exit_zero_cnt << 24 | trail_cnt << 16 |
1738379bc100SJani Nikula 						clk_zero_cnt << 8 | prepare_cnt;
1739379bc100SJani Nikula 
1740379bc100SJani Nikula 	/*
1741379bc100SJani Nikula 	 * LP to HS switch count = 4TLPX + PREP_COUNT * mul + EXIT_ZERO_COUNT *
1742379bc100SJani Nikula 	 *					mul + 10UI + Extra Byte Count
1743379bc100SJani Nikula 	 *
1744379bc100SJani Nikula 	 * HS to LP switch count = THS-TRAIL + 2TLPX + Extra Byte Count
1745379bc100SJani Nikula 	 * Extra Byte Count is calculated according to number of lanes.
1746379bc100SJani Nikula 	 * High Low Switch Count is the Max of LP to HS and
1747379bc100SJani Nikula 	 * HS to LP switch count
1748379bc100SJani Nikula 	 *
1749379bc100SJani Nikula 	 */
1750379bc100SJani Nikula 	tlpx_ui = DIV_ROUND_UP(tlpx_ns * ui_den, ui_num);
1751379bc100SJani Nikula 
1752379bc100SJani Nikula 	/* B044 */
1753379bc100SJani Nikula 	/* FIXME:
1754379bc100SJani Nikula 	 * The comment above does not match with the code */
1755379bc100SJani Nikula 	lp_to_hs_switch = DIV_ROUND_UP(4 * tlpx_ui + prepare_cnt * mul +
1756379bc100SJani Nikula 						exit_zero_cnt * mul + 10, 8);
1757379bc100SJani Nikula 
1758379bc100SJani Nikula 	hs_to_lp_switch = DIV_ROUND_UP(mipi_config->ths_trail + 2 * tlpx_ui, 8);
1759379bc100SJani Nikula 
1760379bc100SJani Nikula 	intel_dsi->hs_to_lp_count = max(lp_to_hs_switch, hs_to_lp_switch);
1761379bc100SJani Nikula 	intel_dsi->hs_to_lp_count += extra_byte_count;
1762379bc100SJani Nikula 
1763379bc100SJani Nikula 	/* B088 */
1764379bc100SJani Nikula 	/* LP -> HS for clock lanes
1765379bc100SJani Nikula 	 * LP clk sync + LP11 + LP01 + tclk_prepare + tclk_zero +
1766379bc100SJani Nikula 	 *						extra byte count
1767379bc100SJani Nikula 	 * 2TPLX + 1TLPX + 1 TPLX(in ns) + prepare_cnt * 2 + clk_zero_cnt *
1768379bc100SJani Nikula 	 *					2(in UI) + extra byte count
1769379bc100SJani Nikula 	 * In byteclks = (4TLPX + prepare_cnt * 2 + clk_zero_cnt *2 (in UI)) /
1770379bc100SJani Nikula 	 *					8 + extra byte count
1771379bc100SJani Nikula 	 */
1772379bc100SJani Nikula 	intel_dsi->clk_lp_to_hs_count =
1773379bc100SJani Nikula 		DIV_ROUND_UP(
1774379bc100SJani Nikula 			4 * tlpx_ui + prepare_cnt * 2 +
1775379bc100SJani Nikula 			clk_zero_cnt * 2,
1776379bc100SJani Nikula 			8);
1777379bc100SJani Nikula 
1778379bc100SJani Nikula 	intel_dsi->clk_lp_to_hs_count += extra_byte_count;
1779379bc100SJani Nikula 
1780379bc100SJani Nikula 	/* HS->LP for Clock Lanes
1781379bc100SJani Nikula 	 * Low Power clock synchronisations + 1Tx byteclk + tclk_trail +
1782379bc100SJani Nikula 	 *						Extra byte count
1783379bc100SJani Nikula 	 * 2TLPX + 8UI + (trail_count*2)(in UI) + Extra byte count
1784379bc100SJani Nikula 	 * In byteclks = (2*TLpx(in UI) + trail_count*2 +8)(in UI)/8 +
1785379bc100SJani Nikula 	 *						Extra byte count
1786379bc100SJani Nikula 	 */
1787379bc100SJani Nikula 	intel_dsi->clk_hs_to_lp_count =
1788379bc100SJani Nikula 		DIV_ROUND_UP(2 * tlpx_ui + trail_cnt * 2 + 8,
1789379bc100SJani Nikula 			8);
1790379bc100SJani Nikula 	intel_dsi->clk_hs_to_lp_count += extra_byte_count;
1791379bc100SJani Nikula 
1792379bc100SJani Nikula 	intel_dsi_log_params(intel_dsi);
1793379bc100SJani Nikula }
1794379bc100SJani Nikula 
1795379bc100SJani Nikula void vlv_dsi_init(struct drm_i915_private *dev_priv)
1796379bc100SJani Nikula {
1797379bc100SJani Nikula 	struct drm_device *dev = &dev_priv->drm;
1798379bc100SJani Nikula 	struct intel_dsi *intel_dsi;
1799379bc100SJani Nikula 	struct intel_encoder *intel_encoder;
1800379bc100SJani Nikula 	struct drm_encoder *encoder;
1801379bc100SJani Nikula 	struct intel_connector *intel_connector;
1802379bc100SJani Nikula 	struct drm_connector *connector;
1803379bc100SJani Nikula 	struct drm_display_mode *current_mode, *fixed_mode;
1804379bc100SJani Nikula 	enum port port;
18056c0a878eSHans de Goede 	enum pipe pipe;
1806379bc100SJani Nikula 
1807f1f76d7aSWambui Karuga 	drm_dbg_kms(&dev_priv->drm, "\n");
1808379bc100SJani Nikula 
1809379bc100SJani Nikula 	/* There is no detection method for MIPI so rely on VBT */
1810379bc100SJani Nikula 	if (!intel_bios_is_dsi_present(dev_priv, &port))
1811379bc100SJani Nikula 		return;
1812379bc100SJani Nikula 
1813379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
1814379bc100SJani Nikula 		dev_priv->mipi_mmio_base = BXT_MIPI_BASE;
1815379bc100SJani Nikula 	else
1816379bc100SJani Nikula 		dev_priv->mipi_mmio_base = VLV_MIPI_BASE;
1817379bc100SJani Nikula 
1818379bc100SJani Nikula 	intel_dsi = kzalloc(sizeof(*intel_dsi), GFP_KERNEL);
1819379bc100SJani Nikula 	if (!intel_dsi)
1820379bc100SJani Nikula 		return;
1821379bc100SJani Nikula 
1822379bc100SJani Nikula 	intel_connector = intel_connector_alloc();
1823379bc100SJani Nikula 	if (!intel_connector) {
1824379bc100SJani Nikula 		kfree(intel_dsi);
1825379bc100SJani Nikula 		return;
1826379bc100SJani Nikula 	}
1827379bc100SJani Nikula 
1828379bc100SJani Nikula 	intel_encoder = &intel_dsi->base;
1829379bc100SJani Nikula 	encoder = &intel_encoder->base;
1830379bc100SJani Nikula 	intel_dsi->attached_connector = intel_connector;
1831379bc100SJani Nikula 
1832379bc100SJani Nikula 	connector = &intel_connector->base;
1833379bc100SJani Nikula 
1834379bc100SJani Nikula 	drm_encoder_init(dev, encoder, &intel_dsi_funcs, DRM_MODE_ENCODER_DSI,
1835379bc100SJani Nikula 			 "DSI %c", port_name(port));
1836379bc100SJani Nikula 
1837379bc100SJani Nikula 	intel_encoder->compute_config = intel_dsi_compute_config;
1838379bc100SJani Nikula 	intel_encoder->pre_enable = intel_dsi_pre_enable;
183921fd23acSJani Nikula 	if (IS_GEN9_LP(dev_priv))
184021fd23acSJani Nikula 		intel_encoder->enable = bxt_dsi_enable;
1841379bc100SJani Nikula 	intel_encoder->disable = intel_dsi_disable;
1842379bc100SJani Nikula 	intel_encoder->post_disable = intel_dsi_post_disable;
1843379bc100SJani Nikula 	intel_encoder->get_hw_state = intel_dsi_get_hw_state;
1844379bc100SJani Nikula 	intel_encoder->get_config = intel_dsi_get_config;
1845379bc100SJani Nikula 	intel_encoder->update_pipe = intel_panel_update_backlight;
1846379bc100SJani Nikula 
1847379bc100SJani Nikula 	intel_connector->get_hw_state = intel_connector_get_hw_state;
1848379bc100SJani Nikula 
1849379bc100SJani Nikula 	intel_encoder->port = port;
1850379bc100SJani Nikula 	intel_encoder->type = INTEL_OUTPUT_DSI;
1851379bc100SJani Nikula 	intel_encoder->power_domain = POWER_DOMAIN_PORT_DSI;
1852379bc100SJani Nikula 	intel_encoder->cloneable = 0;
1853379bc100SJani Nikula 
1854379bc100SJani Nikula 	/*
1855379bc100SJani Nikula 	 * On BYT/CHV, pipe A maps to MIPI DSI port A, pipe B maps to MIPI DSI
1856379bc100SJani Nikula 	 * port C. BXT isn't limited like this.
1857379bc100SJani Nikula 	 */
1858379bc100SJani Nikula 	if (IS_GEN9_LP(dev_priv))
185934053ee1SVille Syrjälä 		intel_encoder->pipe_mask = ~0;
1860379bc100SJani Nikula 	else if (port == PORT_A)
1861981329ceSVille Syrjälä 		intel_encoder->pipe_mask = BIT(PIPE_A);
1862379bc100SJani Nikula 	else
1863981329ceSVille Syrjälä 		intel_encoder->pipe_mask = BIT(PIPE_B);
1864379bc100SJani Nikula 
1865379bc100SJani Nikula 	if (dev_priv->vbt.dsi.config->dual_link)
1866379bc100SJani Nikula 		intel_dsi->ports = BIT(PORT_A) | BIT(PORT_C);
1867379bc100SJani Nikula 	else
1868379bc100SJani Nikula 		intel_dsi->ports = BIT(port);
1869379bc100SJani Nikula 
1870379bc100SJani Nikula 	intel_dsi->dcs_backlight_ports = dev_priv->vbt.dsi.bl_ports;
1871379bc100SJani Nikula 	intel_dsi->dcs_cabc_ports = dev_priv->vbt.dsi.cabc_ports;
1872379bc100SJani Nikula 
1873379bc100SJani Nikula 	/* Create a DSI host (and a device) for each port. */
1874379bc100SJani Nikula 	for_each_dsi_port(port, intel_dsi->ports) {
1875379bc100SJani Nikula 		struct intel_dsi_host *host;
1876379bc100SJani Nikula 
1877379bc100SJani Nikula 		host = intel_dsi_host_init(intel_dsi, &intel_dsi_host_ops,
1878379bc100SJani Nikula 					   port);
1879379bc100SJani Nikula 		if (!host)
1880379bc100SJani Nikula 			goto err;
1881379bc100SJani Nikula 
1882379bc100SJani Nikula 		intel_dsi->dsi_hosts[port] = host;
1883379bc100SJani Nikula 	}
1884379bc100SJani Nikula 
1885379bc100SJani Nikula 	if (!intel_dsi_vbt_init(intel_dsi, MIPI_DSI_GENERIC_PANEL_ID)) {
1886f1f76d7aSWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "no device found\n");
1887379bc100SJani Nikula 		goto err;
1888379bc100SJani Nikula 	}
1889379bc100SJani Nikula 
1890379bc100SJani Nikula 	/* Use clock read-back from current hw-state for fastboot */
1891379bc100SJani Nikula 	current_mode = intel_encoder_current_mode(intel_encoder);
1892379bc100SJani Nikula 	if (current_mode) {
1893f1f76d7aSWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "Calculated pclk %d GOP %d\n",
1894379bc100SJani Nikula 			    intel_dsi->pclk, current_mode->clock);
1895379bc100SJani Nikula 		if (intel_fuzzy_clock_check(intel_dsi->pclk,
1896379bc100SJani Nikula 					    current_mode->clock)) {
1897f1f76d7aSWambui Karuga 			drm_dbg_kms(&dev_priv->drm, "Using GOP pclk\n");
1898379bc100SJani Nikula 			intel_dsi->pclk = current_mode->clock;
1899379bc100SJani Nikula 		}
1900379bc100SJani Nikula 
1901379bc100SJani Nikula 		kfree(current_mode);
1902379bc100SJani Nikula 	}
1903379bc100SJani Nikula 
1904379bc100SJani Nikula 	vlv_dphy_param_init(intel_dsi);
1905379bc100SJani Nikula 
19066c0a878eSHans de Goede 	intel_dsi_vbt_gpio_init(intel_dsi,
19076c0a878eSHans de Goede 				intel_dsi_get_hw_state(intel_encoder, &pipe));
1908379bc100SJani Nikula 
1909379bc100SJani Nikula 	drm_connector_init(dev, connector, &intel_dsi_connector_funcs,
1910379bc100SJani Nikula 			   DRM_MODE_CONNECTOR_DSI);
1911379bc100SJani Nikula 
1912379bc100SJani Nikula 	drm_connector_helper_add(connector, &intel_dsi_connector_helper_funcs);
1913379bc100SJani Nikula 
1914379bc100SJani Nikula 	connector->display_info.subpixel_order = SubPixelHorizontalRGB; /*XXX*/
1915379bc100SJani Nikula 	connector->interlace_allowed = false;
1916379bc100SJani Nikula 	connector->doublescan_allowed = false;
1917379bc100SJani Nikula 
1918379bc100SJani Nikula 	intel_connector_attach_encoder(intel_connector, intel_encoder);
1919379bc100SJani Nikula 
1920379bc100SJani Nikula 	mutex_lock(&dev->mode_config.mutex);
1921379bc100SJani Nikula 	fixed_mode = intel_panel_vbt_fixed_mode(intel_connector);
1922379bc100SJani Nikula 	mutex_unlock(&dev->mode_config.mutex);
1923379bc100SJani Nikula 
1924379bc100SJani Nikula 	if (!fixed_mode) {
1925f1f76d7aSWambui Karuga 		drm_dbg_kms(&dev_priv->drm, "no fixed mode\n");
1926379bc100SJani Nikula 		goto err_cleanup_connector;
1927379bc100SJani Nikula 	}
1928379bc100SJani Nikula 
1929379bc100SJani Nikula 	intel_panel_init(&intel_connector->panel, fixed_mode, NULL);
1930379bc100SJani Nikula 	intel_panel_setup_backlight(connector, INVALID_PIPE);
1931379bc100SJani Nikula 
1932f384e48dSVandita Kulkarni 	vlv_dsi_add_properties(intel_connector);
1933379bc100SJani Nikula 
1934379bc100SJani Nikula 	return;
1935379bc100SJani Nikula 
1936379bc100SJani Nikula err_cleanup_connector:
1937379bc100SJani Nikula 	drm_connector_cleanup(&intel_connector->base);
1938379bc100SJani Nikula err:
1939379bc100SJani Nikula 	drm_encoder_cleanup(&intel_encoder->base);
1940379bc100SJani Nikula 	kfree(intel_dsi);
1941379bc100SJani Nikula 	kfree(intel_connector);
1942379bc100SJani Nikula }
1943