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