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