xref: /linux/drivers/gpu/drm/mediatek/mtk_crtc.c (revision 815e260a18a3af4dab59025ee99a7156c0e8b5e0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2015 MediaTek Inc.
4  */
5 
6 #include <linux/clk.h>
7 #include <linux/dma-mapping.h>
8 #include <linux/mailbox_controller.h>
9 #include <linux/of.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/soc/mediatek/mtk-cmdq.h>
12 #include <linux/soc/mediatek/mtk-mmsys.h>
13 #include <linux/soc/mediatek/mtk-mutex.h>
14 
15 #include <asm/barrier.h>
16 
17 #include <drm/drm_atomic.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_print.h>
20 #include <drm/drm_probe_helper.h>
21 #include <drm/drm_vblank.h>
22 
23 #include "mtk_crtc.h"
24 #include "mtk_ddp_comp.h"
25 #include "mtk_drm_drv.h"
26 #include "mtk_gem.h"
27 #include "mtk_plane.h"
28 
29 /*
30  * struct mtk_crtc - MediaTek specific crtc structure.
31  * @base: crtc object.
32  * @enabled: records whether crtc_enable succeeded
33  * @planes: array of 4 drm_plane structures, one for each overlay plane
34  * @pending_planes: whether any plane has pending changes to be applied
35  * @mmsys_dev: pointer to the mmsys device for configuration registers
36  * @mutex: handle to one of the ten disp_mutex streams
37  * @ddp_comp_nr: number of components in ddp_comp
38  * @ddp_comp: array of pointers the mtk_ddp_comp structures used by this crtc
39  *
40  * TODO: Needs update: this header is missing a bunch of member descriptions.
41  */
42 struct mtk_crtc {
43 	struct drm_crtc			base;
44 	bool				enabled;
45 
46 	bool				pending_needs_vblank;
47 	struct drm_pending_vblank_event	*event;
48 
49 	struct drm_plane		*planes;
50 	unsigned int			layer_nr;
51 	bool				pending_planes;
52 	bool				pending_async_planes;
53 
54 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
55 	struct cmdq_client		cmdq_client;
56 	struct cmdq_pkt			cmdq_handle;
57 	u32				cmdq_event;
58 	u32				cmdq_vblank_cnt;
59 	wait_queue_head_t		cb_blocking_queue;
60 #endif
61 
62 	struct device			*mmsys_dev;
63 	struct device			*dma_dev;
64 	struct mtk_mutex		*mutex;
65 	unsigned int			ddp_comp_nr;
66 	struct mtk_ddp_comp		**ddp_comp;
67 	unsigned int			num_conn_routes;
68 	const struct mtk_drm_route	*conn_routes;
69 
70 	/* lock for display hardware access */
71 	struct mutex			hw_lock;
72 	bool				config_updating;
73 	/* lock for config_updating to cmd buffer */
74 	spinlock_t			config_lock;
75 };
76 
77 struct mtk_crtc_state {
78 	struct drm_crtc_state		base;
79 
80 	bool				pending_config;
81 	unsigned int			pending_width;
82 	unsigned int			pending_height;
83 	unsigned int			pending_vrefresh;
84 };
85 
86 static inline struct mtk_crtc *to_mtk_crtc(struct drm_crtc *c)
87 {
88 	return container_of(c, struct mtk_crtc, base);
89 }
90 
91 static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s)
92 {
93 	return container_of(s, struct mtk_crtc_state, base);
94 }
95 
96 static void mtk_crtc_finish_page_flip(struct mtk_crtc *mtk_crtc)
97 {
98 	struct drm_crtc *crtc = &mtk_crtc->base;
99 	unsigned long flags;
100 
101 	if (mtk_crtc->event) {
102 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
103 		drm_crtc_send_vblank_event(crtc, mtk_crtc->event);
104 		drm_crtc_vblank_put(crtc);
105 		mtk_crtc->event = NULL;
106 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
107 	}
108 }
109 
110 static void mtk_drm_finish_page_flip(struct mtk_crtc *mtk_crtc)
111 {
112 	unsigned long flags;
113 
114 	drm_crtc_handle_vblank(&mtk_crtc->base);
115 
116 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
117 	if (mtk_crtc->cmdq_client.chan)
118 		return;
119 #endif
120 
121 	spin_lock_irqsave(&mtk_crtc->config_lock, flags);
122 	if (!mtk_crtc->config_updating && mtk_crtc->pending_needs_vblank) {
123 		mtk_crtc_finish_page_flip(mtk_crtc);
124 		mtk_crtc->pending_needs_vblank = false;
125 	}
126 	spin_unlock_irqrestore(&mtk_crtc->config_lock, flags);
127 }
128 
129 static void mtk_crtc_destroy(struct drm_crtc *crtc)
130 {
131 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
132 	int i;
133 
134 	mtk_mutex_put(mtk_crtc->mutex);
135 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
136 	if (mtk_crtc->cmdq_client.chan) {
137 		cmdq_pkt_destroy(&mtk_crtc->cmdq_client, &mtk_crtc->cmdq_handle);
138 		mbox_free_channel(mtk_crtc->cmdq_client.chan);
139 		mtk_crtc->cmdq_client.chan = NULL;
140 	}
141 #endif
142 
143 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
144 		struct mtk_ddp_comp *comp;
145 
146 		comp = mtk_crtc->ddp_comp[i];
147 		mtk_ddp_comp_unregister_vblank_cb(comp);
148 	}
149 
150 	drm_crtc_cleanup(crtc);
151 }
152 
153 static void mtk_crtc_reset(struct drm_crtc *crtc)
154 {
155 	struct mtk_crtc_state *state;
156 
157 	if (crtc->state)
158 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
159 
160 	kfree(to_mtk_crtc_state(crtc->state));
161 	crtc->state = NULL;
162 
163 	state = kzalloc(sizeof(*state), GFP_KERNEL);
164 	if (state)
165 		__drm_atomic_helper_crtc_reset(crtc, &state->base);
166 }
167 
168 static struct drm_crtc_state *mtk_crtc_duplicate_state(struct drm_crtc *crtc)
169 {
170 	struct mtk_crtc_state *state;
171 
172 	state = kmalloc(sizeof(*state), GFP_KERNEL);
173 	if (!state)
174 		return NULL;
175 
176 	__drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
177 
178 	WARN_ON(state->base.crtc != crtc);
179 	state->base.crtc = crtc;
180 	state->pending_config = false;
181 
182 	return &state->base;
183 }
184 
185 static void mtk_crtc_destroy_state(struct drm_crtc *crtc,
186 				   struct drm_crtc_state *state)
187 {
188 	__drm_atomic_helper_crtc_destroy_state(state);
189 	kfree(to_mtk_crtc_state(state));
190 }
191 
192 static enum drm_mode_status
193 mtk_crtc_mode_valid(struct drm_crtc *crtc, const struct drm_display_mode *mode)
194 {
195 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
196 	enum drm_mode_status status = MODE_OK;
197 	int i;
198 
199 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
200 		status = mtk_ddp_comp_mode_valid(mtk_crtc->ddp_comp[i], mode);
201 		if (status != MODE_OK)
202 			break;
203 	}
204 	return status;
205 }
206 
207 static bool mtk_crtc_mode_fixup(struct drm_crtc *crtc,
208 				const struct drm_display_mode *mode,
209 				struct drm_display_mode *adjusted_mode)
210 {
211 	/* Nothing to do here, but this callback is mandatory. */
212 	return true;
213 }
214 
215 static void mtk_crtc_mode_set_nofb(struct drm_crtc *crtc)
216 {
217 	struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
218 
219 	state->pending_width = crtc->mode.hdisplay;
220 	state->pending_height = crtc->mode.vdisplay;
221 	state->pending_vrefresh = drm_mode_vrefresh(&crtc->mode);
222 	wmb();	/* Make sure the above parameters are set before update */
223 	state->pending_config = true;
224 }
225 
226 static int mtk_crtc_ddp_clk_enable(struct mtk_crtc *mtk_crtc)
227 {
228 	int ret;
229 	int i;
230 
231 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
232 		ret = mtk_ddp_comp_clk_enable(mtk_crtc->ddp_comp[i]);
233 		if (ret) {
234 			DRM_ERROR("Failed to enable clock %d: %d\n", i, ret);
235 			goto err;
236 		}
237 	}
238 
239 	return 0;
240 err:
241 	while (--i >= 0)
242 		mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]);
243 	return ret;
244 }
245 
246 static void mtk_crtc_ddp_clk_disable(struct mtk_crtc *mtk_crtc)
247 {
248 	int i;
249 
250 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
251 		mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]);
252 }
253 
254 static
255 struct mtk_ddp_comp *mtk_ddp_comp_for_plane(struct drm_crtc *crtc,
256 					    struct drm_plane *plane,
257 					    unsigned int *local_layer)
258 {
259 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
260 	struct mtk_ddp_comp *comp;
261 	int i, count = 0;
262 	unsigned int local_index = plane - mtk_crtc->planes;
263 
264 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
265 		comp = mtk_crtc->ddp_comp[i];
266 		if (local_index < (count + mtk_ddp_comp_layer_nr(comp))) {
267 			*local_layer = local_index - count;
268 			return comp;
269 		}
270 		count += mtk_ddp_comp_layer_nr(comp);
271 	}
272 
273 	WARN(1, "Failed to find component for plane %d\n", plane->index);
274 	return NULL;
275 }
276 
277 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
278 static void ddp_cmdq_cb(struct mbox_client *cl, void *mssg)
279 {
280 	struct cmdq_cb_data *data = mssg;
281 	struct cmdq_client *cmdq_cl = container_of(cl, struct cmdq_client, client);
282 	struct mtk_crtc *mtk_crtc = container_of(cmdq_cl, struct mtk_crtc, cmdq_client);
283 	struct mtk_crtc_state *state;
284 	unsigned int i;
285 	unsigned long flags;
286 
287 	if (data->sta < 0)
288 		return;
289 
290 	state = to_mtk_crtc_state(mtk_crtc->base.state);
291 
292 	spin_lock_irqsave(&mtk_crtc->config_lock, flags);
293 	if (mtk_crtc->config_updating)
294 		goto ddp_cmdq_cb_out;
295 
296 	state->pending_config = false;
297 
298 	if (mtk_crtc->pending_planes) {
299 		for (i = 0; i < mtk_crtc->layer_nr; i++) {
300 			struct drm_plane *plane = &mtk_crtc->planes[i];
301 			struct mtk_plane_state *plane_state;
302 
303 			plane_state = to_mtk_plane_state(plane->state);
304 
305 			plane_state->pending.config = false;
306 		}
307 		mtk_crtc->pending_planes = false;
308 	}
309 
310 	if (mtk_crtc->pending_async_planes) {
311 		for (i = 0; i < mtk_crtc->layer_nr; i++) {
312 			struct drm_plane *plane = &mtk_crtc->planes[i];
313 			struct mtk_plane_state *plane_state;
314 
315 			plane_state = to_mtk_plane_state(plane->state);
316 
317 			plane_state->pending.async_config = false;
318 		}
319 		mtk_crtc->pending_async_planes = false;
320 	}
321 
322 ddp_cmdq_cb_out:
323 
324 	if (mtk_crtc->pending_needs_vblank) {
325 		mtk_crtc_finish_page_flip(mtk_crtc);
326 		mtk_crtc->pending_needs_vblank = false;
327 	}
328 
329 	spin_unlock_irqrestore(&mtk_crtc->config_lock, flags);
330 
331 	mtk_crtc->cmdq_vblank_cnt = 0;
332 	wake_up(&mtk_crtc->cb_blocking_queue);
333 }
334 #endif
335 
336 static int mtk_crtc_ddp_hw_init(struct mtk_crtc *mtk_crtc)
337 {
338 	struct drm_crtc *crtc = &mtk_crtc->base;
339 	struct drm_connector *connector;
340 	struct drm_encoder *encoder;
341 	struct drm_connector_list_iter conn_iter;
342 	unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC;
343 	int ret;
344 	int i;
345 
346 	if (WARN_ON(!crtc->state))
347 		return -EINVAL;
348 
349 	width = crtc->state->adjusted_mode.hdisplay;
350 	height = crtc->state->adjusted_mode.vdisplay;
351 	vrefresh = drm_mode_vrefresh(&crtc->state->adjusted_mode);
352 
353 	drm_for_each_encoder(encoder, crtc->dev) {
354 		if (encoder->crtc != crtc)
355 			continue;
356 
357 		drm_connector_list_iter_begin(crtc->dev, &conn_iter);
358 		drm_for_each_connector_iter(connector, &conn_iter) {
359 			if (connector->encoder != encoder)
360 				continue;
361 			if (connector->display_info.bpc != 0 &&
362 			    bpc > connector->display_info.bpc)
363 				bpc = connector->display_info.bpc;
364 		}
365 		drm_connector_list_iter_end(&conn_iter);
366 	}
367 
368 	ret = pm_runtime_resume_and_get(crtc->dev->dev);
369 	if (ret < 0) {
370 		DRM_ERROR("Failed to enable power domain: %d\n", ret);
371 		return ret;
372 	}
373 
374 	ret = mtk_mutex_prepare(mtk_crtc->mutex);
375 	if (ret < 0) {
376 		DRM_ERROR("Failed to enable mutex clock: %d\n", ret);
377 		goto err_pm_runtime_put;
378 	}
379 
380 	ret = mtk_crtc_ddp_clk_enable(mtk_crtc);
381 	if (ret < 0) {
382 		DRM_ERROR("Failed to enable component clocks: %d\n", ret);
383 		goto err_mutex_unprepare;
384 	}
385 
386 	for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
387 		if (!mtk_ddp_comp_connect(mtk_crtc->ddp_comp[i], mtk_crtc->mmsys_dev,
388 					  mtk_crtc->ddp_comp[i + 1]->id))
389 			mtk_mmsys_ddp_connect(mtk_crtc->mmsys_dev,
390 					      mtk_crtc->ddp_comp[i]->id,
391 					      mtk_crtc->ddp_comp[i + 1]->id);
392 		if (!mtk_ddp_comp_add(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
393 			mtk_mutex_add_comp(mtk_crtc->mutex,
394 					   mtk_crtc->ddp_comp[i]->id);
395 	}
396 	if (!mtk_ddp_comp_add(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
397 		mtk_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
398 	mtk_mutex_enable(mtk_crtc->mutex);
399 
400 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
401 		struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i];
402 
403 		if (i == 1)
404 			mtk_ddp_comp_bgclr_in_on(comp);
405 
406 		mtk_ddp_comp_config(comp, width, height, vrefresh, bpc, NULL);
407 		mtk_ddp_comp_start(comp);
408 	}
409 
410 	/* Initially configure all planes */
411 	for (i = 0; i < mtk_crtc->layer_nr; i++) {
412 		struct drm_plane *plane = &mtk_crtc->planes[i];
413 		struct mtk_plane_state *plane_state;
414 		struct mtk_ddp_comp *comp;
415 		unsigned int local_layer;
416 
417 		plane_state = to_mtk_plane_state(plane->state);
418 
419 		/* should not enable layer before crtc enabled */
420 		plane_state->pending.enable = false;
421 		comp = mtk_ddp_comp_for_plane(crtc, plane, &local_layer);
422 		if (comp)
423 			mtk_ddp_comp_layer_config(comp, local_layer,
424 						  plane_state, NULL);
425 	}
426 
427 	return 0;
428 
429 err_mutex_unprepare:
430 	mtk_mutex_unprepare(mtk_crtc->mutex);
431 err_pm_runtime_put:
432 	pm_runtime_put(crtc->dev->dev);
433 	return ret;
434 }
435 
436 static void mtk_crtc_ddp_hw_fini(struct mtk_crtc *mtk_crtc)
437 {
438 	struct drm_device *drm = mtk_crtc->base.dev;
439 	struct drm_crtc *crtc = &mtk_crtc->base;
440 	unsigned long flags;
441 	int i;
442 
443 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
444 		mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]);
445 		if (i == 1)
446 			mtk_ddp_comp_bgclr_in_off(mtk_crtc->ddp_comp[i]);
447 	}
448 
449 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
450 		if (!mtk_ddp_comp_remove(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
451 			mtk_mutex_remove_comp(mtk_crtc->mutex,
452 					      mtk_crtc->ddp_comp[i]->id);
453 	mtk_mutex_disable(mtk_crtc->mutex);
454 	for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
455 		if (!mtk_ddp_comp_disconnect(mtk_crtc->ddp_comp[i], mtk_crtc->mmsys_dev,
456 					     mtk_crtc->ddp_comp[i + 1]->id))
457 			mtk_mmsys_ddp_disconnect(mtk_crtc->mmsys_dev,
458 						 mtk_crtc->ddp_comp[i]->id,
459 						 mtk_crtc->ddp_comp[i + 1]->id);
460 		if (!mtk_ddp_comp_remove(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
461 			mtk_mutex_remove_comp(mtk_crtc->mutex,
462 					      mtk_crtc->ddp_comp[i]->id);
463 	}
464 	if (!mtk_ddp_comp_remove(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
465 		mtk_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
466 	mtk_crtc_ddp_clk_disable(mtk_crtc);
467 	mtk_mutex_unprepare(mtk_crtc->mutex);
468 
469 	pm_runtime_put(drm->dev);
470 
471 	if (crtc->state->event && !crtc->state->active) {
472 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
473 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
474 		crtc->state->event = NULL;
475 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
476 	}
477 }
478 
479 static void mtk_crtc_ddp_config(struct drm_crtc *crtc,
480 				struct cmdq_pkt *cmdq_handle)
481 {
482 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
483 	struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state);
484 	struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
485 	unsigned int i;
486 	unsigned int local_layer;
487 
488 	/*
489 	 * TODO: instead of updating the registers here, we should prepare
490 	 * working registers in atomic_commit and let the hardware command
491 	 * queue update module registers on vblank.
492 	 */
493 	if (state->pending_config) {
494 		mtk_ddp_comp_config(comp, state->pending_width,
495 				    state->pending_height,
496 				    state->pending_vrefresh, 0,
497 				    cmdq_handle);
498 
499 		if (!cmdq_handle)
500 			state->pending_config = false;
501 	}
502 
503 	if (mtk_crtc->pending_planes) {
504 		for (i = 0; i < mtk_crtc->layer_nr; i++) {
505 			struct drm_plane *plane = &mtk_crtc->planes[i];
506 			struct mtk_plane_state *plane_state;
507 
508 			plane_state = to_mtk_plane_state(plane->state);
509 
510 			if (!plane_state->pending.config)
511 				continue;
512 
513 			comp = mtk_ddp_comp_for_plane(crtc, plane, &local_layer);
514 
515 			if (comp)
516 				mtk_ddp_comp_layer_config(comp, local_layer,
517 							  plane_state,
518 							  cmdq_handle);
519 			if (!cmdq_handle)
520 				plane_state->pending.config = false;
521 		}
522 
523 		if (!cmdq_handle)
524 			mtk_crtc->pending_planes = false;
525 	}
526 
527 	if (mtk_crtc->pending_async_planes) {
528 		for (i = 0; i < mtk_crtc->layer_nr; i++) {
529 			struct drm_plane *plane = &mtk_crtc->planes[i];
530 			struct mtk_plane_state *plane_state;
531 
532 			plane_state = to_mtk_plane_state(plane->state);
533 
534 			if (!plane_state->pending.async_config)
535 				continue;
536 
537 			comp = mtk_ddp_comp_for_plane(crtc, plane, &local_layer);
538 
539 			if (comp)
540 				mtk_ddp_comp_layer_config(comp, local_layer,
541 							  plane_state,
542 							  cmdq_handle);
543 			if (!cmdq_handle)
544 				plane_state->pending.async_config = false;
545 		}
546 
547 		if (!cmdq_handle)
548 			mtk_crtc->pending_async_planes = false;
549 	}
550 }
551 
552 static void mtk_crtc_update_config(struct mtk_crtc *mtk_crtc, bool needs_vblank)
553 {
554 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
555 	struct cmdq_pkt *cmdq_handle = &mtk_crtc->cmdq_handle;
556 #endif
557 	struct drm_crtc *crtc = &mtk_crtc->base;
558 	struct mtk_drm_private *priv = crtc->dev->dev_private;
559 	unsigned int pending_planes = 0, pending_async_planes = 0;
560 	int i;
561 	unsigned long flags;
562 
563 	mutex_lock(&mtk_crtc->hw_lock);
564 
565 	spin_lock_irqsave(&mtk_crtc->config_lock, flags);
566 	mtk_crtc->config_updating = true;
567 	spin_unlock_irqrestore(&mtk_crtc->config_lock, flags);
568 
569 	if (needs_vblank)
570 		mtk_crtc->pending_needs_vblank = true;
571 
572 	for (i = 0; i < mtk_crtc->layer_nr; i++) {
573 		struct drm_plane *plane = &mtk_crtc->planes[i];
574 		struct mtk_plane_state *plane_state;
575 
576 		plane_state = to_mtk_plane_state(plane->state);
577 		if (plane_state->pending.dirty) {
578 			plane_state->pending.config = true;
579 			plane_state->pending.dirty = false;
580 			pending_planes |= BIT(i);
581 		} else if (plane_state->pending.async_dirty) {
582 			plane_state->pending.async_config = true;
583 			plane_state->pending.async_dirty = false;
584 			pending_async_planes |= BIT(i);
585 		}
586 	}
587 	if (pending_planes)
588 		mtk_crtc->pending_planes = true;
589 	if (pending_async_planes)
590 		mtk_crtc->pending_async_planes = true;
591 
592 	if (priv->data->shadow_register) {
593 		mtk_mutex_acquire(mtk_crtc->mutex);
594 		mtk_crtc_ddp_config(crtc, NULL);
595 		mtk_mutex_release(mtk_crtc->mutex);
596 	}
597 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
598 	if (mtk_crtc->cmdq_client.chan) {
599 		mbox_flush(mtk_crtc->cmdq_client.chan, 2000);
600 		cmdq_handle->cmd_buf_size = 0;
601 		cmdq_pkt_clear_event(cmdq_handle, mtk_crtc->cmdq_event);
602 		cmdq_pkt_wfe(cmdq_handle, mtk_crtc->cmdq_event, false);
603 		mtk_crtc_ddp_config(crtc, cmdq_handle);
604 		cmdq_pkt_eoc(cmdq_handle);
605 		dma_sync_single_for_device(mtk_crtc->cmdq_client.chan->mbox->dev,
606 					   cmdq_handle->pa_base,
607 					   cmdq_handle->cmd_buf_size,
608 					   DMA_TO_DEVICE);
609 		/*
610 		 * CMDQ command should execute in next 3 vblank.
611 		 * One vblank interrupt before send message (occasionally)
612 		 * and one vblank interrupt after cmdq done,
613 		 * so it's timeout after 3 vblank interrupt.
614 		 * If it fail to execute in next 3 vblank, timeout happen.
615 		 */
616 		mtk_crtc->cmdq_vblank_cnt = 3;
617 
618 		spin_lock_irqsave(&mtk_crtc->config_lock, flags);
619 		mtk_crtc->config_updating = false;
620 		spin_unlock_irqrestore(&mtk_crtc->config_lock, flags);
621 
622 		mbox_send_message(mtk_crtc->cmdq_client.chan, cmdq_handle);
623 		mbox_client_txdone(mtk_crtc->cmdq_client.chan, 0);
624 		goto update_config_out;
625 	}
626 #endif
627 	spin_lock_irqsave(&mtk_crtc->config_lock, flags);
628 	mtk_crtc->config_updating = false;
629 	spin_unlock_irqrestore(&mtk_crtc->config_lock, flags);
630 
631 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
632 update_config_out:
633 #endif
634 	mutex_unlock(&mtk_crtc->hw_lock);
635 }
636 
637 static void mtk_crtc_ddp_irq(void *data)
638 {
639 	struct drm_crtc *crtc = data;
640 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
641 	struct mtk_drm_private *priv = crtc->dev->dev_private;
642 
643 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
644 	if (!priv->data->shadow_register && !mtk_crtc->cmdq_client.chan)
645 		mtk_crtc_ddp_config(crtc, NULL);
646 	else if (mtk_crtc->cmdq_vblank_cnt > 0 && --mtk_crtc->cmdq_vblank_cnt == 0)
647 		DRM_ERROR("mtk_crtc %d CMDQ execute command timeout!\n",
648 			  drm_crtc_index(&mtk_crtc->base));
649 #else
650 	if (!priv->data->shadow_register)
651 		mtk_crtc_ddp_config(crtc, NULL);
652 #endif
653 	mtk_drm_finish_page_flip(mtk_crtc);
654 }
655 
656 static int mtk_crtc_enable_vblank(struct drm_crtc *crtc)
657 {
658 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
659 	struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
660 
661 	mtk_ddp_comp_enable_vblank(comp);
662 
663 	return 0;
664 }
665 
666 static void mtk_crtc_disable_vblank(struct drm_crtc *crtc)
667 {
668 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
669 	struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
670 
671 	mtk_ddp_comp_disable_vblank(comp);
672 }
673 
674 static void mtk_crtc_update_output(struct drm_crtc *crtc,
675 				   struct drm_atomic_state *state)
676 {
677 	int crtc_index = drm_crtc_index(crtc);
678 	int i;
679 	struct device *dev;
680 	struct drm_crtc_state *crtc_state = state->crtcs[crtc_index].new_state;
681 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
682 	struct mtk_drm_private *priv;
683 	unsigned int encoder_mask = crtc_state->encoder_mask;
684 
685 	if (!crtc_state->connectors_changed)
686 		return;
687 
688 	if (!mtk_crtc->num_conn_routes)
689 		return;
690 
691 	priv = ((struct mtk_drm_private *)crtc->dev->dev_private)->all_drm_private[crtc_index];
692 	dev = priv->dev;
693 
694 	dev_dbg(dev, "connector change:%d, encoder mask:0x%x for crtc:%d\n",
695 		crtc_state->connectors_changed, encoder_mask, crtc_index);
696 
697 	for (i = 0; i < mtk_crtc->num_conn_routes; i++) {
698 		unsigned int comp_id = mtk_crtc->conn_routes[i].route_ddp;
699 		struct mtk_ddp_comp *comp = &priv->ddp_comp[comp_id];
700 
701 		if (comp->encoder_index >= 0 &&
702 		    (encoder_mask & BIT(comp->encoder_index))) {
703 			mtk_crtc->ddp_comp[mtk_crtc->ddp_comp_nr - 1] = comp;
704 			dev_dbg(dev, "Add comp_id: %d at path index %d\n",
705 				comp->id, mtk_crtc->ddp_comp_nr - 1);
706 			break;
707 		}
708 	}
709 }
710 
711 int mtk_crtc_plane_check(struct drm_crtc *crtc, struct drm_plane *plane,
712 			 struct mtk_plane_state *state)
713 {
714 	unsigned int local_layer;
715 	struct mtk_ddp_comp *comp;
716 
717 	comp = mtk_ddp_comp_for_plane(crtc, plane, &local_layer);
718 	if (comp)
719 		return mtk_ddp_comp_layer_check(comp, local_layer, state);
720 	return 0;
721 }
722 
723 void mtk_crtc_plane_disable(struct drm_crtc *crtc, struct drm_plane *plane)
724 {
725 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
726 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
727 	struct mtk_plane_state *plane_state = to_mtk_plane_state(plane->state);
728 	int i;
729 
730 	/* no need to wait for disabling the plane by CPU */
731 	if (!mtk_crtc->cmdq_client.chan)
732 		return;
733 
734 	if (!mtk_crtc->enabled)
735 		return;
736 
737 	/* set pending plane state to disabled */
738 	for (i = 0; i < mtk_crtc->layer_nr; i++) {
739 		struct drm_plane *mtk_plane = &mtk_crtc->planes[i];
740 		struct mtk_plane_state *mtk_plane_state = to_mtk_plane_state(mtk_plane->state);
741 
742 		if (mtk_plane->index == plane->index) {
743 			memcpy(mtk_plane_state, plane_state, sizeof(*plane_state));
744 			break;
745 		}
746 	}
747 	mtk_crtc_update_config(mtk_crtc, false);
748 
749 	/* wait for planes to be disabled by CMDQ */
750 	wait_event_timeout(mtk_crtc->cb_blocking_queue,
751 			   mtk_crtc->cmdq_vblank_cnt == 0,
752 			   msecs_to_jiffies(500));
753 #endif
754 }
755 
756 void mtk_crtc_async_update(struct drm_crtc *crtc, struct drm_plane *plane,
757 			   struct drm_atomic_state *state)
758 {
759 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
760 
761 	if (!mtk_crtc->enabled)
762 		return;
763 
764 	mtk_crtc_update_config(mtk_crtc, false);
765 }
766 
767 static void mtk_crtc_atomic_enable(struct drm_crtc *crtc,
768 				   struct drm_atomic_state *state)
769 {
770 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
771 	struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
772 	int ret;
773 
774 	DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
775 
776 	ret = mtk_ddp_comp_power_on(comp);
777 	if (ret < 0) {
778 		DRM_DEV_ERROR(comp->dev, "Failed to enable power domain: %d\n", ret);
779 		return;
780 	}
781 
782 	mtk_crtc_update_output(crtc, state);
783 
784 	ret = mtk_crtc_ddp_hw_init(mtk_crtc);
785 	if (ret) {
786 		mtk_ddp_comp_power_off(comp);
787 		return;
788 	}
789 
790 	drm_crtc_vblank_on(crtc);
791 	mtk_crtc->enabled = true;
792 }
793 
794 static void mtk_crtc_atomic_disable(struct drm_crtc *crtc,
795 				    struct drm_atomic_state *state)
796 {
797 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
798 	struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
799 	int i;
800 
801 	DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
802 	if (!mtk_crtc->enabled)
803 		return;
804 
805 	/* Set all pending plane state to disabled */
806 	for (i = 0; i < mtk_crtc->layer_nr; i++) {
807 		struct drm_plane *plane = &mtk_crtc->planes[i];
808 		struct mtk_plane_state *plane_state;
809 
810 		plane_state = to_mtk_plane_state(plane->state);
811 		plane_state->pending.enable = false;
812 		plane_state->pending.config = true;
813 	}
814 	mtk_crtc->pending_planes = true;
815 
816 	mtk_crtc_update_config(mtk_crtc, false);
817 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
818 	/* Wait for planes to be disabled by cmdq */
819 	if (mtk_crtc->cmdq_client.chan)
820 		wait_event_timeout(mtk_crtc->cb_blocking_queue,
821 				   mtk_crtc->cmdq_vblank_cnt == 0,
822 				   msecs_to_jiffies(500));
823 #endif
824 	/* Wait for planes to be disabled */
825 	drm_crtc_wait_one_vblank(crtc);
826 
827 	drm_crtc_vblank_off(crtc);
828 	mtk_crtc_ddp_hw_fini(mtk_crtc);
829 	mtk_ddp_comp_power_off(comp);
830 
831 	mtk_crtc->enabled = false;
832 }
833 
834 static void mtk_crtc_atomic_begin(struct drm_crtc *crtc,
835 				  struct drm_atomic_state *state)
836 {
837 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
838 									  crtc);
839 	struct mtk_crtc_state *mtk_crtc_state = to_mtk_crtc_state(crtc_state);
840 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
841 	unsigned long flags;
842 
843 	if (mtk_crtc->event && mtk_crtc_state->base.event)
844 		DRM_ERROR("new event while there is still a pending event\n");
845 
846 	if (mtk_crtc_state->base.event) {
847 		mtk_crtc_state->base.event->pipe = drm_crtc_index(crtc);
848 		WARN_ON(drm_crtc_vblank_get(crtc) != 0);
849 
850 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
851 		mtk_crtc->event = mtk_crtc_state->base.event;
852 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
853 
854 		mtk_crtc_state->base.event = NULL;
855 	}
856 }
857 
858 static void mtk_crtc_atomic_flush(struct drm_crtc *crtc,
859 				  struct drm_atomic_state *state)
860 {
861 	struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
862 	int i;
863 
864 	if (crtc->state->color_mgmt_changed)
865 		for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
866 			mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state);
867 			mtk_ddp_ctm_set(mtk_crtc->ddp_comp[i], crtc->state);
868 		}
869 	mtk_crtc_update_config(mtk_crtc, !!mtk_crtc->event);
870 }
871 
872 static const struct drm_crtc_funcs mtk_crtc_funcs = {
873 	.set_config		= drm_atomic_helper_set_config,
874 	.page_flip		= drm_atomic_helper_page_flip,
875 	.destroy		= mtk_crtc_destroy,
876 	.reset			= mtk_crtc_reset,
877 	.atomic_duplicate_state	= mtk_crtc_duplicate_state,
878 	.atomic_destroy_state	= mtk_crtc_destroy_state,
879 	.enable_vblank		= mtk_crtc_enable_vblank,
880 	.disable_vblank		= mtk_crtc_disable_vblank,
881 };
882 
883 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = {
884 	.mode_fixup	= mtk_crtc_mode_fixup,
885 	.mode_set_nofb	= mtk_crtc_mode_set_nofb,
886 	.mode_valid	= mtk_crtc_mode_valid,
887 	.atomic_begin	= mtk_crtc_atomic_begin,
888 	.atomic_flush	= mtk_crtc_atomic_flush,
889 	.atomic_enable	= mtk_crtc_atomic_enable,
890 	.atomic_disable	= mtk_crtc_atomic_disable,
891 };
892 
893 static int mtk_crtc_init(struct drm_device *drm, struct mtk_crtc *mtk_crtc,
894 			 unsigned int pipe)
895 {
896 	struct drm_plane *primary = NULL;
897 	struct drm_plane *cursor = NULL;
898 	int i, ret;
899 
900 	for (i = 0; i < mtk_crtc->layer_nr; i++) {
901 		if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_PRIMARY)
902 			primary = &mtk_crtc->planes[i];
903 		else if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_CURSOR)
904 			cursor = &mtk_crtc->planes[i];
905 	}
906 
907 	ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor,
908 					&mtk_crtc_funcs, NULL);
909 	if (ret)
910 		goto err_cleanup_crtc;
911 
912 	drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs);
913 
914 	return 0;
915 
916 err_cleanup_crtc:
917 	drm_crtc_cleanup(&mtk_crtc->base);
918 	return ret;
919 }
920 
921 static int mtk_crtc_num_comp_planes(struct mtk_crtc *mtk_crtc, int comp_idx)
922 {
923 	struct mtk_ddp_comp *comp;
924 
925 	if (comp_idx > 1)
926 		return 0;
927 
928 	comp = mtk_crtc->ddp_comp[comp_idx];
929 	if (!comp->funcs)
930 		return 0;
931 
932 	if (comp_idx == 1 && !comp->funcs->bgclr_in_on)
933 		return 0;
934 
935 	return mtk_ddp_comp_layer_nr(comp);
936 }
937 
938 static inline
939 enum drm_plane_type mtk_crtc_plane_type(unsigned int plane_idx,
940 					unsigned int num_planes)
941 {
942 	if (plane_idx == 0)
943 		return DRM_PLANE_TYPE_PRIMARY;
944 	else if (plane_idx == (num_planes - 1))
945 		return DRM_PLANE_TYPE_CURSOR;
946 	else
947 		return DRM_PLANE_TYPE_OVERLAY;
948 
949 }
950 
951 static int mtk_crtc_init_comp_planes(struct drm_device *drm_dev,
952 				     struct mtk_crtc *mtk_crtc,
953 				     int comp_idx, int pipe)
954 {
955 	int num_planes = mtk_crtc_num_comp_planes(mtk_crtc, comp_idx);
956 	struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[comp_idx];
957 	int i, ret;
958 
959 	for (i = 0; i < num_planes; i++) {
960 		ret = mtk_plane_init(drm_dev,
961 				&mtk_crtc->planes[mtk_crtc->layer_nr],
962 				BIT(pipe),
963 				mtk_crtc_plane_type(mtk_crtc->layer_nr, num_planes),
964 				mtk_ddp_comp_supported_rotations(comp),
965 				mtk_ddp_comp_get_blend_modes(comp),
966 				mtk_ddp_comp_get_formats(comp),
967 				mtk_ddp_comp_get_num_formats(comp),
968 				mtk_ddp_comp_is_afbc_supported(comp), i);
969 		if (ret)
970 			return ret;
971 
972 		mtk_crtc->layer_nr++;
973 	}
974 	return 0;
975 }
976 
977 struct device *mtk_crtc_dma_dev_get(struct drm_crtc *crtc)
978 {
979 	struct mtk_crtc *mtk_crtc = NULL;
980 
981 	if (!crtc)
982 		return NULL;
983 
984 	mtk_crtc = to_mtk_crtc(crtc);
985 	if (!mtk_crtc)
986 		return NULL;
987 
988 	return mtk_crtc->dma_dev;
989 }
990 
991 int mtk_crtc_create(struct drm_device *drm_dev, const unsigned int *path,
992 		    unsigned int path_len, int priv_data_index,
993 		    const struct mtk_drm_route *conn_routes,
994 		    unsigned int num_conn_routes)
995 {
996 	struct mtk_drm_private *priv = drm_dev->dev_private;
997 	struct device *dev = drm_dev->dev;
998 	struct mtk_crtc *mtk_crtc;
999 	unsigned int num_comp_planes = 0;
1000 	int ret;
1001 	int i;
1002 	bool has_ctm = false;
1003 	uint gamma_lut_size = 0;
1004 	struct drm_crtc *tmp;
1005 	int crtc_i = 0;
1006 
1007 	if (!path)
1008 		return 0;
1009 
1010 	priv = priv->all_drm_private[priv_data_index];
1011 
1012 	drm_for_each_crtc(tmp, drm_dev)
1013 		crtc_i++;
1014 
1015 	for (i = 0; i < path_len; i++) {
1016 		enum mtk_ddp_comp_id comp_id = path[i];
1017 		struct device_node *node;
1018 		struct mtk_ddp_comp *comp;
1019 
1020 		node = priv->comp_node[comp_id];
1021 		comp = &priv->ddp_comp[comp_id];
1022 
1023 		/* Not all drm components have a DTS device node, such as ovl_adaptor,
1024 		 * which is the drm bring up sub driver
1025 		 */
1026 		if (!node && comp_id != DDP_COMPONENT_DRM_OVL_ADAPTOR) {
1027 			dev_info(dev,
1028 				"Not creating crtc %d because component %d is disabled or missing\n",
1029 				crtc_i, comp_id);
1030 			return 0;
1031 		}
1032 
1033 		if (!comp->dev) {
1034 			dev_err(dev, "Component %pOF not initialized\n", node);
1035 			return -ENODEV;
1036 		}
1037 	}
1038 
1039 	mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL);
1040 	if (!mtk_crtc)
1041 		return -ENOMEM;
1042 
1043 	mtk_crtc->mmsys_dev = priv->mmsys_dev;
1044 	mtk_crtc->ddp_comp_nr = path_len;
1045 	mtk_crtc->ddp_comp = devm_kcalloc(dev,
1046 					  mtk_crtc->ddp_comp_nr + (conn_routes ? 1 : 0),
1047 					  sizeof(*mtk_crtc->ddp_comp),
1048 					  GFP_KERNEL);
1049 	if (!mtk_crtc->ddp_comp)
1050 		return -ENOMEM;
1051 
1052 	mtk_crtc->mutex = mtk_mutex_get(priv->mutex_dev);
1053 	if (IS_ERR(mtk_crtc->mutex)) {
1054 		ret = PTR_ERR(mtk_crtc->mutex);
1055 		dev_err(dev, "Failed to get mutex: %d\n", ret);
1056 		return ret;
1057 	}
1058 
1059 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
1060 		unsigned int comp_id = path[i];
1061 		struct mtk_ddp_comp *comp;
1062 
1063 		comp = &priv->ddp_comp[comp_id];
1064 		mtk_crtc->ddp_comp[i] = comp;
1065 
1066 		if (comp->funcs) {
1067 			if (comp->funcs->gamma_set && comp->funcs->gamma_get_lut_size) {
1068 				unsigned int lut_sz = mtk_ddp_gamma_get_lut_size(comp);
1069 
1070 				if (lut_sz)
1071 					gamma_lut_size = lut_sz;
1072 			}
1073 
1074 			if (comp->funcs->ctm_set)
1075 				has_ctm = true;
1076 		}
1077 
1078 		mtk_ddp_comp_register_vblank_cb(comp, mtk_crtc_ddp_irq,
1079 						&mtk_crtc->base);
1080 	}
1081 
1082 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
1083 		num_comp_planes += mtk_crtc_num_comp_planes(mtk_crtc, i);
1084 
1085 	mtk_crtc->planes = devm_kcalloc(dev, num_comp_planes,
1086 					sizeof(struct drm_plane), GFP_KERNEL);
1087 	if (!mtk_crtc->planes)
1088 		return -ENOMEM;
1089 
1090 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
1091 		ret = mtk_crtc_init_comp_planes(drm_dev, mtk_crtc, i, crtc_i);
1092 		if (ret)
1093 			return ret;
1094 	}
1095 
1096 	/*
1097 	 * Default to use the first component as the dma dev.
1098 	 * In the case of ovl_adaptor sub driver, it needs to use the
1099 	 * dma_dev_get function to get representative dma dev.
1100 	 */
1101 	mtk_crtc->dma_dev = mtk_ddp_comp_dma_dev_get(&priv->ddp_comp[path[0]]);
1102 
1103 	ret = mtk_crtc_init(drm_dev, mtk_crtc, crtc_i);
1104 	if (ret < 0)
1105 		return ret;
1106 
1107 	if (gamma_lut_size)
1108 		drm_mode_crtc_set_gamma_size(&mtk_crtc->base, gamma_lut_size);
1109 	drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, has_ctm, gamma_lut_size);
1110 	mutex_init(&mtk_crtc->hw_lock);
1111 	spin_lock_init(&mtk_crtc->config_lock);
1112 
1113 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
1114 	i = priv->mbox_index++;
1115 	mtk_crtc->cmdq_client.client.dev = mtk_crtc->mmsys_dev;
1116 	mtk_crtc->cmdq_client.client.tx_block = false;
1117 	mtk_crtc->cmdq_client.client.knows_txdone = true;
1118 	mtk_crtc->cmdq_client.client.rx_callback = ddp_cmdq_cb;
1119 	mtk_crtc->cmdq_client.chan =
1120 			mbox_request_channel(&mtk_crtc->cmdq_client.client, i);
1121 	if (IS_ERR(mtk_crtc->cmdq_client.chan)) {
1122 		dev_dbg(dev, "mtk_crtc %d failed to create mailbox client, writing register by CPU now\n",
1123 			drm_crtc_index(&mtk_crtc->base));
1124 		mtk_crtc->cmdq_client.chan = NULL;
1125 	}
1126 
1127 	if (mtk_crtc->cmdq_client.chan) {
1128 		ret = of_property_read_u32_index(priv->mutex_node,
1129 						 "mediatek,gce-events",
1130 						 i,
1131 						 &mtk_crtc->cmdq_event);
1132 		if (ret) {
1133 			dev_dbg(dev, "mtk_crtc %d failed to get mediatek,gce-events property\n",
1134 				drm_crtc_index(&mtk_crtc->base));
1135 			mbox_free_channel(mtk_crtc->cmdq_client.chan);
1136 			mtk_crtc->cmdq_client.chan = NULL;
1137 		} else {
1138 			ret = cmdq_pkt_create(&mtk_crtc->cmdq_client,
1139 					      &mtk_crtc->cmdq_handle,
1140 					      PAGE_SIZE);
1141 			if (ret) {
1142 				dev_dbg(dev, "mtk_crtc %d failed to create cmdq packet\n",
1143 					drm_crtc_index(&mtk_crtc->base));
1144 				mbox_free_channel(mtk_crtc->cmdq_client.chan);
1145 				mtk_crtc->cmdq_client.chan = NULL;
1146 			}
1147 		}
1148 
1149 		/* for sending blocking cmd in crtc disable */
1150 		init_waitqueue_head(&mtk_crtc->cb_blocking_queue);
1151 	}
1152 #endif
1153 
1154 	if (conn_routes) {
1155 		for (i = 0; i < num_conn_routes; i++) {
1156 			unsigned int comp_id = conn_routes[i].route_ddp;
1157 			struct device_node *node = priv->comp_node[comp_id];
1158 			struct mtk_ddp_comp *comp = &priv->ddp_comp[comp_id];
1159 
1160 			if (!comp->dev) {
1161 				dev_dbg(dev, "comp_id:%d, Component %pOF not initialized\n",
1162 					comp_id, node);
1163 				/* mark encoder_index to -1, if route comp device is not enabled */
1164 				comp->encoder_index = -1;
1165 				continue;
1166 			}
1167 
1168 			mtk_ddp_comp_encoder_index_set(&priv->ddp_comp[comp_id]);
1169 		}
1170 
1171 		mtk_crtc->num_conn_routes = num_conn_routes;
1172 		mtk_crtc->conn_routes = conn_routes;
1173 
1174 		/* increase ddp_comp_nr at the end of mtk_crtc_create */
1175 		mtk_crtc->ddp_comp_nr++;
1176 	}
1177 
1178 	return 0;
1179 }
1180