1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
7 #include <linux/pm_runtime.h>
8 #include <linux/soc/mediatek/mtk-cmdq.h>
10 #include <asm/barrier.h>
11 #include <soc/mediatek/smi.h>
13 #include <drm/drm_atomic_helper.h>
14 #include <drm/drm_plane_helper.h>
15 #include <drm/drm_probe_helper.h>
16 #include <drm/drm_vblank.h>
18 #include "mtk_drm_drv.h"
19 #include "mtk_drm_crtc.h"
20 #include "mtk_drm_ddp.h"
21 #include "mtk_drm_ddp_comp.h"
22 #include "mtk_drm_gem.h"
23 #include "mtk_drm_plane.h"
26 * struct mtk_drm_crtc - MediaTek specific crtc structure.
28 * @enabled: records whether crtc_enable succeeded
29 * @planes: array of 4 drm_plane structures, one for each overlay plane
30 * @pending_planes: whether any plane has pending changes to be applied
31 * @config_regs: memory mapped mmsys configuration register space
32 * @mutex: handle to one of the ten disp_mutex streams
33 * @ddp_comp_nr: number of components in ddp_comp
34 * @ddp_comp: array of pointers the mtk_ddp_comp structures used by this crtc
40 bool pending_needs_vblank;
41 struct drm_pending_vblank_event *event;
43 struct drm_plane *planes;
44 unsigned int layer_nr;
46 bool pending_async_planes;
48 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
49 struct cmdq_client *cmdq_client;
53 void __iomem *config_regs;
54 struct mtk_disp_mutex *mutex;
55 unsigned int ddp_comp_nr;
56 struct mtk_ddp_comp **ddp_comp;
58 /* lock for display hardware access */
62 struct mtk_crtc_state {
63 struct drm_crtc_state base;
66 unsigned int pending_width;
67 unsigned int pending_height;
68 unsigned int pending_vrefresh;
71 static inline struct mtk_drm_crtc *to_mtk_crtc(struct drm_crtc *c)
73 return container_of(c, struct mtk_drm_crtc, base);
76 static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s)
78 return container_of(s, struct mtk_crtc_state, base);
81 static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
83 struct drm_crtc *crtc = &mtk_crtc->base;
86 spin_lock_irqsave(&crtc->dev->event_lock, flags);
87 drm_crtc_send_vblank_event(crtc, mtk_crtc->event);
88 drm_crtc_vblank_put(crtc);
89 mtk_crtc->event = NULL;
90 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
93 static void mtk_drm_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
95 drm_crtc_handle_vblank(&mtk_crtc->base);
96 if (mtk_crtc->pending_needs_vblank) {
97 mtk_drm_crtc_finish_page_flip(mtk_crtc);
98 mtk_crtc->pending_needs_vblank = false;
102 static void mtk_drm_crtc_destroy(struct drm_crtc *crtc)
104 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
106 mtk_disp_mutex_put(mtk_crtc->mutex);
108 drm_crtc_cleanup(crtc);
111 static void mtk_drm_crtc_reset(struct drm_crtc *crtc)
113 struct mtk_crtc_state *state;
116 __drm_atomic_helper_crtc_destroy_state(crtc->state);
118 state = to_mtk_crtc_state(crtc->state);
119 memset(state, 0, sizeof(*state));
121 state = kzalloc(sizeof(*state), GFP_KERNEL);
124 crtc->state = &state->base;
127 state->base.crtc = crtc;
130 static struct drm_crtc_state *mtk_drm_crtc_duplicate_state(struct drm_crtc *crtc)
132 struct mtk_crtc_state *state;
134 state = kzalloc(sizeof(*state), GFP_KERNEL);
138 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
140 WARN_ON(state->base.crtc != crtc);
141 state->base.crtc = crtc;
146 static void mtk_drm_crtc_destroy_state(struct drm_crtc *crtc,
147 struct drm_crtc_state *state)
149 __drm_atomic_helper_crtc_destroy_state(state);
150 kfree(to_mtk_crtc_state(state));
153 static bool mtk_drm_crtc_mode_fixup(struct drm_crtc *crtc,
154 const struct drm_display_mode *mode,
155 struct drm_display_mode *adjusted_mode)
157 /* Nothing to do here, but this callback is mandatory. */
161 static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
163 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
165 state->pending_width = crtc->mode.hdisplay;
166 state->pending_height = crtc->mode.vdisplay;
167 state->pending_vrefresh = crtc->mode.vrefresh;
168 wmb(); /* Make sure the above parameters are set before update */
169 state->pending_config = true;
172 static int mtk_drm_crtc_enable_vblank(struct drm_crtc *crtc)
174 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
175 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
177 mtk_ddp_comp_enable_vblank(comp, &mtk_crtc->base);
182 static void mtk_drm_crtc_disable_vblank(struct drm_crtc *crtc)
184 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
185 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
187 mtk_ddp_comp_disable_vblank(comp);
190 static int mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc *mtk_crtc)
195 DRM_DEBUG_DRIVER("%s\n", __func__);
196 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
197 ret = clk_prepare_enable(mtk_crtc->ddp_comp[i]->clk);
199 DRM_ERROR("Failed to enable clock %d: %d\n", i, ret);
207 clk_disable_unprepare(mtk_crtc->ddp_comp[i]->clk);
211 static void mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc *mtk_crtc)
215 DRM_DEBUG_DRIVER("%s\n", __func__);
216 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
217 clk_disable_unprepare(mtk_crtc->ddp_comp[i]->clk);
221 struct mtk_ddp_comp *mtk_drm_ddp_comp_for_plane(struct drm_crtc *crtc,
222 struct drm_plane *plane,
223 unsigned int *local_layer)
225 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
226 struct mtk_ddp_comp *comp;
228 unsigned int local_index = plane - mtk_crtc->planes;
230 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
231 comp = mtk_crtc->ddp_comp[i];
232 if (local_index < (count + mtk_ddp_comp_layer_nr(comp))) {
233 *local_layer = local_index - count;
236 count += mtk_ddp_comp_layer_nr(comp);
239 WARN(1, "Failed to find component for plane %d\n", plane->index);
243 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
244 static void ddp_cmdq_cb(struct cmdq_cb_data data)
246 cmdq_pkt_destroy(data.data);
250 static int mtk_crtc_ddp_hw_init(struct mtk_drm_crtc *mtk_crtc)
252 struct drm_crtc *crtc = &mtk_crtc->base;
253 struct drm_connector *connector;
254 struct drm_encoder *encoder;
255 struct drm_connector_list_iter conn_iter;
256 unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC;
260 DRM_DEBUG_DRIVER("%s\n", __func__);
261 if (WARN_ON(!crtc->state))
264 width = crtc->state->adjusted_mode.hdisplay;
265 height = crtc->state->adjusted_mode.vdisplay;
266 vrefresh = crtc->state->adjusted_mode.vrefresh;
268 drm_for_each_encoder(encoder, crtc->dev) {
269 if (encoder->crtc != crtc)
272 drm_connector_list_iter_begin(crtc->dev, &conn_iter);
273 drm_for_each_connector_iter(connector, &conn_iter) {
274 if (connector->encoder != encoder)
276 if (connector->display_info.bpc != 0 &&
277 bpc > connector->display_info.bpc)
278 bpc = connector->display_info.bpc;
280 drm_connector_list_iter_end(&conn_iter);
283 ret = pm_runtime_get_sync(crtc->dev->dev);
285 DRM_ERROR("Failed to enable power domain: %d\n", ret);
289 ret = mtk_disp_mutex_prepare(mtk_crtc->mutex);
291 DRM_ERROR("Failed to enable mutex clock: %d\n", ret);
292 goto err_pm_runtime_put;
295 ret = mtk_crtc_ddp_clk_enable(mtk_crtc);
297 DRM_ERROR("Failed to enable component clocks: %d\n", ret);
298 goto err_mutex_unprepare;
301 DRM_DEBUG_DRIVER("mediatek_ddp_ddp_path_setup\n");
302 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
303 mtk_ddp_add_comp_to_path(mtk_crtc->config_regs,
304 mtk_crtc->ddp_comp[i]->id,
305 mtk_crtc->ddp_comp[i + 1]->id);
306 mtk_disp_mutex_add_comp(mtk_crtc->mutex,
307 mtk_crtc->ddp_comp[i]->id);
309 mtk_disp_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
310 mtk_disp_mutex_enable(mtk_crtc->mutex);
312 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
313 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i];
316 mtk_ddp_comp_bgclr_in_on(comp);
318 mtk_ddp_comp_config(comp, width, height, vrefresh, bpc, NULL);
319 mtk_ddp_comp_start(comp);
322 /* Initially configure all planes */
323 for (i = 0; i < mtk_crtc->layer_nr; i++) {
324 struct drm_plane *plane = &mtk_crtc->planes[i];
325 struct mtk_plane_state *plane_state;
326 struct mtk_ddp_comp *comp;
327 unsigned int local_layer;
329 plane_state = to_mtk_plane_state(plane->state);
330 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer);
332 mtk_ddp_comp_layer_config(comp, local_layer,
339 mtk_disp_mutex_unprepare(mtk_crtc->mutex);
341 pm_runtime_put(crtc->dev->dev);
345 static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc)
347 struct drm_device *drm = mtk_crtc->base.dev;
348 struct drm_crtc *crtc = &mtk_crtc->base;
351 DRM_DEBUG_DRIVER("%s\n", __func__);
352 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
353 mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]);
355 mtk_ddp_comp_bgclr_in_off(mtk_crtc->ddp_comp[i]);
358 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
359 mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
360 mtk_crtc->ddp_comp[i]->id);
361 mtk_disp_mutex_disable(mtk_crtc->mutex);
362 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
363 mtk_ddp_remove_comp_from_path(mtk_crtc->config_regs,
364 mtk_crtc->ddp_comp[i]->id,
365 mtk_crtc->ddp_comp[i + 1]->id);
366 mtk_disp_mutex_remove_comp(mtk_crtc->mutex,
367 mtk_crtc->ddp_comp[i]->id);
369 mtk_disp_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
370 mtk_crtc_ddp_clk_disable(mtk_crtc);
371 mtk_disp_mutex_unprepare(mtk_crtc->mutex);
373 pm_runtime_put(drm->dev);
375 if (crtc->state->event && !crtc->state->active) {
376 spin_lock_irq(&crtc->dev->event_lock);
377 drm_crtc_send_vblank_event(crtc, crtc->state->event);
378 crtc->state->event = NULL;
379 spin_unlock_irq(&crtc->dev->event_lock);
383 static void mtk_crtc_ddp_config(struct drm_crtc *crtc,
384 struct cmdq_pkt *cmdq_handle)
386 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
387 struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state);
388 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
390 unsigned int local_layer;
393 * TODO: instead of updating the registers here, we should prepare
394 * working registers in atomic_commit and let the hardware command
395 * queue update module registers on vblank.
397 if (state->pending_config) {
398 mtk_ddp_comp_config(comp, state->pending_width,
399 state->pending_height,
400 state->pending_vrefresh, 0,
403 state->pending_config = false;
406 if (mtk_crtc->pending_planes) {
407 for (i = 0; i < mtk_crtc->layer_nr; i++) {
408 struct drm_plane *plane = &mtk_crtc->planes[i];
409 struct mtk_plane_state *plane_state;
411 plane_state = to_mtk_plane_state(plane->state);
413 if (!plane_state->pending.config)
416 comp = mtk_drm_ddp_comp_for_plane(crtc, plane,
420 mtk_ddp_comp_layer_config(comp, local_layer,
423 plane_state->pending.config = false;
425 mtk_crtc->pending_planes = false;
428 if (mtk_crtc->pending_async_planes) {
429 for (i = 0; i < mtk_crtc->layer_nr; i++) {
430 struct drm_plane *plane = &mtk_crtc->planes[i];
431 struct mtk_plane_state *plane_state;
433 plane_state = to_mtk_plane_state(plane->state);
435 if (!plane_state->pending.async_config)
438 comp = mtk_drm_ddp_comp_for_plane(crtc, plane,
442 mtk_ddp_comp_layer_config(comp, local_layer,
445 plane_state->pending.async_config = false;
447 mtk_crtc->pending_async_planes = false;
451 static void mtk_drm_crtc_hw_config(struct mtk_drm_crtc *mtk_crtc)
453 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
454 struct cmdq_pkt *cmdq_handle;
456 struct drm_crtc *crtc = &mtk_crtc->base;
457 struct mtk_drm_private *priv = crtc->dev->dev_private;
458 unsigned int pending_planes = 0, pending_async_planes = 0;
461 mutex_lock(&mtk_crtc->hw_lock);
462 for (i = 0; i < mtk_crtc->layer_nr; i++) {
463 struct drm_plane *plane = &mtk_crtc->planes[i];
464 struct mtk_plane_state *plane_state;
466 plane_state = to_mtk_plane_state(plane->state);
467 if (plane_state->pending.dirty) {
468 plane_state->pending.config = true;
469 plane_state->pending.dirty = false;
470 pending_planes |= BIT(i);
471 } else if (plane_state->pending.async_dirty) {
472 plane_state->pending.async_config = true;
473 plane_state->pending.async_dirty = false;
474 pending_async_planes |= BIT(i);
478 mtk_crtc->pending_planes = true;
479 if (pending_async_planes)
480 mtk_crtc->pending_async_planes = true;
482 if (priv->data->shadow_register) {
483 mtk_disp_mutex_acquire(mtk_crtc->mutex);
484 mtk_crtc_ddp_config(crtc, NULL);
485 mtk_disp_mutex_release(mtk_crtc->mutex);
487 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
488 if (mtk_crtc->cmdq_client) {
489 mbox_flush(mtk_crtc->cmdq_client->chan, 2000);
490 cmdq_handle = cmdq_pkt_create(mtk_crtc->cmdq_client, PAGE_SIZE);
491 cmdq_pkt_clear_event(cmdq_handle, mtk_crtc->cmdq_event);
492 cmdq_pkt_wfe(cmdq_handle, mtk_crtc->cmdq_event);
493 mtk_crtc_ddp_config(crtc, cmdq_handle);
494 cmdq_pkt_flush_async(cmdq_handle, ddp_cmdq_cb, cmdq_handle);
497 mutex_unlock(&mtk_crtc->hw_lock);
500 int mtk_drm_crtc_plane_check(struct drm_crtc *crtc, struct drm_plane *plane,
501 struct mtk_plane_state *state)
503 unsigned int local_layer;
504 struct mtk_ddp_comp *comp;
506 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer);
508 return mtk_ddp_comp_layer_check(comp, local_layer, state);
512 void mtk_drm_crtc_async_update(struct drm_crtc *crtc, struct drm_plane *plane,
513 struct drm_plane_state *new_state)
515 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
516 const struct drm_plane_helper_funcs *plane_helper_funcs =
517 plane->helper_private;
519 if (!mtk_crtc->enabled)
522 plane_helper_funcs->atomic_update(plane, new_state);
523 mtk_drm_crtc_hw_config(mtk_crtc);
526 static void mtk_drm_crtc_atomic_enable(struct drm_crtc *crtc,
527 struct drm_crtc_state *old_state)
529 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
530 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
533 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
535 ret = mtk_smi_larb_get(comp->larb_dev);
537 DRM_ERROR("Failed to get larb: %d\n", ret);
541 ret = mtk_crtc_ddp_hw_init(mtk_crtc);
543 mtk_smi_larb_put(comp->larb_dev);
547 drm_crtc_vblank_on(crtc);
548 mtk_crtc->enabled = true;
551 static void mtk_drm_crtc_atomic_disable(struct drm_crtc *crtc,
552 struct drm_crtc_state *old_state)
554 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
555 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
558 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
559 if (!mtk_crtc->enabled)
562 /* Set all pending plane state to disabled */
563 for (i = 0; i < mtk_crtc->layer_nr; i++) {
564 struct drm_plane *plane = &mtk_crtc->planes[i];
565 struct mtk_plane_state *plane_state;
567 plane_state = to_mtk_plane_state(plane->state);
568 plane_state->pending.enable = false;
569 plane_state->pending.config = true;
571 mtk_crtc->pending_planes = true;
573 mtk_drm_crtc_hw_config(mtk_crtc);
574 /* Wait for planes to be disabled */
575 drm_crtc_wait_one_vblank(crtc);
577 drm_crtc_vblank_off(crtc);
578 mtk_crtc_ddp_hw_fini(mtk_crtc);
579 mtk_smi_larb_put(comp->larb_dev);
581 mtk_crtc->enabled = false;
584 static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc,
585 struct drm_crtc_state *old_crtc_state)
587 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
588 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
590 if (mtk_crtc->event && state->base.event)
591 DRM_ERROR("new event while there is still a pending event\n");
593 if (state->base.event) {
594 state->base.event->pipe = drm_crtc_index(crtc);
595 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
596 mtk_crtc->event = state->base.event;
597 state->base.event = NULL;
601 static void mtk_drm_crtc_atomic_flush(struct drm_crtc *crtc,
602 struct drm_crtc_state *old_crtc_state)
604 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
608 mtk_crtc->pending_needs_vblank = true;
609 if (crtc->state->color_mgmt_changed)
610 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
611 mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state);
612 mtk_ddp_ctm_set(mtk_crtc->ddp_comp[i], crtc->state);
614 mtk_drm_crtc_hw_config(mtk_crtc);
617 static const struct drm_crtc_funcs mtk_crtc_funcs = {
618 .set_config = drm_atomic_helper_set_config,
619 .page_flip = drm_atomic_helper_page_flip,
620 .destroy = mtk_drm_crtc_destroy,
621 .reset = mtk_drm_crtc_reset,
622 .atomic_duplicate_state = mtk_drm_crtc_duplicate_state,
623 .atomic_destroy_state = mtk_drm_crtc_destroy_state,
624 .gamma_set = drm_atomic_helper_legacy_gamma_set,
625 .enable_vblank = mtk_drm_crtc_enable_vblank,
626 .disable_vblank = mtk_drm_crtc_disable_vblank,
629 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = {
630 .mode_fixup = mtk_drm_crtc_mode_fixup,
631 .mode_set_nofb = mtk_drm_crtc_mode_set_nofb,
632 .atomic_begin = mtk_drm_crtc_atomic_begin,
633 .atomic_flush = mtk_drm_crtc_atomic_flush,
634 .atomic_enable = mtk_drm_crtc_atomic_enable,
635 .atomic_disable = mtk_drm_crtc_atomic_disable,
638 static int mtk_drm_crtc_init(struct drm_device *drm,
639 struct mtk_drm_crtc *mtk_crtc,
642 struct drm_plane *primary = NULL;
643 struct drm_plane *cursor = NULL;
646 for (i = 0; i < mtk_crtc->layer_nr; i++) {
647 if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_PRIMARY)
648 primary = &mtk_crtc->planes[i];
649 else if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_CURSOR)
650 cursor = &mtk_crtc->planes[i];
653 ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor,
654 &mtk_crtc_funcs, NULL);
656 goto err_cleanup_crtc;
658 drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs);
663 drm_crtc_cleanup(&mtk_crtc->base);
667 void mtk_crtc_ddp_irq(struct drm_crtc *crtc, struct mtk_ddp_comp *comp)
669 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
670 struct mtk_drm_private *priv = crtc->dev->dev_private;
672 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
673 if (!priv->data->shadow_register && !mtk_crtc->cmdq_client)
675 if (!priv->data->shadow_register)
677 mtk_crtc_ddp_config(crtc, NULL);
679 mtk_drm_finish_page_flip(mtk_crtc);
682 static int mtk_drm_crtc_num_comp_planes(struct mtk_drm_crtc *mtk_crtc,
685 struct mtk_ddp_comp *comp;
690 comp = mtk_crtc->ddp_comp[comp_idx];
694 if (comp_idx == 1 && !comp->funcs->bgclr_in_on)
697 return mtk_ddp_comp_layer_nr(comp);
701 enum drm_plane_type mtk_drm_crtc_plane_type(unsigned int plane_idx,
702 unsigned int num_planes)
705 return DRM_PLANE_TYPE_PRIMARY;
706 else if (plane_idx == (num_planes - 1))
707 return DRM_PLANE_TYPE_CURSOR;
709 return DRM_PLANE_TYPE_OVERLAY;
713 static int mtk_drm_crtc_init_comp_planes(struct drm_device *drm_dev,
714 struct mtk_drm_crtc *mtk_crtc,
715 int comp_idx, int pipe)
717 int num_planes = mtk_drm_crtc_num_comp_planes(mtk_crtc, comp_idx);
718 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[comp_idx];
721 for (i = 0; i < num_planes; i++) {
722 ret = mtk_plane_init(drm_dev,
723 &mtk_crtc->planes[mtk_crtc->layer_nr],
725 mtk_drm_crtc_plane_type(mtk_crtc->layer_nr,
727 mtk_ddp_comp_supported_rotations(comp));
731 mtk_crtc->layer_nr++;
736 int mtk_drm_crtc_create(struct drm_device *drm_dev,
737 const enum mtk_ddp_comp_id *path, unsigned int path_len)
739 struct mtk_drm_private *priv = drm_dev->dev_private;
740 struct device *dev = drm_dev->dev;
741 struct mtk_drm_crtc *mtk_crtc;
742 unsigned int num_comp_planes = 0;
743 int pipe = priv->num_pipes;
746 bool has_ctm = false;
747 uint gamma_lut_size = 0;
752 for (i = 0; i < path_len; i++) {
753 enum mtk_ddp_comp_id comp_id = path[i];
754 struct device_node *node;
756 node = priv->comp_node[comp_id];
759 "Not creating crtc %d because component %d is disabled or missing\n",
765 mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL);
769 mtk_crtc->config_regs = priv->config_regs;
770 mtk_crtc->ddp_comp_nr = path_len;
771 mtk_crtc->ddp_comp = devm_kmalloc_array(dev, mtk_crtc->ddp_comp_nr,
772 sizeof(*mtk_crtc->ddp_comp),
774 if (!mtk_crtc->ddp_comp)
777 mtk_crtc->mutex = mtk_disp_mutex_get(priv->mutex_dev, pipe);
778 if (IS_ERR(mtk_crtc->mutex)) {
779 ret = PTR_ERR(mtk_crtc->mutex);
780 dev_err(dev, "Failed to get mutex: %d\n", ret);
784 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
785 enum mtk_ddp_comp_id comp_id = path[i];
786 struct mtk_ddp_comp *comp;
787 struct device_node *node;
789 node = priv->comp_node[comp_id];
790 comp = priv->ddp_comp[comp_id];
792 dev_err(dev, "Component %pOF not initialized\n", node);
797 mtk_crtc->ddp_comp[i] = comp;
800 if (comp->funcs->gamma_set)
801 gamma_lut_size = MTK_LUT_SIZE;
803 if (comp->funcs->ctm_set)
808 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
809 num_comp_planes += mtk_drm_crtc_num_comp_planes(mtk_crtc, i);
811 mtk_crtc->planes = devm_kcalloc(dev, num_comp_planes,
812 sizeof(struct drm_plane), GFP_KERNEL);
814 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
815 ret = mtk_drm_crtc_init_comp_planes(drm_dev, mtk_crtc, i,
821 ret = mtk_drm_crtc_init(drm_dev, mtk_crtc, pipe);
826 drm_mode_crtc_set_gamma_size(&mtk_crtc->base, gamma_lut_size);
827 drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, has_ctm, gamma_lut_size);
829 mutex_init(&mtk_crtc->hw_lock);
831 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
832 mtk_crtc->cmdq_client =
833 cmdq_mbox_create(dev, drm_crtc_index(&mtk_crtc->base),
835 if (IS_ERR(mtk_crtc->cmdq_client)) {
836 dev_dbg(dev, "mtk_crtc %d failed to create mailbox client, writing register by CPU now\n",
837 drm_crtc_index(&mtk_crtc->base));
838 mtk_crtc->cmdq_client = NULL;
840 ret = of_property_read_u32_index(priv->mutex_node,
841 "mediatek,gce-events",
842 drm_crtc_index(&mtk_crtc->base),
843 &mtk_crtc->cmdq_event);
845 dev_dbg(dev, "mtk_crtc %d failed to get mediatek,gce-events property\n",
846 drm_crtc_index(&mtk_crtc->base));