xref: /linux/drivers/gpu/drm/msm/dsi/dsi_host.c (revision ab91ba61871a896ca2ecc6e048d80a152136da44)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/clk.h>
7 #include <linux/delay.h>
8 #include <linux/dma-mapping.h>
9 #include <linux/err.h>
10 #include <linux/interrupt.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/of.h>
13 #include <linux/of_graph.h>
14 #include <linux/of_irq.h>
15 #include <linux/pinctrl/consumer.h>
16 #include <linux/pm_opp.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <linux/spinlock.h>
20 
21 #include <video/mipi_display.h>
22 
23 #include <drm/display/drm_dsc_helper.h>
24 #include <drm/drm_of.h>
25 
26 #include "dsi.h"
27 #include "dsi.xml.h"
28 #include "sfpb.xml.h"
29 #include "dsi_cfg.h"
30 #include "msm_dsc_helper.h"
31 #include "msm_kms.h"
32 #include "msm_gem.h"
33 #include "phy/dsi_phy.h"
34 
35 #define DSI_RESET_TOGGLE_DELAY_MS 20
36 
37 static int dsi_populate_dsc_params(struct msm_dsi_host *msm_host, struct drm_dsc_config *dsc);
38 
39 static int dsi_get_version(const void __iomem *base, u32 *major, u32 *minor)
40 {
41 	u32 ver;
42 
43 	if (!major || !minor)
44 		return -EINVAL;
45 
46 	/*
47 	 * From DSI6G(v3), addition of a 6G_HW_VERSION register at offset 0
48 	 * makes all other registers 4-byte shifted down.
49 	 *
50 	 * In order to identify between DSI6G(v3) and beyond, and DSIv2 and
51 	 * older, we read the DSI_VERSION register without any shift(offset
52 	 * 0x1f0). In the case of DSIv2, this hast to be a non-zero value. In
53 	 * the case of DSI6G, this has to be zero (the offset points to a
54 	 * scratch register which we never touch)
55 	 */
56 
57 	ver = readl(base + REG_DSI_VERSION);
58 	if (ver) {
59 		/* older dsi host, there is no register shift */
60 		ver = FIELD(ver, DSI_VERSION_MAJOR);
61 		if (ver <= MSM_DSI_VER_MAJOR_V2) {
62 			/* old versions */
63 			*major = ver;
64 			*minor = 0;
65 			return 0;
66 		} else {
67 			return -EINVAL;
68 		}
69 	} else {
70 		/*
71 		 * newer host, offset 0 has 6G_HW_VERSION, the rest of the
72 		 * registers are shifted down, read DSI_VERSION again with
73 		 * the shifted offset
74 		 */
75 		ver = readl(base + DSI_6G_REG_SHIFT + REG_DSI_VERSION);
76 		ver = FIELD(ver, DSI_VERSION_MAJOR);
77 		if (ver == MSM_DSI_VER_MAJOR_6G) {
78 			/* 6G version */
79 			*major = ver;
80 			*minor = readl(base + REG_DSI_6G_HW_VERSION);
81 			return 0;
82 		} else {
83 			return -EINVAL;
84 		}
85 	}
86 }
87 
88 #define DSI_ERR_STATE_ACK			0x0000
89 #define DSI_ERR_STATE_TIMEOUT			0x0001
90 #define DSI_ERR_STATE_DLN0_PHY			0x0002
91 #define DSI_ERR_STATE_FIFO			0x0004
92 #define DSI_ERR_STATE_MDP_FIFO_UNDERFLOW	0x0008
93 #define DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION	0x0010
94 #define DSI_ERR_STATE_PLL_UNLOCKED		0x0020
95 
96 #define DSI_CLK_CTRL_ENABLE_CLKS	\
97 		(DSI_CLK_CTRL_AHBS_HCLK_ON | DSI_CLK_CTRL_AHBM_SCLK_ON | \
98 		DSI_CLK_CTRL_PCLK_ON | DSI_CLK_CTRL_DSICLK_ON | \
99 		DSI_CLK_CTRL_BYTECLK_ON | DSI_CLK_CTRL_ESCCLK_ON | \
100 		DSI_CLK_CTRL_FORCE_ON_DYN_AHBM_HCLK)
101 
102 struct msm_dsi_host {
103 	struct mipi_dsi_host base;
104 
105 	struct platform_device *pdev;
106 	struct drm_device *dev;
107 
108 	int id;
109 
110 	void __iomem *ctrl_base;
111 	phys_addr_t ctrl_size;
112 	struct regulator_bulk_data *supplies;
113 
114 	int num_bus_clks;
115 	struct clk_bulk_data bus_clks[DSI_BUS_CLK_MAX];
116 
117 	struct clk *byte_clk;
118 	struct clk *esc_clk;
119 	struct clk *pixel_clk;
120 	struct clk *byte_intf_clk;
121 
122 	/*
123 	 * Clocks which needs to be properly parented between DISPCC and DSI PHY
124 	 * PLL:
125 	 */
126 	struct clk *byte_src_clk;
127 	struct clk *pixel_src_clk;
128 	struct clk *dsi_pll_byte_clk;
129 	struct clk *dsi_pll_pixel_clk;
130 
131 	unsigned long byte_clk_rate;
132 	bool byte_intf_clk_div_2;
133 	unsigned long pixel_clk_rate;
134 	unsigned long esc_clk_rate;
135 
136 	/* DSI v2 specific clocks */
137 	struct clk *src_clk;
138 
139 	unsigned long src_clk_rate;
140 
141 	const struct msm_dsi_cfg_handler *cfg_hnd;
142 
143 	struct completion dma_comp;
144 	struct completion video_comp;
145 	struct mutex dev_mutex;
146 	struct mutex cmd_mutex;
147 	spinlock_t intr_lock; /* Protect interrupt ctrl register */
148 
149 	u32 err_work_state;
150 	struct work_struct err_work;
151 	struct workqueue_struct *workqueue;
152 
153 	/* DSI 6G TX buffer*/
154 	struct drm_gem_object *tx_gem_obj;
155 	struct drm_gpuvm *vm;
156 
157 	/* DSI v2 TX buffer */
158 	void *tx_buf;
159 	dma_addr_t tx_buf_paddr;
160 
161 	int tx_size;
162 
163 	u8 *rx_buf;
164 
165 	struct regmap *sfpb;
166 
167 	struct drm_display_mode *mode;
168 	struct drm_dsc_config *dsc;
169 	unsigned int dsc_slice_per_pkt;
170 
171 	/* connected device info */
172 	unsigned int channel;
173 	unsigned int lanes;
174 	enum mipi_dsi_pixel_format format;
175 	unsigned long mode_flags;
176 
177 	/* lane data parsed via DT */
178 	int dlane_swap;
179 	int num_data_lanes;
180 
181 	/* from phy DT */
182 	bool cphy_mode;
183 
184 	u32 dma_cmd_ctrl_restore;
185 
186 	bool registered;
187 	bool power_on;
188 	bool enabled;
189 	int irq;
190 };
191 
192 static inline u32 dsi_read(struct msm_dsi_host *msm_host, u32 reg)
193 {
194 	return readl(msm_host->ctrl_base + reg);
195 }
196 
197 static inline void dsi_write(struct msm_dsi_host *msm_host, u32 reg, u32 data)
198 {
199 	writel(data, msm_host->ctrl_base + reg);
200 }
201 
202 static const struct msm_dsi_cfg_handler *
203 dsi_get_config(struct msm_dsi_host *msm_host)
204 {
205 	const struct msm_dsi_cfg_handler *cfg_hnd = NULL;
206 	struct device *dev = &msm_host->pdev->dev;
207 	struct clk *ahb_clk;
208 	int ret;
209 	u32 major = 0, minor = 0;
210 
211 	ahb_clk = msm_clk_get(msm_host->pdev, "iface");
212 	if (IS_ERR(ahb_clk)) {
213 		dev_err_probe(dev, PTR_ERR(ahb_clk), "%s: cannot get interface clock\n",
214 			      __func__);
215 		goto exit;
216 	}
217 
218 	pm_runtime_get_sync(dev);
219 
220 	ret = clk_prepare_enable(ahb_clk);
221 	if (ret) {
222 		dev_err_probe(dev, ret, "%s: unable to enable ahb_clk\n", __func__);
223 		goto runtime_put;
224 	}
225 
226 	ret = dsi_get_version(msm_host->ctrl_base, &major, &minor);
227 	if (ret) {
228 		dev_err_probe(dev, ret, "%s: Invalid version\n", __func__);
229 		goto disable_clks;
230 	}
231 
232 	cfg_hnd = msm_dsi_cfg_get(major, minor);
233 
234 	DBG("%s: Version %x:%x\n", __func__, major, minor);
235 
236 disable_clks:
237 	clk_disable_unprepare(ahb_clk);
238 runtime_put:
239 	pm_runtime_put_sync(dev);
240 exit:
241 	return cfg_hnd;
242 }
243 
244 static inline struct msm_dsi_host *to_msm_dsi_host(struct mipi_dsi_host *host)
245 {
246 	return container_of(host, struct msm_dsi_host, base);
247 }
248 
249 int dsi_clk_init_v2(struct msm_dsi_host *msm_host)
250 {
251 	struct platform_device *pdev = msm_host->pdev;
252 	int ret = 0;
253 
254 	msm_host->src_clk = msm_clk_get(pdev, "src");
255 
256 	if (IS_ERR(msm_host->src_clk)) {
257 		ret = PTR_ERR(msm_host->src_clk);
258 		pr_err("%s: can't find src clock. ret=%d\n",
259 			__func__, ret);
260 		msm_host->src_clk = NULL;
261 		return ret;
262 	}
263 
264 	return ret;
265 }
266 
267 int dsi_clk_init_6g_v2(struct msm_dsi_host *msm_host)
268 {
269 	struct platform_device *pdev = msm_host->pdev;
270 	int ret = 0;
271 
272 	msm_host->byte_intf_clk = msm_clk_get(pdev, "byte_intf");
273 	if (IS_ERR(msm_host->byte_intf_clk)) {
274 		ret = PTR_ERR(msm_host->byte_intf_clk);
275 		pr_err("%s: can't find byte_intf clock. ret=%d\n",
276 			__func__, ret);
277 	}
278 
279 	return ret;
280 }
281 
282 int dsi_clk_init_6g_v2_9(struct msm_dsi_host *msm_host)
283 {
284 	struct device *dev = &msm_host->pdev->dev;
285 	int ret;
286 
287 	ret = dsi_clk_init_6g_v2(msm_host);
288 	if (ret)
289 		return ret;
290 
291 	msm_host->byte_src_clk = devm_clk_get(dev, "byte_src");
292 	if (IS_ERR(msm_host->byte_src_clk))
293 		return dev_err_probe(dev, PTR_ERR(msm_host->byte_src_clk),
294 				     "can't get byte_src clock\n");
295 
296 	msm_host->dsi_pll_byte_clk = devm_clk_get(dev, "dsi_pll_byte");
297 	if (IS_ERR(msm_host->dsi_pll_byte_clk))
298 		return dev_err_probe(dev, PTR_ERR(msm_host->dsi_pll_byte_clk),
299 				     "can't get dsi_pll_byte clock\n");
300 
301 	msm_host->pixel_src_clk = devm_clk_get(dev, "pixel_src");
302 	if (IS_ERR(msm_host->pixel_src_clk))
303 		return dev_err_probe(dev, PTR_ERR(msm_host->pixel_src_clk),
304 				     "can't get pixel_src clock\n");
305 
306 	msm_host->dsi_pll_pixel_clk = devm_clk_get(dev, "dsi_pll_pixel");
307 	if (IS_ERR(msm_host->dsi_pll_pixel_clk))
308 		return dev_err_probe(dev, PTR_ERR(msm_host->dsi_pll_pixel_clk),
309 				     "can't get dsi_pll_pixel clock\n");
310 
311 	return 0;
312 }
313 
314 static int dsi_clk_init(struct msm_dsi_host *msm_host)
315 {
316 	struct platform_device *pdev = msm_host->pdev;
317 	const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
318 	const struct msm_dsi_config *cfg = cfg_hnd->cfg;
319 	int i, ret = 0;
320 
321 	/* get bus clocks */
322 	for (i = 0; i < cfg->num_bus_clks; i++)
323 		msm_host->bus_clks[i].id = cfg->bus_clk_names[i];
324 	msm_host->num_bus_clks = cfg->num_bus_clks;
325 
326 	ret = devm_clk_bulk_get(&pdev->dev, msm_host->num_bus_clks, msm_host->bus_clks);
327 	if (ret < 0)
328 		return dev_err_probe(&pdev->dev, ret, "Unable to get clocks\n");
329 
330 	/* get link and source clocks */
331 	msm_host->byte_clk = msm_clk_get(pdev, "byte");
332 	if (IS_ERR(msm_host->byte_clk))
333 		return dev_err_probe(&pdev->dev, PTR_ERR(msm_host->byte_clk),
334 				     "%s: can't find dsi_byte clock\n",
335 				     __func__);
336 
337 	msm_host->pixel_clk = msm_clk_get(pdev, "pixel");
338 	if (IS_ERR(msm_host->pixel_clk))
339 		return dev_err_probe(&pdev->dev, PTR_ERR(msm_host->pixel_clk),
340 				     "%s: can't find dsi_pixel clock\n",
341 				     __func__);
342 
343 	msm_host->esc_clk = msm_clk_get(pdev, "core");
344 	if (IS_ERR(msm_host->esc_clk))
345 		return dev_err_probe(&pdev->dev, PTR_ERR(msm_host->esc_clk),
346 				     "%s: can't find dsi_esc clock\n",
347 				     __func__);
348 
349 	if (cfg_hnd->ops->clk_init_ver)
350 		ret = cfg_hnd->ops->clk_init_ver(msm_host);
351 
352 	return ret;
353 }
354 
355 int msm_dsi_runtime_suspend(struct device *dev)
356 {
357 	struct platform_device *pdev = to_platform_device(dev);
358 	struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
359 	struct mipi_dsi_host *host = msm_dsi->host;
360 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
361 
362 	if (!msm_host->cfg_hnd)
363 		return 0;
364 
365 	clk_bulk_disable_unprepare(msm_host->num_bus_clks, msm_host->bus_clks);
366 
367 	return 0;
368 }
369 
370 int msm_dsi_runtime_resume(struct device *dev)
371 {
372 	struct platform_device *pdev = to_platform_device(dev);
373 	struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
374 	struct mipi_dsi_host *host = msm_dsi->host;
375 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
376 
377 	if (!msm_host->cfg_hnd)
378 		return 0;
379 
380 	return clk_bulk_prepare_enable(msm_host->num_bus_clks, msm_host->bus_clks);
381 }
382 
383 int dsi_link_clk_set_rate_6g(struct msm_dsi_host *msm_host)
384 {
385 	unsigned long byte_intf_clk_rate;
386 	long rounded_byte_clk_rate;
387 	int ret;
388 
389 	rounded_byte_clk_rate = clk_round_rate(msm_host->byte_clk,
390 					       msm_host->byte_clk_rate);
391 	if (rounded_byte_clk_rate < 0) {
392 		pr_err("%s: failed to round byte clock rate, %ld\n",
393 		       __func__, rounded_byte_clk_rate);
394 		return rounded_byte_clk_rate;
395 	}
396 
397 	msm_host->byte_clk_rate = rounded_byte_clk_rate;
398 
399 	DBG("Set clk rates: pclk=%lu, byteclk=%lu",
400 	    msm_host->pixel_clk_rate, msm_host->byte_clk_rate);
401 
402 	ret = dev_pm_opp_set_rate(&msm_host->pdev->dev,
403 				  msm_host->byte_clk_rate);
404 	if (ret) {
405 		pr_err("%s: dev_pm_opp_set_rate failed %d\n", __func__, ret);
406 		return ret;
407 	}
408 
409 	ret = clk_set_rate(msm_host->pixel_clk, msm_host->pixel_clk_rate);
410 	if (ret) {
411 		pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
412 		return ret;
413 	}
414 
415 	if (msm_host->byte_intf_clk) {
416 		byte_intf_clk_rate = msm_host->byte_clk_rate;
417 		if (msm_host->byte_intf_clk_div_2)
418 			byte_intf_clk_rate /= 2;
419 
420 		ret = clk_set_rate(msm_host->byte_intf_clk, byte_intf_clk_rate);
421 		if (ret) {
422 			pr_err("%s: Failed to set rate byte intf clk, %d\n",
423 			       __func__, ret);
424 			return ret;
425 		}
426 	}
427 
428 	return 0;
429 }
430 
431 int dsi_link_clk_set_rate_6g_v2_9(struct msm_dsi_host *msm_host)
432 {
433 	struct device *dev = &msm_host->pdev->dev;
434 	int ret;
435 
436 	/*
437 	 * DSI PHY PLLs have to be enabled to allow reparenting to them, so
438 	 * cannot use assigned-clock-parents.
439 	 */
440 	ret = clk_set_parent(msm_host->byte_src_clk, msm_host->dsi_pll_byte_clk);
441 	if (ret)
442 		dev_err(dev, "Failed to parent byte_src -> dsi_pll_byte: %d\n", ret);
443 
444 	ret = clk_set_parent(msm_host->pixel_src_clk, msm_host->dsi_pll_pixel_clk);
445 	if (ret)
446 		dev_err(dev, "Failed to parent pixel_src -> dsi_pll_pixel: %d\n", ret);
447 
448 	return dsi_link_clk_set_rate_6g(msm_host);
449 }
450 
451 int dsi_link_clk_enable_6g(struct msm_dsi_host *msm_host)
452 {
453 	int ret;
454 
455 	ret = clk_prepare_enable(msm_host->esc_clk);
456 	if (ret) {
457 		pr_err("%s: Failed to enable dsi esc clk\n", __func__);
458 		goto error;
459 	}
460 
461 	ret = clk_prepare_enable(msm_host->byte_clk);
462 	if (ret) {
463 		pr_err("%s: Failed to enable dsi byte clk\n", __func__);
464 		goto byte_clk_err;
465 	}
466 
467 	ret = clk_prepare_enable(msm_host->pixel_clk);
468 	if (ret) {
469 		pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
470 		goto pixel_clk_err;
471 	}
472 
473 	ret = clk_prepare_enable(msm_host->byte_intf_clk);
474 	if (ret) {
475 		pr_err("%s: Failed to enable byte intf clk\n",
476 			   __func__);
477 		goto byte_intf_clk_err;
478 	}
479 
480 	return 0;
481 
482 byte_intf_clk_err:
483 	clk_disable_unprepare(msm_host->pixel_clk);
484 pixel_clk_err:
485 	clk_disable_unprepare(msm_host->byte_clk);
486 byte_clk_err:
487 	clk_disable_unprepare(msm_host->esc_clk);
488 error:
489 	return ret;
490 }
491 
492 int dsi_link_clk_set_rate_v2(struct msm_dsi_host *msm_host)
493 {
494 	int ret;
495 
496 	DBG("Set clk rates: pclk=%lu, byteclk=%lu, esc_clk=%lu, dsi_src_clk=%lu",
497 	    msm_host->pixel_clk_rate, msm_host->byte_clk_rate,
498 	    msm_host->esc_clk_rate, msm_host->src_clk_rate);
499 
500 	ret = clk_set_rate(msm_host->byte_clk, msm_host->byte_clk_rate);
501 	if (ret) {
502 		pr_err("%s: Failed to set rate byte clk, %d\n", __func__, ret);
503 		return ret;
504 	}
505 
506 	ret = clk_set_rate(msm_host->esc_clk, msm_host->esc_clk_rate);
507 	if (ret) {
508 		pr_err("%s: Failed to set rate esc clk, %d\n", __func__, ret);
509 		return ret;
510 	}
511 
512 	ret = clk_set_rate(msm_host->src_clk, msm_host->src_clk_rate);
513 	if (ret) {
514 		pr_err("%s: Failed to set rate src clk, %d\n", __func__, ret);
515 		return ret;
516 	}
517 
518 	ret = clk_set_rate(msm_host->pixel_clk, msm_host->pixel_clk_rate);
519 	if (ret) {
520 		pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
521 		return ret;
522 	}
523 
524 	return 0;
525 }
526 
527 int dsi_link_clk_enable_v2(struct msm_dsi_host *msm_host)
528 {
529 	int ret;
530 
531 	ret = clk_prepare_enable(msm_host->byte_clk);
532 	if (ret) {
533 		pr_err("%s: Failed to enable dsi byte clk\n", __func__);
534 		goto error;
535 	}
536 
537 	ret = clk_prepare_enable(msm_host->esc_clk);
538 	if (ret) {
539 		pr_err("%s: Failed to enable dsi esc clk\n", __func__);
540 		goto esc_clk_err;
541 	}
542 
543 	ret = clk_prepare_enable(msm_host->src_clk);
544 	if (ret) {
545 		pr_err("%s: Failed to enable dsi src clk\n", __func__);
546 		goto src_clk_err;
547 	}
548 
549 	ret = clk_prepare_enable(msm_host->pixel_clk);
550 	if (ret) {
551 		pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
552 		goto pixel_clk_err;
553 	}
554 
555 	return 0;
556 
557 pixel_clk_err:
558 	clk_disable_unprepare(msm_host->src_clk);
559 src_clk_err:
560 	clk_disable_unprepare(msm_host->esc_clk);
561 esc_clk_err:
562 	clk_disable_unprepare(msm_host->byte_clk);
563 error:
564 	return ret;
565 }
566 
567 void dsi_link_clk_disable_6g(struct msm_dsi_host *msm_host)
568 {
569 	clk_disable_unprepare(msm_host->esc_clk);
570 	clk_disable_unprepare(msm_host->pixel_clk);
571 	clk_disable_unprepare(msm_host->byte_intf_clk);
572 	clk_disable_unprepare(msm_host->byte_clk);
573 }
574 
575 void dsi_link_clk_disable_v2(struct msm_dsi_host *msm_host)
576 {
577 	clk_disable_unprepare(msm_host->pixel_clk);
578 	clk_disable_unprepare(msm_host->src_clk);
579 	clk_disable_unprepare(msm_host->esc_clk);
580 	clk_disable_unprepare(msm_host->byte_clk);
581 }
582 
583 /**
584  * dsi_adjust_pclk_for_compression() - Adjust the pclk rate for compression case
585  * @mode: The selected mode for the DSI output
586  * @dsc: DRM DSC configuration for this DSI output
587  * @is_bonded_dsi: True if two DSI controllers are bonded
588  *
589  * Adjust the pclk rate by calculating a new hdisplay proportional to
590  * the compression ratio such that:
591  *     new_hdisplay = old_hdisplay * compressed_bpp / uncompressed_bpp
592  *
593  * Porches do not need to be adjusted:
594  * - For VIDEO mode they are not compressed by DSC and are passed as is.
595  * - For CMD mode there are no actual porches. Instead these fields
596  *   currently represent the overhead to the image data transfer. As such, they
597  *   are calculated for the final mode parameters (after the compression) and
598  *   are not to be adjusted too.
599  *
600  *  FIXME: Reconsider this if/when CMD mode handling is rewritten to use
601  *  transfer time and data overhead as a starting point of the calculations.
602  */
603 static unsigned long
604 dsi_adjust_pclk_for_compression(const struct drm_display_mode *mode,
605 				const struct drm_dsc_config *dsc,
606 				bool is_bonded_dsi)
607 {
608 	int hdisplay, new_hdisplay, new_htotal;
609 
610 	/*
611 	 * For bonded DSI, split hdisplay across two links and round up each
612 	 * half separately, passing the full hdisplay would only round up once.
613 	 * This also aligns with the hdisplay we program later in
614 	 * dsi_timing_setup()
615 	 */
616 	hdisplay = mode->hdisplay;
617 	if (is_bonded_dsi)
618 		hdisplay /= 2;
619 
620 	new_hdisplay = DIV_ROUND_UP(hdisplay * drm_dsc_get_bpp_int(dsc),
621 				    dsc->bits_per_component * 3);
622 
623 	if (is_bonded_dsi)
624 		new_hdisplay *= 2;
625 
626 	new_htotal = mode->htotal - mode->hdisplay + new_hdisplay;
627 
628 	return mult_frac(mode->clock * 1000u, new_htotal, mode->htotal);
629 }
630 
631 static unsigned long dsi_get_pclk_rate(const struct drm_display_mode *mode,
632 		const struct drm_dsc_config *dsc, bool is_bonded_dsi)
633 {
634 	unsigned long pclk_rate;
635 
636 	pclk_rate = mode->clock * 1000u;
637 
638 	if (dsc)
639 		pclk_rate = dsi_adjust_pclk_for_compression(mode, dsc, is_bonded_dsi);
640 
641 	/*
642 	 * For bonded DSI mode, the current DRM mode has the complete width of the
643 	 * panel. Since, the complete panel is driven by two DSI controllers,
644 	 * the clock rates have to be split between the two dsi controllers.
645 	 * Adjust the byte and pixel clock rates for each dsi host accordingly.
646 	 */
647 	if (is_bonded_dsi)
648 		pclk_rate /= 2;
649 
650 	return pclk_rate;
651 }
652 
653 unsigned long dsi_byte_clk_get_rate(struct mipi_dsi_host *host, bool is_bonded_dsi,
654 				    const struct drm_display_mode *mode)
655 {
656 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
657 	u8 lanes = msm_host->lanes;
658 	u32 bpp = mipi_dsi_pixel_format_to_bpp(msm_host->format);
659 	unsigned long pclk_rate = dsi_get_pclk_rate(mode, msm_host->dsc, is_bonded_dsi);
660 	unsigned long pclk_bpp;
661 
662 	if (lanes == 0) {
663 		pr_err("%s: forcing mdss_dsi lanes to 1\n", __func__);
664 		lanes = 1;
665 	}
666 
667 	/* CPHY "byte_clk" is in units of 16 bits */
668 	if (msm_host->cphy_mode)
669 		pclk_bpp = mult_frac(pclk_rate, bpp, 16 * lanes);
670 	else
671 		pclk_bpp = mult_frac(pclk_rate, bpp, 8 * lanes);
672 
673 	return pclk_bpp;
674 }
675 
676 static void dsi_calc_pclk(struct msm_dsi_host *msm_host, bool is_bonded_dsi)
677 {
678 	msm_host->pixel_clk_rate = dsi_get_pclk_rate(msm_host->mode, msm_host->dsc, is_bonded_dsi);
679 	msm_host->byte_clk_rate = dsi_byte_clk_get_rate(&msm_host->base, is_bonded_dsi,
680 							msm_host->mode);
681 
682 	DBG("pclk=%lu, bclk=%lu", msm_host->pixel_clk_rate,
683 				msm_host->byte_clk_rate);
684 }
685 
686 int dsi_calc_clk_rate_6g(struct msm_dsi_host *msm_host, bool is_bonded_dsi)
687 {
688 	if (!msm_host->mode) {
689 		pr_err("%s: mode not set\n", __func__);
690 		return -EINVAL;
691 	}
692 
693 	dsi_calc_pclk(msm_host, is_bonded_dsi);
694 	msm_host->esc_clk_rate = clk_get_rate(msm_host->esc_clk);
695 	return 0;
696 }
697 
698 int dsi_calc_clk_rate_v2(struct msm_dsi_host *msm_host, bool is_bonded_dsi)
699 {
700 	u32 bpp = mipi_dsi_pixel_format_to_bpp(msm_host->format);
701 	unsigned int esc_mhz, esc_div;
702 	unsigned long byte_mhz;
703 
704 	dsi_calc_pclk(msm_host, is_bonded_dsi);
705 
706 	msm_host->src_clk_rate = mult_frac(msm_host->pixel_clk_rate, bpp, 8);
707 
708 	/*
709 	 * esc clock is byte clock followed by a 4 bit divider,
710 	 * we need to find an escape clock frequency within the
711 	 * mipi DSI spec range within the maximum divider limit
712 	 * We iterate here between an escape clock frequencey
713 	 * between 20 Mhz to 5 Mhz and pick up the first one
714 	 * that can be supported by our divider
715 	 */
716 
717 	byte_mhz = msm_host->byte_clk_rate / 1000000;
718 
719 	for (esc_mhz = 20; esc_mhz >= 5; esc_mhz--) {
720 		esc_div = DIV_ROUND_UP(byte_mhz, esc_mhz);
721 
722 		/*
723 		 * TODO: Ideally, we shouldn't know what sort of divider
724 		 * is available in mmss_cc, we're just assuming that
725 		 * it'll always be a 4 bit divider. Need to come up with
726 		 * a better way here.
727 		 */
728 		if (esc_div >= 1 && esc_div <= 16)
729 			break;
730 	}
731 
732 	if (esc_mhz < 5)
733 		return -EINVAL;
734 
735 	msm_host->esc_clk_rate = msm_host->byte_clk_rate / esc_div;
736 
737 	DBG("esc=%lu, src=%lu", msm_host->esc_clk_rate,
738 		msm_host->src_clk_rate);
739 
740 	return 0;
741 }
742 
743 static void dsi_intr_ctrl(struct msm_dsi_host *msm_host, u32 mask, int enable)
744 {
745 	u32 intr;
746 	unsigned long flags;
747 
748 	spin_lock_irqsave(&msm_host->intr_lock, flags);
749 	intr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
750 
751 	if (enable)
752 		intr |= mask;
753 	else
754 		intr &= ~mask;
755 
756 	DBG("intr=%x enable=%d", intr, enable);
757 
758 	dsi_write(msm_host, REG_DSI_INTR_CTRL, intr);
759 	spin_unlock_irqrestore(&msm_host->intr_lock, flags);
760 }
761 
762 static inline enum dsi_traffic_mode dsi_get_traffic_mode(const u32 mode_flags)
763 {
764 	if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
765 		return BURST_MODE;
766 	else if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
767 		return NON_BURST_SYNCH_PULSE;
768 
769 	return NON_BURST_SYNCH_EVENT;
770 }
771 
772 static inline enum dsi_vid_dst_format
773 dsi_get_vid_fmt(const enum mipi_dsi_pixel_format mipi_fmt)
774 {
775 	switch (mipi_fmt) {
776 	case MIPI_DSI_FMT_RGB101010:	return VID_DST_FORMAT_RGB101010;
777 	case MIPI_DSI_FMT_RGB888:	return VID_DST_FORMAT_RGB888;
778 	case MIPI_DSI_FMT_RGB666:	return VID_DST_FORMAT_RGB666_LOOSE;
779 	case MIPI_DSI_FMT_RGB666_PACKED:	return VID_DST_FORMAT_RGB666;
780 	case MIPI_DSI_FMT_RGB565:	return VID_DST_FORMAT_RGB565;
781 	default:			return VID_DST_FORMAT_RGB888;
782 	}
783 }
784 
785 static inline enum dsi_cmd_dst_format
786 dsi_get_cmd_fmt(const enum mipi_dsi_pixel_format mipi_fmt)
787 {
788 	switch (mipi_fmt) {
789 	case MIPI_DSI_FMT_RGB101010:	return CMD_DST_FORMAT_RGB101010;
790 	case MIPI_DSI_FMT_RGB888:	return CMD_DST_FORMAT_RGB888;
791 	case MIPI_DSI_FMT_RGB666_PACKED:
792 	case MIPI_DSI_FMT_RGB666:	return CMD_DST_FORMAT_RGB666;
793 	case MIPI_DSI_FMT_RGB565:	return CMD_DST_FORMAT_RGB565;
794 	default:			return CMD_DST_FORMAT_RGB888;
795 	}
796 }
797 
798 static void dsi_ctrl_disable(struct msm_dsi_host *msm_host)
799 {
800 	dsi_write(msm_host, REG_DSI_CTRL, 0);
801 }
802 
803 static bool msm_dsi_host_version_geq(struct msm_dsi_host *msm_host,
804 				    u32 major, u32 minor)
805 {
806 	return msm_host->cfg_hnd->major > major ||
807 	       (msm_host->cfg_hnd->major == major &&
808 	       msm_host->cfg_hnd->minor >= minor);
809 }
810 
811 bool msm_dsi_host_is_wide_bus_enabled(struct mipi_dsi_host *host)
812 {
813 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
814 
815 	return msm_host->dsc &&
816 		msm_dsi_host_version_geq(msm_host, MSM_DSI_VER_MAJOR_6G,
817 					MSM_DSI_6G_VER_MINOR_V2_5_0);
818 }
819 
820 static void dsi_ctrl_enable(struct msm_dsi_host *msm_host,
821 			struct msm_dsi_phy_shared_timings *phy_shared_timings, struct msm_dsi_phy *phy)
822 {
823 	u32 flags = msm_host->mode_flags;
824 	enum mipi_dsi_pixel_format mipi_fmt = msm_host->format;
825 	const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
826 	u32 data = 0, lane_ctrl = 0;
827 
828 	if (flags & MIPI_DSI_MODE_VIDEO) {
829 		if (flags & MIPI_DSI_MODE_VIDEO_HSE)
830 			data |= DSI_VID_CFG0_PULSE_MODE_HSA_HE;
831 		if (flags & MIPI_DSI_MODE_VIDEO_NO_HFP)
832 			data |= DSI_VID_CFG0_HFP_POWER_STOP;
833 		if (flags & MIPI_DSI_MODE_VIDEO_NO_HBP)
834 			data |= DSI_VID_CFG0_HBP_POWER_STOP;
835 		if (flags & MIPI_DSI_MODE_VIDEO_NO_HSA)
836 			data |= DSI_VID_CFG0_HSA_POWER_STOP;
837 		/* Always set low power stop mode for BLLP
838 		 * to let command engine send packets
839 		 */
840 		data |= DSI_VID_CFG0_EOF_BLLP_POWER_STOP |
841 			DSI_VID_CFG0_BLLP_POWER_STOP;
842 		data |= DSI_VID_CFG0_TRAFFIC_MODE(dsi_get_traffic_mode(flags));
843 		data |= DSI_VID_CFG0_DST_FORMAT(dsi_get_vid_fmt(mipi_fmt));
844 		data |= DSI_VID_CFG0_VIRT_CHANNEL(msm_host->channel);
845 		if (msm_dsi_host_is_wide_bus_enabled(&msm_host->base))
846 			data |= DSI_VID_CFG0_DATABUS_WIDEN;
847 		dsi_write(msm_host, REG_DSI_VID_CFG0, data);
848 
849 		/* Do not swap RGB colors */
850 		data = DSI_VID_CFG1_RGB_SWAP(SWAP_RGB);
851 		dsi_write(msm_host, REG_DSI_VID_CFG1, 0);
852 	} else {
853 		/* Do not swap RGB colors */
854 		data = DSI_CMD_CFG0_RGB_SWAP(SWAP_RGB);
855 		data |= DSI_CMD_CFG0_DST_FORMAT(dsi_get_cmd_fmt(mipi_fmt));
856 		dsi_write(msm_host, REG_DSI_CMD_CFG0, data);
857 
858 		data = DSI_CMD_CFG1_WR_MEM_START(MIPI_DCS_WRITE_MEMORY_START) |
859 			DSI_CMD_CFG1_WR_MEM_CONTINUE(
860 					MIPI_DCS_WRITE_MEMORY_CONTINUE);
861 		/* Always insert DCS command */
862 		data |= DSI_CMD_CFG1_INSERT_DCS_COMMAND;
863 		dsi_write(msm_host, REG_DSI_CMD_CFG1, data);
864 
865 		if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
866 			data = dsi_read(msm_host, REG_DSI_CMD_MODE_MDP_CTRL2);
867 
868 			if (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_3)
869 				data |= DSI_CMD_MODE_MDP_CTRL2_BURST_MODE;
870 
871 			if (msm_dsi_host_is_wide_bus_enabled(&msm_host->base))
872 				data |= DSI_CMD_MODE_MDP_CTRL2_DATABUS_WIDEN;
873 
874 			dsi_write(msm_host, REG_DSI_CMD_MODE_MDP_CTRL2, data);
875 		}
876 	}
877 
878 	dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL,
879 			DSI_CMD_DMA_CTRL_FROM_FRAME_BUFFER |
880 			DSI_CMD_DMA_CTRL_LOW_POWER);
881 
882 	data = 0;
883 	/* Always assume dedicated TE pin */
884 	data |= DSI_TRIG_CTRL_TE;
885 	data |= DSI_TRIG_CTRL_MDP_TRIGGER(TRIGGER_NONE);
886 	data |= DSI_TRIG_CTRL_DMA_TRIGGER(TRIGGER_SW);
887 	data |= DSI_TRIG_CTRL_STREAM(msm_host->channel);
888 	if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
889 		(cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_2))
890 		data |= DSI_TRIG_CTRL_BLOCK_DMA_WITHIN_FRAME;
891 	dsi_write(msm_host, REG_DSI_TRIG_CTRL, data);
892 
893 	data = DSI_CLKOUT_TIMING_CTRL_T_CLK_POST(phy_shared_timings->clk_post) |
894 		DSI_CLKOUT_TIMING_CTRL_T_CLK_PRE(phy_shared_timings->clk_pre);
895 	dsi_write(msm_host, REG_DSI_CLKOUT_TIMING_CTRL, data);
896 
897 	if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
898 	    (cfg_hnd->minor > MSM_DSI_6G_VER_MINOR_V1_0) &&
899 	    phy_shared_timings->clk_pre_inc_by_2)
900 		dsi_write(msm_host, REG_DSI_T_CLK_PRE_EXTEND,
901 			  DSI_T_CLK_PRE_EXTEND_INC_BY_2_BYTECLK);
902 
903 	data = 0;
904 	if (!(flags & MIPI_DSI_MODE_NO_EOT_PACKET))
905 		data |= DSI_EOT_PACKET_CTRL_TX_EOT_APPEND;
906 	dsi_write(msm_host, REG_DSI_EOT_PACKET_CTRL, data);
907 
908 	/* allow only ack-err-status to generate interrupt */
909 	dsi_write(msm_host, REG_DSI_ERR_INT_MASK0, 0x13ff3fe0);
910 
911 	dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
912 
913 	dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
914 
915 	data = DSI_CTRL_CLK_EN;
916 
917 	DBG("lane number=%d", msm_host->lanes);
918 	data |= ((DSI_CTRL_LANE0 << msm_host->lanes) - DSI_CTRL_LANE0);
919 
920 	dsi_write(msm_host, REG_DSI_LANE_SWAP_CTRL,
921 		  DSI_LANE_SWAP_CTRL_DLN_SWAP_SEL(msm_host->dlane_swap));
922 
923 	if (!(flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)) {
924 		lane_ctrl = dsi_read(msm_host, REG_DSI_LANE_CTRL);
925 
926 		if (msm_dsi_phy_set_continuous_clock(phy, true))
927 			lane_ctrl &= ~DSI_LANE_CTRL_HS_REQ_SEL_PHY;
928 
929 		dsi_write(msm_host, REG_DSI_LANE_CTRL,
930 			lane_ctrl | DSI_LANE_CTRL_CLKLN_HS_FORCE_REQUEST);
931 	}
932 
933 	data |= DSI_CTRL_ENABLE;
934 
935 	dsi_write(msm_host, REG_DSI_CTRL, data);
936 
937 	if (msm_host->cphy_mode)
938 		dsi_write(msm_host, REG_DSI_CPHY_MODE_CTRL, BIT(0));
939 }
940 
941 static void dsi_update_dsc_timing(struct msm_dsi_host *msm_host, bool is_cmd_mode)
942 {
943 	struct drm_dsc_config *dsc = msm_host->dsc;
944 	u32 reg, reg_ctrl, reg_ctrl2;
945 	u32 slice_per_intf, total_bytes_per_intf;
946 	u32 pkt_per_line;
947 	u32 eol_byte_num;
948 	u32 bytes_per_pkt;
949 
950 	/* first calculate dsc parameters and then program
951 	 * compress mode registers
952 	 */
953 	slice_per_intf = dsc->slice_count;
954 
955 	total_bytes_per_intf = dsc->slice_chunk_size * slice_per_intf;
956 	bytes_per_pkt = dsc->slice_chunk_size * msm_host->dsc_slice_per_pkt;
957 
958 	eol_byte_num = total_bytes_per_intf % 3;
959 	pkt_per_line = slice_per_intf / msm_host->dsc_slice_per_pkt;
960 
961 	if (is_cmd_mode) /* packet data type */
962 		reg = DSI_COMMAND_COMPRESSION_MODE_CTRL_STREAM0_DATATYPE(MIPI_DSI_DCS_LONG_WRITE);
963 	else
964 		reg = DSI_VIDEO_COMPRESSION_MODE_CTRL_DATATYPE(MIPI_DSI_COMPRESSED_PIXEL_STREAM);
965 
966 	/* DSI_VIDEO_COMPRESSION_MODE & DSI_COMMAND_COMPRESSION_MODE
967 	 * registers have similar offsets, so for below common code use
968 	 * DSI_VIDEO_COMPRESSION_MODE_XXXX for setting bits
969 	 *
970 	 * pkt_per_line is log2 encoded, >>1 works for supported values (1,2,4)
971 	 */
972 	if (pkt_per_line > 4)
973 		drm_warn_once(msm_host->dev, "pkt_per_line too big");
974 	reg |= DSI_VIDEO_COMPRESSION_MODE_CTRL_PKT_PER_LINE(pkt_per_line >> 1);
975 	reg |= DSI_VIDEO_COMPRESSION_MODE_CTRL_EOL_BYTE_NUM(eol_byte_num);
976 	reg |= DSI_VIDEO_COMPRESSION_MODE_CTRL_EN;
977 
978 	if (is_cmd_mode) {
979 		reg_ctrl = dsi_read(msm_host, REG_DSI_COMMAND_COMPRESSION_MODE_CTRL);
980 		reg_ctrl2 = dsi_read(msm_host, REG_DSI_COMMAND_COMPRESSION_MODE_CTRL2);
981 
982 		reg_ctrl &= ~0xffff;
983 		reg_ctrl |= reg;
984 
985 		reg_ctrl2 &= ~DSI_COMMAND_COMPRESSION_MODE_CTRL2_STREAM0_SLICE_WIDTH__MASK;
986 		reg_ctrl2 |= DSI_COMMAND_COMPRESSION_MODE_CTRL2_STREAM0_SLICE_WIDTH(dsc->slice_chunk_size);
987 
988 		dsi_write(msm_host, REG_DSI_COMMAND_COMPRESSION_MODE_CTRL, reg_ctrl);
989 		dsi_write(msm_host, REG_DSI_COMMAND_COMPRESSION_MODE_CTRL2, reg_ctrl2);
990 	} else {
991 		reg |= DSI_VIDEO_COMPRESSION_MODE_CTRL_WC(bytes_per_pkt);
992 		dsi_write(msm_host, REG_DSI_VIDEO_COMPRESSION_MODE_CTRL, reg);
993 	}
994 }
995 
996 static void dsi_timing_setup(struct msm_dsi_host *msm_host, bool is_bonded_dsi)
997 {
998 	struct drm_display_mode *mode = msm_host->mode;
999 	u32 hs_start = 0, vs_start = 0; /* take sync start as 0 */
1000 	u32 h_total = mode->htotal;
1001 	u32 v_total = mode->vtotal;
1002 	u32 hs_end = mode->hsync_end - mode->hsync_start;
1003 	u32 vs_end = mode->vsync_end - mode->vsync_start;
1004 	u32 ha_start = h_total - mode->hsync_start;
1005 	u32 ha_end = ha_start + mode->hdisplay;
1006 	u32 va_start = v_total - mode->vsync_start;
1007 	u32 va_end = va_start + mode->vdisplay;
1008 	u32 hdisplay = mode->hdisplay;
1009 	u32 wc;
1010 	int ret;
1011 	bool wide_bus_enabled = msm_dsi_host_is_wide_bus_enabled(&msm_host->base);
1012 
1013 	DBG("");
1014 
1015 	/*
1016 	 * For bonded DSI mode, the current DRM mode has
1017 	 * the complete width of the panel. Since, the complete
1018 	 * panel is driven by two DSI controllers, the horizontal
1019 	 * timings have to be split between the two dsi controllers.
1020 	 * Adjust the DSI host timing values accordingly.
1021 	 */
1022 	if (is_bonded_dsi) {
1023 		h_total /= 2;
1024 		hs_end /= 2;
1025 		ha_start /= 2;
1026 		ha_end /= 2;
1027 		hdisplay /= 2;
1028 	}
1029 
1030 	if (msm_host->dsc) {
1031 		struct drm_dsc_config *dsc = msm_host->dsc;
1032 		u32 bits_per_pclk;
1033 
1034 		/* update dsc params with timing params */
1035 		if (!dsc || !mode->hdisplay || !mode->vdisplay) {
1036 			pr_err("DSI: invalid input: pic_width: %d pic_height: %d\n",
1037 			       mode->hdisplay, mode->vdisplay);
1038 			return;
1039 		}
1040 
1041 		dsc->pic_width = mode->hdisplay;
1042 		dsc->pic_height = mode->vdisplay;
1043 		DBG("Mode %dx%d\n", dsc->pic_width, dsc->pic_height);
1044 
1045 		/* we do the calculations for dsc parameters here so that
1046 		 * panel can use these parameters
1047 		 */
1048 		ret = dsi_populate_dsc_params(msm_host, dsc);
1049 		if (ret)
1050 			return;
1051 
1052 		/*
1053 		 * DPU sends 3 bytes per pclk cycle to DSI. If widebus is
1054 		 * enabled, MDP always sends out 48-bit compressed data per
1055 		 * pclk and on average, for video mode, DSI consumes only an
1056 		 * amount of compressed data equivalent to the uncompressed
1057 		 * pixel depth per pclk.
1058 		 *
1059 		 * Calculate the number of pclks needed to transmit one line of
1060 		 * the compressed data.
1061 
1062 		 * The back/font porch and pulse width are kept intact. For
1063 		 * VIDEO mode they represent timing parameters rather than
1064 		 * actual data transfer, see the documentation for
1065 		 * dsi_adjust_pclk_for_compression(). For CMD mode they are
1066 		 * unused anyway.
1067 		 */
1068 		h_total -= hdisplay;
1069 		if (wide_bus_enabled) {
1070 			if (msm_host->mode_flags & MIPI_DSI_MODE_VIDEO)
1071 				bits_per_pclk = dsc->bits_per_component * 3;
1072 			else
1073 				bits_per_pclk = 48;
1074 		} else {
1075 			bits_per_pclk = 24;
1076 		}
1077 
1078 		hdisplay = DIV_ROUND_UP(msm_dsc_get_bytes_per_line(msm_host->dsc) * 8, bits_per_pclk);
1079 
1080 		h_total += hdisplay;
1081 		ha_end = ha_start + hdisplay;
1082 	}
1083 
1084 	if (msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) {
1085 		if (msm_host->dsc)
1086 			dsi_update_dsc_timing(msm_host, false);
1087 
1088 		dsi_write(msm_host, REG_DSI_ACTIVE_H,
1089 			DSI_ACTIVE_H_START(ha_start) |
1090 			DSI_ACTIVE_H_END(ha_end));
1091 		dsi_write(msm_host, REG_DSI_ACTIVE_V,
1092 			DSI_ACTIVE_V_START(va_start) |
1093 			DSI_ACTIVE_V_END(va_end));
1094 		dsi_write(msm_host, REG_DSI_TOTAL,
1095 			DSI_TOTAL_H_TOTAL(h_total - 1) |
1096 			DSI_TOTAL_V_TOTAL(v_total - 1));
1097 
1098 		dsi_write(msm_host, REG_DSI_ACTIVE_HSYNC,
1099 			DSI_ACTIVE_HSYNC_START(hs_start) |
1100 			DSI_ACTIVE_HSYNC_END(hs_end));
1101 		dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_HPOS, 0);
1102 		dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_VPOS,
1103 			DSI_ACTIVE_VSYNC_VPOS_START(vs_start) |
1104 			DSI_ACTIVE_VSYNC_VPOS_END(vs_end));
1105 	} else {		/* command mode */
1106 		if (msm_host->dsc)
1107 			dsi_update_dsc_timing(msm_host, true);
1108 
1109 		/* image data and 1 byte write_memory_start cmd */
1110 		if (!msm_host->dsc)
1111 			wc = hdisplay * mipi_dsi_pixel_format_to_bpp(msm_host->format) / 8 + 1;
1112 		else
1113 			/*
1114 			 * When DSC is enabled, WC = slice_chunk_size * slice_per_pkt + 1.
1115 			 */
1116 			wc = msm_host->dsc->slice_chunk_size * msm_host->dsc_slice_per_pkt + 1;
1117 
1118 		dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM0_CTRL,
1119 			DSI_CMD_MDP_STREAM0_CTRL_WORD_COUNT(wc) |
1120 			DSI_CMD_MDP_STREAM0_CTRL_VIRTUAL_CHANNEL(
1121 					msm_host->channel) |
1122 			DSI_CMD_MDP_STREAM0_CTRL_DATA_TYPE(
1123 					MIPI_DSI_DCS_LONG_WRITE));
1124 
1125 		dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM0_TOTAL,
1126 			DSI_CMD_MDP_STREAM0_TOTAL_H_TOTAL(hdisplay) |
1127 			DSI_CMD_MDP_STREAM0_TOTAL_V_TOTAL(mode->vdisplay));
1128 	}
1129 }
1130 
1131 static void dsi_sw_reset(struct msm_dsi_host *msm_host)
1132 {
1133 	u32 ctrl;
1134 
1135 	ctrl = dsi_read(msm_host, REG_DSI_CTRL);
1136 
1137 	if (ctrl & DSI_CTRL_ENABLE) {
1138 		dsi_write(msm_host, REG_DSI_CTRL, ctrl & ~DSI_CTRL_ENABLE);
1139 		/*
1140 		 * dsi controller need to be disabled before
1141 		 * clocks turned on
1142 		 */
1143 		wmb();
1144 	}
1145 
1146 	dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
1147 	wmb(); /* clocks need to be enabled before reset */
1148 
1149 	/* dsi controller can only be reset while clocks are running */
1150 	dsi_write(msm_host, REG_DSI_RESET, 1);
1151 	msleep(DSI_RESET_TOGGLE_DELAY_MS); /* make sure reset happen */
1152 	dsi_write(msm_host, REG_DSI_RESET, 0);
1153 	wmb(); /* controller out of reset */
1154 
1155 	if (ctrl & DSI_CTRL_ENABLE) {
1156 		dsi_write(msm_host, REG_DSI_CTRL, ctrl);
1157 		wmb();	/* make sure dsi controller enabled again */
1158 	}
1159 }
1160 
1161 static void dsi_op_mode_config(struct msm_dsi_host *msm_host,
1162 					bool video_mode, bool enable)
1163 {
1164 	u32 dsi_ctrl;
1165 
1166 	dsi_ctrl = dsi_read(msm_host, REG_DSI_CTRL);
1167 
1168 	if (!enable) {
1169 		dsi_ctrl &= ~(DSI_CTRL_ENABLE | DSI_CTRL_VID_MODE_EN |
1170 				DSI_CTRL_CMD_MODE_EN);
1171 		dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE |
1172 					DSI_IRQ_MASK_VIDEO_DONE, 0);
1173 	} else {
1174 		if (video_mode) {
1175 			dsi_ctrl |= DSI_CTRL_VID_MODE_EN;
1176 		} else {		/* command mode */
1177 			dsi_ctrl |= DSI_CTRL_CMD_MODE_EN;
1178 			dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE, 1);
1179 		}
1180 		dsi_ctrl |= DSI_CTRL_ENABLE;
1181 	}
1182 
1183 	dsi_write(msm_host, REG_DSI_CTRL, dsi_ctrl);
1184 }
1185 
1186 static void dsi_set_tx_power_mode(int mode, struct msm_dsi_host *msm_host)
1187 {
1188 	u32 data;
1189 
1190 	data = dsi_read(msm_host, REG_DSI_CMD_DMA_CTRL);
1191 
1192 	if (mode == 0)
1193 		data &= ~DSI_CMD_DMA_CTRL_LOW_POWER;
1194 	else
1195 		data |= DSI_CMD_DMA_CTRL_LOW_POWER;
1196 
1197 	dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL, data);
1198 }
1199 
1200 static void dsi_wait4video_done(struct msm_dsi_host *msm_host)
1201 {
1202 	u32 ret = 0;
1203 	struct device *dev = &msm_host->pdev->dev;
1204 
1205 	dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 1);
1206 
1207 	reinit_completion(&msm_host->video_comp);
1208 
1209 	ret = wait_for_completion_timeout(&msm_host->video_comp,
1210 			msecs_to_jiffies(70));
1211 
1212 	if (ret == 0)
1213 		DRM_DEV_ERROR(dev, "wait for video done timed out\n");
1214 
1215 	dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 0);
1216 }
1217 
1218 static void dsi_wait4video_eng_busy(struct msm_dsi_host *msm_host)
1219 {
1220 	u32 data;
1221 
1222 	if (!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO))
1223 		return;
1224 
1225 	data = dsi_read(msm_host, REG_DSI_STATUS0);
1226 
1227 	/* if video mode engine is not busy, its because
1228 	 * either timing engine was not turned on or the
1229 	 * DSI controller has finished transmitting the video
1230 	 * data already, so no need to wait in those cases
1231 	 */
1232 	if (!(data & DSI_STATUS0_VIDEO_MODE_ENGINE_BUSY))
1233 		return;
1234 
1235 	if (msm_host->power_on && msm_host->enabled) {
1236 		dsi_wait4video_done(msm_host);
1237 		/* delay 4 ms to skip BLLP */
1238 		usleep_range(2000, 4000);
1239 	}
1240 }
1241 
1242 int dsi_tx_buf_alloc_6g(struct msm_dsi_host *msm_host, int size)
1243 {
1244 	struct drm_device *dev = msm_host->dev;
1245 	struct msm_drm_private *priv = dev->dev_private;
1246 	uint64_t iova;
1247 	u8 *data;
1248 
1249 	msm_host->vm = drm_gpuvm_get(priv->kms->vm);
1250 
1251 	data = msm_gem_kernel_new(dev, size, MSM_BO_WC,
1252 					msm_host->vm,
1253 					&msm_host->tx_gem_obj, &iova);
1254 
1255 	if (IS_ERR(data)) {
1256 		msm_host->tx_gem_obj = NULL;
1257 		drm_gpuvm_put(msm_host->vm);
1258 		msm_host->vm = NULL;
1259 		return PTR_ERR(data);
1260 	}
1261 
1262 	msm_gem_object_set_name(msm_host->tx_gem_obj, "tx_gem");
1263 
1264 	msm_host->tx_size = msm_host->tx_gem_obj->size;
1265 
1266 	return 0;
1267 }
1268 
1269 int dsi_tx_buf_alloc_v2(struct msm_dsi_host *msm_host, int size)
1270 {
1271 	struct drm_device *dev = msm_host->dev;
1272 
1273 	msm_host->tx_buf = dma_alloc_coherent(dev->dev, size,
1274 					&msm_host->tx_buf_paddr, GFP_KERNEL);
1275 	if (!msm_host->tx_buf)
1276 		return -ENOMEM;
1277 
1278 	msm_host->tx_size = size;
1279 
1280 	return 0;
1281 }
1282 
1283 void msm_dsi_tx_buf_free(struct mipi_dsi_host *host)
1284 {
1285 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1286 	struct drm_device *dev = msm_host->dev;
1287 
1288 	/*
1289 	 * This is possible if we're tearing down before we've had a chance to
1290 	 * fully initialize. A very real possibility if our probe is deferred,
1291 	 * in which case we'll hit msm_dsi_host_destroy() without having run
1292 	 * through the dsi_tx_buf_alloc().
1293 	 */
1294 	if (!dev)
1295 		return;
1296 
1297 	if (msm_host->tx_gem_obj) {
1298 		msm_gem_kernel_put(msm_host->tx_gem_obj, msm_host->vm);
1299 		drm_gpuvm_put(msm_host->vm);
1300 		msm_host->tx_gem_obj = NULL;
1301 		msm_host->vm = NULL;
1302 	}
1303 
1304 	if (msm_host->tx_buf)
1305 		dma_free_coherent(dev->dev, msm_host->tx_size, msm_host->tx_buf,
1306 			msm_host->tx_buf_paddr);
1307 }
1308 
1309 void *dsi_tx_buf_get_6g(struct msm_dsi_host *msm_host)
1310 {
1311 	return msm_gem_get_vaddr(msm_host->tx_gem_obj);
1312 }
1313 
1314 void *dsi_tx_buf_get_v2(struct msm_dsi_host *msm_host)
1315 {
1316 	return msm_host->tx_buf;
1317 }
1318 
1319 void dsi_tx_buf_put_6g(struct msm_dsi_host *msm_host)
1320 {
1321 	msm_gem_put_vaddr(msm_host->tx_gem_obj);
1322 }
1323 
1324 /*
1325  * prepare cmd buffer to be txed
1326  */
1327 static int dsi_cmd_dma_add(struct msm_dsi_host *msm_host,
1328 			   const struct mipi_dsi_msg *msg)
1329 {
1330 	const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
1331 	struct mipi_dsi_packet packet;
1332 	int len;
1333 	int ret;
1334 	u8 *data;
1335 
1336 	ret = mipi_dsi_create_packet(&packet, msg);
1337 	if (ret) {
1338 		pr_err("%s: create packet failed, %d\n", __func__, ret);
1339 		return ret;
1340 	}
1341 	len = (packet.size + 3) & (~0x3);
1342 
1343 	if (len > msm_host->tx_size) {
1344 		pr_err("%s: packet size is too big\n", __func__);
1345 		return -EINVAL;
1346 	}
1347 
1348 	data = cfg_hnd->ops->tx_buf_get(msm_host);
1349 	if (IS_ERR(data)) {
1350 		ret = PTR_ERR(data);
1351 		pr_err("%s: get vaddr failed, %d\n", __func__, ret);
1352 		return ret;
1353 	}
1354 
1355 	/* MSM specific command format in memory */
1356 	data[0] = packet.header[1];
1357 	data[1] = packet.header[2];
1358 	data[2] = packet.header[0];
1359 	data[3] = BIT(7); /* Last packet */
1360 	if (mipi_dsi_packet_format_is_long(msg->type))
1361 		data[3] |= BIT(6);
1362 	if (msg->rx_buf && msg->rx_len)
1363 		data[3] |= BIT(5);
1364 
1365 	/* Long packet */
1366 	if (packet.payload && packet.payload_length)
1367 		memcpy(data + 4, packet.payload, packet.payload_length);
1368 
1369 	/* Append 0xff to the end */
1370 	if (packet.size < len)
1371 		memset(data + packet.size, 0xff, len - packet.size);
1372 
1373 	if (cfg_hnd->ops->tx_buf_put)
1374 		cfg_hnd->ops->tx_buf_put(msm_host);
1375 
1376 	return len;
1377 }
1378 
1379 /*
1380  * dsi_short_read1_resp: 1 parameter
1381  */
1382 static int dsi_short_read1_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1383 {
1384 	u8 *data = msg->rx_buf;
1385 
1386 	if (data && (msg->rx_len >= 1)) {
1387 		*data = buf[1]; /* strip out dcs type */
1388 		return 1;
1389 	}
1390 
1391 	pr_err("%s: read data does not match with rx_buf len %zu\n",
1392 		__func__, msg->rx_len);
1393 	return -EINVAL;
1394 }
1395 
1396 /*
1397  * dsi_short_read2_resp: 2 parameter
1398  */
1399 static int dsi_short_read2_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1400 {
1401 	u8 *data = msg->rx_buf;
1402 
1403 	if (data && (msg->rx_len >= 2)) {
1404 		data[0] = buf[1]; /* strip out dcs type */
1405 		data[1] = buf[2];
1406 		return 2;
1407 	}
1408 
1409 	pr_err("%s: read data does not match with rx_buf len %zu\n",
1410 		__func__, msg->rx_len);
1411 	return -EINVAL;
1412 }
1413 
1414 static int dsi_long_read_resp(u8 *buf, const struct mipi_dsi_msg *msg)
1415 {
1416 	/* strip out 4 byte dcs header */
1417 	if (msg->rx_buf && msg->rx_len)
1418 		memcpy(msg->rx_buf, buf + 4, msg->rx_len);
1419 
1420 	return msg->rx_len;
1421 }
1422 
1423 int dsi_dma_base_get_6g(struct msm_dsi_host *msm_host, uint64_t *dma_base)
1424 {
1425 	struct drm_device *dev = msm_host->dev;
1426 	struct msm_drm_private *priv = dev->dev_private;
1427 
1428 	if (!dma_base)
1429 		return -EINVAL;
1430 
1431 	return msm_gem_get_and_pin_iova(msm_host->tx_gem_obj,
1432 				priv->kms->vm, dma_base);
1433 }
1434 
1435 int dsi_dma_base_get_v2(struct msm_dsi_host *msm_host, uint64_t *dma_base)
1436 {
1437 	if (!dma_base)
1438 		return -EINVAL;
1439 
1440 	*dma_base = msm_host->tx_buf_paddr;
1441 	return 0;
1442 }
1443 
1444 static int dsi_cmd_dma_tx(struct msm_dsi_host *msm_host, int len)
1445 {
1446 	const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
1447 	int ret;
1448 	uint64_t dma_base;
1449 	bool triggered;
1450 
1451 	ret = cfg_hnd->ops->dma_base_get(msm_host, &dma_base);
1452 	if (ret) {
1453 		pr_err("%s: failed to get iova: %d\n", __func__, ret);
1454 		return ret;
1455 	}
1456 
1457 	reinit_completion(&msm_host->dma_comp);
1458 
1459 	dsi_wait4video_eng_busy(msm_host);
1460 
1461 	triggered = msm_dsi_manager_cmd_xfer_trigger(
1462 						msm_host->id, dma_base, len);
1463 	if (triggered) {
1464 		ret = wait_for_completion_timeout(&msm_host->dma_comp,
1465 					msecs_to_jiffies(200));
1466 		DBG("ret=%d", ret);
1467 		if (ret == 0)
1468 			ret = -ETIMEDOUT;
1469 		else
1470 			ret = len;
1471 	} else {
1472 		ret = len;
1473 	}
1474 
1475 	return ret;
1476 }
1477 
1478 static int dsi_cmd_dma_rx(struct msm_dsi_host *msm_host,
1479 			u8 *buf, int rx_byte, int pkt_size)
1480 {
1481 	u32 *temp, data;
1482 	int i, j = 0, cnt;
1483 	u32 read_cnt;
1484 	u8 reg[16];
1485 	int repeated_bytes = 0;
1486 	int buf_offset = buf - msm_host->rx_buf;
1487 
1488 	temp = (u32 *)reg;
1489 	cnt = (rx_byte + 3) >> 2;
1490 	if (cnt > 4)
1491 		cnt = 4; /* 4 x 32 bits registers only */
1492 
1493 	if (rx_byte == 4)
1494 		read_cnt = 4;
1495 	else
1496 		read_cnt = pkt_size + 6;
1497 
1498 	/*
1499 	 * In case of multiple reads from the panel, after the first read, there
1500 	 * is possibility that there are some bytes in the payload repeating in
1501 	 * the RDBK_DATA registers. Since we read all the parameters from the
1502 	 * panel right from the first byte for every pass. We need to skip the
1503 	 * repeating bytes and then append the new parameters to the rx buffer.
1504 	 */
1505 	if (read_cnt > 16) {
1506 		int bytes_shifted;
1507 		/* Any data more than 16 bytes will be shifted out.
1508 		 * The temp read buffer should already contain these bytes.
1509 		 * The remaining bytes in read buffer are the repeated bytes.
1510 		 */
1511 		bytes_shifted = read_cnt - 16;
1512 		repeated_bytes = buf_offset - bytes_shifted;
1513 	}
1514 
1515 	for (i = cnt - 1; i >= 0; i--) {
1516 		data = dsi_read(msm_host, REG_DSI_RDBK_DATA(i));
1517 		*temp++ = ntohl(data); /* to host byte order */
1518 		DBG("data = 0x%x and ntohl(data) = 0x%x", data, ntohl(data));
1519 	}
1520 
1521 	for (i = repeated_bytes; i < 16; i++)
1522 		buf[j++] = reg[i];
1523 
1524 	return j;
1525 }
1526 
1527 static int dsi_cmds2buf_tx(struct msm_dsi_host *msm_host,
1528 				const struct mipi_dsi_msg *msg)
1529 {
1530 	int len, ret;
1531 	int bllp_len = msm_host->mode->hdisplay *
1532 			mipi_dsi_pixel_format_to_bpp(msm_host->format) / 8;
1533 
1534 	len = dsi_cmd_dma_add(msm_host, msg);
1535 	if (len < 0) {
1536 		pr_err("%s: failed to add cmd type = 0x%x\n",
1537 			__func__,  msg->type);
1538 		return len;
1539 	}
1540 
1541 	/*
1542 	 * for video mode, do not send cmds more than
1543 	 * one pixel line, since it only transmit it
1544 	 * during BLLP.
1545 	 *
1546 	 * TODO: if the command is sent in LP mode, the bit rate is only
1547 	 * half of esc clk rate. In this case, if the video is already
1548 	 * actively streaming, we need to check more carefully if the
1549 	 * command can be fit into one BLLP.
1550 	 */
1551 	if ((msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) && (len > bllp_len)) {
1552 		pr_err("%s: cmd cannot fit into BLLP period, len=%d\n",
1553 			__func__, len);
1554 		return -EINVAL;
1555 	}
1556 
1557 	ret = dsi_cmd_dma_tx(msm_host, len);
1558 	if (ret < 0) {
1559 		pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, len=%d, ret=%d\n",
1560 			__func__, msg->type, (*(u8 *)(msg->tx_buf)), len, ret);
1561 		return ret;
1562 	} else if (ret < len) {
1563 		pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, ret=%d len=%d\n",
1564 			__func__, msg->type, (*(u8 *)(msg->tx_buf)), ret, len);
1565 		return -EIO;
1566 	}
1567 
1568 	return len;
1569 }
1570 
1571 static void dsi_err_worker(struct work_struct *work)
1572 {
1573 	struct msm_dsi_host *msm_host =
1574 		container_of(work, struct msm_dsi_host, err_work);
1575 	u32 status = msm_host->err_work_state;
1576 
1577 	pr_err_ratelimited("%s: status=%x\n", __func__, status);
1578 	if (status & DSI_ERR_STATE_MDP_FIFO_UNDERFLOW)
1579 		dsi_sw_reset(msm_host);
1580 
1581 	/* It is safe to clear here because error irq is disabled. */
1582 	msm_host->err_work_state = 0;
1583 
1584 	/* enable dsi error interrupt */
1585 	dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
1586 }
1587 
1588 static void dsi_ack_err_status(struct msm_dsi_host *msm_host)
1589 {
1590 	u32 status;
1591 
1592 	status = dsi_read(msm_host, REG_DSI_ACK_ERR_STATUS);
1593 
1594 	if (status) {
1595 		dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, status);
1596 		/* Writing of an extra 0 needed to clear error bits */
1597 		dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, 0);
1598 		msm_host->err_work_state |= DSI_ERR_STATE_ACK;
1599 	}
1600 }
1601 
1602 static void dsi_timeout_status(struct msm_dsi_host *msm_host)
1603 {
1604 	u32 status;
1605 
1606 	status = dsi_read(msm_host, REG_DSI_TIMEOUT_STATUS);
1607 
1608 	if (status) {
1609 		dsi_write(msm_host, REG_DSI_TIMEOUT_STATUS, status);
1610 		msm_host->err_work_state |= DSI_ERR_STATE_TIMEOUT;
1611 	}
1612 }
1613 
1614 static void dsi_dln0_phy_err(struct msm_dsi_host *msm_host)
1615 {
1616 	u32 status;
1617 
1618 	status = dsi_read(msm_host, REG_DSI_DLN0_PHY_ERR);
1619 
1620 	if (status & (DSI_DLN0_PHY_ERR_DLN0_ERR_ESC |
1621 			DSI_DLN0_PHY_ERR_DLN0_ERR_SYNC_ESC |
1622 			DSI_DLN0_PHY_ERR_DLN0_ERR_CONTROL |
1623 			DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP0 |
1624 			DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP1)) {
1625 		dsi_write(msm_host, REG_DSI_DLN0_PHY_ERR, status);
1626 		msm_host->err_work_state |= DSI_ERR_STATE_DLN0_PHY;
1627 	}
1628 }
1629 
1630 static void dsi_fifo_status(struct msm_dsi_host *msm_host)
1631 {
1632 	u32 status;
1633 
1634 	status = dsi_read(msm_host, REG_DSI_FIFO_STATUS);
1635 
1636 	/* fifo underflow, overflow */
1637 	if (status) {
1638 		dsi_write(msm_host, REG_DSI_FIFO_STATUS, status);
1639 		msm_host->err_work_state |= DSI_ERR_STATE_FIFO;
1640 		if (status & DSI_FIFO_STATUS_CMD_MDP_FIFO_UNDERFLOW)
1641 			msm_host->err_work_state |=
1642 					DSI_ERR_STATE_MDP_FIFO_UNDERFLOW;
1643 	}
1644 }
1645 
1646 static void dsi_status(struct msm_dsi_host *msm_host)
1647 {
1648 	u32 status;
1649 
1650 	status = dsi_read(msm_host, REG_DSI_STATUS0);
1651 
1652 	if (status & DSI_STATUS0_INTERLEAVE_OP_CONTENTION) {
1653 		dsi_write(msm_host, REG_DSI_STATUS0, status);
1654 		msm_host->err_work_state |=
1655 			DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION;
1656 	}
1657 }
1658 
1659 static void dsi_clk_status(struct msm_dsi_host *msm_host)
1660 {
1661 	u32 status;
1662 
1663 	status = dsi_read(msm_host, REG_DSI_CLK_STATUS);
1664 
1665 	if (status & DSI_CLK_STATUS_PLL_UNLOCKED) {
1666 		dsi_write(msm_host, REG_DSI_CLK_STATUS, status);
1667 		msm_host->err_work_state |= DSI_ERR_STATE_PLL_UNLOCKED;
1668 	}
1669 }
1670 
1671 static void dsi_error(struct msm_dsi_host *msm_host)
1672 {
1673 	/* disable dsi error interrupt */
1674 	dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 0);
1675 
1676 	dsi_clk_status(msm_host);
1677 	dsi_fifo_status(msm_host);
1678 	dsi_ack_err_status(msm_host);
1679 	dsi_timeout_status(msm_host);
1680 	dsi_status(msm_host);
1681 	dsi_dln0_phy_err(msm_host);
1682 
1683 	queue_work(msm_host->workqueue, &msm_host->err_work);
1684 }
1685 
1686 static irqreturn_t dsi_host_irq(int irq, void *ptr)
1687 {
1688 	struct msm_dsi_host *msm_host = ptr;
1689 	u32 isr;
1690 	unsigned long flags;
1691 
1692 	if (!msm_host->ctrl_base)
1693 		return IRQ_HANDLED;
1694 
1695 	spin_lock_irqsave(&msm_host->intr_lock, flags);
1696 	isr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
1697 	dsi_write(msm_host, REG_DSI_INTR_CTRL, isr);
1698 	spin_unlock_irqrestore(&msm_host->intr_lock, flags);
1699 
1700 	DBG("isr=0x%x, id=%d", isr, msm_host->id);
1701 
1702 	if (isr & DSI_IRQ_ERROR)
1703 		dsi_error(msm_host);
1704 
1705 	if (isr & DSI_IRQ_VIDEO_DONE)
1706 		complete(&msm_host->video_comp);
1707 
1708 	if (isr & DSI_IRQ_CMD_DMA_DONE)
1709 		complete(&msm_host->dma_comp);
1710 
1711 	return IRQ_HANDLED;
1712 }
1713 
1714 static int dsi_host_attach(struct mipi_dsi_host *host,
1715 					struct mipi_dsi_device *dsi)
1716 {
1717 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1718 	int ret;
1719 
1720 	if (dsi->lanes > msm_host->num_data_lanes)
1721 		return -EINVAL;
1722 
1723 	msm_host->channel = dsi->channel;
1724 	msm_host->lanes = dsi->lanes;
1725 	msm_host->format = dsi->format;
1726 	msm_host->mode_flags = dsi->mode_flags;
1727 	if (dsi->dsc) {
1728 		msm_host->dsc = dsi->dsc;
1729 		if (dsi->mode_flags & MIPI_DSI_MODE_DSC_ALL_SLICES_IN_PKT)
1730 			msm_host->dsc_slice_per_pkt = dsi->dsc->slice_count;
1731 		else
1732 			msm_host->dsc_slice_per_pkt = 1;
1733 	}
1734 
1735 	if (msm_host->format == MIPI_DSI_FMT_RGB101010) {
1736 		if (!msm_dsi_host_version_geq(msm_host, MSM_DSI_VER_MAJOR_6G,
1737 					      MSM_DSI_6G_VER_MINOR_V2_1_0)) {
1738 			DRM_DEV_ERROR(&msm_host->pdev->dev,
1739 				      "RGB101010 not supported on this DSI controller\n");
1740 			return -EINVAL;
1741 		}
1742 
1743 		/*
1744 		 * Downstream overrides RGB101010 back to RGB888 when DSC is enabled
1745 		 * but widebus is not. Using RGB101010 in this case may require some
1746 		 * extra changes.
1747 		 */
1748 		if (msm_host->dsc &&
1749 		    !msm_dsi_host_is_wide_bus_enabled(&msm_host->base)) {
1750 			dev_warn(&msm_host->pdev->dev,
1751 				 "RGB101010 with DSC but without widebus, may need extra changes\n");
1752 		}
1753 	}
1754 
1755 	ret = dsi_dev_attach(msm_host->pdev);
1756 	if (ret)
1757 		return ret;
1758 
1759 	DBG("id=%d", msm_host->id);
1760 
1761 	return 0;
1762 }
1763 
1764 static int dsi_host_detach(struct mipi_dsi_host *host,
1765 					struct mipi_dsi_device *dsi)
1766 {
1767 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1768 
1769 	dsi_dev_detach(msm_host->pdev);
1770 
1771 	DBG("id=%d", msm_host->id);
1772 
1773 	return 0;
1774 }
1775 
1776 static ssize_t dsi_host_transfer(struct mipi_dsi_host *host,
1777 					const struct mipi_dsi_msg *msg)
1778 {
1779 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
1780 	int ret;
1781 
1782 	if (!msg || !msm_host->power_on)
1783 		return -EINVAL;
1784 
1785 	mutex_lock(&msm_host->cmd_mutex);
1786 	ret = msm_dsi_manager_cmd_xfer(msm_host->id, msg);
1787 	mutex_unlock(&msm_host->cmd_mutex);
1788 
1789 	return ret;
1790 }
1791 
1792 static const struct mipi_dsi_host_ops dsi_host_ops = {
1793 	.attach = dsi_host_attach,
1794 	.detach = dsi_host_detach,
1795 	.transfer = dsi_host_transfer,
1796 };
1797 
1798 /*
1799  * List of supported physical to logical lane mappings.
1800  * For example, the 2nd entry represents the following mapping:
1801  *
1802  * "3012": Logic 3->Phys 0; Logic 0->Phys 1; Logic 1->Phys 2; Logic 2->Phys 3;
1803  */
1804 static const int supported_data_lane_swaps[][4] = {
1805 	{ 0, 1, 2, 3 },
1806 	{ 3, 0, 1, 2 },
1807 	{ 2, 3, 0, 1 },
1808 	{ 1, 2, 3, 0 },
1809 	{ 0, 3, 2, 1 },
1810 	{ 1, 0, 3, 2 },
1811 	{ 2, 1, 0, 3 },
1812 	{ 3, 2, 1, 0 },
1813 };
1814 
1815 static int dsi_host_parse_lane_data(struct msm_dsi_host *msm_host,
1816 				    struct device_node *ep)
1817 {
1818 	struct device *dev = &msm_host->pdev->dev;
1819 	struct property *prop;
1820 	u32 lane_map[4];
1821 	int ret, i, len, num_lanes;
1822 
1823 	prop = of_find_property(ep, "data-lanes", &len);
1824 	if (!prop) {
1825 		DRM_DEV_DEBUG(dev,
1826 			"failed to find data lane mapping, using default\n");
1827 		/* Set the number of date lanes to 4 by default. */
1828 		msm_host->num_data_lanes = 4;
1829 		return 0;
1830 	}
1831 
1832 	num_lanes = drm_of_get_data_lanes_count(ep, 1, 4);
1833 	if (num_lanes < 0) {
1834 		DRM_DEV_ERROR(dev, "bad number of data lanes\n");
1835 		return num_lanes;
1836 	}
1837 
1838 	msm_host->num_data_lanes = num_lanes;
1839 
1840 	ret = of_property_read_u32_array(ep, "data-lanes", lane_map,
1841 					 num_lanes);
1842 	if (ret) {
1843 		DRM_DEV_ERROR(dev, "failed to read lane data\n");
1844 		return ret;
1845 	}
1846 
1847 	/*
1848 	 * compare DT specified physical-logical lane mappings with the ones
1849 	 * supported by hardware
1850 	 */
1851 	for (i = 0; i < ARRAY_SIZE(supported_data_lane_swaps); i++) {
1852 		const int *swap = supported_data_lane_swaps[i];
1853 		int j;
1854 
1855 		/*
1856 		 * the data-lanes array we get from DT has a logical->physical
1857 		 * mapping. The "data lane swap" register field represents
1858 		 * supported configurations in a physical->logical mapping.
1859 		 * Translate the DT mapping to what we understand and find a
1860 		 * configuration that works.
1861 		 */
1862 		for (j = 0; j < num_lanes; j++) {
1863 			if (lane_map[j] < 0 || lane_map[j] > 3)
1864 				DRM_DEV_ERROR(dev, "bad physical lane entry %u\n",
1865 					lane_map[j]);
1866 
1867 			if (swap[lane_map[j]] != j)
1868 				break;
1869 		}
1870 
1871 		if (j == num_lanes) {
1872 			msm_host->dlane_swap = i;
1873 			return 0;
1874 		}
1875 	}
1876 
1877 	return -EINVAL;
1878 }
1879 
1880 static int dsi_populate_dsc_params(struct msm_dsi_host *msm_host, struct drm_dsc_config *dsc)
1881 {
1882 	int ret;
1883 
1884 	if (dsc->bits_per_pixel & 0xf) {
1885 		DRM_DEV_ERROR(&msm_host->pdev->dev, "DSI does not support fractional bits_per_pixel\n");
1886 		return -EINVAL;
1887 	}
1888 
1889 	switch (dsc->bits_per_component) {
1890 	case 8:
1891 	case 10:
1892 	case 12:
1893 		/*
1894 		 * Only 8, 10, and 12 bpc are supported for DSC 1.1 block.
1895 		 * If additional bpc values need to be supported, update
1896 		 * this quard with the appropriate DSC version verification.
1897 		 */
1898 		break;
1899 	default:
1900 		DRM_DEV_ERROR(&msm_host->pdev->dev,
1901 			      "Unsupported bits_per_component value: %d\n",
1902 			      dsc->bits_per_component);
1903 		return -EOPNOTSUPP;
1904 	}
1905 
1906 	dsc->simple_422 = 0;
1907 	dsc->convert_rgb = 1;
1908 	dsc->vbr_enable = 0;
1909 
1910 	drm_dsc_set_const_params(dsc);
1911 	drm_dsc_set_rc_buf_thresh(dsc);
1912 
1913 	/* DPU supports only pre-SCR panels */
1914 	ret = drm_dsc_setup_rc_params(dsc, DRM_DSC_1_1_PRE_SCR);
1915 	if (ret) {
1916 		DRM_DEV_ERROR(&msm_host->pdev->dev, "could not find DSC RC parameters\n");
1917 		return ret;
1918 	}
1919 
1920 	dsc->initial_scale_value = drm_dsc_initial_scale_value(dsc);
1921 	dsc->line_buf_depth = dsc->bits_per_component + 1;
1922 
1923 	return drm_dsc_compute_rc_parameters(dsc);
1924 }
1925 
1926 static int dsi_host_parse_dt(struct msm_dsi_host *msm_host)
1927 {
1928 	struct msm_dsi *msm_dsi = platform_get_drvdata(msm_host->pdev);
1929 	struct device *dev = &msm_host->pdev->dev;
1930 	struct device_node *np = dev->of_node;
1931 	struct device_node *endpoint;
1932 	const char *te_source;
1933 	int ret = 0;
1934 
1935 	/*
1936 	 * Get the endpoint of the output port of the DSI host. In our case,
1937 	 * this is mapped to port number with reg = 1. Don't return an error if
1938 	 * the remote endpoint isn't defined. It's possible that there is
1939 	 * nothing connected to the dsi output.
1940 	 */
1941 	endpoint = of_graph_get_endpoint_by_regs(np, 1, -1);
1942 	if (!endpoint) {
1943 		DRM_DEV_DEBUG(dev, "%s: no endpoint\n", __func__);
1944 		return 0;
1945 	}
1946 
1947 	ret = dsi_host_parse_lane_data(msm_host, endpoint);
1948 	if (ret) {
1949 		DRM_DEV_ERROR(dev, "%s: invalid lane configuration %d\n",
1950 			__func__, ret);
1951 		ret = -EINVAL;
1952 		goto err;
1953 	}
1954 
1955 	ret = of_property_read_string(endpoint, "qcom,te-source", &te_source);
1956 	if (ret && ret != -EINVAL) {
1957 		DRM_DEV_ERROR(dev, "%s: invalid TE source configuration %d\n",
1958 			__func__, ret);
1959 		goto err;
1960 	}
1961 	if (!ret) {
1962 		msm_dsi->te_source = devm_kstrdup(dev, te_source, GFP_KERNEL);
1963 		if (!msm_dsi->te_source) {
1964 			DRM_DEV_ERROR(dev, "%s: failed to allocate te_source\n",
1965 				__func__);
1966 			ret = -ENOMEM;
1967 			goto err;
1968 		}
1969 	}
1970 	ret = 0;
1971 
1972 	if (of_property_present(np, "syscon-sfpb")) {
1973 		msm_host->sfpb = syscon_regmap_lookup_by_phandle(np,
1974 					"syscon-sfpb");
1975 		if (IS_ERR(msm_host->sfpb)) {
1976 			DRM_DEV_ERROR(dev, "%s: failed to get sfpb regmap\n",
1977 				__func__);
1978 			ret = PTR_ERR(msm_host->sfpb);
1979 		}
1980 	}
1981 
1982 err:
1983 	of_node_put(endpoint);
1984 
1985 	return ret;
1986 }
1987 
1988 static int dsi_host_get_id(struct msm_dsi_host *msm_host)
1989 {
1990 	struct platform_device *pdev = msm_host->pdev;
1991 	const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
1992 	struct resource *res;
1993 	int i, j;
1994 
1995 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dsi_ctrl");
1996 	if (!res)
1997 		return -EINVAL;
1998 
1999 	for (i = 0; i < VARIANTS_MAX; i++)
2000 		for (j = 0; j < DSI_MAX; j++)
2001 			if (cfg->io_start[i][j] == res->start)
2002 				return j;
2003 
2004 	return -EINVAL;
2005 }
2006 
2007 int msm_dsi_host_init(struct msm_dsi *msm_dsi)
2008 {
2009 	struct msm_dsi_host *msm_host = NULL;
2010 	struct platform_device *pdev = msm_dsi->pdev;
2011 	const struct msm_dsi_config *cfg;
2012 	int ret;
2013 
2014 	msm_host = devm_kzalloc(&pdev->dev, sizeof(*msm_host), GFP_KERNEL);
2015 	if (!msm_host)
2016 		return -ENOMEM;
2017 
2018 	msm_host->pdev = pdev;
2019 	msm_dsi->host = &msm_host->base;
2020 
2021 	ret = dsi_host_parse_dt(msm_host);
2022 	if (ret)
2023 		return dev_err_probe(&pdev->dev, ret, "%s: failed to parse dt\n",
2024 				     __func__);
2025 
2026 	msm_host->ctrl_base = msm_ioremap_size(pdev, "dsi_ctrl", &msm_host->ctrl_size);
2027 	if (IS_ERR(msm_host->ctrl_base))
2028 		return dev_err_probe(&pdev->dev, PTR_ERR(msm_host->ctrl_base),
2029 				     "%s: unable to map Dsi ctrl base\n", __func__);
2030 
2031 	pm_runtime_enable(&pdev->dev);
2032 
2033 	msm_host->cfg_hnd = dsi_get_config(msm_host);
2034 	if (!msm_host->cfg_hnd)
2035 		return dev_err_probe(&pdev->dev, -EINVAL,
2036 				     "%s: get config failed\n", __func__);
2037 	cfg = msm_host->cfg_hnd->cfg;
2038 
2039 	msm_host->id = dsi_host_get_id(msm_host);
2040 	if (msm_host->id < 0)
2041 		return dev_err_probe(&pdev->dev, msm_host->id,
2042 				     "%s: unable to identify DSI host index\n",
2043 				     __func__);
2044 
2045 	/* fixup base address by io offset */
2046 	msm_host->ctrl_base += cfg->io_offset;
2047 	msm_host->ctrl_size -= cfg->io_offset;
2048 
2049 	ret = devm_regulator_bulk_get_const(&pdev->dev, cfg->num_regulators,
2050 					    cfg->regulator_data,
2051 					    &msm_host->supplies);
2052 	if (ret)
2053 		return ret;
2054 
2055 	ret = dsi_clk_init(msm_host);
2056 	if (ret)
2057 		return dev_err_probe(&pdev->dev, ret, "%s: unable to initialize dsi clks\n", __func__);
2058 
2059 	msm_host->rx_buf = devm_kzalloc(&pdev->dev, SZ_4K, GFP_KERNEL);
2060 	if (!msm_host->rx_buf)
2061 		return -ENOMEM;
2062 
2063 	ret = devm_pm_opp_set_clkname(&pdev->dev, "byte");
2064 	if (ret)
2065 		return ret;
2066 	/* OPP table is optional */
2067 	ret = devm_pm_opp_of_add_table(&pdev->dev);
2068 	if (ret && ret != -ENODEV)
2069 		return dev_err_probe(&pdev->dev, ret, "invalid OPP table in device tree\n");
2070 
2071 	msm_host->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
2072 	if (!msm_host->irq)
2073 		return dev_err_probe(&pdev->dev, -EINVAL, "failed to get irq\n");
2074 
2075 	/* do not autoenable, will be enabled later */
2076 	ret = devm_request_irq(&pdev->dev, msm_host->irq, dsi_host_irq,
2077 			IRQF_TRIGGER_HIGH | IRQF_NO_AUTOEN,
2078 			"dsi_isr", msm_host);
2079 	if (ret < 0)
2080 		return dev_err_probe(&pdev->dev, ret, "failed to request IRQ%u\n",
2081 				     msm_host->irq);
2082 
2083 	init_completion(&msm_host->dma_comp);
2084 	init_completion(&msm_host->video_comp);
2085 	mutex_init(&msm_host->dev_mutex);
2086 	mutex_init(&msm_host->cmd_mutex);
2087 	spin_lock_init(&msm_host->intr_lock);
2088 
2089 	/* setup workqueue */
2090 	msm_host->workqueue = alloc_ordered_workqueue("dsi_drm_work", 0);
2091 	if (!msm_host->workqueue)
2092 		return -ENOMEM;
2093 
2094 	INIT_WORK(&msm_host->err_work, dsi_err_worker);
2095 
2096 	msm_dsi->id = msm_host->id;
2097 
2098 	DBG("Dsi Host %d initialized", msm_host->id);
2099 	return 0;
2100 }
2101 
2102 void msm_dsi_host_destroy(struct mipi_dsi_host *host)
2103 {
2104 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2105 
2106 	DBG("");
2107 	if (msm_host->workqueue) {
2108 		destroy_workqueue(msm_host->workqueue);
2109 		msm_host->workqueue = NULL;
2110 	}
2111 
2112 	mutex_destroy(&msm_host->cmd_mutex);
2113 	mutex_destroy(&msm_host->dev_mutex);
2114 
2115 	pm_runtime_disable(&msm_host->pdev->dev);
2116 }
2117 
2118 int msm_dsi_host_modeset_init(struct mipi_dsi_host *host,
2119 					struct drm_device *dev)
2120 {
2121 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2122 	const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2123 	int ret;
2124 
2125 	msm_host->dev = dev;
2126 
2127 	ret = cfg_hnd->ops->tx_buf_alloc(msm_host, SZ_4K);
2128 	if (ret) {
2129 		pr_err("%s: alloc tx gem obj failed, %d\n", __func__, ret);
2130 		return ret;
2131 	}
2132 
2133 	return 0;
2134 }
2135 
2136 int msm_dsi_host_register(struct mipi_dsi_host *host)
2137 {
2138 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2139 	int ret;
2140 
2141 	/* Register mipi dsi host */
2142 	if (!msm_host->registered) {
2143 		host->dev = &msm_host->pdev->dev;
2144 		host->ops = &dsi_host_ops;
2145 		ret = mipi_dsi_host_register(host);
2146 		if (ret)
2147 			return ret;
2148 
2149 		msm_host->registered = true;
2150 	}
2151 
2152 	return 0;
2153 }
2154 
2155 void msm_dsi_host_unregister(struct mipi_dsi_host *host)
2156 {
2157 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2158 
2159 	if (msm_host->registered) {
2160 		mipi_dsi_host_unregister(host);
2161 		host->dev = NULL;
2162 		host->ops = NULL;
2163 		msm_host->registered = false;
2164 	}
2165 }
2166 
2167 int msm_dsi_host_xfer_prepare(struct mipi_dsi_host *host,
2168 				const struct mipi_dsi_msg *msg)
2169 {
2170 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2171 	const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2172 
2173 	/* TODO: make sure dsi_cmd_mdp is idle.
2174 	 * Since DSI6G v1.2.0, we can set DSI_TRIG_CTRL.BLOCK_DMA_WITHIN_FRAME
2175 	 * to ask H/W to wait until cmd mdp is idle. S/W wait is not needed.
2176 	 * How to handle the old versions? Wait for mdp cmd done?
2177 	 */
2178 
2179 	/*
2180 	 * mdss interrupt is generated in mdp core clock domain
2181 	 * mdp clock need to be enabled to receive dsi interrupt
2182 	 */
2183 	pm_runtime_get_sync(&msm_host->pdev->dev);
2184 	cfg_hnd->ops->link_clk_set_rate(msm_host);
2185 	cfg_hnd->ops->link_clk_enable(msm_host);
2186 
2187 	/* TODO: vote for bus bandwidth */
2188 
2189 	if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
2190 		dsi_set_tx_power_mode(0, msm_host);
2191 
2192 	msm_host->dma_cmd_ctrl_restore = dsi_read(msm_host, REG_DSI_CTRL);
2193 	dsi_write(msm_host, REG_DSI_CTRL,
2194 		msm_host->dma_cmd_ctrl_restore |
2195 		DSI_CTRL_CMD_MODE_EN |
2196 		DSI_CTRL_ENABLE);
2197 	dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 1);
2198 
2199 	return 0;
2200 }
2201 
2202 void msm_dsi_host_xfer_restore(struct mipi_dsi_host *host,
2203 				const struct mipi_dsi_msg *msg)
2204 {
2205 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2206 	const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2207 
2208 	dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 0);
2209 	dsi_write(msm_host, REG_DSI_CTRL, msm_host->dma_cmd_ctrl_restore);
2210 
2211 	if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
2212 		dsi_set_tx_power_mode(1, msm_host);
2213 
2214 	/* TODO: unvote for bus bandwidth */
2215 
2216 	cfg_hnd->ops->link_clk_disable(msm_host);
2217 	pm_runtime_put(&msm_host->pdev->dev);
2218 }
2219 
2220 int msm_dsi_host_cmd_tx(struct mipi_dsi_host *host,
2221 				const struct mipi_dsi_msg *msg)
2222 {
2223 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2224 
2225 	return dsi_cmds2buf_tx(msm_host, msg);
2226 }
2227 
2228 int msm_dsi_host_cmd_rx(struct mipi_dsi_host *host,
2229 				const struct mipi_dsi_msg *msg)
2230 {
2231 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2232 	const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2233 	int data_byte, rx_byte, dlen, end;
2234 	int short_response, diff, pkt_size, ret = 0;
2235 	char cmd;
2236 	int rlen = msg->rx_len;
2237 	u8 *buf;
2238 
2239 	if (rlen <= 2) {
2240 		short_response = 1;
2241 		pkt_size = rlen;
2242 		rx_byte = 4;
2243 	} else {
2244 		short_response = 0;
2245 		data_byte = 10;	/* first read */
2246 		if (rlen < data_byte)
2247 			pkt_size = rlen;
2248 		else
2249 			pkt_size = data_byte;
2250 		rx_byte = data_byte + 6; /* 4 header + 2 crc */
2251 	}
2252 
2253 	buf = msm_host->rx_buf;
2254 	end = 0;
2255 	while (!end) {
2256 		u8 tx[2] = {pkt_size & 0xff, pkt_size >> 8};
2257 		struct mipi_dsi_msg max_pkt_size_msg = {
2258 			.channel = msg->channel,
2259 			.type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
2260 			.tx_len = 2,
2261 			.tx_buf = tx,
2262 		};
2263 
2264 		DBG("rlen=%d pkt_size=%d rx_byte=%d",
2265 			rlen, pkt_size, rx_byte);
2266 
2267 		ret = dsi_cmds2buf_tx(msm_host, &max_pkt_size_msg);
2268 		if (ret < 2) {
2269 			pr_err("%s: Set max pkt size failed, %d\n",
2270 				__func__, ret);
2271 			return -EINVAL;
2272 		}
2273 
2274 		if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
2275 			(cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_1)) {
2276 			/* Clear the RDBK_DATA registers */
2277 			dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL,
2278 					DSI_RDBK_DATA_CTRL_CLR);
2279 			wmb(); /* make sure the RDBK registers are cleared */
2280 			dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL, 0);
2281 			wmb(); /* release cleared status before transfer */
2282 		}
2283 
2284 		ret = dsi_cmds2buf_tx(msm_host, msg);
2285 		if (ret < 0) {
2286 			pr_err("%s: Read cmd Tx failed, %d\n", __func__, ret);
2287 			return ret;
2288 		} else if (ret < msg->tx_len) {
2289 			pr_err("%s: Read cmd Tx failed, too short: %d\n", __func__, ret);
2290 			return -ECOMM;
2291 		}
2292 
2293 		/*
2294 		 * once cmd_dma_done interrupt received,
2295 		 * return data from client is ready and stored
2296 		 * at RDBK_DATA register already
2297 		 * since rx fifo is 16 bytes, dcs header is kept at first loop,
2298 		 * after that dcs header lost during shift into registers
2299 		 */
2300 		dlen = dsi_cmd_dma_rx(msm_host, buf, rx_byte, pkt_size);
2301 
2302 		if (dlen <= 0)
2303 			return 0;
2304 
2305 		if (short_response)
2306 			break;
2307 
2308 		if (rlen <= data_byte) {
2309 			diff = data_byte - rlen;
2310 			end = 1;
2311 		} else {
2312 			diff = 0;
2313 			rlen -= data_byte;
2314 		}
2315 
2316 		if (!end) {
2317 			dlen -= 2; /* 2 crc */
2318 			dlen -= diff;
2319 			buf += dlen;	/* next start position */
2320 			data_byte = 14;	/* NOT first read */
2321 			if (rlen < data_byte)
2322 				pkt_size += rlen;
2323 			else
2324 				pkt_size += data_byte;
2325 			DBG("buf=%p dlen=%d diff=%d", buf, dlen, diff);
2326 		}
2327 	}
2328 
2329 	/*
2330 	 * For single Long read, if the requested rlen < 10,
2331 	 * we need to shift the start position of rx
2332 	 * data buffer to skip the bytes which are not
2333 	 * updated.
2334 	 */
2335 	if (pkt_size < 10 && !short_response)
2336 		buf = msm_host->rx_buf + (10 - rlen);
2337 	else
2338 		buf = msm_host->rx_buf;
2339 
2340 	cmd = buf[0];
2341 	switch (cmd) {
2342 	case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
2343 		pr_err("%s: rx ACK_ERR_PACLAGE\n", __func__);
2344 		ret = 0;
2345 		break;
2346 	case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
2347 	case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
2348 		ret = dsi_short_read1_resp(buf, msg);
2349 		break;
2350 	case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
2351 	case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
2352 		ret = dsi_short_read2_resp(buf, msg);
2353 		break;
2354 	case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
2355 	case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
2356 		ret = dsi_long_read_resp(buf, msg);
2357 		break;
2358 	default:
2359 		pr_warn("%s:Invalid response cmd\n", __func__);
2360 		ret = 0;
2361 	}
2362 
2363 	return ret;
2364 }
2365 
2366 void msm_dsi_host_cmd_xfer_commit(struct mipi_dsi_host *host, u32 dma_base,
2367 				  u32 len)
2368 {
2369 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2370 
2371 	dsi_write(msm_host, REG_DSI_DMA_BASE, dma_base);
2372 	dsi_write(msm_host, REG_DSI_DMA_LEN, len);
2373 	dsi_write(msm_host, REG_DSI_TRIG_DMA, 1);
2374 
2375 	/* Make sure trigger happens */
2376 	wmb();
2377 }
2378 
2379 void msm_dsi_host_set_phy_mode(struct mipi_dsi_host *host,
2380 	struct msm_dsi_phy *src_phy)
2381 {
2382 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2383 
2384 	msm_host->cphy_mode = src_phy->cphy_mode;
2385 }
2386 
2387 void msm_dsi_host_reset_phy(struct mipi_dsi_host *host)
2388 {
2389 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2390 
2391 	DBG("");
2392 	dsi_write(msm_host, REG_DSI_PHY_RESET, DSI_PHY_RESET_RESET);
2393 	/* Make sure fully reset */
2394 	wmb();
2395 	udelay(1000);
2396 	dsi_write(msm_host, REG_DSI_PHY_RESET, 0);
2397 	udelay(100);
2398 }
2399 
2400 void msm_dsi_host_get_phy_clk_req(struct mipi_dsi_host *host,
2401 			struct msm_dsi_phy_clk_request *clk_req,
2402 			bool is_bonded_dsi)
2403 {
2404 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2405 	const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2406 	int ret;
2407 
2408 	ret = cfg_hnd->ops->calc_clk_rate(msm_host, is_bonded_dsi);
2409 	if (ret) {
2410 		pr_err("%s: unable to calc clk rate, %d\n", __func__, ret);
2411 		return;
2412 	}
2413 
2414 	/* CPHY transmits 16 bits over 7 clock cycles
2415 	 * "byte_clk" is in units of 16-bits (see dsi_calc_pclk),
2416 	 * so multiply by 7 to get the "bitclk rate"
2417 	 */
2418 	if (msm_host->cphy_mode)
2419 		clk_req->bitclk_rate = msm_host->byte_clk_rate * 7;
2420 	else
2421 		clk_req->bitclk_rate = msm_host->byte_clk_rate * 8;
2422 	clk_req->escclk_rate = msm_host->esc_clk_rate;
2423 }
2424 
2425 void msm_dsi_host_enable_irq(struct mipi_dsi_host *host)
2426 {
2427 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2428 
2429 	enable_irq(msm_host->irq);
2430 }
2431 
2432 void msm_dsi_host_disable_irq(struct mipi_dsi_host *host)
2433 {
2434 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2435 
2436 	disable_irq(msm_host->irq);
2437 }
2438 
2439 int msm_dsi_host_enable(struct mipi_dsi_host *host)
2440 {
2441 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2442 
2443 	dsi_op_mode_config(msm_host,
2444 		!!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), true);
2445 
2446 	/* TODO: clock should be turned off for command mode,
2447 	 * and only turned on before MDP START.
2448 	 * This part of code should be enabled once mdp driver support it.
2449 	 */
2450 	/* if (msm_panel->mode == MSM_DSI_CMD_MODE) {
2451 	 *	dsi_link_clk_disable(msm_host);
2452 	 *	pm_runtime_put(&msm_host->pdev->dev);
2453 	 * }
2454 	 */
2455 	msm_host->enabled = true;
2456 	return 0;
2457 }
2458 
2459 int msm_dsi_host_disable(struct mipi_dsi_host *host)
2460 {
2461 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2462 
2463 	msm_host->enabled = false;
2464 	dsi_op_mode_config(msm_host,
2465 		!!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), false);
2466 
2467 	/* Since we have disabled INTF, the video engine won't stop so that
2468 	 * the cmd engine will be blocked.
2469 	 * Reset to disable video engine so that we can send off cmd.
2470 	 */
2471 	dsi_sw_reset(msm_host);
2472 
2473 	return 0;
2474 }
2475 
2476 static void msm_dsi_sfpb_config(struct msm_dsi_host *msm_host, bool enable)
2477 {
2478 	enum sfpb_ahb_arb_master_port_en en;
2479 
2480 	if (!msm_host->sfpb)
2481 		return;
2482 
2483 	en = enable ? SFPB_MASTER_PORT_ENABLE : SFPB_MASTER_PORT_DISABLE;
2484 
2485 	regmap_update_bits(msm_host->sfpb, REG_SFPB_GPREG,
2486 			SFPB_GPREG_MASTER_PORT_EN__MASK,
2487 			SFPB_GPREG_MASTER_PORT_EN(en));
2488 }
2489 
2490 int msm_dsi_host_power_on(struct mipi_dsi_host *host,
2491 			struct msm_dsi_phy_shared_timings *phy_shared_timings,
2492 			bool is_bonded_dsi, struct msm_dsi_phy *phy)
2493 {
2494 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2495 	const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2496 	int ret = 0;
2497 
2498 	mutex_lock(&msm_host->dev_mutex);
2499 	if (msm_host->power_on) {
2500 		DBG("dsi host already on");
2501 		goto unlock_ret;
2502 	}
2503 
2504 	msm_host->byte_intf_clk_div_2 = phy_shared_timings->byte_intf_clk_div_2;
2505 
2506 	msm_dsi_sfpb_config(msm_host, true);
2507 
2508 	ret = regulator_bulk_enable(msm_host->cfg_hnd->cfg->num_regulators,
2509 				    msm_host->supplies);
2510 	if (ret) {
2511 		pr_err("%s:Failed to enable vregs.ret=%d\n",
2512 			__func__, ret);
2513 		goto unlock_ret;
2514 	}
2515 
2516 	pm_runtime_get_sync(&msm_host->pdev->dev);
2517 	ret = cfg_hnd->ops->link_clk_set_rate(msm_host);
2518 	if (!ret)
2519 		ret = cfg_hnd->ops->link_clk_enable(msm_host);
2520 	if (ret) {
2521 		pr_err("%s: failed to enable link clocks. ret=%d\n",
2522 		       __func__, ret);
2523 		goto fail_disable_reg;
2524 	}
2525 
2526 	ret = pinctrl_pm_select_default_state(&msm_host->pdev->dev);
2527 	if (ret) {
2528 		pr_err("%s: failed to set pinctrl default state, %d\n",
2529 			__func__, ret);
2530 		goto fail_disable_clk;
2531 	}
2532 
2533 	dsi_timing_setup(msm_host, is_bonded_dsi);
2534 	dsi_sw_reset(msm_host);
2535 	dsi_ctrl_enable(msm_host, phy_shared_timings, phy);
2536 
2537 	msm_host->power_on = true;
2538 	mutex_unlock(&msm_host->dev_mutex);
2539 
2540 	return 0;
2541 
2542 fail_disable_clk:
2543 	cfg_hnd->ops->link_clk_disable(msm_host);
2544 	pm_runtime_put(&msm_host->pdev->dev);
2545 fail_disable_reg:
2546 	regulator_bulk_disable(msm_host->cfg_hnd->cfg->num_regulators,
2547 			       msm_host->supplies);
2548 unlock_ret:
2549 	mutex_unlock(&msm_host->dev_mutex);
2550 	return ret;
2551 }
2552 
2553 int msm_dsi_host_power_off(struct mipi_dsi_host *host)
2554 {
2555 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2556 	const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
2557 
2558 	mutex_lock(&msm_host->dev_mutex);
2559 	if (!msm_host->power_on) {
2560 		DBG("dsi host already off");
2561 		goto unlock_ret;
2562 	}
2563 
2564 	dsi_ctrl_disable(msm_host);
2565 
2566 	pinctrl_pm_select_sleep_state(&msm_host->pdev->dev);
2567 
2568 	cfg_hnd->ops->link_clk_disable(msm_host);
2569 	pm_runtime_put(&msm_host->pdev->dev);
2570 
2571 	regulator_bulk_disable(msm_host->cfg_hnd->cfg->num_regulators,
2572 			       msm_host->supplies);
2573 
2574 	msm_dsi_sfpb_config(msm_host, false);
2575 
2576 	DBG("-");
2577 
2578 	msm_host->power_on = false;
2579 
2580 unlock_ret:
2581 	mutex_unlock(&msm_host->dev_mutex);
2582 	return 0;
2583 }
2584 
2585 int msm_dsi_host_set_display_mode(struct mipi_dsi_host *host,
2586 				  const struct drm_display_mode *mode)
2587 {
2588 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2589 
2590 	if (msm_host->mode) {
2591 		drm_mode_destroy(msm_host->dev, msm_host->mode);
2592 		msm_host->mode = NULL;
2593 	}
2594 
2595 	msm_host->mode = drm_mode_duplicate(msm_host->dev, mode);
2596 	if (!msm_host->mode) {
2597 		pr_err("%s: cannot duplicate mode\n", __func__);
2598 		return -ENOMEM;
2599 	}
2600 
2601 	return 0;
2602 }
2603 
2604 enum drm_mode_status msm_dsi_host_check_dsc(struct mipi_dsi_host *host,
2605 					    const struct drm_display_mode *mode)
2606 {
2607 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2608 	struct drm_dsc_config *dsc = msm_host->dsc;
2609 	int pic_width = mode->hdisplay;
2610 	int pic_height = mode->vdisplay;
2611 
2612 	if (!msm_host->dsc)
2613 		return MODE_OK;
2614 
2615 	if (pic_width % dsc->slice_width) {
2616 		pr_err("DSI: pic_width %d has to be multiple of slice %d\n",
2617 		       pic_width, dsc->slice_width);
2618 		return MODE_H_ILLEGAL;
2619 	}
2620 
2621 	if (pic_height % dsc->slice_height) {
2622 		pr_err("DSI: pic_height %d has to be multiple of slice %d\n",
2623 		       pic_height, dsc->slice_height);
2624 		return MODE_V_ILLEGAL;
2625 	}
2626 
2627 	return MODE_OK;
2628 }
2629 
2630 unsigned long msm_dsi_host_get_mode_flags(struct mipi_dsi_host *host)
2631 {
2632 	return to_msm_dsi_host(host)->mode_flags;
2633 }
2634 
2635 void msm_dsi_host_snapshot(struct msm_disp_state *disp_state, struct mipi_dsi_host *host)
2636 {
2637 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2638 
2639 	pm_runtime_get_sync(&msm_host->pdev->dev);
2640 
2641 	msm_disp_snapshot_add_block(disp_state, msm_host->ctrl_size,
2642 			msm_host->ctrl_base, "dsi%d_ctrl", msm_host->id);
2643 
2644 	pm_runtime_put_sync(&msm_host->pdev->dev);
2645 }
2646 
2647 static void msm_dsi_host_video_test_pattern_setup(struct msm_dsi_host *msm_host)
2648 {
2649 	u32 reg;
2650 
2651 	reg = dsi_read(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL);
2652 
2653 	dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_VIDEO_INIT_VAL, 0xff);
2654 	/* draw checkered rectangle pattern */
2655 	dsi_write(msm_host, REG_DSI_TPG_MAIN_CONTROL,
2656 			DSI_TPG_MAIN_CONTROL_CHECKERED_RECTANGLE_PATTERN);
2657 	/* use 24-bit RGB test pttern */
2658 	dsi_write(msm_host, REG_DSI_TPG_VIDEO_CONFIG,
2659 			DSI_TPG_VIDEO_CONFIG_BPP(VIDEO_CONFIG_24BPP) |
2660 			DSI_TPG_VIDEO_CONFIG_RGB);
2661 
2662 	reg |= DSI_TEST_PATTERN_GEN_CTRL_VIDEO_PATTERN_SEL(VID_MDSS_GENERAL_PATTERN);
2663 	dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL, reg);
2664 
2665 	DBG("Video test pattern setup done\n");
2666 }
2667 
2668 static void msm_dsi_host_cmd_test_pattern_setup(struct msm_dsi_host *msm_host)
2669 {
2670 	u32 reg;
2671 
2672 	reg = dsi_read(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL);
2673 
2674 	/* initial value for test pattern */
2675 	dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CMD_MDP_INIT_VAL0, 0xff);
2676 
2677 	reg |= DSI_TEST_PATTERN_GEN_CTRL_CMD_MDP_STREAM0_PATTERN_SEL(CMD_MDP_MDSS_GENERAL_PATTERN);
2678 
2679 	dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL, reg);
2680 	/* draw checkered rectangle pattern */
2681 	dsi_write(msm_host, REG_DSI_TPG_MAIN_CONTROL2,
2682 			DSI_TPG_MAIN_CONTROL2_CMD_MDP0_CHECKERED_RECTANGLE_PATTERN);
2683 
2684 	DBG("Cmd test pattern setup done\n");
2685 }
2686 
2687 void msm_dsi_host_test_pattern_en(struct mipi_dsi_host *host)
2688 {
2689 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2690 	bool is_video_mode = !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO);
2691 	u32 reg;
2692 
2693 	if (is_video_mode)
2694 		msm_dsi_host_video_test_pattern_setup(msm_host);
2695 	else
2696 		msm_dsi_host_cmd_test_pattern_setup(msm_host);
2697 
2698 	reg = dsi_read(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL);
2699 	/* enable the test pattern generator */
2700 	dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL, (reg | DSI_TEST_PATTERN_GEN_CTRL_EN));
2701 
2702 	/* for command mode need to trigger one frame from tpg */
2703 	if (!is_video_mode)
2704 		dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CMD_STREAM0_TRIGGER,
2705 				DSI_TEST_PATTERN_GEN_CMD_STREAM0_TRIGGER_SW_TRIGGER);
2706 }
2707 
2708 struct drm_dsc_config *msm_dsi_host_get_dsc_config(struct mipi_dsi_host *host)
2709 {
2710 	struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
2711 
2712 	return msm_host->dsc;
2713 }
2714