1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
7 #include <linux/dma-mapping.h>
8 #include <linux/mailbox_controller.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/soc/mediatek/mtk-cmdq.h>
11 #include <linux/soc/mediatek/mtk-mmsys.h>
12 #include <linux/soc/mediatek/mtk-mutex.h>
14 #include <asm/barrier.h>
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_plane_helper.h>
19 #include <drm/drm_probe_helper.h>
20 #include <drm/drm_vblank.h>
22 #include "mtk_drm_drv.h"
23 #include "mtk_drm_crtc.h"
24 #include "mtk_drm_ddp_comp.h"
25 #include "mtk_drm_gem.h"
26 #include "mtk_drm_plane.h"
29 * struct mtk_drm_crtc - MediaTek specific crtc structure.
31 * @enabled: records whether crtc_enable succeeded
32 * @planes: array of 4 drm_plane structures, one for each overlay plane
33 * @pending_planes: whether any plane has pending changes to be applied
34 * @mmsys_dev: pointer to the mmsys device for configuration registers
35 * @mutex: handle to one of the ten disp_mutex streams
36 * @ddp_comp_nr: number of components in ddp_comp
37 * @ddp_comp: array of pointers the mtk_ddp_comp structures used by this crtc
39 * TODO: Needs update: this header is missing a bunch of member descriptions.
45 bool pending_needs_vblank;
46 struct drm_pending_vblank_event *event;
48 struct drm_plane *planes;
49 unsigned int layer_nr;
51 bool pending_async_planes;
53 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
54 struct cmdq_client cmdq_client;
55 struct cmdq_pkt cmdq_handle;
58 wait_queue_head_t cb_blocking_queue;
61 struct device *mmsys_dev;
62 struct mtk_mutex *mutex;
63 unsigned int ddp_comp_nr;
64 struct mtk_ddp_comp **ddp_comp;
66 /* lock for display hardware access */
71 struct mtk_crtc_state {
72 struct drm_crtc_state base;
75 unsigned int pending_width;
76 unsigned int pending_height;
77 unsigned int pending_vrefresh;
80 static inline struct mtk_drm_crtc *to_mtk_crtc(struct drm_crtc *c)
82 return container_of(c, struct mtk_drm_crtc, base);
85 static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s)
87 return container_of(s, struct mtk_crtc_state, base);
90 static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
92 struct drm_crtc *crtc = &mtk_crtc->base;
95 spin_lock_irqsave(&crtc->dev->event_lock, flags);
96 drm_crtc_send_vblank_event(crtc, mtk_crtc->event);
97 drm_crtc_vblank_put(crtc);
98 mtk_crtc->event = NULL;
99 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
102 static void mtk_drm_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
104 drm_crtc_handle_vblank(&mtk_crtc->base);
105 if (!mtk_crtc->config_updating && mtk_crtc->pending_needs_vblank) {
106 mtk_drm_crtc_finish_page_flip(mtk_crtc);
107 mtk_crtc->pending_needs_vblank = false;
111 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
112 static int mtk_drm_cmdq_pkt_create(struct cmdq_client *client, struct cmdq_pkt *pkt,
118 pkt->va_base = kzalloc(size, GFP_KERNEL);
123 pkt->buf_size = size;
124 pkt->cl = (void *)client;
126 dev = client->chan->mbox->dev;
127 dma_addr = dma_map_single(dev, pkt->va_base, pkt->buf_size,
129 if (dma_mapping_error(dev, dma_addr)) {
130 dev_err(dev, "dma map failed, size=%u\n", (u32)(u64)size);
136 pkt->pa_base = dma_addr;
141 static void mtk_drm_cmdq_pkt_destroy(struct cmdq_pkt *pkt)
143 struct cmdq_client *client = (struct cmdq_client *)pkt->cl;
145 dma_unmap_single(client->chan->mbox->dev, pkt->pa_base, pkt->buf_size,
152 static void mtk_drm_crtc_destroy(struct drm_crtc *crtc)
154 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
156 mtk_mutex_put(mtk_crtc->mutex);
157 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
158 mtk_drm_cmdq_pkt_destroy(&mtk_crtc->cmdq_handle);
160 if (mtk_crtc->cmdq_client.chan) {
161 mbox_free_channel(mtk_crtc->cmdq_client.chan);
162 mtk_crtc->cmdq_client.chan = NULL;
165 drm_crtc_cleanup(crtc);
168 static void mtk_drm_crtc_reset(struct drm_crtc *crtc)
170 struct mtk_crtc_state *state;
173 __drm_atomic_helper_crtc_destroy_state(crtc->state);
175 kfree(to_mtk_crtc_state(crtc->state));
178 state = kzalloc(sizeof(*state), GFP_KERNEL);
180 __drm_atomic_helper_crtc_reset(crtc, &state->base);
183 static struct drm_crtc_state *mtk_drm_crtc_duplicate_state(struct drm_crtc *crtc)
185 struct mtk_crtc_state *state;
187 state = kzalloc(sizeof(*state), GFP_KERNEL);
191 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
193 WARN_ON(state->base.crtc != crtc);
194 state->base.crtc = crtc;
199 static void mtk_drm_crtc_destroy_state(struct drm_crtc *crtc,
200 struct drm_crtc_state *state)
202 __drm_atomic_helper_crtc_destroy_state(state);
203 kfree(to_mtk_crtc_state(state));
206 static bool mtk_drm_crtc_mode_fixup(struct drm_crtc *crtc,
207 const struct drm_display_mode *mode,
208 struct drm_display_mode *adjusted_mode)
210 /* Nothing to do here, but this callback is mandatory. */
214 static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
216 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
218 state->pending_width = crtc->mode.hdisplay;
219 state->pending_height = crtc->mode.vdisplay;
220 state->pending_vrefresh = drm_mode_vrefresh(&crtc->mode);
221 wmb(); /* Make sure the above parameters are set before update */
222 state->pending_config = true;
225 static int mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc *mtk_crtc)
230 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
231 ret = mtk_ddp_comp_clk_enable(mtk_crtc->ddp_comp[i]);
233 DRM_ERROR("Failed to enable clock %d: %d\n", i, ret);
241 mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]);
245 static void mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc *mtk_crtc)
249 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
250 mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]);
254 struct mtk_ddp_comp *mtk_drm_ddp_comp_for_plane(struct drm_crtc *crtc,
255 struct drm_plane *plane,
256 unsigned int *local_layer)
258 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
259 struct mtk_ddp_comp *comp;
261 unsigned int local_index = plane - mtk_crtc->planes;
263 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
264 comp = mtk_crtc->ddp_comp[i];
265 if (local_index < (count + mtk_ddp_comp_layer_nr(comp))) {
266 *local_layer = local_index - count;
269 count += mtk_ddp_comp_layer_nr(comp);
272 WARN(1, "Failed to find component for plane %d\n", plane->index);
276 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
277 static void ddp_cmdq_cb(struct mbox_client *cl, void *mssg)
279 struct cmdq_cb_data *data = mssg;
280 struct cmdq_client *cmdq_cl = container_of(cl, struct cmdq_client, client);
281 struct mtk_drm_crtc *mtk_crtc = container_of(cmdq_cl, struct mtk_drm_crtc, cmdq_client);
282 struct mtk_crtc_state *state;
288 state = to_mtk_crtc_state(mtk_crtc->base.state);
290 state->pending_config = false;
292 if (mtk_crtc->pending_planes) {
293 for (i = 0; i < mtk_crtc->layer_nr; i++) {
294 struct drm_plane *plane = &mtk_crtc->planes[i];
295 struct mtk_plane_state *plane_state;
297 plane_state = to_mtk_plane_state(plane->state);
299 plane_state->pending.config = false;
301 mtk_crtc->pending_planes = false;
304 if (mtk_crtc->pending_async_planes) {
305 for (i = 0; i < mtk_crtc->layer_nr; i++) {
306 struct drm_plane *plane = &mtk_crtc->planes[i];
307 struct mtk_plane_state *plane_state;
309 plane_state = to_mtk_plane_state(plane->state);
311 plane_state->pending.async_config = false;
313 mtk_crtc->pending_async_planes = false;
316 mtk_crtc->cmdq_vblank_cnt = 0;
317 wake_up(&mtk_crtc->cb_blocking_queue);
321 static int mtk_crtc_ddp_hw_init(struct mtk_drm_crtc *mtk_crtc)
323 struct drm_crtc *crtc = &mtk_crtc->base;
324 struct drm_connector *connector;
325 struct drm_encoder *encoder;
326 struct drm_connector_list_iter conn_iter;
327 unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC;
331 if (WARN_ON(!crtc->state))
334 width = crtc->state->adjusted_mode.hdisplay;
335 height = crtc->state->adjusted_mode.vdisplay;
336 vrefresh = drm_mode_vrefresh(&crtc->state->adjusted_mode);
338 drm_for_each_encoder(encoder, crtc->dev) {
339 if (encoder->crtc != crtc)
342 drm_connector_list_iter_begin(crtc->dev, &conn_iter);
343 drm_for_each_connector_iter(connector, &conn_iter) {
344 if (connector->encoder != encoder)
346 if (connector->display_info.bpc != 0 &&
347 bpc > connector->display_info.bpc)
348 bpc = connector->display_info.bpc;
350 drm_connector_list_iter_end(&conn_iter);
353 ret = pm_runtime_resume_and_get(crtc->dev->dev);
355 DRM_ERROR("Failed to enable power domain: %d\n", ret);
359 ret = mtk_mutex_prepare(mtk_crtc->mutex);
361 DRM_ERROR("Failed to enable mutex clock: %d\n", ret);
362 goto err_pm_runtime_put;
365 ret = mtk_crtc_ddp_clk_enable(mtk_crtc);
367 DRM_ERROR("Failed to enable component clocks: %d\n", ret);
368 goto err_mutex_unprepare;
371 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
372 mtk_mmsys_ddp_connect(mtk_crtc->mmsys_dev,
373 mtk_crtc->ddp_comp[i]->id,
374 mtk_crtc->ddp_comp[i + 1]->id);
375 mtk_mutex_add_comp(mtk_crtc->mutex,
376 mtk_crtc->ddp_comp[i]->id);
378 mtk_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
379 mtk_mutex_enable(mtk_crtc->mutex);
381 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
382 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i];
385 mtk_ddp_comp_bgclr_in_on(comp);
387 mtk_ddp_comp_config(comp, width, height, vrefresh, bpc, NULL);
388 mtk_ddp_comp_start(comp);
391 /* Initially configure all planes */
392 for (i = 0; i < mtk_crtc->layer_nr; i++) {
393 struct drm_plane *plane = &mtk_crtc->planes[i];
394 struct mtk_plane_state *plane_state;
395 struct mtk_ddp_comp *comp;
396 unsigned int local_layer;
398 plane_state = to_mtk_plane_state(plane->state);
399 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer);
401 mtk_ddp_comp_layer_config(comp, local_layer,
408 mtk_mutex_unprepare(mtk_crtc->mutex);
410 pm_runtime_put(crtc->dev->dev);
414 static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc)
416 struct drm_device *drm = mtk_crtc->base.dev;
417 struct drm_crtc *crtc = &mtk_crtc->base;
420 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
421 mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]);
423 mtk_ddp_comp_bgclr_in_off(mtk_crtc->ddp_comp[i]);
426 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
427 mtk_mutex_remove_comp(mtk_crtc->mutex,
428 mtk_crtc->ddp_comp[i]->id);
429 mtk_mutex_disable(mtk_crtc->mutex);
430 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
431 mtk_mmsys_ddp_disconnect(mtk_crtc->mmsys_dev,
432 mtk_crtc->ddp_comp[i]->id,
433 mtk_crtc->ddp_comp[i + 1]->id);
434 mtk_mutex_remove_comp(mtk_crtc->mutex,
435 mtk_crtc->ddp_comp[i]->id);
437 mtk_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
438 mtk_crtc_ddp_clk_disable(mtk_crtc);
439 mtk_mutex_unprepare(mtk_crtc->mutex);
441 pm_runtime_put(drm->dev);
443 if (crtc->state->event && !crtc->state->active) {
444 spin_lock_irq(&crtc->dev->event_lock);
445 drm_crtc_send_vblank_event(crtc, crtc->state->event);
446 crtc->state->event = NULL;
447 spin_unlock_irq(&crtc->dev->event_lock);
451 static void mtk_crtc_ddp_config(struct drm_crtc *crtc,
452 struct cmdq_pkt *cmdq_handle)
454 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
455 struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state);
456 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
458 unsigned int local_layer;
461 * TODO: instead of updating the registers here, we should prepare
462 * working registers in atomic_commit and let the hardware command
463 * queue update module registers on vblank.
465 if (state->pending_config) {
466 mtk_ddp_comp_config(comp, state->pending_width,
467 state->pending_height,
468 state->pending_vrefresh, 0,
472 state->pending_config = false;
475 if (mtk_crtc->pending_planes) {
476 for (i = 0; i < mtk_crtc->layer_nr; i++) {
477 struct drm_plane *plane = &mtk_crtc->planes[i];
478 struct mtk_plane_state *plane_state;
480 plane_state = to_mtk_plane_state(plane->state);
482 if (!plane_state->pending.config)
485 comp = mtk_drm_ddp_comp_for_plane(crtc, plane,
489 mtk_ddp_comp_layer_config(comp, local_layer,
493 plane_state->pending.config = false;
497 mtk_crtc->pending_planes = false;
500 if (mtk_crtc->pending_async_planes) {
501 for (i = 0; i < mtk_crtc->layer_nr; i++) {
502 struct drm_plane *plane = &mtk_crtc->planes[i];
503 struct mtk_plane_state *plane_state;
505 plane_state = to_mtk_plane_state(plane->state);
507 if (!plane_state->pending.async_config)
510 comp = mtk_drm_ddp_comp_for_plane(crtc, plane,
514 mtk_ddp_comp_layer_config(comp, local_layer,
518 plane_state->pending.async_config = false;
522 mtk_crtc->pending_async_planes = false;
526 static void mtk_drm_crtc_update_config(struct mtk_drm_crtc *mtk_crtc,
529 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
530 struct cmdq_pkt *cmdq_handle = &mtk_crtc->cmdq_handle;
532 struct drm_crtc *crtc = &mtk_crtc->base;
533 struct mtk_drm_private *priv = crtc->dev->dev_private;
534 unsigned int pending_planes = 0, pending_async_planes = 0;
537 mutex_lock(&mtk_crtc->hw_lock);
538 mtk_crtc->config_updating = true;
540 mtk_crtc->pending_needs_vblank = true;
542 for (i = 0; i < mtk_crtc->layer_nr; i++) {
543 struct drm_plane *plane = &mtk_crtc->planes[i];
544 struct mtk_plane_state *plane_state;
546 plane_state = to_mtk_plane_state(plane->state);
547 if (plane_state->pending.dirty) {
548 plane_state->pending.config = true;
549 plane_state->pending.dirty = false;
550 pending_planes |= BIT(i);
551 } else if (plane_state->pending.async_dirty) {
552 plane_state->pending.async_config = true;
553 plane_state->pending.async_dirty = false;
554 pending_async_planes |= BIT(i);
558 mtk_crtc->pending_planes = true;
559 if (pending_async_planes)
560 mtk_crtc->pending_async_planes = true;
562 if (priv->data->shadow_register) {
563 mtk_mutex_acquire(mtk_crtc->mutex);
564 mtk_crtc_ddp_config(crtc, NULL);
565 mtk_mutex_release(mtk_crtc->mutex);
567 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
568 if (mtk_crtc->cmdq_client.chan) {
569 mbox_flush(mtk_crtc->cmdq_client.chan, 2000);
570 cmdq_handle->cmd_buf_size = 0;
571 cmdq_pkt_clear_event(cmdq_handle, mtk_crtc->cmdq_event);
572 cmdq_pkt_wfe(cmdq_handle, mtk_crtc->cmdq_event, false);
573 mtk_crtc_ddp_config(crtc, cmdq_handle);
574 cmdq_pkt_finalize(cmdq_handle);
575 dma_sync_single_for_device(mtk_crtc->cmdq_client.chan->mbox->dev,
576 cmdq_handle->pa_base,
577 cmdq_handle->cmd_buf_size,
580 * CMDQ command should execute in next 3 vblank.
581 * One vblank interrupt before send message (occasionally)
582 * and one vblank interrupt after cmdq done,
583 * so it's timeout after 3 vblank interrupt.
584 * If it fail to execute in next 3 vblank, timeout happen.
586 mtk_crtc->cmdq_vblank_cnt = 3;
588 mbox_send_message(mtk_crtc->cmdq_client.chan, cmdq_handle);
589 mbox_client_txdone(mtk_crtc->cmdq_client.chan, 0);
592 mtk_crtc->config_updating = false;
593 mutex_unlock(&mtk_crtc->hw_lock);
596 static void mtk_crtc_ddp_irq(void *data)
598 struct drm_crtc *crtc = data;
599 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
600 struct mtk_drm_private *priv = crtc->dev->dev_private;
602 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
603 if (!priv->data->shadow_register && !mtk_crtc->cmdq_client.chan)
604 mtk_crtc_ddp_config(crtc, NULL);
605 else if (mtk_crtc->cmdq_vblank_cnt > 0 && --mtk_crtc->cmdq_vblank_cnt == 0)
606 DRM_ERROR("mtk_crtc %d CMDQ execute command timeout!\n",
607 drm_crtc_index(&mtk_crtc->base));
609 if (!priv->data->shadow_register)
610 mtk_crtc_ddp_config(crtc, NULL);
612 mtk_drm_finish_page_flip(mtk_crtc);
615 static int mtk_drm_crtc_enable_vblank(struct drm_crtc *crtc)
617 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
618 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
620 mtk_ddp_comp_enable_vblank(comp, mtk_crtc_ddp_irq, &mtk_crtc->base);
625 static void mtk_drm_crtc_disable_vblank(struct drm_crtc *crtc)
627 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
628 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
630 mtk_ddp_comp_disable_vblank(comp);
633 int mtk_drm_crtc_plane_check(struct drm_crtc *crtc, struct drm_plane *plane,
634 struct mtk_plane_state *state)
636 unsigned int local_layer;
637 struct mtk_ddp_comp *comp;
639 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer);
641 return mtk_ddp_comp_layer_check(comp, local_layer, state);
645 void mtk_drm_crtc_async_update(struct drm_crtc *crtc, struct drm_plane *plane,
646 struct drm_atomic_state *state)
648 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
650 if (!mtk_crtc->enabled)
653 mtk_drm_crtc_update_config(mtk_crtc, false);
656 static void mtk_drm_crtc_atomic_enable(struct drm_crtc *crtc,
657 struct drm_atomic_state *state)
659 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
660 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
663 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
665 ret = pm_runtime_resume_and_get(comp->dev);
667 DRM_DEV_ERROR(comp->dev, "Failed to enable power domain: %d\n", ret);
671 ret = mtk_crtc_ddp_hw_init(mtk_crtc);
673 pm_runtime_put(comp->dev);
677 drm_crtc_vblank_on(crtc);
678 mtk_crtc->enabled = true;
681 static void mtk_drm_crtc_atomic_disable(struct drm_crtc *crtc,
682 struct drm_atomic_state *state)
684 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
685 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
688 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
689 if (!mtk_crtc->enabled)
692 /* Set all pending plane state to disabled */
693 for (i = 0; i < mtk_crtc->layer_nr; i++) {
694 struct drm_plane *plane = &mtk_crtc->planes[i];
695 struct mtk_plane_state *plane_state;
697 plane_state = to_mtk_plane_state(plane->state);
698 plane_state->pending.enable = false;
699 plane_state->pending.config = true;
701 mtk_crtc->pending_planes = true;
703 mtk_drm_crtc_update_config(mtk_crtc, false);
704 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
705 /* Wait for planes to be disabled by cmdq */
706 if (mtk_crtc->cmdq_client.chan)
707 wait_event_timeout(mtk_crtc->cb_blocking_queue,
708 mtk_crtc->cmdq_vblank_cnt == 0,
709 msecs_to_jiffies(500));
711 /* Wait for planes to be disabled */
712 drm_crtc_wait_one_vblank(crtc);
714 drm_crtc_vblank_off(crtc);
715 mtk_crtc_ddp_hw_fini(mtk_crtc);
716 ret = pm_runtime_put(comp->dev);
718 DRM_DEV_ERROR(comp->dev, "Failed to disable power domain: %d\n", ret);
720 mtk_crtc->enabled = false;
723 static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc,
724 struct drm_atomic_state *state)
726 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
728 struct mtk_crtc_state *mtk_crtc_state = to_mtk_crtc_state(crtc_state);
729 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
731 if (mtk_crtc->event && mtk_crtc_state->base.event)
732 DRM_ERROR("new event while there is still a pending event\n");
734 if (mtk_crtc_state->base.event) {
735 mtk_crtc_state->base.event->pipe = drm_crtc_index(crtc);
736 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
737 mtk_crtc->event = mtk_crtc_state->base.event;
738 mtk_crtc_state->base.event = NULL;
742 static void mtk_drm_crtc_atomic_flush(struct drm_crtc *crtc,
743 struct drm_atomic_state *state)
745 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
748 if (crtc->state->color_mgmt_changed)
749 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
750 mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state);
751 mtk_ddp_ctm_set(mtk_crtc->ddp_comp[i], crtc->state);
753 mtk_drm_crtc_update_config(mtk_crtc, !!mtk_crtc->event);
756 static const struct drm_crtc_funcs mtk_crtc_funcs = {
757 .set_config = drm_atomic_helper_set_config,
758 .page_flip = drm_atomic_helper_page_flip,
759 .destroy = mtk_drm_crtc_destroy,
760 .reset = mtk_drm_crtc_reset,
761 .atomic_duplicate_state = mtk_drm_crtc_duplicate_state,
762 .atomic_destroy_state = mtk_drm_crtc_destroy_state,
763 .enable_vblank = mtk_drm_crtc_enable_vblank,
764 .disable_vblank = mtk_drm_crtc_disable_vblank,
767 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = {
768 .mode_fixup = mtk_drm_crtc_mode_fixup,
769 .mode_set_nofb = mtk_drm_crtc_mode_set_nofb,
770 .atomic_begin = mtk_drm_crtc_atomic_begin,
771 .atomic_flush = mtk_drm_crtc_atomic_flush,
772 .atomic_enable = mtk_drm_crtc_atomic_enable,
773 .atomic_disable = mtk_drm_crtc_atomic_disable,
776 static int mtk_drm_crtc_init(struct drm_device *drm,
777 struct mtk_drm_crtc *mtk_crtc,
780 struct drm_plane *primary = NULL;
781 struct drm_plane *cursor = NULL;
784 for (i = 0; i < mtk_crtc->layer_nr; i++) {
785 if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_PRIMARY)
786 primary = &mtk_crtc->planes[i];
787 else if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_CURSOR)
788 cursor = &mtk_crtc->planes[i];
791 ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor,
792 &mtk_crtc_funcs, NULL);
794 goto err_cleanup_crtc;
796 drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs);
801 drm_crtc_cleanup(&mtk_crtc->base);
805 static int mtk_drm_crtc_num_comp_planes(struct mtk_drm_crtc *mtk_crtc,
808 struct mtk_ddp_comp *comp;
813 comp = mtk_crtc->ddp_comp[comp_idx];
817 if (comp_idx == 1 && !comp->funcs->bgclr_in_on)
820 return mtk_ddp_comp_layer_nr(comp);
824 enum drm_plane_type mtk_drm_crtc_plane_type(unsigned int plane_idx,
825 unsigned int num_planes)
828 return DRM_PLANE_TYPE_PRIMARY;
829 else if (plane_idx == (num_planes - 1))
830 return DRM_PLANE_TYPE_CURSOR;
832 return DRM_PLANE_TYPE_OVERLAY;
836 static int mtk_drm_crtc_init_comp_planes(struct drm_device *drm_dev,
837 struct mtk_drm_crtc *mtk_crtc,
838 int comp_idx, int pipe)
840 int num_planes = mtk_drm_crtc_num_comp_planes(mtk_crtc, comp_idx);
841 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[comp_idx];
844 for (i = 0; i < num_planes; i++) {
845 ret = mtk_plane_init(drm_dev,
846 &mtk_crtc->planes[mtk_crtc->layer_nr],
848 mtk_drm_crtc_plane_type(mtk_crtc->layer_nr,
850 mtk_ddp_comp_supported_rotations(comp));
854 mtk_crtc->layer_nr++;
859 int mtk_drm_crtc_create(struct drm_device *drm_dev,
860 const enum mtk_ddp_comp_id *path, unsigned int path_len)
862 struct mtk_drm_private *priv = drm_dev->dev_private;
863 struct device *dev = drm_dev->dev;
864 struct mtk_drm_crtc *mtk_crtc;
865 unsigned int num_comp_planes = 0;
866 int pipe = priv->num_pipes;
869 bool has_ctm = false;
870 uint gamma_lut_size = 0;
875 for (i = 0; i < path_len; i++) {
876 enum mtk_ddp_comp_id comp_id = path[i];
877 struct device_node *node;
878 struct mtk_ddp_comp *comp;
880 node = priv->comp_node[comp_id];
881 comp = &priv->ddp_comp[comp_id];
885 "Not creating crtc %d because component %d is disabled or missing\n",
891 dev_err(dev, "Component %pOF not initialized\n", node);
896 mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL);
900 mtk_crtc->mmsys_dev = priv->mmsys_dev;
901 mtk_crtc->ddp_comp_nr = path_len;
902 mtk_crtc->ddp_comp = devm_kmalloc_array(dev, mtk_crtc->ddp_comp_nr,
903 sizeof(*mtk_crtc->ddp_comp),
905 if (!mtk_crtc->ddp_comp)
908 mtk_crtc->mutex = mtk_mutex_get(priv->mutex_dev);
909 if (IS_ERR(mtk_crtc->mutex)) {
910 ret = PTR_ERR(mtk_crtc->mutex);
911 dev_err(dev, "Failed to get mutex: %d\n", ret);
915 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
916 enum mtk_ddp_comp_id comp_id = path[i];
917 struct mtk_ddp_comp *comp;
919 comp = &priv->ddp_comp[comp_id];
920 mtk_crtc->ddp_comp[i] = comp;
923 if (comp->funcs->gamma_set)
924 gamma_lut_size = MTK_LUT_SIZE;
926 if (comp->funcs->ctm_set)
931 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
932 num_comp_planes += mtk_drm_crtc_num_comp_planes(mtk_crtc, i);
934 mtk_crtc->planes = devm_kcalloc(dev, num_comp_planes,
935 sizeof(struct drm_plane), GFP_KERNEL);
937 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
938 ret = mtk_drm_crtc_init_comp_planes(drm_dev, mtk_crtc, i,
944 ret = mtk_drm_crtc_init(drm_dev, mtk_crtc, pipe);
949 drm_mode_crtc_set_gamma_size(&mtk_crtc->base, gamma_lut_size);
950 drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, has_ctm, gamma_lut_size);
952 mutex_init(&mtk_crtc->hw_lock);
954 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
955 mtk_crtc->cmdq_client.client.dev = mtk_crtc->mmsys_dev;
956 mtk_crtc->cmdq_client.client.tx_block = false;
957 mtk_crtc->cmdq_client.client.knows_txdone = true;
958 mtk_crtc->cmdq_client.client.rx_callback = ddp_cmdq_cb;
959 mtk_crtc->cmdq_client.chan =
960 mbox_request_channel(&mtk_crtc->cmdq_client.client,
961 drm_crtc_index(&mtk_crtc->base));
962 if (IS_ERR(mtk_crtc->cmdq_client.chan)) {
963 dev_dbg(dev, "mtk_crtc %d failed to create mailbox client, writing register by CPU now\n",
964 drm_crtc_index(&mtk_crtc->base));
965 mtk_crtc->cmdq_client.chan = NULL;
968 if (mtk_crtc->cmdq_client.chan) {
969 ret = of_property_read_u32_index(priv->mutex_node,
970 "mediatek,gce-events",
971 drm_crtc_index(&mtk_crtc->base),
972 &mtk_crtc->cmdq_event);
974 dev_dbg(dev, "mtk_crtc %d failed to get mediatek,gce-events property\n",
975 drm_crtc_index(&mtk_crtc->base));
976 mbox_free_channel(mtk_crtc->cmdq_client.chan);
977 mtk_crtc->cmdq_client.chan = NULL;
979 ret = mtk_drm_cmdq_pkt_create(&mtk_crtc->cmdq_client,
980 &mtk_crtc->cmdq_handle,
983 dev_dbg(dev, "mtk_crtc %d failed to create cmdq packet\n",
984 drm_crtc_index(&mtk_crtc->base));
985 mbox_free_channel(mtk_crtc->cmdq_client.chan);
986 mtk_crtc->cmdq_client.chan = NULL;
990 /* for sending blocking cmd in crtc disable */
991 init_waitqueue_head(&mtk_crtc->cb_blocking_queue);