xref: /linux/drivers/gpu/drm/i915/display/vlv_dsi.c (revision ca7fc6a8ae28eaec8c194dc5f8db03f928b2e454)
1 /*
2  * Copyright © 2013 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Author: Jani Nikula <jani.nikula@intel.com>
24  */
25 
26 #include <linux/dmi.h>
27 #include <linux/slab.h>
28 
29 #include <drm/drm_atomic_helper.h>
30 #include <drm/drm_crtc.h>
31 #include <drm/drm_edid.h>
32 #include <drm/drm_mipi_dsi.h>
33 #include <drm/drm_print.h>
34 #include <drm/drm_probe_helper.h>
35 
36 #include "intel_atomic.h"
37 #include "intel_backlight.h"
38 #include "intel_connector.h"
39 #include "intel_crtc.h"
40 #include "intel_de.h"
41 #include "intel_display_regs.h"
42 #include "intel_display_types.h"
43 #include "intel_display_utils.h"
44 #include "intel_dsi.h"
45 #include "intel_dsi_vbt.h"
46 #include "intel_fifo_underrun.h"
47 #include "intel_panel.h"
48 #include "intel_pfit.h"
49 #include "skl_scaler.h"
50 #include "vlv_dsi.h"
51 #include "vlv_dsi_pll.h"
52 #include "vlv_dsi_regs.h"
53 #include "vlv_sideband.h"
54 
55 /* return pixels in terms of txbyteclkhs */
56 static u16 txbyteclkhs(u16 pixels, int bpp, int lane_count,
57 		       u16 burst_mode_ratio)
58 {
59 	return DIV_ROUND_UP(DIV_ROUND_UP(pixels * bpp * burst_mode_ratio,
60 					 8 * 100), lane_count);
61 }
62 
63 /* return pixels equivalent to txbyteclkhs */
64 static u16 pixels_from_txbyteclkhs(u16 clk_hs, int bpp, int lane_count,
65 			u16 burst_mode_ratio)
66 {
67 	return DIV_ROUND_UP((clk_hs * lane_count * 8 * 100),
68 						(bpp * burst_mode_ratio));
69 }
70 
71 static enum mipi_dsi_pixel_format pixel_format_from_register_bits(u32 fmt)
72 {
73 	switch (fmt) {
74 	case VID_MODE_FORMAT_RGB888:
75 		return MIPI_DSI_FMT_RGB888;
76 	case VID_MODE_FORMAT_RGB666:
77 		return MIPI_DSI_FMT_RGB666;
78 	case VID_MODE_FORMAT_RGB666_PACKED:
79 		return MIPI_DSI_FMT_RGB666_PACKED;
80 	case VID_MODE_FORMAT_RGB565:
81 		return MIPI_DSI_FMT_RGB565;
82 	default:
83 		MISSING_CASE(fmt);
84 		return MIPI_DSI_FMT_RGB666;
85 	}
86 }
87 
88 void vlv_dsi_wait_for_fifo_empty(struct intel_dsi *intel_dsi, enum port port)
89 {
90 	struct intel_display *display = to_intel_display(&intel_dsi->base);
91 	u32 mask;
92 
93 	mask = LP_CTRL_FIFO_EMPTY | HS_CTRL_FIFO_EMPTY |
94 		LP_DATA_FIFO_EMPTY | HS_DATA_FIFO_EMPTY;
95 
96 	if (intel_de_wait_for_set_ms(display, MIPI_GEN_FIFO_STAT(display, port),
97 				     mask, 100))
98 		drm_err(display->drm, "DPI FIFOs are not empty\n");
99 }
100 
101 static void write_data(struct intel_display *display,
102 		       i915_reg_t reg,
103 		       const u8 *data, u32 len)
104 {
105 	u32 i, j;
106 
107 	for (i = 0; i < len; i += 4) {
108 		u32 val = 0;
109 
110 		for (j = 0; j < min_t(u32, len - i, 4); j++)
111 			val |= *data++ << 8 * j;
112 
113 		intel_de_write(display, reg, val);
114 	}
115 }
116 
117 static void read_data(struct intel_display *display,
118 		      i915_reg_t reg,
119 		      u8 *data, u32 len)
120 {
121 	u32 i, j;
122 
123 	for (i = 0; i < len; i += 4) {
124 		u32 val = intel_de_read(display, reg);
125 
126 		for (j = 0; j < min_t(u32, len - i, 4); j++)
127 			*data++ = val >> 8 * j;
128 	}
129 }
130 
131 static ssize_t intel_dsi_host_transfer(struct mipi_dsi_host *host,
132 				       const struct mipi_dsi_msg *msg)
133 {
134 	struct intel_dsi_host *intel_dsi_host = to_intel_dsi_host(host);
135 	struct intel_dsi *intel_dsi = intel_dsi_host->intel_dsi;
136 	struct intel_display *display = to_intel_display(&intel_dsi->base);
137 	enum port port = intel_dsi_host->port;
138 	struct mipi_dsi_packet packet;
139 	ssize_t ret;
140 	const u8 *header;
141 	i915_reg_t data_reg, ctrl_reg;
142 	u32 data_mask, ctrl_mask;
143 
144 	ret = mipi_dsi_create_packet(&packet, msg);
145 	if (ret < 0)
146 		return ret;
147 
148 	header = packet.header;
149 
150 	if (msg->flags & MIPI_DSI_MSG_USE_LPM) {
151 		data_reg = MIPI_LP_GEN_DATA(display, port);
152 		data_mask = LP_DATA_FIFO_FULL;
153 		ctrl_reg = MIPI_LP_GEN_CTRL(display, port);
154 		ctrl_mask = LP_CTRL_FIFO_FULL;
155 	} else {
156 		data_reg = MIPI_HS_GEN_DATA(display, port);
157 		data_mask = HS_DATA_FIFO_FULL;
158 		ctrl_reg = MIPI_HS_GEN_CTRL(display, port);
159 		ctrl_mask = HS_CTRL_FIFO_FULL;
160 	}
161 
162 	/* note: this is never true for reads */
163 	if (packet.payload_length) {
164 		if (intel_de_wait_for_clear_ms(display, MIPI_GEN_FIFO_STAT(display, port),
165 					       data_mask, 50))
166 			drm_err(display->drm,
167 				"Timeout waiting for HS/LP DATA FIFO !full\n");
168 
169 		write_data(display, data_reg, packet.payload,
170 			   packet.payload_length);
171 	}
172 
173 	if (msg->rx_len) {
174 		intel_de_write(display, MIPI_INTR_STAT(display, port),
175 			       GEN_READ_DATA_AVAIL);
176 	}
177 
178 	if (intel_de_wait_for_clear_ms(display, MIPI_GEN_FIFO_STAT(display, port),
179 				       ctrl_mask, 50)) {
180 		drm_err(display->drm,
181 			"Timeout waiting for HS/LP CTRL FIFO !full\n");
182 	}
183 
184 	intel_de_write(display, ctrl_reg,
185 		       header[2] << 16 | header[1] << 8 | header[0]);
186 
187 	/* ->rx_len is set only for reads */
188 	if (msg->rx_len) {
189 		data_mask = GEN_READ_DATA_AVAIL;
190 		if (intel_de_wait_for_set_ms(display, MIPI_INTR_STAT(display, port),
191 					     data_mask, 50))
192 			drm_err(display->drm,
193 				"Timeout waiting for read data.\n");
194 
195 		read_data(display, data_reg, msg->rx_buf, msg->rx_len);
196 	}
197 
198 	/* XXX: fix for reads and writes */
199 	return 4 + packet.payload_length;
200 }
201 
202 static int intel_dsi_host_attach(struct mipi_dsi_host *host,
203 				 struct mipi_dsi_device *dsi)
204 {
205 	return 0;
206 }
207 
208 static int intel_dsi_host_detach(struct mipi_dsi_host *host,
209 				 struct mipi_dsi_device *dsi)
210 {
211 	return 0;
212 }
213 
214 static const struct mipi_dsi_host_ops intel_dsi_host_ops = {
215 	.attach = intel_dsi_host_attach,
216 	.detach = intel_dsi_host_detach,
217 	.transfer = intel_dsi_host_transfer,
218 };
219 
220 /*
221  * send a video mode command
222  *
223  * XXX: commands with data in MIPI_DPI_DATA?
224  */
225 static int dpi_send_cmd(struct intel_dsi *intel_dsi, u32 cmd, bool hs,
226 			enum port port)
227 {
228 	struct intel_display *display = to_intel_display(&intel_dsi->base);
229 	u32 mask;
230 
231 	/* XXX: pipe, hs */
232 	if (hs)
233 		cmd &= ~DPI_LP_MODE;
234 	else
235 		cmd |= DPI_LP_MODE;
236 
237 	/* clear bit */
238 	intel_de_write(display, MIPI_INTR_STAT(display, port), SPL_PKT_SENT_INTERRUPT);
239 
240 	/* XXX: old code skips write if control unchanged */
241 	if (cmd == intel_de_read(display, MIPI_DPI_CONTROL(display, port)))
242 		drm_dbg_kms(display->drm,
243 			    "Same special packet %02x twice in a row.\n", cmd);
244 
245 	intel_de_write(display, MIPI_DPI_CONTROL(display, port), cmd);
246 
247 	mask = SPL_PKT_SENT_INTERRUPT;
248 	if (intel_de_wait_for_set_ms(display, MIPI_INTR_STAT(display, port), mask, 100))
249 		drm_err(display->drm,
250 			"Video mode command 0x%08x send failed.\n", cmd);
251 
252 	return 0;
253 }
254 
255 static void band_gap_reset(struct intel_display *display)
256 {
257 	vlv_flisdsi_get(display->drm);
258 
259 	vlv_flisdsi_write(display->drm, 0x08, 0x0001);
260 	vlv_flisdsi_write(display->drm, 0x0F, 0x0005);
261 	vlv_flisdsi_write(display->drm, 0x0F, 0x0025);
262 	udelay(150);
263 	vlv_flisdsi_write(display->drm, 0x0F, 0x0000);
264 	vlv_flisdsi_write(display->drm, 0x08, 0x0000);
265 
266 	vlv_flisdsi_put(display->drm);
267 }
268 
269 static int intel_dsi_compute_config(struct intel_encoder *encoder,
270 				    struct intel_crtc_state *pipe_config,
271 				    struct drm_connector_state *conn_state)
272 {
273 	struct intel_display *display = to_intel_display(encoder);
274 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
275 	struct intel_connector *intel_connector = intel_dsi->attached_connector;
276 	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
277 	int ret;
278 
279 	drm_dbg_kms(display->drm, "\n");
280 	pipe_config->sink_format = INTEL_OUTPUT_FORMAT_RGB;
281 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
282 
283 	ret = intel_panel_compute_config(intel_connector, adjusted_mode);
284 	if (ret)
285 		return ret;
286 
287 	ret = intel_pfit_compute_config(pipe_config, conn_state);
288 	if (ret)
289 		return ret;
290 
291 	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
292 		return -EINVAL;
293 
294 	/* DSI uses short packets for sync events, so clear mode flags for DSI */
295 	adjusted_mode->flags = 0;
296 
297 	if (intel_dsi->pixel_format == MIPI_DSI_FMT_RGB888)
298 		pipe_config->pipe_bpp = 24;
299 	else
300 		pipe_config->pipe_bpp = 18;
301 
302 	if (display->platform.geminilake || display->platform.broxton) {
303 		/* Enable Frame time stamp based scanline reporting */
304 		pipe_config->mode_flags |=
305 			I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP;
306 
307 		/* Dual link goes to DSI transcoder A. */
308 		if (intel_dsi->ports == BIT(PORT_C))
309 			pipe_config->cpu_transcoder = TRANSCODER_DSI_C;
310 		else
311 			pipe_config->cpu_transcoder = TRANSCODER_DSI_A;
312 
313 		ret = bxt_dsi_pll_compute(encoder, pipe_config);
314 		if (ret)
315 			return -EINVAL;
316 	} else {
317 		ret = vlv_dsi_pll_compute(encoder, pipe_config);
318 		if (ret)
319 			return -EINVAL;
320 	}
321 
322 	pipe_config->clock_set = true;
323 
324 	return 0;
325 }
326 
327 static bool glk_dsi_enable_io(struct intel_encoder *encoder)
328 {
329 	struct intel_display *display = to_intel_display(encoder);
330 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
331 	enum port port;
332 	bool cold_boot = false;
333 
334 	/* Set the MIPI mode
335 	 * If MIPI_Mode is off, then writing to LP_Wake bit is not reflecting.
336 	 * Power ON MIPI IO first and then write into IO reset and LP wake bits
337 	 */
338 	for_each_dsi_port(port, intel_dsi->ports)
339 		intel_de_rmw(display, MIPI_CTRL(display, port), 0, GLK_MIPIIO_ENABLE);
340 
341 	/* Put the IO into reset */
342 	intel_de_rmw(display, MIPI_CTRL(display, PORT_A), GLK_MIPIIO_RESET_RELEASED, 0);
343 
344 	/* Program LP Wake */
345 	for_each_dsi_port(port, intel_dsi->ports) {
346 		u32 tmp = intel_de_read(display, MIPI_DEVICE_READY(display, port));
347 
348 		intel_de_rmw(display, MIPI_CTRL(display, port),
349 			     GLK_LP_WAKE, (tmp & DEVICE_READY) ? GLK_LP_WAKE : 0);
350 	}
351 
352 	/* Wait for Pwr ACK */
353 	for_each_dsi_port(port, intel_dsi->ports) {
354 		if (intel_de_wait_for_set_ms(display, MIPI_CTRL(display, port),
355 					     GLK_MIPIIO_PORT_POWERED, 20))
356 			drm_err(display->drm, "MIPIO port is powergated\n");
357 	}
358 
359 	/* Check for cold boot scenario */
360 	for_each_dsi_port(port, intel_dsi->ports) {
361 		cold_boot |=
362 			!(intel_de_read(display, MIPI_DEVICE_READY(display, port)) & DEVICE_READY);
363 	}
364 
365 	return cold_boot;
366 }
367 
368 static void glk_dsi_device_ready(struct intel_encoder *encoder)
369 {
370 	struct intel_display *display = to_intel_display(encoder);
371 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
372 	enum port port;
373 
374 	/* Wait for MIPI PHY status bit to set */
375 	for_each_dsi_port(port, intel_dsi->ports) {
376 		if (intel_de_wait_for_set_ms(display, MIPI_CTRL(display, port),
377 					     GLK_PHY_STATUS_PORT_READY, 20))
378 			drm_err(display->drm, "PHY is not ON\n");
379 	}
380 
381 	/* Get IO out of reset */
382 	intel_de_rmw(display, MIPI_CTRL(display, PORT_A), 0, GLK_MIPIIO_RESET_RELEASED);
383 
384 	/* Get IO out of Low power state*/
385 	for_each_dsi_port(port, intel_dsi->ports) {
386 		if (!(intel_de_read(display, MIPI_DEVICE_READY(display, port)) & DEVICE_READY)) {
387 			intel_de_rmw(display, MIPI_DEVICE_READY(display, port),
388 				     ULPS_STATE_MASK, DEVICE_READY);
389 			usleep_range(10, 15);
390 		} else {
391 			/* Enter ULPS */
392 			intel_de_rmw(display, MIPI_DEVICE_READY(display, port),
393 				     ULPS_STATE_MASK, ULPS_STATE_ENTER | DEVICE_READY);
394 
395 			/* Wait for ULPS active */
396 			if (intel_de_wait_for_clear_ms(display, MIPI_CTRL(display, port),
397 						       GLK_ULPS_NOT_ACTIVE, 20))
398 				drm_err(display->drm, "ULPS not active\n");
399 
400 			/* Exit ULPS */
401 			intel_de_rmw(display, MIPI_DEVICE_READY(display, port),
402 				     ULPS_STATE_MASK, ULPS_STATE_EXIT | DEVICE_READY);
403 
404 			/* Enter Normal Mode */
405 			intel_de_rmw(display, MIPI_DEVICE_READY(display, port),
406 				     ULPS_STATE_MASK,
407 				     ULPS_STATE_NORMAL_OPERATION | DEVICE_READY);
408 
409 			intel_de_rmw(display, MIPI_CTRL(display, port), GLK_LP_WAKE, 0);
410 		}
411 	}
412 
413 	/* Wait for Stop state */
414 	for_each_dsi_port(port, intel_dsi->ports) {
415 		if (intel_de_wait_for_set_ms(display, MIPI_CTRL(display, port),
416 					     GLK_DATA_LANE_STOP_STATE, 20))
417 			drm_err(display->drm,
418 				"Date lane not in STOP state\n");
419 	}
420 
421 	/* Wait for AFE LATCH */
422 	for_each_dsi_port(port, intel_dsi->ports) {
423 		if (intel_de_wait_for_set_ms(display, BXT_MIPI_PORT_CTRL(port),
424 					     AFE_LATCHOUT, 20))
425 			drm_err(display->drm,
426 				"D-PHY not entering LP-11 state\n");
427 	}
428 }
429 
430 static void bxt_dsi_device_ready(struct intel_encoder *encoder)
431 {
432 	struct intel_display *display = to_intel_display(encoder);
433 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
434 	enum port port;
435 	u32 val;
436 
437 	drm_dbg_kms(display->drm, "\n");
438 
439 	/* Enable MIPI PHY transparent latch */
440 	for_each_dsi_port(port, intel_dsi->ports) {
441 		intel_de_rmw(display, BXT_MIPI_PORT_CTRL(port), 0, LP_OUTPUT_HOLD);
442 		usleep_range(2000, 2500);
443 	}
444 
445 	/* Clear ULPS and set device ready */
446 	for_each_dsi_port(port, intel_dsi->ports) {
447 		val = intel_de_read(display, MIPI_DEVICE_READY(display, port));
448 		val &= ~ULPS_STATE_MASK;
449 		intel_de_write(display, MIPI_DEVICE_READY(display, port), val);
450 		usleep_range(2000, 2500);
451 		val |= DEVICE_READY;
452 		intel_de_write(display, MIPI_DEVICE_READY(display, port), val);
453 	}
454 }
455 
456 static void vlv_dsi_device_ready(struct intel_encoder *encoder)
457 {
458 	struct intel_display *display = to_intel_display(encoder);
459 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
460 	enum port port;
461 
462 	drm_dbg_kms(display->drm, "\n");
463 
464 	vlv_flisdsi_get(display->drm);
465 	/* program rcomp for compliance, reduce from 50 ohms to 45 ohms
466 	 * needed everytime after power gate */
467 	vlv_flisdsi_write(display->drm, 0x04, 0x0004);
468 	vlv_flisdsi_put(display->drm);
469 
470 	/* bandgap reset is needed after everytime we do power gate */
471 	band_gap_reset(display);
472 
473 	for_each_dsi_port(port, intel_dsi->ports) {
474 
475 		intel_de_write(display, MIPI_DEVICE_READY(display, port),
476 			       ULPS_STATE_ENTER);
477 		usleep_range(2500, 3000);
478 
479 		/* Enable MIPI PHY transparent latch
480 		 * Common bit for both MIPI Port A & MIPI Port C
481 		 * No similar bit in MIPI Port C reg
482 		 */
483 		intel_de_rmw(display, VLV_MIPI_PORT_CTRL(PORT_A), 0, LP_OUTPUT_HOLD);
484 		usleep_range(1000, 1500);
485 
486 		intel_de_write(display, MIPI_DEVICE_READY(display, port),
487 			       ULPS_STATE_EXIT);
488 		usleep_range(2500, 3000);
489 
490 		intel_de_write(display, MIPI_DEVICE_READY(display, port),
491 			       DEVICE_READY);
492 		usleep_range(2500, 3000);
493 	}
494 }
495 
496 static void intel_dsi_device_ready(struct intel_encoder *encoder)
497 {
498 	struct intel_display *display = to_intel_display(encoder);
499 
500 	if (display->platform.geminilake)
501 		glk_dsi_device_ready(encoder);
502 	else if (display->platform.geminilake || display->platform.broxton)
503 		bxt_dsi_device_ready(encoder);
504 	else
505 		vlv_dsi_device_ready(encoder);
506 }
507 
508 static void glk_dsi_enter_low_power_mode(struct intel_encoder *encoder)
509 {
510 	struct intel_display *display = to_intel_display(encoder);
511 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
512 	enum port port;
513 
514 	/* Enter ULPS */
515 	for_each_dsi_port(port, intel_dsi->ports)
516 		intel_de_rmw(display, MIPI_DEVICE_READY(display, port),
517 			     ULPS_STATE_MASK, ULPS_STATE_ENTER | DEVICE_READY);
518 
519 	/* Wait for MIPI PHY status bit to unset */
520 	for_each_dsi_port(port, intel_dsi->ports) {
521 		if (intel_de_wait_for_clear_ms(display, MIPI_CTRL(display, port),
522 					       GLK_PHY_STATUS_PORT_READY, 20))
523 			drm_err(display->drm, "PHY is not turning OFF\n");
524 	}
525 
526 	/* Wait for Pwr ACK bit to unset */
527 	for_each_dsi_port(port, intel_dsi->ports) {
528 		if (intel_de_wait_for_clear_ms(display, MIPI_CTRL(display, port),
529 					       GLK_MIPIIO_PORT_POWERED, 20))
530 			drm_err(display->drm,
531 				"MIPI IO Port is not powergated\n");
532 	}
533 }
534 
535 static void glk_dsi_disable_mipi_io(struct intel_encoder *encoder)
536 {
537 	struct intel_display *display = to_intel_display(encoder);
538 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
539 	enum port port;
540 
541 	/* Put the IO into reset */
542 	intel_de_rmw(display, MIPI_CTRL(display, PORT_A), GLK_MIPIIO_RESET_RELEASED, 0);
543 
544 	/* Wait for MIPI PHY status bit to unset */
545 	for_each_dsi_port(port, intel_dsi->ports) {
546 		if (intel_de_wait_for_clear_ms(display, MIPI_CTRL(display, port),
547 					       GLK_PHY_STATUS_PORT_READY, 20))
548 			drm_err(display->drm, "PHY is not turning OFF\n");
549 	}
550 
551 	/* Clear MIPI mode */
552 	for_each_dsi_port(port, intel_dsi->ports)
553 		intel_de_rmw(display, MIPI_CTRL(display, port), GLK_MIPIIO_ENABLE, 0);
554 }
555 
556 static void glk_dsi_clear_device_ready(struct intel_encoder *encoder)
557 {
558 	glk_dsi_enter_low_power_mode(encoder);
559 	glk_dsi_disable_mipi_io(encoder);
560 }
561 
562 static i915_reg_t port_ctrl_reg(struct intel_display *display, enum port port)
563 {
564 	return display->platform.geminilake || display->platform.broxton ?
565 		BXT_MIPI_PORT_CTRL(port) : VLV_MIPI_PORT_CTRL(port);
566 }
567 
568 static void vlv_dsi_clear_device_ready(struct intel_encoder *encoder)
569 {
570 	struct intel_display *display = to_intel_display(encoder);
571 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
572 	enum port port;
573 
574 	drm_dbg_kms(display->drm, "\n");
575 	for_each_dsi_port(port, intel_dsi->ports) {
576 		/* Common bit for both MIPI Port A & MIPI Port C on VLV/CHV */
577 		i915_reg_t port_ctrl = display->platform.broxton ?
578 			BXT_MIPI_PORT_CTRL(port) : VLV_MIPI_PORT_CTRL(PORT_A);
579 
580 		intel_de_write(display, MIPI_DEVICE_READY(display, port),
581 			       DEVICE_READY | ULPS_STATE_ENTER);
582 		usleep_range(2000, 2500);
583 
584 		intel_de_write(display, MIPI_DEVICE_READY(display, port),
585 			       DEVICE_READY | ULPS_STATE_EXIT);
586 		usleep_range(2000, 2500);
587 
588 		intel_de_write(display, MIPI_DEVICE_READY(display, port),
589 			       DEVICE_READY | ULPS_STATE_ENTER);
590 		usleep_range(2000, 2500);
591 
592 		/*
593 		 * On VLV/CHV, wait till Clock lanes are in LP-00 state for MIPI
594 		 * Port A only. MIPI Port C has no similar bit for checking.
595 		 */
596 		if ((display->platform.broxton || port == PORT_A) &&
597 		    intel_de_wait_for_clear_ms(display, port_ctrl,
598 					       AFE_LATCHOUT, 30))
599 			drm_err(display->drm, "DSI LP not going Low\n");
600 
601 		/* Disable MIPI PHY transparent latch */
602 		intel_de_rmw(display, port_ctrl, LP_OUTPUT_HOLD, 0);
603 		usleep_range(1000, 1500);
604 
605 		intel_de_write(display, MIPI_DEVICE_READY(display, port), 0x00);
606 		usleep_range(2000, 2500);
607 	}
608 }
609 
610 static void intel_dsi_port_enable(struct intel_encoder *encoder,
611 				  const struct intel_crtc_state *crtc_state)
612 {
613 	struct intel_display *display = to_intel_display(encoder);
614 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
615 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
616 	enum port port;
617 
618 	if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
619 		u32 temp = intel_dsi->pixel_overlap;
620 
621 		if (display->platform.geminilake || display->platform.broxton) {
622 			for_each_dsi_port(port, intel_dsi->ports)
623 				intel_de_rmw(display, MIPI_CTRL(display, port),
624 					     BXT_PIXEL_OVERLAP_CNT_MASK,
625 					     temp << BXT_PIXEL_OVERLAP_CNT_SHIFT);
626 		} else {
627 			intel_de_rmw(display, VLV_CHICKEN_3,
628 				     PIXEL_OVERLAP_CNT_MASK,
629 				     temp << PIXEL_OVERLAP_CNT_SHIFT);
630 		}
631 	}
632 
633 	for_each_dsi_port(port, intel_dsi->ports) {
634 		i915_reg_t port_ctrl = port_ctrl_reg(display, port);
635 		u32 temp;
636 
637 		temp = intel_de_read(display, port_ctrl);
638 
639 		temp &= ~LANE_CONFIGURATION_MASK;
640 		temp &= ~DUAL_LINK_MODE_MASK;
641 
642 		if (intel_dsi->ports == (BIT(PORT_A) | BIT(PORT_C))) {
643 			temp |= (intel_dsi->dual_link - 1)
644 						<< DUAL_LINK_MODE_SHIFT;
645 			if (display->platform.broxton)
646 				temp |= LANE_CONFIGURATION_DUAL_LINK_A;
647 			else
648 				temp |= crtc->pipe ?
649 					LANE_CONFIGURATION_DUAL_LINK_B :
650 					LANE_CONFIGURATION_DUAL_LINK_A;
651 		}
652 
653 		if (intel_dsi->pixel_format != MIPI_DSI_FMT_RGB888)
654 			temp |= DITHERING_ENABLE;
655 
656 		/* assert ip_tg_enable signal */
657 		intel_de_write(display, port_ctrl, temp | DPI_ENABLE);
658 		intel_de_posting_read(display, port_ctrl);
659 	}
660 }
661 
662 static void intel_dsi_port_disable(struct intel_encoder *encoder)
663 {
664 	struct intel_display *display = to_intel_display(encoder);
665 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
666 	enum port port;
667 
668 	for_each_dsi_port(port, intel_dsi->ports) {
669 		i915_reg_t port_ctrl = port_ctrl_reg(display, port);
670 
671 		/* de-assert ip_tg_enable signal */
672 		intel_de_rmw(display, port_ctrl, DPI_ENABLE, 0);
673 		intel_de_posting_read(display, port_ctrl);
674 	}
675 }
676 
677 static void intel_dsi_prepare(struct intel_encoder *encoder,
678 			      const struct intel_crtc_state *pipe_config);
679 static void intel_dsi_unprepare(struct intel_encoder *encoder);
680 
681 /*
682  * Panel enable/disable sequences from the VBT spec.
683  *
684  * Note the spec has AssertReset / DeassertReset swapped from their
685  * usual naming. We use the normal names to avoid confusion (so below
686  * they are swapped compared to the spec).
687  *
688  * Steps starting with MIPI refer to VBT sequences, note that for v2
689  * VBTs several steps which have a VBT in v2 are expected to be handled
690  * directly by the driver, by directly driving gpios for example.
691  *
692  * v2 video mode seq         v3 video mode seq         command mode seq
693  * - power on                - MIPIPanelPowerOn        - power on
694  * - wait t1+t2                                        - wait t1+t2
695  * - MIPIDeassertResetPin    - MIPIDeassertResetPin    - MIPIDeassertResetPin
696  * - io lines to lp-11       - io lines to lp-11       - io lines to lp-11
697  * - MIPISendInitialDcsCmds  - MIPISendInitialDcsCmds  - MIPISendInitialDcsCmds
698  *                                                     - MIPITearOn
699  *                                                     - MIPIDisplayOn
700  * - turn on DPI             - turn on DPI             - set pipe to dsr mode
701  * - MIPIDisplayOn           - MIPIDisplayOn
702  * - wait t5                                           - wait t5
703  * - backlight on            - MIPIBacklightOn         - backlight on
704  * ...                       ...                       ... issue mem cmds ...
705  * - backlight off           - MIPIBacklightOff        - backlight off
706  * - wait t6                                           - wait t6
707  * - MIPIDisplayOff
708  * - turn off DPI            - turn off DPI            - disable pipe dsr mode
709  *                                                     - MIPITearOff
710  *                           - MIPIDisplayOff          - MIPIDisplayOff
711  * - io lines to lp-00       - io lines to lp-00       - io lines to lp-00
712  * - MIPIAssertResetPin      - MIPIAssertResetPin      - MIPIAssertResetPin
713  * - wait t3                                           - wait t3
714  * - power off               - MIPIPanelPowerOff       - power off
715  * - wait t4                                           - wait t4
716  */
717 
718 /*
719  * DSI port enable has to be done before pipe and plane enable, so we do it in
720  * the pre_enable hook instead of the enable hook.
721  */
722 static void intel_dsi_pre_enable(struct intel_atomic_state *state,
723 				 struct intel_encoder *encoder,
724 				 const struct intel_crtc_state *pipe_config,
725 				 const struct drm_connector_state *conn_state)
726 {
727 	struct intel_display *display = to_intel_display(encoder);
728 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
729 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
730 	enum pipe pipe = crtc->pipe;
731 	enum port port;
732 	bool glk_cold_boot = false;
733 
734 	drm_dbg_kms(display->drm, "\n");
735 
736 	intel_dsi_wait_panel_power_cycle(intel_dsi);
737 
738 	intel_set_cpu_fifo_underrun_reporting(display, pipe, true);
739 
740 	/*
741 	 * The BIOS may leave the PLL in a wonky state where it doesn't
742 	 * lock. It needs to be fully powered down to fix it.
743 	 */
744 	if (display->platform.geminilake || display->platform.broxton) {
745 		bxt_dsi_pll_disable(encoder);
746 		bxt_dsi_pll_enable(encoder, pipe_config);
747 	} else {
748 		vlv_dsi_pll_disable(encoder);
749 		vlv_dsi_pll_enable(encoder, pipe_config);
750 	}
751 
752 	if (display->platform.broxton) {
753 		/* Add MIPI IO reset programming for modeset */
754 		intel_de_rmw(display, BXT_P_CR_GT_DISP_PWRON, 0, MIPIO_RST_CTRL);
755 
756 		/* Power up DSI regulator */
757 		intel_de_write(display, BXT_P_DSI_REGULATOR_CFG, STAP_SELECT);
758 		intel_de_write(display, BXT_P_DSI_REGULATOR_TX_CTRL, 0);
759 	}
760 
761 	if (display->platform.valleyview || display->platform.cherryview) {
762 		/* Disable DPOunit clock gating, can stall pipe */
763 		intel_de_rmw(display, VLV_DSPCLK_GATE_D,
764 			     0, DPOUNIT_CLOCK_GATE_DISABLE);
765 	}
766 
767 	if (!display->platform.geminilake)
768 		intel_dsi_prepare(encoder, pipe_config);
769 
770 	/* Give the panel time to power-on and then deassert its reset */
771 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_ON);
772 	msleep(intel_dsi->panel_on_delay);
773 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DEASSERT_RESET);
774 
775 	if (display->platform.geminilake) {
776 		glk_cold_boot = glk_dsi_enable_io(encoder);
777 
778 		/* Prepare port in cold boot(s3/s4) scenario */
779 		if (glk_cold_boot)
780 			intel_dsi_prepare(encoder, pipe_config);
781 	}
782 
783 	/* Put device in ready state (LP-11) */
784 	intel_dsi_device_ready(encoder);
785 
786 	/* Prepare port in normal boot scenario */
787 	if (display->platform.geminilake && !glk_cold_boot)
788 		intel_dsi_prepare(encoder, pipe_config);
789 
790 	/* Send initialization commands in LP mode */
791 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_INIT_OTP);
792 
793 	/*
794 	 * Enable port in pre-enable phase itself because as per hw team
795 	 * recommendation, port should be enabled before plane & pipe
796 	 */
797 	if (is_cmd_mode(intel_dsi)) {
798 		for_each_dsi_port(port, intel_dsi->ports)
799 			intel_de_write(display,
800 				       MIPI_MAX_RETURN_PKT_SIZE(display, port), 8 * 4);
801 		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_TEAR_ON);
802 		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
803 	} else {
804 		msleep(20); /* XXX */
805 		for_each_dsi_port(port, intel_dsi->ports)
806 			dpi_send_cmd(intel_dsi, TURN_ON, false, port);
807 		msleep(100);
808 
809 		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
810 
811 		intel_dsi_port_enable(encoder, pipe_config);
812 	}
813 
814 	intel_backlight_enable(pipe_config, conn_state);
815 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_ON);
816 }
817 
818 static void bxt_dsi_enable(struct intel_atomic_state *state,
819 			   struct intel_encoder *encoder,
820 			   const struct intel_crtc_state *crtc_state,
821 			   const struct drm_connector_state *conn_state)
822 {
823 	intel_crtc_vblank_on(crtc_state);
824 }
825 
826 /*
827  * DSI port disable has to be done after pipe and plane disable, so we do it in
828  * the post_disable hook.
829  */
830 static void intel_dsi_disable(struct intel_atomic_state *state,
831 			      struct intel_encoder *encoder,
832 			      const struct intel_crtc_state *old_crtc_state,
833 			      const struct drm_connector_state *old_conn_state)
834 {
835 	struct intel_display *display = to_intel_display(encoder);
836 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
837 	enum port port;
838 
839 	drm_dbg_kms(display->drm, "\n");
840 
841 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_OFF);
842 	intel_backlight_disable(old_conn_state);
843 
844 	/*
845 	 * According to the spec we should send SHUTDOWN before
846 	 * MIPI_SEQ_DISPLAY_OFF only for v3+ VBTs, but field testing
847 	 * has shown that the v3 sequence works for v2 VBTs too
848 	 */
849 	if (is_vid_mode(intel_dsi)) {
850 		/* Send Shutdown command to the panel in LP mode */
851 		for_each_dsi_port(port, intel_dsi->ports)
852 			dpi_send_cmd(intel_dsi, SHUTDOWN, false, port);
853 		msleep(10);
854 	}
855 }
856 
857 static void intel_dsi_clear_device_ready(struct intel_encoder *encoder)
858 {
859 	struct intel_display *display = to_intel_display(encoder);
860 
861 	if (display->platform.geminilake)
862 		glk_dsi_clear_device_ready(encoder);
863 	else
864 		vlv_dsi_clear_device_ready(encoder);
865 }
866 
867 static void intel_dsi_post_disable(struct intel_atomic_state *state,
868 				   struct intel_encoder *encoder,
869 				   const struct intel_crtc_state *old_crtc_state,
870 				   const struct drm_connector_state *old_conn_state)
871 {
872 	struct intel_display *display = to_intel_display(encoder);
873 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
874 	enum port port;
875 
876 	drm_dbg_kms(display->drm, "\n");
877 
878 	if (display->platform.geminilake || display->platform.broxton) {
879 		intel_crtc_vblank_off(old_crtc_state);
880 
881 		skl_scaler_disable(old_crtc_state);
882 	}
883 
884 	if (is_vid_mode(intel_dsi)) {
885 		for_each_dsi_port(port, intel_dsi->ports)
886 			vlv_dsi_wait_for_fifo_empty(intel_dsi, port);
887 
888 		intel_dsi_port_disable(encoder);
889 		usleep_range(2000, 5000);
890 	}
891 
892 	intel_dsi_unprepare(encoder);
893 
894 	/*
895 	 * if disable packets are sent before sending shutdown packet then in
896 	 * some next enable sequence send turn on packet error is observed
897 	 */
898 	if (is_cmd_mode(intel_dsi))
899 		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_TEAR_OFF);
900 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_OFF);
901 
902 	/* Transition to LP-00 */
903 	intel_dsi_clear_device_ready(encoder);
904 
905 	if (display->platform.broxton) {
906 		/* Power down DSI regulator to save power */
907 		intel_de_write(display, BXT_P_DSI_REGULATOR_CFG, STAP_SELECT);
908 		intel_de_write(display, BXT_P_DSI_REGULATOR_TX_CTRL,
909 			       HS_IO_CTRL_SELECT);
910 
911 		/* Add MIPI IO reset programming for modeset */
912 		intel_de_rmw(display, BXT_P_CR_GT_DISP_PWRON, MIPIO_RST_CTRL, 0);
913 	}
914 
915 	if (display->platform.geminilake || display->platform.broxton) {
916 		bxt_dsi_pll_disable(encoder);
917 	} else {
918 		vlv_dsi_pll_disable(encoder);
919 
920 		intel_de_rmw(display, VLV_DSPCLK_GATE_D,
921 			     DPOUNIT_CLOCK_GATE_DISABLE, 0);
922 	}
923 
924 	/* Assert reset */
925 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_ASSERT_RESET);
926 
927 	msleep(intel_dsi->panel_off_delay);
928 	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_OFF);
929 
930 	intel_dsi->panel_power_off_time = ktime_get_boottime();
931 }
932 
933 static bool intel_dsi_get_hw_state(struct intel_encoder *encoder,
934 				   enum pipe *pipe)
935 {
936 	struct intel_display *display = to_intel_display(encoder);
937 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
938 	struct ref_tracker *wakeref;
939 	enum port port;
940 	bool active = false;
941 
942 	drm_dbg_kms(display->drm, "\n");
943 
944 	wakeref = intel_display_power_get_if_enabled(display,
945 						     encoder->power_domain);
946 	if (!wakeref)
947 		return false;
948 
949 	/*
950 	 * On Broxton the PLL needs to be enabled with a valid divider
951 	 * configuration, otherwise accessing DSI registers will hang the
952 	 * machine. See BSpec North Display Engine registers/MIPI[BXT].
953 	 */
954 	if ((display->platform.geminilake || display->platform.broxton) &&
955 	    !bxt_dsi_pll_is_enabled(display))
956 		goto out_put_power;
957 
958 	/* XXX: this only works for one DSI output */
959 	for_each_dsi_port(port, intel_dsi->ports) {
960 		i915_reg_t port_ctrl = port_ctrl_reg(display, port);
961 		bool enabled = intel_de_read(display, port_ctrl) & DPI_ENABLE;
962 
963 		/*
964 		 * Due to some hardware limitations on VLV/CHV, the DPI enable
965 		 * bit in port C control register does not get set. As a
966 		 * workaround, check pipe B conf instead.
967 		 */
968 		if ((display->platform.valleyview || display->platform.cherryview) &&
969 		    port == PORT_C)
970 			enabled = intel_de_read(display,
971 						TRANSCONF(display, PIPE_B)) & TRANSCONF_ENABLE;
972 
973 		/* Try command mode if video mode not enabled */
974 		if (!enabled) {
975 			u32 tmp = intel_de_read(display,
976 						MIPI_DSI_FUNC_PRG(display, port));
977 			enabled = tmp & CMD_MODE_DATA_WIDTH_MASK;
978 		}
979 
980 		if (!enabled)
981 			continue;
982 
983 		if (!(intel_de_read(display, MIPI_DEVICE_READY(display, port)) & DEVICE_READY))
984 			continue;
985 
986 		if (display->platform.geminilake || display->platform.broxton) {
987 			u32 tmp = intel_de_read(display, MIPI_CTRL(display, port));
988 			tmp &= BXT_PIPE_SELECT_MASK;
989 			tmp >>= BXT_PIPE_SELECT_SHIFT;
990 
991 			if (drm_WARN_ON(display->drm, tmp > PIPE_C))
992 				continue;
993 
994 			*pipe = tmp;
995 		} else {
996 			*pipe = port == PORT_A ? PIPE_A : PIPE_B;
997 		}
998 
999 		active = true;
1000 		break;
1001 	}
1002 
1003 out_put_power:
1004 	intel_display_power_put(display, encoder->power_domain, wakeref);
1005 
1006 	return active;
1007 }
1008 
1009 static void bxt_dsi_get_pipe_config(struct intel_encoder *encoder,
1010 				    struct intel_crtc_state *pipe_config)
1011 {
1012 	struct intel_display *display = to_intel_display(encoder);
1013 	struct drm_display_mode *adjusted_mode =
1014 					&pipe_config->hw.adjusted_mode;
1015 	struct drm_display_mode *adjusted_mode_sw;
1016 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
1017 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1018 	unsigned int lane_count = intel_dsi->lane_count;
1019 	unsigned int bpp, fmt;
1020 	enum port port;
1021 	u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
1022 	u16 hfp_sw, hsync_sw, hbp_sw;
1023 	u16 crtc_htotal_sw, crtc_hsync_start_sw, crtc_hsync_end_sw,
1024 				crtc_hblank_start_sw, crtc_hblank_end_sw;
1025 
1026 	/* FIXME: hw readout should not depend on SW state */
1027 	adjusted_mode_sw = &crtc->config->hw.adjusted_mode;
1028 
1029 	/*
1030 	 * Atleast one port is active as encoder->get_config called only if
1031 	 * encoder->get_hw_state() returns true.
1032 	 */
1033 	for_each_dsi_port(port, intel_dsi->ports) {
1034 		if (intel_de_read(display, BXT_MIPI_PORT_CTRL(port)) & DPI_ENABLE)
1035 			break;
1036 	}
1037 
1038 	fmt = intel_de_read(display, MIPI_DSI_FUNC_PRG(display, port)) & VID_MODE_FORMAT_MASK;
1039 	bpp = mipi_dsi_pixel_format_to_bpp(
1040 			pixel_format_from_register_bits(fmt));
1041 
1042 	pipe_config->pipe_bpp = bdw_get_pipe_misc_bpp(crtc);
1043 
1044 	/* Enable Frame time stamo based scanline reporting */
1045 	pipe_config->mode_flags |=
1046 		I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP;
1047 
1048 	/* In terms of pixels */
1049 	adjusted_mode->crtc_hdisplay =
1050 				intel_de_read(display,
1051 				              BXT_MIPI_TRANS_HACTIVE(port));
1052 	adjusted_mode->crtc_vdisplay =
1053 				intel_de_read(display,
1054 				              BXT_MIPI_TRANS_VACTIVE(port));
1055 	adjusted_mode->crtc_vtotal =
1056 				intel_de_read(display,
1057 				              BXT_MIPI_TRANS_VTOTAL(port)) + 1;
1058 
1059 	hactive = adjusted_mode->crtc_hdisplay;
1060 	hfp = intel_de_read(display, MIPI_HFP_COUNT(display, port));
1061 
1062 	/*
1063 	 * Meaningful for video mode non-burst sync pulse mode only,
1064 	 * can be zero for non-burst sync events and burst modes
1065 	 */
1066 	hsync = intel_de_read(display, MIPI_HSYNC_PADDING_COUNT(display, port));
1067 	hbp = intel_de_read(display, MIPI_HBP_COUNT(display, port));
1068 
1069 	/* horizontal values are in terms of high speed byte clock */
1070 	hfp = pixels_from_txbyteclkhs(hfp, bpp, lane_count,
1071 						intel_dsi->burst_mode_ratio);
1072 	hsync = pixels_from_txbyteclkhs(hsync, bpp, lane_count,
1073 						intel_dsi->burst_mode_ratio);
1074 	hbp = pixels_from_txbyteclkhs(hbp, bpp, lane_count,
1075 						intel_dsi->burst_mode_ratio);
1076 
1077 	if (intel_dsi->dual_link) {
1078 		hfp *= 2;
1079 		hsync *= 2;
1080 		hbp *= 2;
1081 	}
1082 
1083 	/* vertical values are in terms of lines */
1084 	vfp = intel_de_read(display, MIPI_VFP_COUNT(display, port));
1085 	vbp = intel_de_read(display, MIPI_VBP_COUNT(display, port));
1086 	vsync = intel_de_read(display, MIPI_VSYNC_PADDING_COUNT(display, port));
1087 
1088 	adjusted_mode->crtc_htotal = hactive + hfp + hsync + hbp;
1089 	adjusted_mode->crtc_hsync_start = hfp + adjusted_mode->crtc_hdisplay;
1090 	adjusted_mode->crtc_hsync_end = hsync + adjusted_mode->crtc_hsync_start;
1091 	adjusted_mode->crtc_hblank_start = adjusted_mode->crtc_hdisplay;
1092 	adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_htotal;
1093 
1094 	drm_WARN_ON(display->drm, adjusted_mode->crtc_vdisplay +
1095 		    vfp + vsync + vbp != adjusted_mode->crtc_vtotal);
1096 	adjusted_mode->crtc_vsync_start = vfp + adjusted_mode->crtc_vdisplay;
1097 	adjusted_mode->crtc_vsync_end = vsync + adjusted_mode->crtc_vsync_start;
1098 	adjusted_mode->crtc_vblank_start = adjusted_mode->crtc_vdisplay;
1099 	adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vtotal;
1100 
1101 	/*
1102 	 * In BXT DSI there is no regs programmed with few horizontal timings
1103 	 * in Pixels but txbyteclkhs.. So retrieval process adds some
1104 	 * ROUND_UP ERRORS in the process of PIXELS<==>txbyteclkhs.
1105 	 * Actually here for the given adjusted_mode, we are calculating the
1106 	 * value programmed to the port and then back to the horizontal timing
1107 	 * param in pixels. This is the expected value, including roundup errors
1108 	 * And if that is same as retrieved value from port, then
1109 	 * (HW state) adjusted_mode's horizontal timings are corrected to
1110 	 * match with SW state to nullify the errors.
1111 	 */
1112 	/* Calculating the value programmed to the Port register */
1113 	hfp_sw = adjusted_mode_sw->crtc_hsync_start -
1114 					adjusted_mode_sw->crtc_hdisplay;
1115 	hsync_sw = adjusted_mode_sw->crtc_hsync_end -
1116 					adjusted_mode_sw->crtc_hsync_start;
1117 	hbp_sw = adjusted_mode_sw->crtc_htotal -
1118 					adjusted_mode_sw->crtc_hsync_end;
1119 
1120 	if (intel_dsi->dual_link) {
1121 		hfp_sw /= 2;
1122 		hsync_sw /= 2;
1123 		hbp_sw /= 2;
1124 	}
1125 
1126 	hfp_sw = txbyteclkhs(hfp_sw, bpp, lane_count,
1127 						intel_dsi->burst_mode_ratio);
1128 	hsync_sw = txbyteclkhs(hsync_sw, bpp, lane_count,
1129 			    intel_dsi->burst_mode_ratio);
1130 	hbp_sw = txbyteclkhs(hbp_sw, bpp, lane_count,
1131 						intel_dsi->burst_mode_ratio);
1132 
1133 	/* Reverse calculating the adjusted mode parameters from port reg vals*/
1134 	hfp_sw = pixels_from_txbyteclkhs(hfp_sw, bpp, lane_count,
1135 						intel_dsi->burst_mode_ratio);
1136 	hsync_sw = pixels_from_txbyteclkhs(hsync_sw, bpp, lane_count,
1137 						intel_dsi->burst_mode_ratio);
1138 	hbp_sw = pixels_from_txbyteclkhs(hbp_sw, bpp, lane_count,
1139 						intel_dsi->burst_mode_ratio);
1140 
1141 	if (intel_dsi->dual_link) {
1142 		hfp_sw *= 2;
1143 		hsync_sw *= 2;
1144 		hbp_sw *= 2;
1145 	}
1146 
1147 	crtc_htotal_sw = adjusted_mode_sw->crtc_hdisplay + hfp_sw +
1148 							hsync_sw + hbp_sw;
1149 	crtc_hsync_start_sw = hfp_sw + adjusted_mode_sw->crtc_hdisplay;
1150 	crtc_hsync_end_sw = hsync_sw + crtc_hsync_start_sw;
1151 	crtc_hblank_start_sw = adjusted_mode_sw->crtc_hdisplay;
1152 	crtc_hblank_end_sw = crtc_htotal_sw;
1153 
1154 	if (adjusted_mode->crtc_htotal == crtc_htotal_sw)
1155 		adjusted_mode->crtc_htotal = adjusted_mode_sw->crtc_htotal;
1156 
1157 	if (adjusted_mode->crtc_hsync_start == crtc_hsync_start_sw)
1158 		adjusted_mode->crtc_hsync_start =
1159 					adjusted_mode_sw->crtc_hsync_start;
1160 
1161 	if (adjusted_mode->crtc_hsync_end == crtc_hsync_end_sw)
1162 		adjusted_mode->crtc_hsync_end =
1163 					adjusted_mode_sw->crtc_hsync_end;
1164 
1165 	if (adjusted_mode->crtc_hblank_start == crtc_hblank_start_sw)
1166 		adjusted_mode->crtc_hblank_start =
1167 					adjusted_mode_sw->crtc_hblank_start;
1168 
1169 	if (adjusted_mode->crtc_hblank_end == crtc_hblank_end_sw)
1170 		adjusted_mode->crtc_hblank_end =
1171 					adjusted_mode_sw->crtc_hblank_end;
1172 }
1173 
1174 static void intel_dsi_get_config(struct intel_encoder *encoder,
1175 				 struct intel_crtc_state *pipe_config)
1176 {
1177 	struct intel_display *display = to_intel_display(encoder);
1178 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1179 	u32 pclk;
1180 
1181 	drm_dbg_kms(display->drm, "\n");
1182 
1183 	pipe_config->output_types |= BIT(INTEL_OUTPUT_DSI);
1184 
1185 	if (display->platform.geminilake || display->platform.broxton) {
1186 		bxt_dsi_get_pipe_config(encoder, pipe_config);
1187 		pclk = bxt_dsi_get_pclk(encoder, pipe_config);
1188 	} else {
1189 		pclk = vlv_dsi_get_pclk(encoder, pipe_config);
1190 	}
1191 
1192 	pipe_config->port_clock = pclk;
1193 
1194 	/* FIXME definitely not right for burst/cmd mode/pixel overlap */
1195 	pipe_config->hw.adjusted_mode.crtc_clock = pclk;
1196 	if (intel_dsi->dual_link)
1197 		pipe_config->hw.adjusted_mode.crtc_clock *= 2;
1198 }
1199 
1200 /* return txclkesc cycles in terms of divider and duration in us */
1201 static u16 txclkesc(u32 divider, unsigned int us)
1202 {
1203 	switch (divider) {
1204 	case ESCAPE_CLOCK_DIVIDER_1:
1205 	default:
1206 		return 20 * us;
1207 	case ESCAPE_CLOCK_DIVIDER_2:
1208 		return 10 * us;
1209 	case ESCAPE_CLOCK_DIVIDER_4:
1210 		return 5 * us;
1211 	}
1212 }
1213 
1214 static void set_dsi_timings(struct intel_encoder *encoder,
1215 			    const struct drm_display_mode *adjusted_mode)
1216 {
1217 	struct intel_display *display = to_intel_display(encoder);
1218 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1219 	enum port port;
1220 	unsigned int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
1221 	unsigned int lane_count = intel_dsi->lane_count;
1222 
1223 	u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
1224 
1225 	hactive = adjusted_mode->crtc_hdisplay;
1226 	hfp = adjusted_mode->crtc_hsync_start - adjusted_mode->crtc_hdisplay;
1227 	hsync = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
1228 	hbp = adjusted_mode->crtc_htotal - adjusted_mode->crtc_hsync_end;
1229 
1230 	if (intel_dsi->dual_link) {
1231 		hactive /= 2;
1232 		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
1233 			hactive += intel_dsi->pixel_overlap;
1234 		hfp /= 2;
1235 		hsync /= 2;
1236 		hbp /= 2;
1237 	}
1238 
1239 	vfp = adjusted_mode->crtc_vsync_start - adjusted_mode->crtc_vdisplay;
1240 	vsync = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
1241 	vbp = adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vsync_end;
1242 
1243 	/* horizontal values are in terms of high speed byte clock */
1244 	hactive = txbyteclkhs(hactive, bpp, lane_count,
1245 			      intel_dsi->burst_mode_ratio);
1246 	hfp = txbyteclkhs(hfp, bpp, lane_count, intel_dsi->burst_mode_ratio);
1247 	hsync = txbyteclkhs(hsync, bpp, lane_count,
1248 			    intel_dsi->burst_mode_ratio);
1249 	hbp = txbyteclkhs(hbp, bpp, lane_count, intel_dsi->burst_mode_ratio);
1250 
1251 	for_each_dsi_port(port, intel_dsi->ports) {
1252 		if (display->platform.geminilake || display->platform.broxton) {
1253 			/*
1254 			 * Program hdisplay and vdisplay on MIPI transcoder.
1255 			 * This is different from calculated hactive and
1256 			 * vactive, as they are calculated per channel basis,
1257 			 * whereas these values should be based on resolution.
1258 			 */
1259 			intel_de_write(display, BXT_MIPI_TRANS_HACTIVE(port),
1260 				       adjusted_mode->crtc_hdisplay);
1261 			intel_de_write(display, BXT_MIPI_TRANS_VACTIVE(port),
1262 				       adjusted_mode->crtc_vdisplay);
1263 			intel_de_write(display, BXT_MIPI_TRANS_VTOTAL(port),
1264 				       adjusted_mode->crtc_vtotal - 1);
1265 		}
1266 
1267 		intel_de_write(display, MIPI_HACTIVE_AREA_COUNT(display, port),
1268 			       hactive);
1269 		intel_de_write(display, MIPI_HFP_COUNT(display, port), hfp);
1270 
1271 		/* meaningful for video mode non-burst sync pulse mode only,
1272 		 * can be zero for non-burst sync events and burst modes */
1273 		intel_de_write(display, MIPI_HSYNC_PADDING_COUNT(display, port),
1274 			       hsync);
1275 		intel_de_write(display, MIPI_HBP_COUNT(display, port), hbp);
1276 
1277 		/* vertical values are in terms of lines */
1278 		intel_de_write(display, MIPI_VFP_COUNT(display, port), vfp);
1279 		intel_de_write(display, MIPI_VSYNC_PADDING_COUNT(display, port),
1280 			       vsync);
1281 		intel_de_write(display, MIPI_VBP_COUNT(display, port), vbp);
1282 	}
1283 }
1284 
1285 static u32 pixel_format_to_reg(enum mipi_dsi_pixel_format fmt)
1286 {
1287 	switch (fmt) {
1288 	case MIPI_DSI_FMT_RGB888:
1289 		return VID_MODE_FORMAT_RGB888;
1290 	case MIPI_DSI_FMT_RGB666:
1291 		return VID_MODE_FORMAT_RGB666;
1292 	case MIPI_DSI_FMT_RGB666_PACKED:
1293 		return VID_MODE_FORMAT_RGB666_PACKED;
1294 	case MIPI_DSI_FMT_RGB565:
1295 		return VID_MODE_FORMAT_RGB565;
1296 	default:
1297 		MISSING_CASE(fmt);
1298 		return VID_MODE_FORMAT_RGB666;
1299 	}
1300 }
1301 
1302 static void intel_dsi_prepare(struct intel_encoder *encoder,
1303 			      const struct intel_crtc_state *pipe_config)
1304 {
1305 	struct intel_display *display = to_intel_display(encoder);
1306 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
1307 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1308 	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
1309 	enum port port;
1310 	unsigned int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
1311 	u32 val, tmp;
1312 	u16 mode_hdisplay;
1313 
1314 	drm_dbg_kms(display->drm, "pipe %c\n", pipe_name(crtc->pipe));
1315 
1316 	mode_hdisplay = adjusted_mode->crtc_hdisplay;
1317 
1318 	if (intel_dsi->dual_link) {
1319 		mode_hdisplay /= 2;
1320 		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
1321 			mode_hdisplay += intel_dsi->pixel_overlap;
1322 	}
1323 
1324 	for_each_dsi_port(port, intel_dsi->ports) {
1325 		if (display->platform.valleyview || display->platform.cherryview) {
1326 			/*
1327 			 * escape clock divider, 20MHz, shared for A and C.
1328 			 * device ready must be off when doing this! txclkesc?
1329 			 */
1330 			tmp = intel_de_read(display, MIPI_CTRL(display, PORT_A));
1331 			tmp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
1332 			intel_de_write(display, MIPI_CTRL(display, PORT_A),
1333 				       tmp | ESCAPE_CLOCK_DIVIDER_1);
1334 
1335 			/* read request priority is per pipe */
1336 			tmp = intel_de_read(display, MIPI_CTRL(display, port));
1337 			tmp &= ~READ_REQUEST_PRIORITY_MASK;
1338 			intel_de_write(display, MIPI_CTRL(display, port),
1339 				       tmp | READ_REQUEST_PRIORITY_HIGH);
1340 		} else if (display->platform.geminilake || display->platform.broxton) {
1341 			enum pipe pipe = crtc->pipe;
1342 
1343 			intel_de_rmw(display, MIPI_CTRL(display, port),
1344 				     BXT_PIPE_SELECT_MASK, BXT_PIPE_SELECT(pipe));
1345 		}
1346 
1347 		/* XXX: why here, why like this? handling in irq handler?! */
1348 		intel_de_write(display, MIPI_INTR_STAT(display, port), 0xffffffff);
1349 		intel_de_write(display, MIPI_INTR_EN(display, port), 0xffffffff);
1350 
1351 		intel_de_write(display, MIPI_DPHY_PARAM(display, port),
1352 			       intel_dsi->dphy_reg);
1353 
1354 		intel_de_write(display, MIPI_DPI_RESOLUTION(display, port),
1355 			       adjusted_mode->crtc_vdisplay << VERTICAL_ADDRESS_SHIFT | mode_hdisplay << HORIZONTAL_ADDRESS_SHIFT);
1356 	}
1357 
1358 	set_dsi_timings(encoder, adjusted_mode);
1359 
1360 	val = intel_dsi->lane_count << DATA_LANES_PRG_REG_SHIFT;
1361 	if (is_cmd_mode(intel_dsi)) {
1362 		val |= intel_dsi->channel << CMD_MODE_CHANNEL_NUMBER_SHIFT;
1363 		val |= CMD_MODE_DATA_WIDTH_8_BIT; /* XXX */
1364 	} else {
1365 		val |= intel_dsi->channel << VID_MODE_CHANNEL_NUMBER_SHIFT;
1366 		val |= pixel_format_to_reg(intel_dsi->pixel_format);
1367 	}
1368 
1369 	tmp = 0;
1370 	if (!intel_dsi->eot_pkt)
1371 		tmp |= EOT_DISABLE;
1372 	if (intel_dsi->clock_stop)
1373 		tmp |= CLOCKSTOP;
1374 
1375 	if (display->platform.geminilake || display->platform.broxton) {
1376 		tmp |= BXT_DPHY_DEFEATURE_EN;
1377 		if (!is_cmd_mode(intel_dsi))
1378 			tmp |= BXT_DEFEATURE_DPI_FIFO_CTR;
1379 	}
1380 
1381 	for_each_dsi_port(port, intel_dsi->ports) {
1382 		intel_de_write(display, MIPI_DSI_FUNC_PRG(display, port), val);
1383 
1384 		/* timeouts for recovery. one frame IIUC. if counter expires,
1385 		 * EOT and stop state. */
1386 
1387 		/*
1388 		 * In burst mode, value greater than one DPI line Time in byte
1389 		 * clock (txbyteclkhs) To timeout this timer 1+ of the above
1390 		 * said value is recommended.
1391 		 *
1392 		 * In non-burst mode, Value greater than one DPI frame time in
1393 		 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
1394 		 * said value is recommended.
1395 		 *
1396 		 * In DBI only mode, value greater than one DBI frame time in
1397 		 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
1398 		 * said value is recommended.
1399 		 */
1400 
1401 		if (is_vid_mode(intel_dsi) &&
1402 			intel_dsi->video_mode == BURST_MODE) {
1403 			intel_de_write(display, MIPI_HS_TX_TIMEOUT(display, port),
1404 				       txbyteclkhs(adjusted_mode->crtc_htotal, bpp, intel_dsi->lane_count, intel_dsi->burst_mode_ratio) + 1);
1405 		} else {
1406 			intel_de_write(display, MIPI_HS_TX_TIMEOUT(display, port),
1407 				       txbyteclkhs(adjusted_mode->crtc_vtotal * adjusted_mode->crtc_htotal, bpp, intel_dsi->lane_count, intel_dsi->burst_mode_ratio) + 1);
1408 		}
1409 		intel_de_write(display, MIPI_LP_RX_TIMEOUT(display, port),
1410 			       intel_dsi->lp_rx_timeout);
1411 		intel_de_write(display, MIPI_TURN_AROUND_TIMEOUT(display, port),
1412 			       intel_dsi->turn_arnd_val);
1413 		intel_de_write(display, MIPI_DEVICE_RESET_TIMER(display, port),
1414 			       intel_dsi->rst_timer_val);
1415 
1416 		/* dphy stuff */
1417 
1418 		/* in terms of low power clock */
1419 		intel_de_write(display, MIPI_INIT_COUNT(display, port),
1420 			       txclkesc(intel_dsi->escape_clk_div, 100));
1421 
1422 		if ((display->platform.geminilake || display->platform.broxton) &&
1423 		    !intel_dsi->dual_link) {
1424 			/*
1425 			 * BXT spec says write MIPI_INIT_COUNT for
1426 			 * both the ports, even if only one is
1427 			 * getting used. So write the other port
1428 			 * if not in dual link mode.
1429 			 */
1430 			intel_de_write(display,
1431 				       MIPI_INIT_COUNT(display, port == PORT_A ? PORT_C : PORT_A),
1432 				       intel_dsi->init_count);
1433 		}
1434 
1435 		/* recovery disables */
1436 		intel_de_write(display, MIPI_EOT_DISABLE(display, port), tmp);
1437 
1438 		/* in terms of low power clock */
1439 		intel_de_write(display, MIPI_INIT_COUNT(display, port),
1440 			       intel_dsi->init_count);
1441 
1442 		/* in terms of txbyteclkhs. actual high to low switch +
1443 		 * MIPI_STOP_STATE_STALL * MIPI_LP_BYTECLK.
1444 		 *
1445 		 * XXX: write MIPI_STOP_STATE_STALL?
1446 		 */
1447 		intel_de_write(display, MIPI_HIGH_LOW_SWITCH_COUNT(display, port),
1448 			       intel_dsi->hs_to_lp_count);
1449 
1450 		/* XXX: low power clock equivalence in terms of byte clock.
1451 		 * the number of byte clocks occupied in one low power clock.
1452 		 * based on txbyteclkhs and txclkesc.
1453 		 * txclkesc time / txbyteclk time * (105 + MIPI_STOP_STATE_STALL
1454 		 * ) / 105.???
1455 		 */
1456 		intel_de_write(display, MIPI_LP_BYTECLK(display, port),
1457 			       intel_dsi->lp_byte_clk);
1458 
1459 		if (display->platform.geminilake) {
1460 			intel_de_write(display, MIPI_TLPX_TIME_COUNT(display, port),
1461 				       intel_dsi->lp_byte_clk);
1462 			/* Shadow of DPHY reg */
1463 			intel_de_write(display, MIPI_CLK_LANE_TIMING(display, port),
1464 				       intel_dsi->dphy_reg);
1465 		}
1466 
1467 		/* the bw essential for transmitting 16 long packets containing
1468 		 * 252 bytes meant for dcs write memory command is programmed in
1469 		 * this register in terms of byte clocks. based on dsi transfer
1470 		 * rate and the number of lanes configured the time taken to
1471 		 * transmit 16 long packets in a dsi stream varies. */
1472 		intel_de_write(display, MIPI_DBI_BW_CTRL(display, port),
1473 			       intel_dsi->bw_timer);
1474 
1475 		intel_de_write(display, MIPI_CLK_LANE_SWITCH_TIME_CNT(display, port),
1476 			       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);
1477 
1478 		if (is_vid_mode(intel_dsi)) {
1479 			u32 fmt = intel_dsi->video_frmt_cfg_bits | IP_TG_CONFIG;
1480 
1481 			/*
1482 			 * Some panels might have resolution which is not a
1483 			 * multiple of 64 like 1366 x 768. Enable RANDOM
1484 			 * resolution support for such panels by default.
1485 			 */
1486 			fmt |= RANDOM_DPI_DISPLAY_RESOLUTION;
1487 
1488 			switch (intel_dsi->video_mode) {
1489 			default:
1490 				MISSING_CASE(intel_dsi->video_mode);
1491 				fallthrough;
1492 			case NON_BURST_SYNC_EVENTS:
1493 				fmt |= VIDEO_MODE_NON_BURST_WITH_SYNC_EVENTS;
1494 				break;
1495 			case NON_BURST_SYNC_PULSE:
1496 				fmt |= VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE;
1497 				break;
1498 			case BURST_MODE:
1499 				fmt |= VIDEO_MODE_BURST;
1500 				break;
1501 			}
1502 
1503 			intel_de_write(display, MIPI_VIDEO_MODE_FORMAT(display, port), fmt);
1504 		}
1505 	}
1506 }
1507 
1508 static void intel_dsi_unprepare(struct intel_encoder *encoder)
1509 {
1510 	struct intel_display *display = to_intel_display(encoder);
1511 	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1512 	enum port port;
1513 
1514 	if (display->platform.geminilake)
1515 		return;
1516 
1517 	for_each_dsi_port(port, intel_dsi->ports) {
1518 		/* Panel commands can be sent when clock is in LP11 */
1519 		intel_de_write(display, MIPI_DEVICE_READY(display, port), 0x0);
1520 
1521 		if (display->platform.geminilake || display->platform.broxton)
1522 			bxt_dsi_reset_clocks(encoder, port);
1523 		else
1524 			vlv_dsi_reset_clocks(encoder, port);
1525 		intel_de_write(display, MIPI_EOT_DISABLE(display, port), CLOCKSTOP);
1526 
1527 		intel_de_rmw(display, MIPI_DSI_FUNC_PRG(display, port), VID_MODE_FORMAT_MASK, 0);
1528 
1529 		intel_de_write(display, MIPI_DEVICE_READY(display, port), 0x1);
1530 	}
1531 }
1532 
1533 static const struct drm_encoder_funcs intel_dsi_funcs = {
1534 	.destroy = intel_encoder_destroy,
1535 };
1536 
1537 static enum drm_mode_status vlv_dsi_mode_valid(struct drm_connector *connector,
1538 					       const struct drm_display_mode *mode)
1539 {
1540 	struct intel_display *display = to_intel_display(connector->dev);
1541 
1542 	if (display->platform.valleyview || display->platform.cherryview) {
1543 		enum drm_mode_status status;
1544 
1545 		status = intel_cpu_transcoder_mode_valid(display, mode);
1546 		if (status != MODE_OK)
1547 			return status;
1548 	}
1549 
1550 	return intel_dsi_mode_valid(connector, mode);
1551 }
1552 
1553 static const struct drm_connector_helper_funcs intel_dsi_connector_helper_funcs = {
1554 	.get_modes = intel_dsi_get_modes,
1555 	.mode_valid = vlv_dsi_mode_valid,
1556 	.atomic_check = intel_digital_connector_atomic_check,
1557 };
1558 
1559 static const struct drm_connector_funcs intel_dsi_connector_funcs = {
1560 	.detect = intel_panel_detect,
1561 	.late_register = intel_connector_register,
1562 	.early_unregister = intel_connector_unregister,
1563 	.destroy = intel_connector_destroy,
1564 	.fill_modes = drm_helper_probe_single_connector_modes,
1565 	.atomic_get_property = intel_digital_connector_atomic_get_property,
1566 	.atomic_set_property = intel_digital_connector_atomic_set_property,
1567 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1568 	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
1569 };
1570 
1571 static void vlv_dsi_add_properties(struct intel_connector *connector)
1572 {
1573 	const struct drm_display_mode *fixed_mode =
1574 		intel_panel_preferred_fixed_mode(connector);
1575 
1576 	intel_attach_scaling_mode_property(&connector->base);
1577 
1578 	drm_connector_set_panel_orientation_with_quirk(&connector->base,
1579 						       intel_dsi_get_panel_orientation(connector),
1580 						       fixed_mode->hdisplay,
1581 						       fixed_mode->vdisplay);
1582 }
1583 
1584 #define NS_KHZ_RATIO		1000000
1585 
1586 #define PREPARE_CNT_MAX		0x3F
1587 #define EXIT_ZERO_CNT_MAX	0x3F
1588 #define CLK_ZERO_CNT_MAX	0xFF
1589 #define TRAIL_CNT_MAX		0x1F
1590 
1591 static void vlv_dphy_param_init(struct intel_dsi *intel_dsi)
1592 {
1593 	struct intel_display *display = to_intel_display(&intel_dsi->base);
1594 	struct intel_connector *connector = intel_dsi->attached_connector;
1595 	struct mipi_config *mipi_config = connector->panel.vbt.dsi.config;
1596 	u32 tlpx_ns, extra_byte_count, tlpx_ui;
1597 	u32 ui_num, ui_den;
1598 	u32 prepare_cnt, exit_zero_cnt, clk_zero_cnt, trail_cnt;
1599 	u32 ths_prepare_ns, tclk_trail_ns;
1600 	u32 tclk_prepare_clkzero, ths_prepare_hszero;
1601 	u32 lp_to_hs_switch, hs_to_lp_switch;
1602 	u32 mul;
1603 
1604 	tlpx_ns = intel_dsi_tlpx_ns(intel_dsi);
1605 
1606 	switch (intel_dsi->lane_count) {
1607 	case 1:
1608 	case 2:
1609 		extra_byte_count = 2;
1610 		break;
1611 	case 3:
1612 		extra_byte_count = 4;
1613 		break;
1614 	case 4:
1615 	default:
1616 		extra_byte_count = 3;
1617 		break;
1618 	}
1619 
1620 	/* in Kbps */
1621 	ui_num = NS_KHZ_RATIO;
1622 	ui_den = intel_dsi_bitrate(intel_dsi);
1623 
1624 	tclk_prepare_clkzero = mipi_config->tclk_prepare_clkzero;
1625 	ths_prepare_hszero = mipi_config->ths_prepare_hszero;
1626 
1627 	/*
1628 	 * B060
1629 	 * LP byte clock = TLPX/ (8UI)
1630 	 */
1631 	intel_dsi->lp_byte_clk = DIV_ROUND_UP(tlpx_ns * ui_den, 8 * ui_num);
1632 
1633 	/* DDR clock period = 2 * UI
1634 	 * UI(sec) = 1/(bitrate * 10^3) (bitrate is in KHZ)
1635 	 * UI(nsec) = 10^6 / bitrate
1636 	 * DDR clock period (nsec) = 2 * UI = (2 * 10^6)/ bitrate
1637 	 * DDR clock count  = ns_value / DDR clock period
1638 	 *
1639 	 * For GEMINILAKE dphy_param_reg will be programmed in terms of
1640 	 * HS byte clock count for other platform in HS ddr clock count
1641 	 */
1642 	mul = display->platform.geminilake ? 8 : 2;
1643 	ths_prepare_ns = max(mipi_config->ths_prepare,
1644 			     mipi_config->tclk_prepare);
1645 
1646 	/* prepare count */
1647 	prepare_cnt = DIV_ROUND_UP(ths_prepare_ns * ui_den, ui_num * mul);
1648 
1649 	if (prepare_cnt > PREPARE_CNT_MAX) {
1650 		drm_dbg_kms(display->drm, "prepare count too high %u\n",
1651 			    prepare_cnt);
1652 		prepare_cnt = PREPARE_CNT_MAX;
1653 	}
1654 
1655 	/* exit zero count */
1656 	exit_zero_cnt = DIV_ROUND_UP(
1657 				(ths_prepare_hszero - ths_prepare_ns) * ui_den,
1658 				ui_num * mul
1659 				);
1660 
1661 	/*
1662 	 * Exit zero is unified val ths_zero and ths_exit
1663 	 * minimum value for ths_exit = 110ns
1664 	 * min (exit_zero_cnt * 2) = 110/UI
1665 	 * exit_zero_cnt = 55/UI
1666 	 */
1667 	if (exit_zero_cnt < (55 * ui_den / ui_num) && (55 * ui_den) % ui_num)
1668 		exit_zero_cnt += 1;
1669 
1670 	if (exit_zero_cnt > EXIT_ZERO_CNT_MAX) {
1671 		drm_dbg_kms(display->drm, "exit zero count too high %u\n",
1672 			    exit_zero_cnt);
1673 		exit_zero_cnt = EXIT_ZERO_CNT_MAX;
1674 	}
1675 
1676 	/* clk zero count */
1677 	clk_zero_cnt = DIV_ROUND_UP(
1678 				(tclk_prepare_clkzero -	ths_prepare_ns)
1679 				* ui_den, ui_num * mul);
1680 
1681 	if (clk_zero_cnt > CLK_ZERO_CNT_MAX) {
1682 		drm_dbg_kms(display->drm, "clock zero count too high %u\n",
1683 			    clk_zero_cnt);
1684 		clk_zero_cnt = CLK_ZERO_CNT_MAX;
1685 	}
1686 
1687 	/* trail count */
1688 	tclk_trail_ns = max(mipi_config->tclk_trail, mipi_config->ths_trail);
1689 	trail_cnt = DIV_ROUND_UP(tclk_trail_ns * ui_den, ui_num * mul);
1690 
1691 	if (trail_cnt > TRAIL_CNT_MAX) {
1692 		drm_dbg_kms(display->drm, "trail count too high %u\n",
1693 			    trail_cnt);
1694 		trail_cnt = TRAIL_CNT_MAX;
1695 	}
1696 
1697 	/* B080 */
1698 	intel_dsi->dphy_reg = exit_zero_cnt << 24 | trail_cnt << 16 |
1699 						clk_zero_cnt << 8 | prepare_cnt;
1700 
1701 	/*
1702 	 * LP to HS switch count = 4TLPX + PREP_COUNT * mul + EXIT_ZERO_COUNT *
1703 	 *					mul + 10UI + Extra Byte Count
1704 	 *
1705 	 * HS to LP switch count = THS-TRAIL + 2TLPX + Extra Byte Count
1706 	 * Extra Byte Count is calculated according to number of lanes.
1707 	 * High Low Switch Count is the Max of LP to HS and
1708 	 * HS to LP switch count
1709 	 *
1710 	 */
1711 	tlpx_ui = DIV_ROUND_UP(tlpx_ns * ui_den, ui_num);
1712 
1713 	/* B044 */
1714 	/* FIXME:
1715 	 * The comment above does not match with the code */
1716 	lp_to_hs_switch = DIV_ROUND_UP(4 * tlpx_ui + prepare_cnt * mul +
1717 						exit_zero_cnt * mul + 10, 8);
1718 
1719 	hs_to_lp_switch = DIV_ROUND_UP(mipi_config->ths_trail + 2 * tlpx_ui, 8);
1720 
1721 	intel_dsi->hs_to_lp_count = max(lp_to_hs_switch, hs_to_lp_switch);
1722 	intel_dsi->hs_to_lp_count += extra_byte_count;
1723 
1724 	/* B088 */
1725 	/* LP -> HS for clock lanes
1726 	 * LP clk sync + LP11 + LP01 + tclk_prepare + tclk_zero +
1727 	 *						extra byte count
1728 	 * 2TPLX + 1TLPX + 1 TPLX(in ns) + prepare_cnt * 2 + clk_zero_cnt *
1729 	 *					2(in UI) + extra byte count
1730 	 * In byteclks = (4TLPX + prepare_cnt * 2 + clk_zero_cnt *2 (in UI)) /
1731 	 *					8 + extra byte count
1732 	 */
1733 	intel_dsi->clk_lp_to_hs_count =
1734 		DIV_ROUND_UP(
1735 			4 * tlpx_ui + prepare_cnt * 2 +
1736 			clk_zero_cnt * 2,
1737 			8);
1738 
1739 	intel_dsi->clk_lp_to_hs_count += extra_byte_count;
1740 
1741 	/* HS->LP for Clock Lanes
1742 	 * Low Power clock synchronisations + 1Tx byteclk + tclk_trail +
1743 	 *						Extra byte count
1744 	 * 2TLPX + 8UI + (trail_count*2)(in UI) + Extra byte count
1745 	 * In byteclks = (2*TLpx(in UI) + trail_count*2 +8)(in UI)/8 +
1746 	 *						Extra byte count
1747 	 */
1748 	intel_dsi->clk_hs_to_lp_count =
1749 		DIV_ROUND_UP(2 * tlpx_ui + trail_cnt * 2 + 8,
1750 			8);
1751 	intel_dsi->clk_hs_to_lp_count += extra_byte_count;
1752 
1753 	intel_dsi_log_params(intel_dsi);
1754 }
1755 
1756 int vlv_dsi_min_cdclk(const struct intel_crtc_state *crtc_state)
1757 {
1758 	struct intel_display *display = to_intel_display(crtc_state);
1759 
1760 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
1761 		return 0;
1762 
1763 	/*
1764 	 * On Valleyview some DSI panels lose (v|h)sync when the clock is lower
1765 	 * than 320000KHz.
1766 	 */
1767 	if (display->platform.valleyview)
1768 		return 320000;
1769 
1770 	/*
1771 	 * On Geminilake once the CDCLK gets as low as 79200
1772 	 * picture gets unstable, despite that values are
1773 	 * correct for DSI PLL and DE PLL.
1774 	 */
1775 	if (display->platform.geminilake)
1776 		return 158400;
1777 
1778 	return 0;
1779 }
1780 
1781 typedef void (*vlv_dsi_dmi_quirk_func)(struct intel_dsi *intel_dsi);
1782 
1783 /*
1784  * Vtotal is wrong on the Asus TF103C leading to the last line of the display
1785  * being shown as the first line. The factory installed Android has a hardcoded
1786  * modeline, causing it to not suffer from this BIOS bug.
1787  *
1788  * Original mode: "1280x800": 60 67700 1280 1312 1328 1376 800 808 812 820 0x8 0xa
1789  * Fixed    mode: "1280x800": 60 67700 1280 1312 1328 1376 800 808 812 816 0x8 0xa
1790  *
1791  * https://gitlab.freedesktop.org/drm/intel/-/issues/9381
1792  */
1793 static void vlv_dsi_asus_tf103c_mode_fixup(struct intel_dsi *intel_dsi)
1794 {
1795 	/* Cast away the const as we want to fixup the mode */
1796 	struct drm_display_mode *fixed_mode = (struct drm_display_mode *)
1797 		intel_panel_preferred_fixed_mode(intel_dsi->attached_connector);
1798 
1799 	if (fixed_mode->vtotal == 820)
1800 		fixed_mode->vtotal -= 4;
1801 }
1802 
1803 /*
1804  * On the Lenovo Yoga Tablet 2 830 / 1050 there are 2 problems:
1805  * 1. The I2C MIPI sequence elements reference bus 3. ACPI has I2C1 - I2C7
1806  *    which under Linux become bus 0 - 6. And the MIPI sequence reference
1807  *    to bus 3 is indented for I2C3 which is bus 2 under Linux.
1808  *
1809  *    Note mipi_exec_i2c() cannot just subtract 1 from the bus
1810  *    given in the I2C MIPI sequence element. Since on other
1811  *    devices the I2C bus-numbers used in the MIPI sequences do
1812  *    actually start at 0.
1813  *
1814  * 2. width_/height_mm contain a bogus 192mm x 120mm size. This is
1815  *    especially a problem on the 8" 830 version which uses a 10:16
1816  *    portrait screen where as the bogus size is 16:10.
1817  *
1818  * https://gitlab.freedesktop.org/drm/intel/-/issues/9379
1819  */
1820 static void vlv_dsi_lenovo_yoga_tab2_size_fixup(struct intel_dsi *intel_dsi)
1821 {
1822 	const struct drm_display_mode *fixed_mode =
1823 		intel_panel_preferred_fixed_mode(intel_dsi->attached_connector);
1824 	struct drm_display_info *info = &intel_dsi->attached_connector->base.display_info;
1825 
1826 	intel_dsi->i2c_bus_num = 2;
1827 
1828 	/*
1829 	 * The 10" 1050 uses a 1920x1200 landscape screen, where as the 8" 830
1830 	 * uses a 1200x1920 portrait screen.
1831 	 */
1832 	if (fixed_mode->hdisplay == 1920) {
1833 		info->width_mm = 216;
1834 		info->height_mm = 135;
1835 	} else {
1836 		info->width_mm = 107;
1837 		info->height_mm = 171;
1838 	}
1839 }
1840 
1841 /*
1842  * On the Lenovo Yoga Tab 3 Pro YT3-X90F there are 2 problems:
1843  * 1. i2c_acpi_find_adapter() picks the wrong adapter causing mipi_exec_i2c()
1844  *    to not work. Fix this by setting i2c_bus_num.
1845  * 2. There is no backlight off MIPI sequence, causing the backlight to stay on.
1846  *    Add a backlight off sequence mirroring the existing backlight on sequence.
1847  *
1848  * https://gitlab.freedesktop.org/drm/intel/-/issues/9380
1849  */
1850 static void vlv_dsi_lenovo_yoga_tab3_backlight_fixup(struct intel_dsi *intel_dsi)
1851 {
1852 	static const u8 backlight_off_sequence[16] = {
1853 		/* Header Seq-id 7, length after header 11 bytes */
1854 		0x07, 0x0b, 0x00, 0x00, 0x00,
1855 		/* MIPI_SEQ_ELEM_I2C bus 0 addr 0x2c reg 0x00 data-len 1 data 0x00 */
1856 		0x04, 0x08, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x01, 0x00,
1857 		/* MIPI_SEQ_ELEM_END */
1858 		0x00
1859 	};
1860 	struct intel_connector *connector = intel_dsi->attached_connector;
1861 
1862 	intel_dsi->i2c_bus_num = 0;
1863 	connector->panel.vbt.dsi.sequence[MIPI_SEQ_BACKLIGHT_OFF] = backlight_off_sequence;
1864 }
1865 
1866 static const struct dmi_system_id vlv_dsi_dmi_quirk_table[] = {
1867 	{
1868 		/* Asus Transformer Pad TF103C */
1869 		.matches = {
1870 			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
1871 			DMI_MATCH(DMI_PRODUCT_NAME, "TF103C"),
1872 		},
1873 		.driver_data = (void *)vlv_dsi_asus_tf103c_mode_fixup,
1874 	},
1875 	{
1876 		/*
1877 		 * Lenovo Yoga Tablet 2 830F/L or 1050F/L (The 8" and 10"
1878 		 * Lenovo Yoga Tablet 2 use the same mainboard)
1879 		 */
1880 		.matches = {
1881 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corp."),
1882 			DMI_MATCH(DMI_PRODUCT_NAME, "VALLEYVIEW C0 PLATFORM"),
1883 			DMI_MATCH(DMI_BOARD_NAME, "BYT-T FFD8"),
1884 			/* Partial match on beginning of BIOS version */
1885 			DMI_MATCH(DMI_BIOS_VERSION, "BLADE_21"),
1886 		},
1887 		.driver_data = (void *)vlv_dsi_lenovo_yoga_tab2_size_fixup,
1888 	},
1889 	{
1890 		/* Lenovo Yoga Tab 3 Pro YT3-X90F */
1891 		.matches = {
1892 			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
1893 			DMI_MATCH(DMI_PRODUCT_VERSION, "Blade3-10A-001"),
1894 		},
1895 		.driver_data = (void *)vlv_dsi_lenovo_yoga_tab3_backlight_fixup,
1896 	},
1897 	{ }
1898 };
1899 
1900 void vlv_dsi_init(struct intel_display *display)
1901 {
1902 	struct intel_dsi *intel_dsi;
1903 	struct intel_encoder *encoder;
1904 	struct intel_connector *connector;
1905 	struct drm_display_mode *current_mode;
1906 	const struct dmi_system_id *dmi_id;
1907 	enum port port;
1908 	enum pipe pipe;
1909 
1910 	drm_dbg_kms(display->drm, "\n");
1911 
1912 	/* There is no detection method for MIPI so rely on VBT */
1913 	if (!intel_bios_is_dsi_present(display, &port))
1914 		return;
1915 
1916 	if (display->platform.geminilake || display->platform.broxton)
1917 		display->dsi.mmio_base = BXT_MIPI_BASE;
1918 	else
1919 		display->dsi.mmio_base = VLV_MIPI_BASE;
1920 
1921 	intel_dsi = kzalloc_obj(*intel_dsi);
1922 	if (!intel_dsi)
1923 		return;
1924 
1925 	connector = intel_connector_alloc();
1926 	if (!connector) {
1927 		kfree(intel_dsi);
1928 		return;
1929 	}
1930 
1931 	encoder = &intel_dsi->base;
1932 	intel_dsi->attached_connector = connector;
1933 
1934 	drm_encoder_init(display->drm, &encoder->base, &intel_dsi_funcs,
1935 			 DRM_MODE_ENCODER_DSI, "DSI %c", port_name(port));
1936 
1937 	encoder->compute_config = intel_dsi_compute_config;
1938 	encoder->pre_enable = intel_dsi_pre_enable;
1939 	if (display->platform.geminilake || display->platform.broxton)
1940 		encoder->enable = bxt_dsi_enable;
1941 	encoder->disable = intel_dsi_disable;
1942 	encoder->post_disable = intel_dsi_post_disable;
1943 	encoder->get_hw_state = intel_dsi_get_hw_state;
1944 	encoder->get_config = intel_dsi_get_config;
1945 	encoder->update_pipe = intel_backlight_update;
1946 	encoder->shutdown = intel_dsi_shutdown;
1947 
1948 	connector->get_hw_state = intel_connector_get_hw_state;
1949 
1950 	encoder->port = port;
1951 	encoder->type = INTEL_OUTPUT_DSI;
1952 	encoder->power_domain = POWER_DOMAIN_PORT_DSI;
1953 	encoder->cloneable = 0;
1954 
1955 	/*
1956 	 * On BYT/CHV, pipe A maps to MIPI DSI port A, pipe B maps to MIPI DSI
1957 	 * port C. BXT isn't limited like this.
1958 	 */
1959 	if (display->platform.geminilake || display->platform.broxton)
1960 		encoder->pipe_mask = ~0;
1961 	else if (port == PORT_A)
1962 		encoder->pipe_mask = BIT(PIPE_A);
1963 	else
1964 		encoder->pipe_mask = BIT(PIPE_B);
1965 
1966 	intel_dsi->panel_power_off_time = ktime_get_boottime();
1967 
1968 	intel_bios_init_panel_late(display, &connector->panel, NULL, NULL);
1969 
1970 	if (connector->panel.vbt.dsi.config->dual_link)
1971 		intel_dsi->ports = BIT(PORT_A) | BIT(PORT_C);
1972 	else
1973 		intel_dsi->ports = BIT(port);
1974 
1975 	if (drm_WARN_ON(display->drm, connector->panel.vbt.dsi.bl_ports & ~intel_dsi->ports))
1976 		connector->panel.vbt.dsi.bl_ports &= intel_dsi->ports;
1977 
1978 	if (drm_WARN_ON(display->drm, connector->panel.vbt.dsi.cabc_ports & ~intel_dsi->ports))
1979 		connector->panel.vbt.dsi.cabc_ports &= intel_dsi->ports;
1980 
1981 	/* Create a DSI host (and a device) for each port. */
1982 	for_each_dsi_port(port, intel_dsi->ports) {
1983 		struct intel_dsi_host *host;
1984 
1985 		host = intel_dsi_host_init(intel_dsi, &intel_dsi_host_ops,
1986 					   port);
1987 		if (!host)
1988 			goto err;
1989 
1990 		intel_dsi->dsi_hosts[port] = host;
1991 	}
1992 
1993 	if (!intel_dsi_vbt_init(intel_dsi, MIPI_DSI_GENERIC_PANEL_ID)) {
1994 		drm_dbg_kms(display->drm, "no device found\n");
1995 		goto err;
1996 	}
1997 
1998 	/* Use clock read-back from current hw-state for fastboot */
1999 	current_mode = intel_encoder_current_mode(encoder);
2000 	if (current_mode) {
2001 		drm_dbg_kms(display->drm, "Calculated pclk %d GOP %d\n",
2002 			    intel_dsi->pclk, current_mode->clock);
2003 		if (intel_fuzzy_clock_check(intel_dsi->pclk,
2004 					    current_mode->clock)) {
2005 			drm_dbg_kms(display->drm, "Using GOP pclk\n");
2006 			intel_dsi->pclk = current_mode->clock;
2007 		}
2008 
2009 		kfree(current_mode);
2010 	}
2011 
2012 	vlv_dphy_param_init(intel_dsi);
2013 
2014 	intel_dsi_vbt_gpio_init(intel_dsi,
2015 				intel_dsi_get_hw_state(encoder, &pipe));
2016 
2017 	drm_connector_init(display->drm, &connector->base, &intel_dsi_connector_funcs,
2018 			   DRM_MODE_CONNECTOR_DSI);
2019 
2020 	drm_connector_helper_add(&connector->base, &intel_dsi_connector_helper_funcs);
2021 
2022 	connector->base.display_info.subpixel_order = SubPixelHorizontalRGB; /*XXX*/
2023 
2024 	intel_connector_attach_encoder(connector, encoder);
2025 
2026 	mutex_lock(&display->drm->mode_config.mutex);
2027 	intel_panel_add_vbt_lfp_fixed_mode(connector);
2028 	mutex_unlock(&display->drm->mode_config.mutex);
2029 
2030 	if (!intel_panel_preferred_fixed_mode(connector)) {
2031 		drm_dbg_kms(display->drm, "no fixed mode\n");
2032 		goto err_cleanup_connector;
2033 	}
2034 
2035 	dmi_id = dmi_first_match(vlv_dsi_dmi_quirk_table);
2036 	if (dmi_id) {
2037 		vlv_dsi_dmi_quirk_func quirk_func =
2038 			(vlv_dsi_dmi_quirk_func)dmi_id->driver_data;
2039 
2040 		quirk_func(intel_dsi);
2041 	}
2042 
2043 	intel_panel_init(connector, NULL);
2044 
2045 	intel_backlight_setup(connector, INVALID_PIPE);
2046 
2047 	vlv_dsi_add_properties(connector);
2048 
2049 	return;
2050 
2051 err_cleanup_connector:
2052 	drm_connector_cleanup(&connector->base);
2053 err:
2054 	drm_encoder_cleanup(&encoder->base);
2055 	kfree(intel_dsi);
2056 	kfree(connector);
2057 }
2058