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/pm_runtime.h>
8 #include <linux/soc/mediatek/mtk-cmdq.h>
9 #include <linux/soc/mediatek/mtk-mmsys.h>
10 
11 #include <asm/barrier.h>
12 #include <soc/mediatek/smi.h>
13 
14 #include <drm/drm_atomic_helper.h>
15 #include <drm/drm_plane_helper.h>
16 #include <drm/drm_probe_helper.h>
17 #include <drm/drm_vblank.h>
18 
19 #include "mtk_drm_drv.h"
20 #include "mtk_drm_crtc.h"
21 #include "mtk_drm_ddp.h"
22 #include "mtk_drm_ddp_comp.h"
23 #include "mtk_drm_gem.h"
24 #include "mtk_drm_plane.h"
25 
26 /**
27  * struct mtk_drm_crtc - MediaTek specific crtc structure.
28  * @base: crtc object.
29  * @enabled: records whether crtc_enable succeeded
30  * @planes: array of 4 drm_plane structures, one for each overlay plane
31  * @pending_planes: whether any plane has pending changes to be applied
32  * @mmsys_dev: pointer to the mmsys device for configuration registers
33  * @mutex: handle to one of the ten disp_mutex streams
34  * @ddp_comp_nr: number of components in ddp_comp
35  * @ddp_comp: array of pointers the mtk_ddp_comp structures used by this crtc
36  */
37 struct mtk_drm_crtc {
38 	struct drm_crtc			base;
39 	bool				enabled;
40 
41 	bool				pending_needs_vblank;
42 	struct drm_pending_vblank_event	*event;
43 
44 	struct drm_plane		*planes;
45 	unsigned int			layer_nr;
46 	bool				pending_planes;
47 	bool				pending_async_planes;
48 
49 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
50 	struct cmdq_client		*cmdq_client;
51 	u32				cmdq_event;
52 #endif
53 
54 	struct device			*mmsys_dev;
55 	struct mtk_disp_mutex		*mutex;
56 	unsigned int			ddp_comp_nr;
57 	struct mtk_ddp_comp		**ddp_comp;
58 
59 	/* lock for display hardware access */
60 	struct mutex			hw_lock;
61 };
62 
63 struct mtk_crtc_state {
64 	struct drm_crtc_state		base;
65 
66 	bool				pending_config;
67 	unsigned int			pending_width;
68 	unsigned int			pending_height;
69 	unsigned int			pending_vrefresh;
70 };
71 
72 static inline struct mtk_drm_crtc *to_mtk_crtc(struct drm_crtc *c)
73 {
74 	return container_of(c, struct mtk_drm_crtc, base);
75 }
76 
77 static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s)
78 {
79 	return container_of(s, struct mtk_crtc_state, base);
80 }
81 
82 static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
83 {
84 	struct drm_crtc *crtc = &mtk_crtc->base;
85 	unsigned long flags;
86 
87 	spin_lock_irqsave(&crtc->dev->event_lock, flags);
88 	drm_crtc_send_vblank_event(crtc, mtk_crtc->event);
89 	drm_crtc_vblank_put(crtc);
90 	mtk_crtc->event = NULL;
91 	spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
92 }
93 
94 static void mtk_drm_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
95 {
96 	drm_crtc_handle_vblank(&mtk_crtc->base);
97 	if (mtk_crtc->pending_needs_vblank) {
98 		mtk_drm_crtc_finish_page_flip(mtk_crtc);
99 		mtk_crtc->pending_needs_vblank = false;
100 	}
101 }
102 
103 static void mtk_drm_crtc_destroy(struct drm_crtc *crtc)
104 {
105 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
106 
107 	mtk_disp_mutex_put(mtk_crtc->mutex);
108 
109 	drm_crtc_cleanup(crtc);
110 }
111 
112 static void mtk_drm_crtc_reset(struct drm_crtc *crtc)
113 {
114 	struct mtk_crtc_state *state;
115 
116 	if (crtc->state)
117 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
118 
119 	kfree(to_mtk_crtc_state(crtc->state));
120 	crtc->state = NULL;
121 
122 	state = kzalloc(sizeof(*state), GFP_KERNEL);
123 	if (state)
124 		__drm_atomic_helper_crtc_reset(crtc, &state->base);
125 }
126 
127 static struct drm_crtc_state *mtk_drm_crtc_duplicate_state(struct drm_crtc *crtc)
128 {
129 	struct mtk_crtc_state *state;
130 
131 	state = kzalloc(sizeof(*state), GFP_KERNEL);
132 	if (!state)
133 		return NULL;
134 
135 	__drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
136 
137 	WARN_ON(state->base.crtc != crtc);
138 	state->base.crtc = crtc;
139 
140 	return &state->base;
141 }
142 
143 static void mtk_drm_crtc_destroy_state(struct drm_crtc *crtc,
144 				       struct drm_crtc_state *state)
145 {
146 	__drm_atomic_helper_crtc_destroy_state(state);
147 	kfree(to_mtk_crtc_state(state));
148 }
149 
150 static bool mtk_drm_crtc_mode_fixup(struct drm_crtc *crtc,
151 				    const struct drm_display_mode *mode,
152 				    struct drm_display_mode *adjusted_mode)
153 {
154 	/* Nothing to do here, but this callback is mandatory. */
155 	return true;
156 }
157 
158 static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
159 {
160 	struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
161 
162 	state->pending_width = crtc->mode.hdisplay;
163 	state->pending_height = crtc->mode.vdisplay;
164 	state->pending_vrefresh = drm_mode_vrefresh(&crtc->mode);
165 	wmb();	/* Make sure the above parameters are set before update */
166 	state->pending_config = true;
167 }
168 
169 static int mtk_drm_crtc_enable_vblank(struct drm_crtc *crtc)
170 {
171 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
172 	struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
173 
174 	mtk_ddp_comp_enable_vblank(comp, &mtk_crtc->base);
175 
176 	return 0;
177 }
178 
179 static void mtk_drm_crtc_disable_vblank(struct drm_crtc *crtc)
180 {
181 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
182 	struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
183 
184 	mtk_ddp_comp_disable_vblank(comp);
185 }
186 
187 static int mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc *mtk_crtc)
188 {
189 	int ret;
190 	int i;
191 
192 	DRM_DEBUG_DRIVER("%s\n", __func__);
193 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
194 		ret = clk_prepare_enable(mtk_crtc->ddp_comp[i]->clk);
195 		if (ret) {
196 			DRM_ERROR("Failed to enable clock %d: %d\n", i, ret);
197 			goto err;
198 		}
199 	}
200 
201 	return 0;
202 err:
203 	while (--i >= 0)
204 		clk_disable_unprepare(mtk_crtc->ddp_comp[i]->clk);
205 	return ret;
206 }
207 
208 static void mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc *mtk_crtc)
209 {
210 	int i;
211 
212 	DRM_DEBUG_DRIVER("%s\n", __func__);
213 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
214 		clk_disable_unprepare(mtk_crtc->ddp_comp[i]->clk);
215 }
216 
217 static
218 struct mtk_ddp_comp *mtk_drm_ddp_comp_for_plane(struct drm_crtc *crtc,
219 						struct drm_plane *plane,
220 						unsigned int *local_layer)
221 {
222 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
223 	struct mtk_ddp_comp *comp;
224 	int i, count = 0;
225 	unsigned int local_index = plane - mtk_crtc->planes;
226 
227 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
228 		comp = mtk_crtc->ddp_comp[i];
229 		if (local_index < (count + mtk_ddp_comp_layer_nr(comp))) {
230 			*local_layer = local_index - count;
231 			return comp;
232 		}
233 		count += mtk_ddp_comp_layer_nr(comp);
234 	}
235 
236 	WARN(1, "Failed to find component for plane %d\n", plane->index);
237 	return NULL;
238 }
239 
240 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
241 static void ddp_cmdq_cb(struct cmdq_cb_data data)
242 {
243 	cmdq_pkt_destroy(data.data);
244 }
245 #endif
246 
247 static int mtk_crtc_ddp_hw_init(struct mtk_drm_crtc *mtk_crtc)
248 {
249 	struct drm_crtc *crtc = &mtk_crtc->base;
250 	struct drm_connector *connector;
251 	struct drm_encoder *encoder;
252 	struct drm_connector_list_iter conn_iter;
253 	unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC;
254 	int ret;
255 	int i;
256 
257 	DRM_DEBUG_DRIVER("%s\n", __func__);
258 	if (WARN_ON(!crtc->state))
259 		return -EINVAL;
260 
261 	width = crtc->state->adjusted_mode.hdisplay;
262 	height = crtc->state->adjusted_mode.vdisplay;
263 	vrefresh = drm_mode_vrefresh(&crtc->state->adjusted_mode);
264 
265 	drm_for_each_encoder(encoder, crtc->dev) {
266 		if (encoder->crtc != crtc)
267 			continue;
268 
269 		drm_connector_list_iter_begin(crtc->dev, &conn_iter);
270 		drm_for_each_connector_iter(connector, &conn_iter) {
271 			if (connector->encoder != encoder)
272 				continue;
273 			if (connector->display_info.bpc != 0 &&
274 			    bpc > connector->display_info.bpc)
275 				bpc = connector->display_info.bpc;
276 		}
277 		drm_connector_list_iter_end(&conn_iter);
278 	}
279 
280 	ret = pm_runtime_get_sync(crtc->dev->dev);
281 	if (ret < 0) {
282 		DRM_ERROR("Failed to enable power domain: %d\n", ret);
283 		return ret;
284 	}
285 
286 	ret = mtk_disp_mutex_prepare(mtk_crtc->mutex);
287 	if (ret < 0) {
288 		DRM_ERROR("Failed to enable mutex clock: %d\n", ret);
289 		goto err_pm_runtime_put;
290 	}
291 
292 	ret = mtk_crtc_ddp_clk_enable(mtk_crtc);
293 	if (ret < 0) {
294 		DRM_ERROR("Failed to enable component clocks: %d\n", ret);
295 		goto err_mutex_unprepare;
296 	}
297 
298 	DRM_DEBUG_DRIVER("mediatek_ddp_ddp_path_setup\n");
299 	for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
300 		mtk_mmsys_ddp_connect(mtk_crtc->mmsys_dev,
301 				      mtk_crtc->ddp_comp[i]->id,
302 				      mtk_crtc->ddp_comp[i + 1]->id);
303 		mtk_disp_mutex_add_comp(mtk_crtc->mutex,
304 					mtk_crtc->ddp_comp[i]->id);
305 	}
306 	mtk_disp_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
307 	mtk_disp_mutex_enable(mtk_crtc->mutex);
308 
309 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
310 		struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i];
311 
312 		if (i == 1)
313 			mtk_ddp_comp_bgclr_in_on(comp);
314 
315 		mtk_ddp_comp_config(comp, width, height, vrefresh, bpc, NULL);
316 		mtk_ddp_comp_start(comp);
317 	}
318 
319 	/* Initially configure all planes */
320 	for (i = 0; i < mtk_crtc->layer_nr; i++) {
321 		struct drm_plane *plane = &mtk_crtc->planes[i];
322 		struct mtk_plane_state *plane_state;
323 		struct mtk_ddp_comp *comp;
324 		unsigned int local_layer;
325 
326 		plane_state = to_mtk_plane_state(plane->state);
327 		comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer);
328 		if (comp)
329 			mtk_ddp_comp_layer_config(comp, local_layer,
330 						  plane_state, NULL);
331 	}
332 
333 	return 0;
334 
335 err_mutex_unprepare:
336 	mtk_disp_mutex_unprepare(mtk_crtc->mutex);
337 err_pm_runtime_put:
338 	pm_runtime_put(crtc->dev->dev);
339 	return ret;
340 }
341 
342 static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc)
343 {
344 	struct drm_device *drm = mtk_crtc->base.dev;
345 	struct drm_crtc *crtc = &mtk_crtc->base;
346 	int i;
347 
348 	DRM_DEBUG_DRIVER("%s\n", __func__);
349 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
350 		mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]);
351 		if (i == 1)
352 			mtk_ddp_comp_bgclr_in_off(mtk_crtc->ddp_comp[i]);
353 	}
354 
355 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
356 		mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
357 					   mtk_crtc->ddp_comp[i]->id);
358 	mtk_disp_mutex_disable(mtk_crtc->mutex);
359 	for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
360 		mtk_mmsys_ddp_disconnect(mtk_crtc->mmsys_dev,
361 					 mtk_crtc->ddp_comp[i]->id,
362 					 mtk_crtc->ddp_comp[i + 1]->id);
363 		mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
364 					   mtk_crtc->ddp_comp[i]->id);
365 	}
366 	mtk_disp_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
367 	mtk_crtc_ddp_clk_disable(mtk_crtc);
368 	mtk_disp_mutex_unprepare(mtk_crtc->mutex);
369 
370 	pm_runtime_put(drm->dev);
371 
372 	if (crtc->state->event && !crtc->state->active) {
373 		spin_lock_irq(&crtc->dev->event_lock);
374 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
375 		crtc->state->event = NULL;
376 		spin_unlock_irq(&crtc->dev->event_lock);
377 	}
378 }
379 
380 static void mtk_crtc_ddp_config(struct drm_crtc *crtc,
381 				struct cmdq_pkt *cmdq_handle)
382 {
383 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
384 	struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state);
385 	struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
386 	unsigned int i;
387 	unsigned int local_layer;
388 
389 	/*
390 	 * TODO: instead of updating the registers here, we should prepare
391 	 * working registers in atomic_commit and let the hardware command
392 	 * queue update module registers on vblank.
393 	 */
394 	if (state->pending_config) {
395 		mtk_ddp_comp_config(comp, state->pending_width,
396 				    state->pending_height,
397 				    state->pending_vrefresh, 0,
398 				    cmdq_handle);
399 
400 		state->pending_config = false;
401 	}
402 
403 	if (mtk_crtc->pending_planes) {
404 		for (i = 0; i < mtk_crtc->layer_nr; i++) {
405 			struct drm_plane *plane = &mtk_crtc->planes[i];
406 			struct mtk_plane_state *plane_state;
407 
408 			plane_state = to_mtk_plane_state(plane->state);
409 
410 			if (!plane_state->pending.config)
411 				continue;
412 
413 			comp = mtk_drm_ddp_comp_for_plane(crtc, plane,
414 							  &local_layer);
415 
416 			if (comp)
417 				mtk_ddp_comp_layer_config(comp, local_layer,
418 							  plane_state,
419 							  cmdq_handle);
420 			plane_state->pending.config = false;
421 		}
422 		mtk_crtc->pending_planes = false;
423 	}
424 
425 	if (mtk_crtc->pending_async_planes) {
426 		for (i = 0; i < mtk_crtc->layer_nr; i++) {
427 			struct drm_plane *plane = &mtk_crtc->planes[i];
428 			struct mtk_plane_state *plane_state;
429 
430 			plane_state = to_mtk_plane_state(plane->state);
431 
432 			if (!plane_state->pending.async_config)
433 				continue;
434 
435 			comp = mtk_drm_ddp_comp_for_plane(crtc, plane,
436 							  &local_layer);
437 
438 			if (comp)
439 				mtk_ddp_comp_layer_config(comp, local_layer,
440 							  plane_state,
441 							  cmdq_handle);
442 			plane_state->pending.async_config = false;
443 		}
444 		mtk_crtc->pending_async_planes = false;
445 	}
446 }
447 
448 static void mtk_drm_crtc_hw_config(struct mtk_drm_crtc *mtk_crtc)
449 {
450 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
451 	struct cmdq_pkt *cmdq_handle;
452 #endif
453 	struct drm_crtc *crtc = &mtk_crtc->base;
454 	struct mtk_drm_private *priv = crtc->dev->dev_private;
455 	unsigned int pending_planes = 0, pending_async_planes = 0;
456 	int i;
457 
458 	mutex_lock(&mtk_crtc->hw_lock);
459 	for (i = 0; i < mtk_crtc->layer_nr; i++) {
460 		struct drm_plane *plane = &mtk_crtc->planes[i];
461 		struct mtk_plane_state *plane_state;
462 
463 		plane_state = to_mtk_plane_state(plane->state);
464 		if (plane_state->pending.dirty) {
465 			plane_state->pending.config = true;
466 			plane_state->pending.dirty = false;
467 			pending_planes |= BIT(i);
468 		} else if (plane_state->pending.async_dirty) {
469 			plane_state->pending.async_config = true;
470 			plane_state->pending.async_dirty = false;
471 			pending_async_planes |= BIT(i);
472 		}
473 	}
474 	if (pending_planes)
475 		mtk_crtc->pending_planes = true;
476 	if (pending_async_planes)
477 		mtk_crtc->pending_async_planes = true;
478 
479 	if (priv->data->shadow_register) {
480 		mtk_disp_mutex_acquire(mtk_crtc->mutex);
481 		mtk_crtc_ddp_config(crtc, NULL);
482 		mtk_disp_mutex_release(mtk_crtc->mutex);
483 	}
484 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
485 	if (mtk_crtc->cmdq_client) {
486 		mbox_flush(mtk_crtc->cmdq_client->chan, 2000);
487 		cmdq_handle = cmdq_pkt_create(mtk_crtc->cmdq_client, PAGE_SIZE);
488 		cmdq_pkt_clear_event(cmdq_handle, mtk_crtc->cmdq_event);
489 		cmdq_pkt_wfe(cmdq_handle, mtk_crtc->cmdq_event);
490 		mtk_crtc_ddp_config(crtc, cmdq_handle);
491 		cmdq_pkt_flush_async(cmdq_handle, ddp_cmdq_cb, cmdq_handle);
492 	}
493 #endif
494 	mutex_unlock(&mtk_crtc->hw_lock);
495 }
496 
497 int mtk_drm_crtc_plane_check(struct drm_crtc *crtc, struct drm_plane *plane,
498 			     struct mtk_plane_state *state)
499 {
500 	unsigned int local_layer;
501 	struct mtk_ddp_comp *comp;
502 
503 	comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer);
504 	if (comp)
505 		return mtk_ddp_comp_layer_check(comp, local_layer, state);
506 	return 0;
507 }
508 
509 void mtk_drm_crtc_async_update(struct drm_crtc *crtc, struct drm_plane *plane,
510 			       struct drm_plane_state *new_state)
511 {
512 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
513 	const struct drm_plane_helper_funcs *plane_helper_funcs =
514 			plane->helper_private;
515 
516 	if (!mtk_crtc->enabled)
517 		return;
518 
519 	plane_helper_funcs->atomic_update(plane, new_state);
520 	mtk_drm_crtc_hw_config(mtk_crtc);
521 }
522 
523 static void mtk_drm_crtc_atomic_enable(struct drm_crtc *crtc,
524 				       struct drm_crtc_state *old_state)
525 {
526 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
527 	struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
528 	int ret;
529 
530 	DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
531 
532 	ret = mtk_smi_larb_get(comp->larb_dev);
533 	if (ret) {
534 		DRM_ERROR("Failed to get larb: %d\n", ret);
535 		return;
536 	}
537 
538 	ret = mtk_crtc_ddp_hw_init(mtk_crtc);
539 	if (ret) {
540 		mtk_smi_larb_put(comp->larb_dev);
541 		return;
542 	}
543 
544 	drm_crtc_vblank_on(crtc);
545 	mtk_crtc->enabled = true;
546 }
547 
548 static void mtk_drm_crtc_atomic_disable(struct drm_crtc *crtc,
549 					struct drm_crtc_state *old_state)
550 {
551 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
552 	struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
553 	int i;
554 
555 	DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
556 	if (!mtk_crtc->enabled)
557 		return;
558 
559 	/* Set all pending plane state to disabled */
560 	for (i = 0; i < mtk_crtc->layer_nr; i++) {
561 		struct drm_plane *plane = &mtk_crtc->planes[i];
562 		struct mtk_plane_state *plane_state;
563 
564 		plane_state = to_mtk_plane_state(plane->state);
565 		plane_state->pending.enable = false;
566 		plane_state->pending.config = true;
567 	}
568 	mtk_crtc->pending_planes = true;
569 
570 	mtk_drm_crtc_hw_config(mtk_crtc);
571 	/* Wait for planes to be disabled */
572 	drm_crtc_wait_one_vblank(crtc);
573 
574 	drm_crtc_vblank_off(crtc);
575 	mtk_crtc_ddp_hw_fini(mtk_crtc);
576 	mtk_smi_larb_put(comp->larb_dev);
577 
578 	mtk_crtc->enabled = false;
579 }
580 
581 static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc,
582 				      struct drm_crtc_state *old_crtc_state)
583 {
584 	struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
585 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
586 
587 	if (mtk_crtc->event && state->base.event)
588 		DRM_ERROR("new event while there is still a pending event\n");
589 
590 	if (state->base.event) {
591 		state->base.event->pipe = drm_crtc_index(crtc);
592 		WARN_ON(drm_crtc_vblank_get(crtc) != 0);
593 		mtk_crtc->event = state->base.event;
594 		state->base.event = NULL;
595 	}
596 }
597 
598 static void mtk_drm_crtc_atomic_flush(struct drm_crtc *crtc,
599 				      struct drm_crtc_state *old_crtc_state)
600 {
601 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
602 	int i;
603 
604 	if (mtk_crtc->event)
605 		mtk_crtc->pending_needs_vblank = true;
606 	if (crtc->state->color_mgmt_changed)
607 		for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
608 			mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state);
609 			mtk_ddp_ctm_set(mtk_crtc->ddp_comp[i], crtc->state);
610 		}
611 	mtk_drm_crtc_hw_config(mtk_crtc);
612 }
613 
614 static const struct drm_crtc_funcs mtk_crtc_funcs = {
615 	.set_config		= drm_atomic_helper_set_config,
616 	.page_flip		= drm_atomic_helper_page_flip,
617 	.destroy		= mtk_drm_crtc_destroy,
618 	.reset			= mtk_drm_crtc_reset,
619 	.atomic_duplicate_state	= mtk_drm_crtc_duplicate_state,
620 	.atomic_destroy_state	= mtk_drm_crtc_destroy_state,
621 	.gamma_set		= drm_atomic_helper_legacy_gamma_set,
622 	.enable_vblank		= mtk_drm_crtc_enable_vblank,
623 	.disable_vblank		= mtk_drm_crtc_disable_vblank,
624 };
625 
626 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = {
627 	.mode_fixup	= mtk_drm_crtc_mode_fixup,
628 	.mode_set_nofb	= mtk_drm_crtc_mode_set_nofb,
629 	.atomic_begin	= mtk_drm_crtc_atomic_begin,
630 	.atomic_flush	= mtk_drm_crtc_atomic_flush,
631 	.atomic_enable	= mtk_drm_crtc_atomic_enable,
632 	.atomic_disable	= mtk_drm_crtc_atomic_disable,
633 };
634 
635 static int mtk_drm_crtc_init(struct drm_device *drm,
636 			     struct mtk_drm_crtc *mtk_crtc,
637 			     unsigned int pipe)
638 {
639 	struct drm_plane *primary = NULL;
640 	struct drm_plane *cursor = NULL;
641 	int i, ret;
642 
643 	for (i = 0; i < mtk_crtc->layer_nr; i++) {
644 		if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_PRIMARY)
645 			primary = &mtk_crtc->planes[i];
646 		else if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_CURSOR)
647 			cursor = &mtk_crtc->planes[i];
648 	}
649 
650 	ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor,
651 					&mtk_crtc_funcs, NULL);
652 	if (ret)
653 		goto err_cleanup_crtc;
654 
655 	drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs);
656 
657 	return 0;
658 
659 err_cleanup_crtc:
660 	drm_crtc_cleanup(&mtk_crtc->base);
661 	return ret;
662 }
663 
664 void mtk_crtc_ddp_irq(struct drm_crtc *crtc, struct mtk_ddp_comp *comp)
665 {
666 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
667 	struct mtk_drm_private *priv = crtc->dev->dev_private;
668 
669 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
670 	if (!priv->data->shadow_register && !mtk_crtc->cmdq_client)
671 #else
672 	if (!priv->data->shadow_register)
673 #endif
674 		mtk_crtc_ddp_config(crtc, NULL);
675 
676 	mtk_drm_finish_page_flip(mtk_crtc);
677 }
678 
679 static int mtk_drm_crtc_num_comp_planes(struct mtk_drm_crtc *mtk_crtc,
680 					int comp_idx)
681 {
682 	struct mtk_ddp_comp *comp;
683 
684 	if (comp_idx > 1)
685 		return 0;
686 
687 	comp = mtk_crtc->ddp_comp[comp_idx];
688 	if (!comp->funcs)
689 		return 0;
690 
691 	if (comp_idx == 1 && !comp->funcs->bgclr_in_on)
692 		return 0;
693 
694 	return mtk_ddp_comp_layer_nr(comp);
695 }
696 
697 static inline
698 enum drm_plane_type mtk_drm_crtc_plane_type(unsigned int plane_idx,
699 					    unsigned int num_planes)
700 {
701 	if (plane_idx == 0)
702 		return DRM_PLANE_TYPE_PRIMARY;
703 	else if (plane_idx == (num_planes - 1))
704 		return DRM_PLANE_TYPE_CURSOR;
705 	else
706 		return DRM_PLANE_TYPE_OVERLAY;
707 
708 }
709 
710 static int mtk_drm_crtc_init_comp_planes(struct drm_device *drm_dev,
711 					 struct mtk_drm_crtc *mtk_crtc,
712 					 int comp_idx, int pipe)
713 {
714 	int num_planes = mtk_drm_crtc_num_comp_planes(mtk_crtc, comp_idx);
715 	struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[comp_idx];
716 	int i, ret;
717 
718 	for (i = 0; i < num_planes; i++) {
719 		ret = mtk_plane_init(drm_dev,
720 				&mtk_crtc->planes[mtk_crtc->layer_nr],
721 				BIT(pipe),
722 				mtk_drm_crtc_plane_type(mtk_crtc->layer_nr,
723 							num_planes),
724 				mtk_ddp_comp_supported_rotations(comp));
725 		if (ret)
726 			return ret;
727 
728 		mtk_crtc->layer_nr++;
729 	}
730 	return 0;
731 }
732 
733 int mtk_drm_crtc_create(struct drm_device *drm_dev,
734 			const enum mtk_ddp_comp_id *path, unsigned int path_len)
735 {
736 	struct mtk_drm_private *priv = drm_dev->dev_private;
737 	struct device *dev = drm_dev->dev;
738 	struct mtk_drm_crtc *mtk_crtc;
739 	unsigned int num_comp_planes = 0;
740 	int pipe = priv->num_pipes;
741 	int ret;
742 	int i;
743 	bool has_ctm = false;
744 	uint gamma_lut_size = 0;
745 
746 	if (!path)
747 		return 0;
748 
749 	for (i = 0; i < path_len; i++) {
750 		enum mtk_ddp_comp_id comp_id = path[i];
751 		struct device_node *node;
752 
753 		node = priv->comp_node[comp_id];
754 		if (!node) {
755 			dev_info(dev,
756 				 "Not creating crtc %d because component %d is disabled or missing\n",
757 				 pipe, comp_id);
758 			return 0;
759 		}
760 	}
761 
762 	mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL);
763 	if (!mtk_crtc)
764 		return -ENOMEM;
765 
766 	mtk_crtc->mmsys_dev = priv->mmsys_dev;
767 	mtk_crtc->ddp_comp_nr = path_len;
768 	mtk_crtc->ddp_comp = devm_kmalloc_array(dev, mtk_crtc->ddp_comp_nr,
769 						sizeof(*mtk_crtc->ddp_comp),
770 						GFP_KERNEL);
771 	if (!mtk_crtc->ddp_comp)
772 		return -ENOMEM;
773 
774 	mtk_crtc->mutex = mtk_disp_mutex_get(priv->mutex_dev, pipe);
775 	if (IS_ERR(mtk_crtc->mutex)) {
776 		ret = PTR_ERR(mtk_crtc->mutex);
777 		dev_err(dev, "Failed to get mutex: %d\n", ret);
778 		return ret;
779 	}
780 
781 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
782 		enum mtk_ddp_comp_id comp_id = path[i];
783 		struct mtk_ddp_comp *comp;
784 		struct device_node *node;
785 
786 		node = priv->comp_node[comp_id];
787 		comp = priv->ddp_comp[comp_id];
788 		if (!comp) {
789 			dev_err(dev, "Component %pOF not initialized\n", node);
790 			ret = -ENODEV;
791 			return ret;
792 		}
793 
794 		mtk_crtc->ddp_comp[i] = comp;
795 
796 		if (comp->funcs) {
797 			if (comp->funcs->gamma_set)
798 				gamma_lut_size = MTK_LUT_SIZE;
799 
800 			if (comp->funcs->ctm_set)
801 				has_ctm = true;
802 		}
803 	}
804 
805 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
806 		num_comp_planes += mtk_drm_crtc_num_comp_planes(mtk_crtc, i);
807 
808 	mtk_crtc->planes = devm_kcalloc(dev, num_comp_planes,
809 					sizeof(struct drm_plane), GFP_KERNEL);
810 
811 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
812 		ret = mtk_drm_crtc_init_comp_planes(drm_dev, mtk_crtc, i,
813 						    pipe);
814 		if (ret)
815 			return ret;
816 	}
817 
818 	ret = mtk_drm_crtc_init(drm_dev, mtk_crtc, pipe);
819 	if (ret < 0)
820 		return ret;
821 
822 	if (gamma_lut_size)
823 		drm_mode_crtc_set_gamma_size(&mtk_crtc->base, gamma_lut_size);
824 	drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, has_ctm, gamma_lut_size);
825 	priv->num_pipes++;
826 	mutex_init(&mtk_crtc->hw_lock);
827 
828 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
829 	mtk_crtc->cmdq_client =
830 			cmdq_mbox_create(dev, drm_crtc_index(&mtk_crtc->base),
831 					 2000);
832 	if (IS_ERR(mtk_crtc->cmdq_client)) {
833 		dev_dbg(dev, "mtk_crtc %d failed to create mailbox client, writing register by CPU now\n",
834 			drm_crtc_index(&mtk_crtc->base));
835 		mtk_crtc->cmdq_client = NULL;
836 	}
837 	ret = of_property_read_u32_index(priv->mutex_node,
838 					 "mediatek,gce-events",
839 					 drm_crtc_index(&mtk_crtc->base),
840 					 &mtk_crtc->cmdq_event);
841 	if (ret)
842 		dev_dbg(dev, "mtk_crtc %d failed to get mediatek,gce-events property\n",
843 			drm_crtc_index(&mtk_crtc->base));
844 #endif
845 	return 0;
846 }
847