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>
9 #include <linux/soc/mediatek/mtk-mmsys.h>
11 #include <asm/barrier.h>
12 #include <soc/mediatek/smi.h>
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>
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"
27 * struct mtk_drm_crtc - MediaTek specific crtc structure.
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
41 bool pending_needs_vblank
;
42 struct drm_pending_vblank_event
*event
;
44 struct drm_plane
*planes
;
45 unsigned int layer_nr
;
47 bool pending_async_planes
;
49 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
50 struct cmdq_client
*cmdq_client
;
54 struct device
*mmsys_dev
;
55 struct mtk_disp_mutex
*mutex
;
56 unsigned int ddp_comp_nr
;
57 struct mtk_ddp_comp
**ddp_comp
;
59 /* lock for display hardware access */
63 struct mtk_crtc_state
{
64 struct drm_crtc_state base
;
67 unsigned int pending_width
;
68 unsigned int pending_height
;
69 unsigned int pending_vrefresh
;
72 static inline struct mtk_drm_crtc
*to_mtk_crtc(struct drm_crtc
*c
)
74 return container_of(c
, struct mtk_drm_crtc
, base
);
77 static inline struct mtk_crtc_state
*to_mtk_crtc_state(struct drm_crtc_state
*s
)
79 return container_of(s
, struct mtk_crtc_state
, base
);
82 static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc
*mtk_crtc
)
84 struct drm_crtc
*crtc
= &mtk_crtc
->base
;
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
);
94 static void mtk_drm_finish_page_flip(struct mtk_drm_crtc
*mtk_crtc
)
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;
103 static void mtk_drm_crtc_destroy(struct drm_crtc
*crtc
)
105 struct mtk_drm_crtc
*mtk_crtc
= to_mtk_crtc(crtc
);
107 mtk_disp_mutex_put(mtk_crtc
->mutex
);
109 drm_crtc_cleanup(crtc
);
112 static void mtk_drm_crtc_reset(struct drm_crtc
*crtc
)
114 struct mtk_crtc_state
*state
;
117 __drm_atomic_helper_crtc_destroy_state(crtc
->state
);
119 state
= to_mtk_crtc_state(crtc
->state
);
120 memset(state
, 0, sizeof(*state
));
122 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
125 crtc
->state
= &state
->base
;
128 state
->base
.crtc
= crtc
;
131 static struct drm_crtc_state
*mtk_drm_crtc_duplicate_state(struct drm_crtc
*crtc
)
133 struct mtk_crtc_state
*state
;
135 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
139 __drm_atomic_helper_crtc_duplicate_state(crtc
, &state
->base
);
141 WARN_ON(state
->base
.crtc
!= crtc
);
142 state
->base
.crtc
= crtc
;
147 static void mtk_drm_crtc_destroy_state(struct drm_crtc
*crtc
,
148 struct drm_crtc_state
*state
)
150 __drm_atomic_helper_crtc_destroy_state(state
);
151 kfree(to_mtk_crtc_state(state
));
154 static bool mtk_drm_crtc_mode_fixup(struct drm_crtc
*crtc
,
155 const struct drm_display_mode
*mode
,
156 struct drm_display_mode
*adjusted_mode
)
158 /* Nothing to do here, but this callback is mandatory. */
162 static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc
*crtc
)
164 struct mtk_crtc_state
*state
= to_mtk_crtc_state(crtc
->state
);
166 state
->pending_width
= crtc
->mode
.hdisplay
;
167 state
->pending_height
= crtc
->mode
.vdisplay
;
168 state
->pending_vrefresh
= drm_mode_vrefresh(&crtc
->mode
);
169 wmb(); /* Make sure the above parameters are set before update */
170 state
->pending_config
= true;
173 static int mtk_drm_crtc_enable_vblank(struct drm_crtc
*crtc
)
175 struct mtk_drm_crtc
*mtk_crtc
= to_mtk_crtc(crtc
);
176 struct mtk_ddp_comp
*comp
= mtk_crtc
->ddp_comp
[0];
178 mtk_ddp_comp_enable_vblank(comp
, &mtk_crtc
->base
);
183 static void mtk_drm_crtc_disable_vblank(struct drm_crtc
*crtc
)
185 struct mtk_drm_crtc
*mtk_crtc
= to_mtk_crtc(crtc
);
186 struct mtk_ddp_comp
*comp
= mtk_crtc
->ddp_comp
[0];
188 mtk_ddp_comp_disable_vblank(comp
);
191 static int mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc
*mtk_crtc
)
196 DRM_DEBUG_DRIVER("%s\n", __func__
);
197 for (i
= 0; i
< mtk_crtc
->ddp_comp_nr
; i
++) {
198 ret
= clk_prepare_enable(mtk_crtc
->ddp_comp
[i
]->clk
);
200 DRM_ERROR("Failed to enable clock %d: %d\n", i
, ret
);
208 clk_disable_unprepare(mtk_crtc
->ddp_comp
[i
]->clk
);
212 static void mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc
*mtk_crtc
)
216 DRM_DEBUG_DRIVER("%s\n", __func__
);
217 for (i
= 0; i
< mtk_crtc
->ddp_comp_nr
; i
++)
218 clk_disable_unprepare(mtk_crtc
->ddp_comp
[i
]->clk
);
222 struct mtk_ddp_comp
*mtk_drm_ddp_comp_for_plane(struct drm_crtc
*crtc
,
223 struct drm_plane
*plane
,
224 unsigned int *local_layer
)
226 struct mtk_drm_crtc
*mtk_crtc
= to_mtk_crtc(crtc
);
227 struct mtk_ddp_comp
*comp
;
229 unsigned int local_index
= plane
- mtk_crtc
->planes
;
231 for (i
= 0; i
< mtk_crtc
->ddp_comp_nr
; i
++) {
232 comp
= mtk_crtc
->ddp_comp
[i
];
233 if (local_index
< (count
+ mtk_ddp_comp_layer_nr(comp
))) {
234 *local_layer
= local_index
- count
;
237 count
+= mtk_ddp_comp_layer_nr(comp
);
240 WARN(1, "Failed to find component for plane %d\n", plane
->index
);
244 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
245 static void ddp_cmdq_cb(struct cmdq_cb_data data
)
247 cmdq_pkt_destroy(data
.data
);
251 static int mtk_crtc_ddp_hw_init(struct mtk_drm_crtc
*mtk_crtc
)
253 struct drm_crtc
*crtc
= &mtk_crtc
->base
;
254 struct drm_connector
*connector
;
255 struct drm_encoder
*encoder
;
256 struct drm_connector_list_iter conn_iter
;
257 unsigned int width
, height
, vrefresh
, bpc
= MTK_MAX_BPC
;
261 DRM_DEBUG_DRIVER("%s\n", __func__
);
262 if (WARN_ON(!crtc
->state
))
265 width
= crtc
->state
->adjusted_mode
.hdisplay
;
266 height
= crtc
->state
->adjusted_mode
.vdisplay
;
267 vrefresh
= drm_mode_vrefresh(&crtc
->state
->adjusted_mode
);
269 drm_for_each_encoder(encoder
, crtc
->dev
) {
270 if (encoder
->crtc
!= crtc
)
273 drm_connector_list_iter_begin(crtc
->dev
, &conn_iter
);
274 drm_for_each_connector_iter(connector
, &conn_iter
) {
275 if (connector
->encoder
!= encoder
)
277 if (connector
->display_info
.bpc
!= 0 &&
278 bpc
> connector
->display_info
.bpc
)
279 bpc
= connector
->display_info
.bpc
;
281 drm_connector_list_iter_end(&conn_iter
);
284 ret
= pm_runtime_get_sync(crtc
->dev
->dev
);
286 DRM_ERROR("Failed to enable power domain: %d\n", ret
);
290 ret
= mtk_disp_mutex_prepare(mtk_crtc
->mutex
);
292 DRM_ERROR("Failed to enable mutex clock: %d\n", ret
);
293 goto err_pm_runtime_put
;
296 ret
= mtk_crtc_ddp_clk_enable(mtk_crtc
);
298 DRM_ERROR("Failed to enable component clocks: %d\n", ret
);
299 goto err_mutex_unprepare
;
302 DRM_DEBUG_DRIVER("mediatek_ddp_ddp_path_setup\n");
303 for (i
= 0; i
< mtk_crtc
->ddp_comp_nr
- 1; i
++) {
304 mtk_mmsys_ddp_connect(mtk_crtc
->mmsys_dev
,
305 mtk_crtc
->ddp_comp
[i
]->id
,
306 mtk_crtc
->ddp_comp
[i
+ 1]->id
);
307 mtk_disp_mutex_add_comp(mtk_crtc
->mutex
,
308 mtk_crtc
->ddp_comp
[i
]->id
);
310 mtk_disp_mutex_add_comp(mtk_crtc
->mutex
, mtk_crtc
->ddp_comp
[i
]->id
);
311 mtk_disp_mutex_enable(mtk_crtc
->mutex
);
313 for (i
= 0; i
< mtk_crtc
->ddp_comp_nr
; i
++) {
314 struct mtk_ddp_comp
*comp
= mtk_crtc
->ddp_comp
[i
];
317 mtk_ddp_comp_bgclr_in_on(comp
);
319 mtk_ddp_comp_config(comp
, width
, height
, vrefresh
, bpc
, NULL
);
320 mtk_ddp_comp_start(comp
);
323 /* Initially configure all planes */
324 for (i
= 0; i
< mtk_crtc
->layer_nr
; i
++) {
325 struct drm_plane
*plane
= &mtk_crtc
->planes
[i
];
326 struct mtk_plane_state
*plane_state
;
327 struct mtk_ddp_comp
*comp
;
328 unsigned int local_layer
;
330 plane_state
= to_mtk_plane_state(plane
->state
);
331 comp
= mtk_drm_ddp_comp_for_plane(crtc
, plane
, &local_layer
);
333 mtk_ddp_comp_layer_config(comp
, local_layer
,
340 mtk_disp_mutex_unprepare(mtk_crtc
->mutex
);
342 pm_runtime_put(crtc
->dev
->dev
);
346 static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc
*mtk_crtc
)
348 struct drm_device
*drm
= mtk_crtc
->base
.dev
;
349 struct drm_crtc
*crtc
= &mtk_crtc
->base
;
352 DRM_DEBUG_DRIVER("%s\n", __func__
);
353 for (i
= 0; i
< mtk_crtc
->ddp_comp_nr
; i
++) {
354 mtk_ddp_comp_stop(mtk_crtc
->ddp_comp
[i
]);
356 mtk_ddp_comp_bgclr_in_off(mtk_crtc
->ddp_comp
[i
]);
359 for (i
= 0; i
< mtk_crtc
->ddp_comp_nr
; i
++)
360 mtk_disp_mutex_remove_comp(mtk_crtc
->mutex
,
361 mtk_crtc
->ddp_comp
[i
]->id
);
362 mtk_disp_mutex_disable(mtk_crtc
->mutex
);
363 for (i
= 0; i
< mtk_crtc
->ddp_comp_nr
- 1; i
++) {
364 mtk_mmsys_ddp_disconnect(mtk_crtc
->mmsys_dev
,
365 mtk_crtc
->ddp_comp
[i
]->id
,
366 mtk_crtc
->ddp_comp
[i
+ 1]->id
);
367 mtk_disp_mutex_remove_comp(mtk_crtc
->mutex
,
368 mtk_crtc
->ddp_comp
[i
]->id
);
370 mtk_disp_mutex_remove_comp(mtk_crtc
->mutex
, mtk_crtc
->ddp_comp
[i
]->id
);
371 mtk_crtc_ddp_clk_disable(mtk_crtc
);
372 mtk_disp_mutex_unprepare(mtk_crtc
->mutex
);
374 pm_runtime_put(drm
->dev
);
376 if (crtc
->state
->event
&& !crtc
->state
->active
) {
377 spin_lock_irq(&crtc
->dev
->event_lock
);
378 drm_crtc_send_vblank_event(crtc
, crtc
->state
->event
);
379 crtc
->state
->event
= NULL
;
380 spin_unlock_irq(&crtc
->dev
->event_lock
);
384 static void mtk_crtc_ddp_config(struct drm_crtc
*crtc
,
385 struct cmdq_pkt
*cmdq_handle
)
387 struct mtk_drm_crtc
*mtk_crtc
= to_mtk_crtc(crtc
);
388 struct mtk_crtc_state
*state
= to_mtk_crtc_state(mtk_crtc
->base
.state
);
389 struct mtk_ddp_comp
*comp
= mtk_crtc
->ddp_comp
[0];
391 unsigned int local_layer
;
394 * TODO: instead of updating the registers here, we should prepare
395 * working registers in atomic_commit and let the hardware command
396 * queue update module registers on vblank.
398 if (state
->pending_config
) {
399 mtk_ddp_comp_config(comp
, state
->pending_width
,
400 state
->pending_height
,
401 state
->pending_vrefresh
, 0,
404 state
->pending_config
= false;
407 if (mtk_crtc
->pending_planes
) {
408 for (i
= 0; i
< mtk_crtc
->layer_nr
; i
++) {
409 struct drm_plane
*plane
= &mtk_crtc
->planes
[i
];
410 struct mtk_plane_state
*plane_state
;
412 plane_state
= to_mtk_plane_state(plane
->state
);
414 if (!plane_state
->pending
.config
)
417 comp
= mtk_drm_ddp_comp_for_plane(crtc
, plane
,
421 mtk_ddp_comp_layer_config(comp
, local_layer
,
424 plane_state
->pending
.config
= false;
426 mtk_crtc
->pending_planes
= false;
429 if (mtk_crtc
->pending_async_planes
) {
430 for (i
= 0; i
< mtk_crtc
->layer_nr
; i
++) {
431 struct drm_plane
*plane
= &mtk_crtc
->planes
[i
];
432 struct mtk_plane_state
*plane_state
;
434 plane_state
= to_mtk_plane_state(plane
->state
);
436 if (!plane_state
->pending
.async_config
)
439 comp
= mtk_drm_ddp_comp_for_plane(crtc
, plane
,
443 mtk_ddp_comp_layer_config(comp
, local_layer
,
446 plane_state
->pending
.async_config
= false;
448 mtk_crtc
->pending_async_planes
= false;
452 static void mtk_drm_crtc_hw_config(struct mtk_drm_crtc
*mtk_crtc
)
454 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
455 struct cmdq_pkt
*cmdq_handle
;
457 struct drm_crtc
*crtc
= &mtk_crtc
->base
;
458 struct mtk_drm_private
*priv
= crtc
->dev
->dev_private
;
459 unsigned int pending_planes
= 0, pending_async_planes
= 0;
462 mutex_lock(&mtk_crtc
->hw_lock
);
463 for (i
= 0; i
< mtk_crtc
->layer_nr
; i
++) {
464 struct drm_plane
*plane
= &mtk_crtc
->planes
[i
];
465 struct mtk_plane_state
*plane_state
;
467 plane_state
= to_mtk_plane_state(plane
->state
);
468 if (plane_state
->pending
.dirty
) {
469 plane_state
->pending
.config
= true;
470 plane_state
->pending
.dirty
= false;
471 pending_planes
|= BIT(i
);
472 } else if (plane_state
->pending
.async_dirty
) {
473 plane_state
->pending
.async_config
= true;
474 plane_state
->pending
.async_dirty
= false;
475 pending_async_planes
|= BIT(i
);
479 mtk_crtc
->pending_planes
= true;
480 if (pending_async_planes
)
481 mtk_crtc
->pending_async_planes
= true;
483 if (priv
->data
->shadow_register
) {
484 mtk_disp_mutex_acquire(mtk_crtc
->mutex
);
485 mtk_crtc_ddp_config(crtc
, NULL
);
486 mtk_disp_mutex_release(mtk_crtc
->mutex
);
488 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
489 if (mtk_crtc
->cmdq_client
) {
490 mbox_flush(mtk_crtc
->cmdq_client
->chan
, 2000);
491 cmdq_handle
= cmdq_pkt_create(mtk_crtc
->cmdq_client
, PAGE_SIZE
);
492 cmdq_pkt_clear_event(cmdq_handle
, mtk_crtc
->cmdq_event
);
493 cmdq_pkt_wfe(cmdq_handle
, mtk_crtc
->cmdq_event
);
494 mtk_crtc_ddp_config(crtc
, cmdq_handle
);
495 cmdq_pkt_flush_async(cmdq_handle
, ddp_cmdq_cb
, cmdq_handle
);
498 mutex_unlock(&mtk_crtc
->hw_lock
);
501 int mtk_drm_crtc_plane_check(struct drm_crtc
*crtc
, struct drm_plane
*plane
,
502 struct mtk_plane_state
*state
)
504 unsigned int local_layer
;
505 struct mtk_ddp_comp
*comp
;
507 comp
= mtk_drm_ddp_comp_for_plane(crtc
, plane
, &local_layer
);
509 return mtk_ddp_comp_layer_check(comp
, local_layer
, state
);
513 void mtk_drm_crtc_async_update(struct drm_crtc
*crtc
, struct drm_plane
*plane
,
514 struct drm_plane_state
*new_state
)
516 struct mtk_drm_crtc
*mtk_crtc
= to_mtk_crtc(crtc
);
517 const struct drm_plane_helper_funcs
*plane_helper_funcs
=
518 plane
->helper_private
;
520 if (!mtk_crtc
->enabled
)
523 plane_helper_funcs
->atomic_update(plane
, new_state
);
524 mtk_drm_crtc_hw_config(mtk_crtc
);
527 static void mtk_drm_crtc_atomic_enable(struct drm_crtc
*crtc
,
528 struct drm_crtc_state
*old_state
)
530 struct mtk_drm_crtc
*mtk_crtc
= to_mtk_crtc(crtc
);
531 struct mtk_ddp_comp
*comp
= mtk_crtc
->ddp_comp
[0];
534 DRM_DEBUG_DRIVER("%s %d\n", __func__
, crtc
->base
.id
);
536 ret
= mtk_smi_larb_get(comp
->larb_dev
);
538 DRM_ERROR("Failed to get larb: %d\n", ret
);
542 ret
= mtk_crtc_ddp_hw_init(mtk_crtc
);
544 mtk_smi_larb_put(comp
->larb_dev
);
548 drm_crtc_vblank_on(crtc
);
549 mtk_crtc
->enabled
= true;
552 static void mtk_drm_crtc_atomic_disable(struct drm_crtc
*crtc
,
553 struct drm_crtc_state
*old_state
)
555 struct mtk_drm_crtc
*mtk_crtc
= to_mtk_crtc(crtc
);
556 struct mtk_ddp_comp
*comp
= mtk_crtc
->ddp_comp
[0];
559 DRM_DEBUG_DRIVER("%s %d\n", __func__
, crtc
->base
.id
);
560 if (!mtk_crtc
->enabled
)
563 /* Set all pending plane state to disabled */
564 for (i
= 0; i
< mtk_crtc
->layer_nr
; i
++) {
565 struct drm_plane
*plane
= &mtk_crtc
->planes
[i
];
566 struct mtk_plane_state
*plane_state
;
568 plane_state
= to_mtk_plane_state(plane
->state
);
569 plane_state
->pending
.enable
= false;
570 plane_state
->pending
.config
= true;
572 mtk_crtc
->pending_planes
= true;
574 mtk_drm_crtc_hw_config(mtk_crtc
);
575 /* Wait for planes to be disabled */
576 drm_crtc_wait_one_vblank(crtc
);
578 drm_crtc_vblank_off(crtc
);
579 mtk_crtc_ddp_hw_fini(mtk_crtc
);
580 mtk_smi_larb_put(comp
->larb_dev
);
582 mtk_crtc
->enabled
= false;
585 static void mtk_drm_crtc_atomic_begin(struct drm_crtc
*crtc
,
586 struct drm_crtc_state
*old_crtc_state
)
588 struct mtk_crtc_state
*state
= to_mtk_crtc_state(crtc
->state
);
589 struct mtk_drm_crtc
*mtk_crtc
= to_mtk_crtc(crtc
);
591 if (mtk_crtc
->event
&& state
->base
.event
)
592 DRM_ERROR("new event while there is still a pending event\n");
594 if (state
->base
.event
) {
595 state
->base
.event
->pipe
= drm_crtc_index(crtc
);
596 WARN_ON(drm_crtc_vblank_get(crtc
) != 0);
597 mtk_crtc
->event
= state
->base
.event
;
598 state
->base
.event
= NULL
;
602 static void mtk_drm_crtc_atomic_flush(struct drm_crtc
*crtc
,
603 struct drm_crtc_state
*old_crtc_state
)
605 struct mtk_drm_crtc
*mtk_crtc
= to_mtk_crtc(crtc
);
609 mtk_crtc
->pending_needs_vblank
= true;
610 if (crtc
->state
->color_mgmt_changed
)
611 for (i
= 0; i
< mtk_crtc
->ddp_comp_nr
; i
++) {
612 mtk_ddp_gamma_set(mtk_crtc
->ddp_comp
[i
], crtc
->state
);
613 mtk_ddp_ctm_set(mtk_crtc
->ddp_comp
[i
], crtc
->state
);
615 mtk_drm_crtc_hw_config(mtk_crtc
);
618 static const struct drm_crtc_funcs mtk_crtc_funcs
= {
619 .set_config
= drm_atomic_helper_set_config
,
620 .page_flip
= drm_atomic_helper_page_flip
,
621 .destroy
= mtk_drm_crtc_destroy
,
622 .reset
= mtk_drm_crtc_reset
,
623 .atomic_duplicate_state
= mtk_drm_crtc_duplicate_state
,
624 .atomic_destroy_state
= mtk_drm_crtc_destroy_state
,
625 .gamma_set
= drm_atomic_helper_legacy_gamma_set
,
626 .enable_vblank
= mtk_drm_crtc_enable_vblank
,
627 .disable_vblank
= mtk_drm_crtc_disable_vblank
,
630 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs
= {
631 .mode_fixup
= mtk_drm_crtc_mode_fixup
,
632 .mode_set_nofb
= mtk_drm_crtc_mode_set_nofb
,
633 .atomic_begin
= mtk_drm_crtc_atomic_begin
,
634 .atomic_flush
= mtk_drm_crtc_atomic_flush
,
635 .atomic_enable
= mtk_drm_crtc_atomic_enable
,
636 .atomic_disable
= mtk_drm_crtc_atomic_disable
,
639 static int mtk_drm_crtc_init(struct drm_device
*drm
,
640 struct mtk_drm_crtc
*mtk_crtc
,
643 struct drm_plane
*primary
= NULL
;
644 struct drm_plane
*cursor
= NULL
;
647 for (i
= 0; i
< mtk_crtc
->layer_nr
; i
++) {
648 if (mtk_crtc
->planes
[i
].type
== DRM_PLANE_TYPE_PRIMARY
)
649 primary
= &mtk_crtc
->planes
[i
];
650 else if (mtk_crtc
->planes
[i
].type
== DRM_PLANE_TYPE_CURSOR
)
651 cursor
= &mtk_crtc
->planes
[i
];
654 ret
= drm_crtc_init_with_planes(drm
, &mtk_crtc
->base
, primary
, cursor
,
655 &mtk_crtc_funcs
, NULL
);
657 goto err_cleanup_crtc
;
659 drm_crtc_helper_add(&mtk_crtc
->base
, &mtk_crtc_helper_funcs
);
664 drm_crtc_cleanup(&mtk_crtc
->base
);
668 void mtk_crtc_ddp_irq(struct drm_crtc
*crtc
, struct mtk_ddp_comp
*comp
)
670 struct mtk_drm_crtc
*mtk_crtc
= to_mtk_crtc(crtc
);
671 struct mtk_drm_private
*priv
= crtc
->dev
->dev_private
;
673 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
674 if (!priv
->data
->shadow_register
&& !mtk_crtc
->cmdq_client
)
676 if (!priv
->data
->shadow_register
)
678 mtk_crtc_ddp_config(crtc
, NULL
);
680 mtk_drm_finish_page_flip(mtk_crtc
);
683 static int mtk_drm_crtc_num_comp_planes(struct mtk_drm_crtc
*mtk_crtc
,
686 struct mtk_ddp_comp
*comp
;
691 comp
= mtk_crtc
->ddp_comp
[comp_idx
];
695 if (comp_idx
== 1 && !comp
->funcs
->bgclr_in_on
)
698 return mtk_ddp_comp_layer_nr(comp
);
702 enum drm_plane_type
mtk_drm_crtc_plane_type(unsigned int plane_idx
,
703 unsigned int num_planes
)
706 return DRM_PLANE_TYPE_PRIMARY
;
707 else if (plane_idx
== (num_planes
- 1))
708 return DRM_PLANE_TYPE_CURSOR
;
710 return DRM_PLANE_TYPE_OVERLAY
;
714 static int mtk_drm_crtc_init_comp_planes(struct drm_device
*drm_dev
,
715 struct mtk_drm_crtc
*mtk_crtc
,
716 int comp_idx
, int pipe
)
718 int num_planes
= mtk_drm_crtc_num_comp_planes(mtk_crtc
, comp_idx
);
719 struct mtk_ddp_comp
*comp
= mtk_crtc
->ddp_comp
[comp_idx
];
722 for (i
= 0; i
< num_planes
; i
++) {
723 ret
= mtk_plane_init(drm_dev
,
724 &mtk_crtc
->planes
[mtk_crtc
->layer_nr
],
726 mtk_drm_crtc_plane_type(mtk_crtc
->layer_nr
,
728 mtk_ddp_comp_supported_rotations(comp
));
732 mtk_crtc
->layer_nr
++;
737 int mtk_drm_crtc_create(struct drm_device
*drm_dev
,
738 const enum mtk_ddp_comp_id
*path
, unsigned int path_len
)
740 struct mtk_drm_private
*priv
= drm_dev
->dev_private
;
741 struct device
*dev
= drm_dev
->dev
;
742 struct mtk_drm_crtc
*mtk_crtc
;
743 unsigned int num_comp_planes
= 0;
744 int pipe
= priv
->num_pipes
;
747 bool has_ctm
= false;
748 uint gamma_lut_size
= 0;
753 for (i
= 0; i
< path_len
; i
++) {
754 enum mtk_ddp_comp_id comp_id
= path
[i
];
755 struct device_node
*node
;
757 node
= priv
->comp_node
[comp_id
];
760 "Not creating crtc %d because component %d is disabled or missing\n",
766 mtk_crtc
= devm_kzalloc(dev
, sizeof(*mtk_crtc
), GFP_KERNEL
);
770 mtk_crtc
->mmsys_dev
= priv
->mmsys_dev
;
771 mtk_crtc
->ddp_comp_nr
= path_len
;
772 mtk_crtc
->ddp_comp
= devm_kmalloc_array(dev
, mtk_crtc
->ddp_comp_nr
,
773 sizeof(*mtk_crtc
->ddp_comp
),
775 if (!mtk_crtc
->ddp_comp
)
778 mtk_crtc
->mutex
= mtk_disp_mutex_get(priv
->mutex_dev
, pipe
);
779 if (IS_ERR(mtk_crtc
->mutex
)) {
780 ret
= PTR_ERR(mtk_crtc
->mutex
);
781 dev_err(dev
, "Failed to get mutex: %d\n", ret
);
785 for (i
= 0; i
< mtk_crtc
->ddp_comp_nr
; i
++) {
786 enum mtk_ddp_comp_id comp_id
= path
[i
];
787 struct mtk_ddp_comp
*comp
;
788 struct device_node
*node
;
790 node
= priv
->comp_node
[comp_id
];
791 comp
= priv
->ddp_comp
[comp_id
];
793 dev_err(dev
, "Component %pOF not initialized\n", node
);
798 mtk_crtc
->ddp_comp
[i
] = comp
;
801 if (comp
->funcs
->gamma_set
)
802 gamma_lut_size
= MTK_LUT_SIZE
;
804 if (comp
->funcs
->ctm_set
)
809 for (i
= 0; i
< mtk_crtc
->ddp_comp_nr
; i
++)
810 num_comp_planes
+= mtk_drm_crtc_num_comp_planes(mtk_crtc
, i
);
812 mtk_crtc
->planes
= devm_kcalloc(dev
, num_comp_planes
,
813 sizeof(struct drm_plane
), GFP_KERNEL
);
815 for (i
= 0; i
< mtk_crtc
->ddp_comp_nr
; i
++) {
816 ret
= mtk_drm_crtc_init_comp_planes(drm_dev
, mtk_crtc
, i
,
822 ret
= mtk_drm_crtc_init(drm_dev
, mtk_crtc
, pipe
);
827 drm_mode_crtc_set_gamma_size(&mtk_crtc
->base
, gamma_lut_size
);
828 drm_crtc_enable_color_mgmt(&mtk_crtc
->base
, 0, has_ctm
, gamma_lut_size
);
830 mutex_init(&mtk_crtc
->hw_lock
);
832 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
833 mtk_crtc
->cmdq_client
=
834 cmdq_mbox_create(dev
, drm_crtc_index(&mtk_crtc
->base
),
836 if (IS_ERR(mtk_crtc
->cmdq_client
)) {
837 dev_dbg(dev
, "mtk_crtc %d failed to create mailbox client, writing register by CPU now\n",
838 drm_crtc_index(&mtk_crtc
->base
));
839 mtk_crtc
->cmdq_client
= NULL
;
841 ret
= of_property_read_u32_index(priv
->mutex_node
,
842 "mediatek,gce-events",
843 drm_crtc_index(&mtk_crtc
->base
),
844 &mtk_crtc
->cmdq_event
);
846 dev_dbg(dev
, "mtk_crtc %d failed to get mediatek,gce-events property\n",
847 drm_crtc_index(&mtk_crtc
->base
));