• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015 MediaTek Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 
14 #include <asm/barrier.h>
15 #include <drm/drmP.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_crtc_helper.h>
18 #include <drm/drm_plane_helper.h>
19 #include <linux/clk.h>
20 #include <linux/pm_runtime.h>
21 #include <soc/mediatek/smi.h>
22 
23 #include "mtk_drm_drv.h"
24 #include "mtk_drm_crtc.h"
25 #include "mtk_drm_ddp.h"
26 #include "mtk_drm_ddp_comp.h"
27 #include "mtk_drm_gem.h"
28 #include "mtk_drm_plane.h"
29 
30 /**
31  * struct mtk_drm_crtc - MediaTek specific crtc structure.
32  * @base: crtc object.
33  * @enabled: records whether crtc_enable succeeded
34  * @planes: array of 4 drm_plane structures, one for each overlay plane
35  * @pending_planes: whether any plane has pending changes to be applied
36  * @config_regs: memory mapped mmsys configuration register space
37  * @mutex: handle to one of the ten disp_mutex streams
38  * @ddp_comp_nr: number of components in ddp_comp
39  * @ddp_comp: array of pointers the mtk_ddp_comp structures used by this crtc
40  */
41 struct mtk_drm_crtc {
42 	struct drm_crtc			base;
43 	bool				enabled;
44 
45 	bool				pending_needs_vblank;
46 	struct drm_pending_vblank_event	*event;
47 
48 	struct drm_plane		planes[OVL_LAYER_NR];
49 	bool				pending_planes;
50 
51 	void __iomem			*config_regs;
52 	struct mtk_disp_mutex		*mutex;
53 	unsigned int			ddp_comp_nr;
54 	struct mtk_ddp_comp		**ddp_comp;
55 };
56 
57 struct mtk_crtc_state {
58 	struct drm_crtc_state		base;
59 
60 	bool				pending_config;
61 	unsigned int			pending_width;
62 	unsigned int			pending_height;
63 	unsigned int			pending_vrefresh;
64 };
65 
to_mtk_crtc(struct drm_crtc * c)66 static inline struct mtk_drm_crtc *to_mtk_crtc(struct drm_crtc *c)
67 {
68 	return container_of(c, struct mtk_drm_crtc, base);
69 }
70 
to_mtk_crtc_state(struct drm_crtc_state * s)71 static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s)
72 {
73 	return container_of(s, struct mtk_crtc_state, base);
74 }
75 
mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc * mtk_crtc)76 static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
77 {
78 	struct drm_crtc *crtc = &mtk_crtc->base;
79 	unsigned long flags;
80 
81 	spin_lock_irqsave(&crtc->dev->event_lock, flags);
82 	drm_crtc_send_vblank_event(crtc, mtk_crtc->event);
83 	drm_crtc_vblank_put(crtc);
84 	mtk_crtc->event = NULL;
85 	spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
86 }
87 
mtk_drm_finish_page_flip(struct mtk_drm_crtc * mtk_crtc)88 static void mtk_drm_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
89 {
90 	drm_crtc_handle_vblank(&mtk_crtc->base);
91 	if (mtk_crtc->pending_needs_vblank) {
92 		mtk_drm_crtc_finish_page_flip(mtk_crtc);
93 		mtk_crtc->pending_needs_vblank = false;
94 	}
95 }
96 
mtk_drm_crtc_destroy(struct drm_crtc * crtc)97 static void mtk_drm_crtc_destroy(struct drm_crtc *crtc)
98 {
99 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
100 	int i;
101 
102 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
103 		clk_unprepare(mtk_crtc->ddp_comp[i]->clk);
104 
105 	mtk_disp_mutex_put(mtk_crtc->mutex);
106 
107 	drm_crtc_cleanup(crtc);
108 }
109 
mtk_drm_crtc_reset(struct drm_crtc * crtc)110 static void mtk_drm_crtc_reset(struct drm_crtc *crtc)
111 {
112 	struct mtk_crtc_state *state;
113 
114 	if (crtc->state) {
115 		__drm_atomic_helper_crtc_destroy_state(crtc->state);
116 
117 		state = to_mtk_crtc_state(crtc->state);
118 		memset(state, 0, sizeof(*state));
119 	} else {
120 		state = kzalloc(sizeof(*state), GFP_KERNEL);
121 		if (!state)
122 			return;
123 		crtc->state = &state->base;
124 	}
125 
126 	state->base.crtc = crtc;
127 }
128 
mtk_drm_crtc_duplicate_state(struct drm_crtc * crtc)129 static struct drm_crtc_state *mtk_drm_crtc_duplicate_state(struct drm_crtc *crtc)
130 {
131 	struct mtk_crtc_state *state;
132 
133 	state = kzalloc(sizeof(*state), GFP_KERNEL);
134 	if (!state)
135 		return NULL;
136 
137 	__drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
138 
139 	WARN_ON(state->base.crtc != crtc);
140 	state->base.crtc = crtc;
141 
142 	return &state->base;
143 }
144 
mtk_drm_crtc_destroy_state(struct drm_crtc * crtc,struct drm_crtc_state * state)145 static void mtk_drm_crtc_destroy_state(struct drm_crtc *crtc,
146 				       struct drm_crtc_state *state)
147 {
148 	__drm_atomic_helper_crtc_destroy_state(state);
149 	kfree(to_mtk_crtc_state(state));
150 }
151 
mtk_drm_crtc_mode_fixup(struct drm_crtc * crtc,const struct drm_display_mode * mode,struct drm_display_mode * adjusted_mode)152 static bool mtk_drm_crtc_mode_fixup(struct drm_crtc *crtc,
153 				    const struct drm_display_mode *mode,
154 				    struct drm_display_mode *adjusted_mode)
155 {
156 	/* Nothing to do here, but this callback is mandatory. */
157 	return true;
158 }
159 
mtk_drm_crtc_mode_set_nofb(struct drm_crtc * crtc)160 static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
161 {
162 	struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
163 
164 	state->pending_width = crtc->mode.hdisplay;
165 	state->pending_height = crtc->mode.vdisplay;
166 	state->pending_vrefresh = crtc->mode.vrefresh;
167 	wmb();	/* Make sure the above parameters are set before update */
168 	state->pending_config = true;
169 }
170 
mtk_drm_crtc_enable_vblank(struct drm_crtc * crtc)171 static int mtk_drm_crtc_enable_vblank(struct drm_crtc *crtc)
172 {
173 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
174 	struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
175 
176 	mtk_ddp_comp_enable_vblank(ovl, &mtk_crtc->base);
177 
178 	return 0;
179 }
180 
mtk_drm_crtc_disable_vblank(struct drm_crtc * crtc)181 static void mtk_drm_crtc_disable_vblank(struct drm_crtc *crtc)
182 {
183 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
184 	struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
185 
186 	mtk_ddp_comp_disable_vblank(ovl);
187 }
188 
mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc * mtk_crtc)189 static int mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc *mtk_crtc)
190 {
191 	int ret;
192 	int i;
193 
194 	DRM_DEBUG_DRIVER("%s\n", __func__);
195 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
196 		ret = clk_enable(mtk_crtc->ddp_comp[i]->clk);
197 		if (ret) {
198 			DRM_ERROR("Failed to enable clock %d: %d\n", i, ret);
199 			goto err;
200 		}
201 	}
202 
203 	return 0;
204 err:
205 	while (--i >= 0)
206 		clk_disable(mtk_crtc->ddp_comp[i]->clk);
207 	return ret;
208 }
209 
mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc * mtk_crtc)210 static void mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc *mtk_crtc)
211 {
212 	int i;
213 
214 	DRM_DEBUG_DRIVER("%s\n", __func__);
215 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
216 		clk_disable(mtk_crtc->ddp_comp[i]->clk);
217 }
218 
mtk_crtc_ddp_hw_init(struct mtk_drm_crtc * mtk_crtc)219 static int mtk_crtc_ddp_hw_init(struct mtk_drm_crtc *mtk_crtc)
220 {
221 	struct drm_crtc *crtc = &mtk_crtc->base;
222 	struct drm_connector *connector;
223 	struct drm_encoder *encoder;
224 	struct drm_connector_list_iter conn_iter;
225 	unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC;
226 	int ret;
227 	int i;
228 
229 	DRM_DEBUG_DRIVER("%s\n", __func__);
230 	if (WARN_ON(!crtc->state))
231 		return -EINVAL;
232 
233 	width = crtc->state->adjusted_mode.hdisplay;
234 	height = crtc->state->adjusted_mode.vdisplay;
235 	vrefresh = crtc->state->adjusted_mode.vrefresh;
236 
237 	drm_for_each_encoder(encoder, crtc->dev) {
238 		if (encoder->crtc != crtc)
239 			continue;
240 
241 		drm_connector_list_iter_begin(crtc->dev, &conn_iter);
242 		drm_for_each_connector_iter(connector, &conn_iter) {
243 			if (connector->encoder != encoder)
244 				continue;
245 			if (connector->display_info.bpc != 0 &&
246 			    bpc > connector->display_info.bpc)
247 				bpc = connector->display_info.bpc;
248 		}
249 		drm_connector_list_iter_end(&conn_iter);
250 	}
251 
252 	ret = pm_runtime_get_sync(crtc->dev->dev);
253 	if (ret < 0) {
254 		DRM_ERROR("Failed to enable power domain: %d\n", ret);
255 		return ret;
256 	}
257 
258 	ret = mtk_disp_mutex_prepare(mtk_crtc->mutex);
259 	if (ret < 0) {
260 		DRM_ERROR("Failed to enable mutex clock: %d\n", ret);
261 		goto err_pm_runtime_put;
262 	}
263 
264 	ret = mtk_crtc_ddp_clk_enable(mtk_crtc);
265 	if (ret < 0) {
266 		DRM_ERROR("Failed to enable component clocks: %d\n", ret);
267 		goto err_mutex_unprepare;
268 	}
269 
270 	DRM_DEBUG_DRIVER("mediatek_ddp_ddp_path_setup\n");
271 	for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
272 		mtk_ddp_add_comp_to_path(mtk_crtc->config_regs,
273 					 mtk_crtc->ddp_comp[i]->id,
274 					 mtk_crtc->ddp_comp[i + 1]->id);
275 		mtk_disp_mutex_add_comp(mtk_crtc->mutex,
276 					mtk_crtc->ddp_comp[i]->id);
277 	}
278 	mtk_disp_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
279 	mtk_disp_mutex_enable(mtk_crtc->mutex);
280 
281 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
282 		struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i];
283 
284 		mtk_ddp_comp_config(comp, width, height, vrefresh, bpc);
285 		mtk_ddp_comp_start(comp);
286 	}
287 
288 	/* Initially configure all planes */
289 	for (i = 0; i < OVL_LAYER_NR; i++) {
290 		struct drm_plane *plane = &mtk_crtc->planes[i];
291 		struct mtk_plane_state *plane_state;
292 
293 		plane_state = to_mtk_plane_state(plane->state);
294 		mtk_ddp_comp_layer_config(mtk_crtc->ddp_comp[0], i,
295 					  plane_state);
296 	}
297 
298 	return 0;
299 
300 err_mutex_unprepare:
301 	mtk_disp_mutex_unprepare(mtk_crtc->mutex);
302 err_pm_runtime_put:
303 	pm_runtime_put(crtc->dev->dev);
304 	return ret;
305 }
306 
mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc * mtk_crtc)307 static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc)
308 {
309 	struct drm_device *drm = mtk_crtc->base.dev;
310 	struct drm_crtc *crtc = &mtk_crtc->base;
311 	int i;
312 
313 	DRM_DEBUG_DRIVER("%s\n", __func__);
314 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
315 		mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]);
316 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
317 		mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
318 					   mtk_crtc->ddp_comp[i]->id);
319 	mtk_disp_mutex_disable(mtk_crtc->mutex);
320 	for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
321 		mtk_ddp_remove_comp_from_path(mtk_crtc->config_regs,
322 					      mtk_crtc->ddp_comp[i]->id,
323 					      mtk_crtc->ddp_comp[i + 1]->id);
324 		mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
325 					   mtk_crtc->ddp_comp[i]->id);
326 	}
327 	mtk_disp_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
328 	mtk_crtc_ddp_clk_disable(mtk_crtc);
329 	mtk_disp_mutex_unprepare(mtk_crtc->mutex);
330 
331 	pm_runtime_put(drm->dev);
332 
333 	if (crtc->state->event && !crtc->state->active) {
334 		spin_lock_irq(&crtc->dev->event_lock);
335 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
336 		crtc->state->event = NULL;
337 		spin_unlock_irq(&crtc->dev->event_lock);
338 	}
339 }
340 
mtk_crtc_ddp_config(struct drm_crtc * crtc)341 static void mtk_crtc_ddp_config(struct drm_crtc *crtc)
342 {
343 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
344 	struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state);
345 	struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
346 	unsigned int i;
347 
348 	/*
349 	 * TODO: instead of updating the registers here, we should prepare
350 	 * working registers in atomic_commit and let the hardware command
351 	 * queue update module registers on vblank.
352 	 */
353 	if (state->pending_config) {
354 		mtk_ddp_comp_config(ovl, state->pending_width,
355 				    state->pending_height,
356 				    state->pending_vrefresh, 0);
357 
358 		state->pending_config = false;
359 	}
360 
361 	if (mtk_crtc->pending_planes) {
362 		for (i = 0; i < OVL_LAYER_NR; i++) {
363 			struct drm_plane *plane = &mtk_crtc->planes[i];
364 			struct mtk_plane_state *plane_state;
365 
366 			plane_state = to_mtk_plane_state(plane->state);
367 
368 			if (plane_state->pending.config) {
369 				mtk_ddp_comp_layer_config(ovl, i, plane_state);
370 				plane_state->pending.config = false;
371 			}
372 		}
373 		mtk_crtc->pending_planes = false;
374 	}
375 }
376 
mtk_drm_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)377 static void mtk_drm_crtc_atomic_enable(struct drm_crtc *crtc,
378 				       struct drm_crtc_state *old_state)
379 {
380 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
381 	struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
382 	int ret;
383 
384 	DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
385 
386 	ret = mtk_smi_larb_get(ovl->larb_dev);
387 	if (ret) {
388 		DRM_ERROR("Failed to get larb: %d\n", ret);
389 		return;
390 	}
391 
392 	ret = mtk_crtc_ddp_hw_init(mtk_crtc);
393 	if (ret) {
394 		mtk_smi_larb_put(ovl->larb_dev);
395 		return;
396 	}
397 
398 	drm_crtc_vblank_on(crtc);
399 	mtk_crtc->enabled = true;
400 }
401 
mtk_drm_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_crtc_state * old_state)402 static void mtk_drm_crtc_atomic_disable(struct drm_crtc *crtc,
403 					struct drm_crtc_state *old_state)
404 {
405 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
406 	struct mtk_ddp_comp *ovl = mtk_crtc->ddp_comp[0];
407 	int i;
408 
409 	DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
410 	if (!mtk_crtc->enabled)
411 		return;
412 
413 	/* Set all pending plane state to disabled */
414 	for (i = 0; i < OVL_LAYER_NR; i++) {
415 		struct drm_plane *plane = &mtk_crtc->planes[i];
416 		struct mtk_plane_state *plane_state;
417 
418 		plane_state = to_mtk_plane_state(plane->state);
419 		plane_state->pending.enable = false;
420 		plane_state->pending.config = true;
421 	}
422 	mtk_crtc->pending_planes = true;
423 
424 	/* Wait for planes to be disabled */
425 	drm_crtc_wait_one_vblank(crtc);
426 
427 	drm_crtc_vblank_off(crtc);
428 	mtk_crtc_ddp_hw_fini(mtk_crtc);
429 	mtk_smi_larb_put(ovl->larb_dev);
430 
431 	mtk_crtc->enabled = false;
432 }
433 
mtk_drm_crtc_atomic_begin(struct drm_crtc * crtc,struct drm_crtc_state * old_crtc_state)434 static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc,
435 				      struct drm_crtc_state *old_crtc_state)
436 {
437 	struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
438 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
439 
440 	if (mtk_crtc->event && state->base.event)
441 		DRM_ERROR("new event while there is still a pending event\n");
442 
443 	if (state->base.event) {
444 		state->base.event->pipe = drm_crtc_index(crtc);
445 		WARN_ON(drm_crtc_vblank_get(crtc) != 0);
446 		mtk_crtc->event = state->base.event;
447 		state->base.event = NULL;
448 	}
449 }
450 
mtk_drm_crtc_atomic_flush(struct drm_crtc * crtc,struct drm_crtc_state * old_crtc_state)451 static void mtk_drm_crtc_atomic_flush(struct drm_crtc *crtc,
452 				      struct drm_crtc_state *old_crtc_state)
453 {
454 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
455 	struct mtk_drm_private *priv = crtc->dev->dev_private;
456 	unsigned int pending_planes = 0;
457 	int i;
458 
459 	if (mtk_crtc->event)
460 		mtk_crtc->pending_needs_vblank = true;
461 	for (i = 0; i < OVL_LAYER_NR; i++) {
462 		struct drm_plane *plane = &mtk_crtc->planes[i];
463 		struct mtk_plane_state *plane_state;
464 
465 		plane_state = to_mtk_plane_state(plane->state);
466 		if (plane_state->pending.dirty) {
467 			plane_state->pending.config = true;
468 			plane_state->pending.dirty = false;
469 			pending_planes |= BIT(i);
470 		}
471 	}
472 	if (pending_planes)
473 		mtk_crtc->pending_planes = true;
474 	if (crtc->state->color_mgmt_changed)
475 		for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
476 			mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state);
477 
478 	if (priv->data->shadow_register) {
479 		mtk_disp_mutex_acquire(mtk_crtc->mutex);
480 		mtk_crtc_ddp_config(crtc);
481 		mtk_disp_mutex_release(mtk_crtc->mutex);
482 	}
483 }
484 
485 static const struct drm_crtc_funcs mtk_crtc_funcs = {
486 	.set_config		= drm_atomic_helper_set_config,
487 	.page_flip		= drm_atomic_helper_page_flip,
488 	.destroy		= mtk_drm_crtc_destroy,
489 	.reset			= mtk_drm_crtc_reset,
490 	.atomic_duplicate_state	= mtk_drm_crtc_duplicate_state,
491 	.atomic_destroy_state	= mtk_drm_crtc_destroy_state,
492 	.gamma_set		= drm_atomic_helper_legacy_gamma_set,
493 	.enable_vblank		= mtk_drm_crtc_enable_vblank,
494 	.disable_vblank		= mtk_drm_crtc_disable_vblank,
495 };
496 
497 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = {
498 	.mode_fixup	= mtk_drm_crtc_mode_fixup,
499 	.mode_set_nofb	= mtk_drm_crtc_mode_set_nofb,
500 	.atomic_begin	= mtk_drm_crtc_atomic_begin,
501 	.atomic_flush	= mtk_drm_crtc_atomic_flush,
502 	.atomic_enable	= mtk_drm_crtc_atomic_enable,
503 	.atomic_disable	= mtk_drm_crtc_atomic_disable,
504 };
505 
mtk_drm_crtc_init(struct drm_device * drm,struct mtk_drm_crtc * mtk_crtc,struct drm_plane * primary,struct drm_plane * cursor,unsigned int pipe)506 static int mtk_drm_crtc_init(struct drm_device *drm,
507 			     struct mtk_drm_crtc *mtk_crtc,
508 			     struct drm_plane *primary,
509 			     struct drm_plane *cursor, unsigned int pipe)
510 {
511 	int ret;
512 
513 	ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor,
514 					&mtk_crtc_funcs, NULL);
515 	if (ret)
516 		goto err_cleanup_crtc;
517 
518 	drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs);
519 
520 	return 0;
521 
522 err_cleanup_crtc:
523 	drm_crtc_cleanup(&mtk_crtc->base);
524 	return ret;
525 }
526 
mtk_crtc_ddp_irq(struct drm_crtc * crtc,struct mtk_ddp_comp * ovl)527 void mtk_crtc_ddp_irq(struct drm_crtc *crtc, struct mtk_ddp_comp *ovl)
528 {
529 	struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
530 	struct mtk_drm_private *priv = crtc->dev->dev_private;
531 
532 	if (!priv->data->shadow_register)
533 		mtk_crtc_ddp_config(crtc);
534 
535 	mtk_drm_finish_page_flip(mtk_crtc);
536 }
537 
mtk_drm_crtc_create(struct drm_device * drm_dev,const enum mtk_ddp_comp_id * path,unsigned int path_len)538 int mtk_drm_crtc_create(struct drm_device *drm_dev,
539 			const enum mtk_ddp_comp_id *path, unsigned int path_len)
540 {
541 	struct mtk_drm_private *priv = drm_dev->dev_private;
542 	struct device *dev = drm_dev->dev;
543 	struct mtk_drm_crtc *mtk_crtc;
544 	enum drm_plane_type type;
545 	unsigned int zpos;
546 	int pipe = priv->num_pipes;
547 	int ret;
548 	int i;
549 
550 	for (i = 0; i < path_len; i++) {
551 		enum mtk_ddp_comp_id comp_id = path[i];
552 		struct device_node *node;
553 
554 		node = priv->comp_node[comp_id];
555 		if (!node) {
556 			dev_info(dev,
557 				 "Not creating crtc %d because component %d is disabled or missing\n",
558 				 pipe, comp_id);
559 			return 0;
560 		}
561 	}
562 
563 	mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL);
564 	if (!mtk_crtc)
565 		return -ENOMEM;
566 
567 	mtk_crtc->config_regs = priv->config_regs;
568 	mtk_crtc->ddp_comp_nr = path_len;
569 	mtk_crtc->ddp_comp = devm_kmalloc_array(dev, mtk_crtc->ddp_comp_nr,
570 						sizeof(*mtk_crtc->ddp_comp),
571 						GFP_KERNEL);
572 	if (!mtk_crtc->ddp_comp)
573 		return -ENOMEM;
574 
575 	mtk_crtc->mutex = mtk_disp_mutex_get(priv->mutex_dev, pipe);
576 	if (IS_ERR(mtk_crtc->mutex)) {
577 		ret = PTR_ERR(mtk_crtc->mutex);
578 		dev_err(dev, "Failed to get mutex: %d\n", ret);
579 		return ret;
580 	}
581 
582 	for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
583 		enum mtk_ddp_comp_id comp_id = path[i];
584 		struct mtk_ddp_comp *comp;
585 		struct device_node *node;
586 
587 		node = priv->comp_node[comp_id];
588 		comp = priv->ddp_comp[comp_id];
589 		if (!comp) {
590 			dev_err(dev, "Component %pOF not initialized\n", node);
591 			ret = -ENODEV;
592 			goto unprepare;
593 		}
594 
595 		ret = clk_prepare(comp->clk);
596 		if (ret) {
597 			dev_err(dev,
598 				"Failed to prepare clock for component %pOF: %d\n",
599 				node, ret);
600 			goto unprepare;
601 		}
602 
603 		mtk_crtc->ddp_comp[i] = comp;
604 	}
605 
606 	for (zpos = 0; zpos < OVL_LAYER_NR; zpos++) {
607 		type = (zpos == 0) ? DRM_PLANE_TYPE_PRIMARY :
608 				(zpos == 1) ? DRM_PLANE_TYPE_CURSOR :
609 						DRM_PLANE_TYPE_OVERLAY;
610 		ret = mtk_plane_init(drm_dev, &mtk_crtc->planes[zpos],
611 				     BIT(pipe), type);
612 		if (ret)
613 			goto unprepare;
614 	}
615 
616 	ret = mtk_drm_crtc_init(drm_dev, mtk_crtc, &mtk_crtc->planes[0],
617 				&mtk_crtc->planes[1], pipe);
618 	if (ret < 0)
619 		goto unprepare;
620 	drm_mode_crtc_set_gamma_size(&mtk_crtc->base, MTK_LUT_SIZE);
621 	drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, false, MTK_LUT_SIZE);
622 	priv->num_pipes++;
623 
624 	return 0;
625 
626 unprepare:
627 	while (--i >= 0)
628 		clk_unprepare(mtk_crtc->ddp_comp[i]->clk);
629 
630 	return ret;
631 }
632