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>
15 #include <soc/mediatek/smi.h>
17 #include <drm/drm_atomic.h>
18 #include <drm/drm_atomic_helper.h>
19 #include <drm/drm_plane_helper.h>
20 #include <drm/drm_probe_helper.h>
21 #include <drm/drm_vblank.h>
23 #include "mtk_drm_drv.h"
24 #include "mtk_drm_crtc.h"
25 #include "mtk_drm_ddp_comp.h"
26 #include "mtk_drm_gem.h"
27 #include "mtk_drm_plane.h"
30 * struct mtk_drm_crtc - MediaTek specific crtc structure.
32 * @enabled: records whether crtc_enable succeeded
33 * @planes: array of 4 drm_plane structures, one for each overlay plane
34 * @pending_planes: whether any plane has pending changes to be applied
35 * @mmsys_dev: pointer to the mmsys device for configuration registers
36 * @mutex: handle to one of the ten disp_mutex streams
37 * @ddp_comp_nr: number of components in ddp_comp
38 * @ddp_comp: array of pointers the mtk_ddp_comp structures used by this crtc
40 * TODO: Needs update: this header is missing a bunch of member descriptions.
46 bool pending_needs_vblank;
47 struct drm_pending_vblank_event *event;
49 struct drm_plane *planes;
50 unsigned int layer_nr;
52 bool pending_async_planes;
54 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
55 struct mbox_client cmdq_cl;
56 struct mbox_chan *cmdq_chan;
60 struct device *mmsys_dev;
61 struct mtk_mutex *mutex;
62 unsigned int ddp_comp_nr;
63 struct mtk_ddp_comp **ddp_comp;
65 /* lock for display hardware access */
70 struct mtk_crtc_state {
71 struct drm_crtc_state base;
74 unsigned int pending_width;
75 unsigned int pending_height;
76 unsigned int pending_vrefresh;
79 static inline struct mtk_drm_crtc *to_mtk_crtc(struct drm_crtc *c)
81 return container_of(c, struct mtk_drm_crtc, base);
84 static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s)
86 return container_of(s, struct mtk_crtc_state, base);
89 static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
91 struct drm_crtc *crtc = &mtk_crtc->base;
94 spin_lock_irqsave(&crtc->dev->event_lock, flags);
95 drm_crtc_send_vblank_event(crtc, mtk_crtc->event);
96 drm_crtc_vblank_put(crtc);
97 mtk_crtc->event = NULL;
98 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
101 static void mtk_drm_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
103 drm_crtc_handle_vblank(&mtk_crtc->base);
104 if (!mtk_crtc->config_updating && mtk_crtc->pending_needs_vblank) {
105 mtk_drm_crtc_finish_page_flip(mtk_crtc);
106 mtk_crtc->pending_needs_vblank = false;
110 static void mtk_drm_crtc_destroy(struct drm_crtc *crtc)
112 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
114 mtk_mutex_put(mtk_crtc->mutex);
116 drm_crtc_cleanup(crtc);
119 static void mtk_drm_crtc_reset(struct drm_crtc *crtc)
121 struct mtk_crtc_state *state;
124 __drm_atomic_helper_crtc_destroy_state(crtc->state);
126 kfree(to_mtk_crtc_state(crtc->state));
129 state = kzalloc(sizeof(*state), GFP_KERNEL);
131 __drm_atomic_helper_crtc_reset(crtc, &state->base);
134 static struct drm_crtc_state *mtk_drm_crtc_duplicate_state(struct drm_crtc *crtc)
136 struct mtk_crtc_state *state;
138 state = kzalloc(sizeof(*state), GFP_KERNEL);
142 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
144 WARN_ON(state->base.crtc != crtc);
145 state->base.crtc = crtc;
150 static void mtk_drm_crtc_destroy_state(struct drm_crtc *crtc,
151 struct drm_crtc_state *state)
153 __drm_atomic_helper_crtc_destroy_state(state);
154 kfree(to_mtk_crtc_state(state));
157 static bool mtk_drm_crtc_mode_fixup(struct drm_crtc *crtc,
158 const struct drm_display_mode *mode,
159 struct drm_display_mode *adjusted_mode)
161 /* Nothing to do here, but this callback is mandatory. */
165 static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
167 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
169 state->pending_width = crtc->mode.hdisplay;
170 state->pending_height = crtc->mode.vdisplay;
171 state->pending_vrefresh = drm_mode_vrefresh(&crtc->mode);
172 wmb(); /* Make sure the above parameters are set before update */
173 state->pending_config = true;
176 static int mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc *mtk_crtc)
181 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
182 ret = mtk_ddp_comp_clk_enable(mtk_crtc->ddp_comp[i]);
184 DRM_ERROR("Failed to enable clock %d: %d\n", i, ret);
192 mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]);
196 static void mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc *mtk_crtc)
200 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
201 mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]);
205 struct mtk_ddp_comp *mtk_drm_ddp_comp_for_plane(struct drm_crtc *crtc,
206 struct drm_plane *plane,
207 unsigned int *local_layer)
209 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
210 struct mtk_ddp_comp *comp;
212 unsigned int local_index = plane - mtk_crtc->planes;
214 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
215 comp = mtk_crtc->ddp_comp[i];
216 if (local_index < (count + mtk_ddp_comp_layer_nr(comp))) {
217 *local_layer = local_index - count;
220 count += mtk_ddp_comp_layer_nr(comp);
223 WARN(1, "Failed to find component for plane %d\n", plane->index);
227 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
228 static struct cmdq_pkt *mtk_drm_cmdq_pkt_create(struct mbox_chan *chan, size_t size)
230 struct cmdq_pkt *pkt;
234 pkt = kzalloc(sizeof(*pkt), GFP_KERNEL);
236 return ERR_PTR(-ENOMEM);
237 pkt->va_base = kzalloc(size, GFP_KERNEL);
240 return ERR_PTR(-ENOMEM);
242 pkt->buf_size = size;
244 dev = chan->mbox->dev;
245 dma_addr = dma_map_single(dev, pkt->va_base, pkt->buf_size,
247 if (dma_mapping_error(dev, dma_addr)) {
248 dev_err(dev, "dma map failed, size=%u\n", (u32)(u64)size);
251 return ERR_PTR(-ENOMEM);
254 pkt->pa_base = dma_addr;
259 static void mtk_drm_cmdq_pkt_destroy(struct mbox_chan *chan, struct cmdq_pkt *pkt)
261 dma_unmap_single(chan->mbox->dev, pkt->pa_base, pkt->buf_size,
267 static void ddp_cmdq_cb(struct mbox_client *cl, void *mssg)
269 struct mtk_drm_crtc *mtk_crtc = container_of(cl, struct mtk_drm_crtc, cmdq_cl);
270 struct cmdq_cb_data *data = mssg;
272 mtk_drm_cmdq_pkt_destroy(mtk_crtc->cmdq_chan, data->pkt);
276 static int mtk_crtc_ddp_hw_init(struct mtk_drm_crtc *mtk_crtc)
278 struct drm_crtc *crtc = &mtk_crtc->base;
279 struct drm_connector *connector;
280 struct drm_encoder *encoder;
281 struct drm_connector_list_iter conn_iter;
282 unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC;
286 if (WARN_ON(!crtc->state))
289 width = crtc->state->adjusted_mode.hdisplay;
290 height = crtc->state->adjusted_mode.vdisplay;
291 vrefresh = drm_mode_vrefresh(&crtc->state->adjusted_mode);
293 drm_for_each_encoder(encoder, crtc->dev) {
294 if (encoder->crtc != crtc)
297 drm_connector_list_iter_begin(crtc->dev, &conn_iter);
298 drm_for_each_connector_iter(connector, &conn_iter) {
299 if (connector->encoder != encoder)
301 if (connector->display_info.bpc != 0 &&
302 bpc > connector->display_info.bpc)
303 bpc = connector->display_info.bpc;
305 drm_connector_list_iter_end(&conn_iter);
308 ret = pm_runtime_resume_and_get(crtc->dev->dev);
310 DRM_ERROR("Failed to enable power domain: %d\n", ret);
314 ret = mtk_mutex_prepare(mtk_crtc->mutex);
316 DRM_ERROR("Failed to enable mutex clock: %d\n", ret);
317 goto err_pm_runtime_put;
320 ret = mtk_crtc_ddp_clk_enable(mtk_crtc);
322 DRM_ERROR("Failed to enable component clocks: %d\n", ret);
323 goto err_mutex_unprepare;
326 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
327 mtk_mmsys_ddp_connect(mtk_crtc->mmsys_dev,
328 mtk_crtc->ddp_comp[i]->id,
329 mtk_crtc->ddp_comp[i + 1]->id);
330 mtk_mutex_add_comp(mtk_crtc->mutex,
331 mtk_crtc->ddp_comp[i]->id);
333 mtk_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
334 mtk_mutex_enable(mtk_crtc->mutex);
336 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
337 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i];
340 mtk_ddp_comp_bgclr_in_on(comp);
342 mtk_ddp_comp_config(comp, width, height, vrefresh, bpc, NULL);
343 mtk_ddp_comp_start(comp);
346 /* Initially configure all planes */
347 for (i = 0; i < mtk_crtc->layer_nr; i++) {
348 struct drm_plane *plane = &mtk_crtc->planes[i];
349 struct mtk_plane_state *plane_state;
350 struct mtk_ddp_comp *comp;
351 unsigned int local_layer;
353 plane_state = to_mtk_plane_state(plane->state);
354 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer);
356 mtk_ddp_comp_layer_config(comp, local_layer,
363 mtk_mutex_unprepare(mtk_crtc->mutex);
365 pm_runtime_put(crtc->dev->dev);
369 static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc)
371 struct drm_device *drm = mtk_crtc->base.dev;
372 struct drm_crtc *crtc = &mtk_crtc->base;
375 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
376 mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]);
378 mtk_ddp_comp_bgclr_in_off(mtk_crtc->ddp_comp[i]);
381 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
382 mtk_mutex_remove_comp(mtk_crtc->mutex,
383 mtk_crtc->ddp_comp[i]->id);
384 mtk_mutex_disable(mtk_crtc->mutex);
385 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
386 mtk_mmsys_ddp_disconnect(mtk_crtc->mmsys_dev,
387 mtk_crtc->ddp_comp[i]->id,
388 mtk_crtc->ddp_comp[i + 1]->id);
389 mtk_mutex_remove_comp(mtk_crtc->mutex,
390 mtk_crtc->ddp_comp[i]->id);
392 mtk_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
393 mtk_crtc_ddp_clk_disable(mtk_crtc);
394 mtk_mutex_unprepare(mtk_crtc->mutex);
396 pm_runtime_put(drm->dev);
398 if (crtc->state->event && !crtc->state->active) {
399 spin_lock_irq(&crtc->dev->event_lock);
400 drm_crtc_send_vblank_event(crtc, crtc->state->event);
401 crtc->state->event = NULL;
402 spin_unlock_irq(&crtc->dev->event_lock);
406 static void mtk_crtc_ddp_config(struct drm_crtc *crtc,
407 struct cmdq_pkt *cmdq_handle)
409 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
410 struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state);
411 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
413 unsigned int local_layer;
416 * TODO: instead of updating the registers here, we should prepare
417 * working registers in atomic_commit and let the hardware command
418 * queue update module registers on vblank.
420 if (state->pending_config) {
421 mtk_ddp_comp_config(comp, state->pending_width,
422 state->pending_height,
423 state->pending_vrefresh, 0,
426 state->pending_config = false;
429 if (mtk_crtc->pending_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.config)
439 comp = mtk_drm_ddp_comp_for_plane(crtc, plane,
443 mtk_ddp_comp_layer_config(comp, local_layer,
446 plane_state->pending.config = false;
448 mtk_crtc->pending_planes = false;
451 if (mtk_crtc->pending_async_planes) {
452 for (i = 0; i < mtk_crtc->layer_nr; i++) {
453 struct drm_plane *plane = &mtk_crtc->planes[i];
454 struct mtk_plane_state *plane_state;
456 plane_state = to_mtk_plane_state(plane->state);
458 if (!plane_state->pending.async_config)
461 comp = mtk_drm_ddp_comp_for_plane(crtc, plane,
465 mtk_ddp_comp_layer_config(comp, local_layer,
468 plane_state->pending.async_config = false;
470 mtk_crtc->pending_async_planes = false;
474 static void mtk_drm_crtc_update_config(struct mtk_drm_crtc *mtk_crtc,
477 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
478 struct cmdq_pkt *cmdq_handle;
480 struct drm_crtc *crtc = &mtk_crtc->base;
481 struct mtk_drm_private *priv = crtc->dev->dev_private;
482 unsigned int pending_planes = 0, pending_async_planes = 0;
485 mutex_lock(&mtk_crtc->hw_lock);
486 mtk_crtc->config_updating = true;
488 mtk_crtc->pending_needs_vblank = true;
490 for (i = 0; i < mtk_crtc->layer_nr; i++) {
491 struct drm_plane *plane = &mtk_crtc->planes[i];
492 struct mtk_plane_state *plane_state;
494 plane_state = to_mtk_plane_state(plane->state);
495 if (plane_state->pending.dirty) {
496 plane_state->pending.config = true;
497 plane_state->pending.dirty = false;
498 pending_planes |= BIT(i);
499 } else if (plane_state->pending.async_dirty) {
500 plane_state->pending.async_config = true;
501 plane_state->pending.async_dirty = false;
502 pending_async_planes |= BIT(i);
506 mtk_crtc->pending_planes = true;
507 if (pending_async_planes)
508 mtk_crtc->pending_async_planes = true;
510 if (priv->data->shadow_register) {
511 mtk_mutex_acquire(mtk_crtc->mutex);
512 mtk_crtc_ddp_config(crtc, NULL);
513 mtk_mutex_release(mtk_crtc->mutex);
515 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
516 if (mtk_crtc->cmdq_chan) {
517 mbox_flush(mtk_crtc->cmdq_chan, 2000);
518 cmdq_handle = mtk_drm_cmdq_pkt_create(mtk_crtc->cmdq_chan, PAGE_SIZE);
519 cmdq_pkt_clear_event(cmdq_handle, mtk_crtc->cmdq_event);
520 cmdq_pkt_wfe(cmdq_handle, mtk_crtc->cmdq_event, false);
521 mtk_crtc_ddp_config(crtc, cmdq_handle);
522 cmdq_pkt_finalize(cmdq_handle);
523 dma_sync_single_for_device(mtk_crtc->cmdq_chan->mbox->dev,
524 cmdq_handle->pa_base,
525 cmdq_handle->cmd_buf_size,
527 mbox_send_message(mtk_crtc->cmdq_chan, cmdq_handle);
528 mbox_client_txdone(mtk_crtc->cmdq_chan, 0);
531 mtk_crtc->config_updating = false;
532 mutex_unlock(&mtk_crtc->hw_lock);
535 static void mtk_crtc_ddp_irq(void *data)
537 struct drm_crtc *crtc = data;
538 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
539 struct mtk_drm_private *priv = crtc->dev->dev_private;
541 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
542 if (!priv->data->shadow_register && !mtk_crtc->cmdq_chan)
544 if (!priv->data->shadow_register)
546 mtk_crtc_ddp_config(crtc, NULL);
548 mtk_drm_finish_page_flip(mtk_crtc);
551 static int mtk_drm_crtc_enable_vblank(struct drm_crtc *crtc)
553 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
554 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
556 mtk_ddp_comp_enable_vblank(comp, mtk_crtc_ddp_irq, &mtk_crtc->base);
561 static void mtk_drm_crtc_disable_vblank(struct drm_crtc *crtc)
563 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
564 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
566 mtk_ddp_comp_disable_vblank(comp);
569 int mtk_drm_crtc_plane_check(struct drm_crtc *crtc, struct drm_plane *plane,
570 struct mtk_plane_state *state)
572 unsigned int local_layer;
573 struct mtk_ddp_comp *comp;
575 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer);
577 return mtk_ddp_comp_layer_check(comp, local_layer, state);
581 void mtk_drm_crtc_async_update(struct drm_crtc *crtc, struct drm_plane *plane,
582 struct drm_atomic_state *state)
584 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
585 const struct drm_plane_helper_funcs *plane_helper_funcs =
586 plane->helper_private;
588 if (!mtk_crtc->enabled)
591 plane_helper_funcs->atomic_update(plane, state);
592 mtk_drm_crtc_update_config(mtk_crtc, false);
595 static void mtk_drm_crtc_atomic_enable(struct drm_crtc *crtc,
596 struct drm_atomic_state *state)
598 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
599 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
602 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
604 ret = mtk_smi_larb_get(comp->larb_dev);
606 DRM_ERROR("Failed to get larb: %d\n", ret);
610 ret = mtk_crtc_ddp_hw_init(mtk_crtc);
612 mtk_smi_larb_put(comp->larb_dev);
616 drm_crtc_vblank_on(crtc);
617 mtk_crtc->enabled = true;
620 static void mtk_drm_crtc_atomic_disable(struct drm_crtc *crtc,
621 struct drm_atomic_state *state)
623 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
624 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
627 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
628 if (!mtk_crtc->enabled)
631 /* Set all pending plane state to disabled */
632 for (i = 0; i < mtk_crtc->layer_nr; i++) {
633 struct drm_plane *plane = &mtk_crtc->planes[i];
634 struct mtk_plane_state *plane_state;
636 plane_state = to_mtk_plane_state(plane->state);
637 plane_state->pending.enable = false;
638 plane_state->pending.config = true;
640 mtk_crtc->pending_planes = true;
642 mtk_drm_crtc_update_config(mtk_crtc, false);
643 /* Wait for planes to be disabled */
644 drm_crtc_wait_one_vblank(crtc);
646 drm_crtc_vblank_off(crtc);
647 mtk_crtc_ddp_hw_fini(mtk_crtc);
648 mtk_smi_larb_put(comp->larb_dev);
650 mtk_crtc->enabled = false;
653 static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc,
654 struct drm_atomic_state *state)
656 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
658 struct mtk_crtc_state *mtk_crtc_state = to_mtk_crtc_state(crtc_state);
659 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
661 if (mtk_crtc->event && mtk_crtc_state->base.event)
662 DRM_ERROR("new event while there is still a pending event\n");
664 if (mtk_crtc_state->base.event) {
665 mtk_crtc_state->base.event->pipe = drm_crtc_index(crtc);
666 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
667 mtk_crtc->event = mtk_crtc_state->base.event;
668 mtk_crtc_state->base.event = NULL;
672 static void mtk_drm_crtc_atomic_flush(struct drm_crtc *crtc,
673 struct drm_atomic_state *state)
675 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
678 if (crtc->state->color_mgmt_changed)
679 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
680 mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state);
681 mtk_ddp_ctm_set(mtk_crtc->ddp_comp[i], crtc->state);
683 mtk_drm_crtc_update_config(mtk_crtc, !!mtk_crtc->event);
686 static const struct drm_crtc_funcs mtk_crtc_funcs = {
687 .set_config = drm_atomic_helper_set_config,
688 .page_flip = drm_atomic_helper_page_flip,
689 .destroy = mtk_drm_crtc_destroy,
690 .reset = mtk_drm_crtc_reset,
691 .atomic_duplicate_state = mtk_drm_crtc_duplicate_state,
692 .atomic_destroy_state = mtk_drm_crtc_destroy_state,
693 .enable_vblank = mtk_drm_crtc_enable_vblank,
694 .disable_vblank = mtk_drm_crtc_disable_vblank,
697 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = {
698 .mode_fixup = mtk_drm_crtc_mode_fixup,
699 .mode_set_nofb = mtk_drm_crtc_mode_set_nofb,
700 .atomic_begin = mtk_drm_crtc_atomic_begin,
701 .atomic_flush = mtk_drm_crtc_atomic_flush,
702 .atomic_enable = mtk_drm_crtc_atomic_enable,
703 .atomic_disable = mtk_drm_crtc_atomic_disable,
706 static int mtk_drm_crtc_init(struct drm_device *drm,
707 struct mtk_drm_crtc *mtk_crtc,
710 struct drm_plane *primary = NULL;
711 struct drm_plane *cursor = NULL;
714 for (i = 0; i < mtk_crtc->layer_nr; i++) {
715 if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_PRIMARY)
716 primary = &mtk_crtc->planes[i];
717 else if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_CURSOR)
718 cursor = &mtk_crtc->planes[i];
721 ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor,
722 &mtk_crtc_funcs, NULL);
724 goto err_cleanup_crtc;
726 drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs);
731 drm_crtc_cleanup(&mtk_crtc->base);
735 static int mtk_drm_crtc_num_comp_planes(struct mtk_drm_crtc *mtk_crtc,
738 struct mtk_ddp_comp *comp;
743 comp = mtk_crtc->ddp_comp[comp_idx];
747 if (comp_idx == 1 && !comp->funcs->bgclr_in_on)
750 return mtk_ddp_comp_layer_nr(comp);
754 enum drm_plane_type mtk_drm_crtc_plane_type(unsigned int plane_idx,
755 unsigned int num_planes)
758 return DRM_PLANE_TYPE_PRIMARY;
759 else if (plane_idx == (num_planes - 1))
760 return DRM_PLANE_TYPE_CURSOR;
762 return DRM_PLANE_TYPE_OVERLAY;
766 static int mtk_drm_crtc_init_comp_planes(struct drm_device *drm_dev,
767 struct mtk_drm_crtc *mtk_crtc,
768 int comp_idx, int pipe)
770 int num_planes = mtk_drm_crtc_num_comp_planes(mtk_crtc, comp_idx);
771 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[comp_idx];
774 for (i = 0; i < num_planes; i++) {
775 ret = mtk_plane_init(drm_dev,
776 &mtk_crtc->planes[mtk_crtc->layer_nr],
778 mtk_drm_crtc_plane_type(mtk_crtc->layer_nr,
780 mtk_ddp_comp_supported_rotations(comp));
784 mtk_crtc->layer_nr++;
789 int mtk_drm_crtc_create(struct drm_device *drm_dev,
790 const enum mtk_ddp_comp_id *path, unsigned int path_len)
792 struct mtk_drm_private *priv = drm_dev->dev_private;
793 struct device *dev = drm_dev->dev;
794 struct mtk_drm_crtc *mtk_crtc;
795 unsigned int num_comp_planes = 0;
796 int pipe = priv->num_pipes;
799 bool has_ctm = false;
800 uint gamma_lut_size = 0;
805 for (i = 0; i < path_len; i++) {
806 enum mtk_ddp_comp_id comp_id = path[i];
807 struct device_node *node;
808 struct mtk_ddp_comp *comp;
810 node = priv->comp_node[comp_id];
811 comp = &priv->ddp_comp[comp_id];
815 "Not creating crtc %d because component %d is disabled or missing\n",
821 dev_err(dev, "Component %pOF not initialized\n", node);
826 mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL);
830 mtk_crtc->mmsys_dev = priv->mmsys_dev;
831 mtk_crtc->ddp_comp_nr = path_len;
832 mtk_crtc->ddp_comp = devm_kmalloc_array(dev, mtk_crtc->ddp_comp_nr,
833 sizeof(*mtk_crtc->ddp_comp),
835 if (!mtk_crtc->ddp_comp)
838 mtk_crtc->mutex = mtk_mutex_get(priv->mutex_dev);
839 if (IS_ERR(mtk_crtc->mutex)) {
840 ret = PTR_ERR(mtk_crtc->mutex);
841 dev_err(dev, "Failed to get mutex: %d\n", ret);
845 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
846 enum mtk_ddp_comp_id comp_id = path[i];
847 struct mtk_ddp_comp *comp;
849 comp = &priv->ddp_comp[comp_id];
850 mtk_crtc->ddp_comp[i] = comp;
853 if (comp->funcs->gamma_set)
854 gamma_lut_size = MTK_LUT_SIZE;
856 if (comp->funcs->ctm_set)
861 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
862 num_comp_planes += mtk_drm_crtc_num_comp_planes(mtk_crtc, i);
864 mtk_crtc->planes = devm_kcalloc(dev, num_comp_planes,
865 sizeof(struct drm_plane), GFP_KERNEL);
867 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
868 ret = mtk_drm_crtc_init_comp_planes(drm_dev, mtk_crtc, i,
874 ret = mtk_drm_crtc_init(drm_dev, mtk_crtc, pipe);
879 drm_mode_crtc_set_gamma_size(&mtk_crtc->base, gamma_lut_size);
880 drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, has_ctm, gamma_lut_size);
882 mutex_init(&mtk_crtc->hw_lock);
884 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
885 mtk_crtc->cmdq_cl.dev = mtk_crtc->mmsys_dev;
886 mtk_crtc->cmdq_cl.tx_block = false;
887 mtk_crtc->cmdq_cl.knows_txdone = true;
888 mtk_crtc->cmdq_cl.rx_callback = ddp_cmdq_cb;
889 mtk_crtc->cmdq_chan =
890 mbox_request_channel(&mtk_crtc->cmdq_cl,
891 drm_crtc_index(&mtk_crtc->base));
892 if (IS_ERR(mtk_crtc->cmdq_chan)) {
893 dev_dbg(dev, "mtk_crtc %d failed to create mailbox client, writing register by CPU now\n",
894 drm_crtc_index(&mtk_crtc->base));
895 mtk_crtc->cmdq_chan = NULL;
898 if (mtk_crtc->cmdq_chan) {
899 ret = of_property_read_u32_index(priv->mutex_node,
900 "mediatek,gce-events",
901 drm_crtc_index(&mtk_crtc->base),
902 &mtk_crtc->cmdq_event);
904 dev_dbg(dev, "mtk_crtc %d failed to get mediatek,gce-events property\n",
905 drm_crtc_index(&mtk_crtc->base));
906 mbox_free_channel(mtk_crtc->cmdq_chan);
907 mtk_crtc->cmdq_chan = NULL;