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>
10 #include <linux/pm_runtime.h>
11 #include <linux/soc/mediatek/mtk-cmdq.h>
12 #include <linux/soc/mediatek/mtk-mmsys.h>
13 #include <linux/soc/mediatek/mtk-mutex.h>
15 #include <asm/barrier.h>
17 #include <drm/drm_atomic.h>
18 #include <drm/drm_atomic_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 device *dma_dev;
63 struct mtk_mutex *mutex;
64 unsigned int ddp_comp_nr;
65 struct mtk_ddp_comp **ddp_comp;
66 unsigned int num_conn_routes;
67 const struct mtk_drm_route *conn_routes;
69 /* lock for display hardware access */
74 struct mtk_crtc_state {
75 struct drm_crtc_state base;
78 unsigned int pending_width;
79 unsigned int pending_height;
80 unsigned int pending_vrefresh;
83 static inline struct mtk_drm_crtc *to_mtk_crtc(struct drm_crtc *c)
85 return container_of(c, struct mtk_drm_crtc, base);
88 static inline struct mtk_crtc_state *to_mtk_crtc_state(struct drm_crtc_state *s)
90 return container_of(s, struct mtk_crtc_state, base);
93 static void mtk_drm_crtc_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
95 struct drm_crtc *crtc = &mtk_crtc->base;
98 if (mtk_crtc->event) {
99 spin_lock_irqsave(&crtc->dev->event_lock, flags);
100 drm_crtc_send_vblank_event(crtc, mtk_crtc->event);
101 drm_crtc_vblank_put(crtc);
102 mtk_crtc->event = NULL;
103 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
107 static void mtk_drm_finish_page_flip(struct mtk_drm_crtc *mtk_crtc)
109 drm_crtc_handle_vblank(&mtk_crtc->base);
110 if (!mtk_crtc->config_updating && mtk_crtc->pending_needs_vblank) {
111 mtk_drm_crtc_finish_page_flip(mtk_crtc);
112 mtk_crtc->pending_needs_vblank = false;
116 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
117 static int mtk_drm_cmdq_pkt_create(struct cmdq_client *client, struct cmdq_pkt *pkt,
123 pkt->va_base = kzalloc(size, GFP_KERNEL);
127 pkt->buf_size = size;
128 pkt->cl = (void *)client;
130 dev = client->chan->mbox->dev;
131 dma_addr = dma_map_single(dev, pkt->va_base, pkt->buf_size,
133 if (dma_mapping_error(dev, dma_addr)) {
134 dev_err(dev, "dma map failed, size=%u\n", (u32)(u64)size);
139 pkt->pa_base = dma_addr;
144 static void mtk_drm_cmdq_pkt_destroy(struct cmdq_pkt *pkt)
146 struct cmdq_client *client = (struct cmdq_client *)pkt->cl;
148 dma_unmap_single(client->chan->mbox->dev, pkt->pa_base, pkt->buf_size,
154 static void mtk_drm_crtc_destroy(struct drm_crtc *crtc)
156 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
159 mtk_mutex_put(mtk_crtc->mutex);
160 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
161 mtk_drm_cmdq_pkt_destroy(&mtk_crtc->cmdq_handle);
163 if (mtk_crtc->cmdq_client.chan) {
164 mbox_free_channel(mtk_crtc->cmdq_client.chan);
165 mtk_crtc->cmdq_client.chan = NULL;
169 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
170 struct mtk_ddp_comp *comp;
172 comp = mtk_crtc->ddp_comp[i];
173 mtk_ddp_comp_unregister_vblank_cb(comp);
176 drm_crtc_cleanup(crtc);
179 static void mtk_drm_crtc_reset(struct drm_crtc *crtc)
181 struct mtk_crtc_state *state;
184 __drm_atomic_helper_crtc_destroy_state(crtc->state);
186 kfree(to_mtk_crtc_state(crtc->state));
189 state = kzalloc(sizeof(*state), GFP_KERNEL);
191 __drm_atomic_helper_crtc_reset(crtc, &state->base);
194 static struct drm_crtc_state *mtk_drm_crtc_duplicate_state(struct drm_crtc *crtc)
196 struct mtk_crtc_state *state;
198 state = kmalloc(sizeof(*state), GFP_KERNEL);
202 __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
204 WARN_ON(state->base.crtc != crtc);
205 state->base.crtc = crtc;
206 state->pending_config = false;
211 static void mtk_drm_crtc_destroy_state(struct drm_crtc *crtc,
212 struct drm_crtc_state *state)
214 __drm_atomic_helper_crtc_destroy_state(state);
215 kfree(to_mtk_crtc_state(state));
218 static enum drm_mode_status
219 mtk_drm_crtc_mode_valid(struct drm_crtc *crtc,
220 const struct drm_display_mode *mode)
222 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
223 enum drm_mode_status status = MODE_OK;
226 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
227 status = mtk_ddp_comp_mode_valid(mtk_crtc->ddp_comp[i], mode);
228 if (status != MODE_OK)
234 static bool mtk_drm_crtc_mode_fixup(struct drm_crtc *crtc,
235 const struct drm_display_mode *mode,
236 struct drm_display_mode *adjusted_mode)
238 /* Nothing to do here, but this callback is mandatory. */
242 static void mtk_drm_crtc_mode_set_nofb(struct drm_crtc *crtc)
244 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
246 state->pending_width = crtc->mode.hdisplay;
247 state->pending_height = crtc->mode.vdisplay;
248 state->pending_vrefresh = drm_mode_vrefresh(&crtc->mode);
249 wmb(); /* Make sure the above parameters are set before update */
250 state->pending_config = true;
253 static int mtk_crtc_ddp_clk_enable(struct mtk_drm_crtc *mtk_crtc)
258 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
259 ret = mtk_ddp_comp_clk_enable(mtk_crtc->ddp_comp[i]);
261 DRM_ERROR("Failed to enable clock %d: %d\n", i, ret);
269 mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]);
273 static void mtk_crtc_ddp_clk_disable(struct mtk_drm_crtc *mtk_crtc)
277 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
278 mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]);
282 struct mtk_ddp_comp *mtk_drm_ddp_comp_for_plane(struct drm_crtc *crtc,
283 struct drm_plane *plane,
284 unsigned int *local_layer)
286 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
287 struct mtk_ddp_comp *comp;
289 unsigned int local_index = plane - mtk_crtc->planes;
291 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
292 comp = mtk_crtc->ddp_comp[i];
293 if (local_index < (count + mtk_ddp_comp_layer_nr(comp))) {
294 *local_layer = local_index - count;
297 count += mtk_ddp_comp_layer_nr(comp);
300 WARN(1, "Failed to find component for plane %d\n", plane->index);
304 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
305 static void ddp_cmdq_cb(struct mbox_client *cl, void *mssg)
307 struct cmdq_cb_data *data = mssg;
308 struct cmdq_client *cmdq_cl = container_of(cl, struct cmdq_client, client);
309 struct mtk_drm_crtc *mtk_crtc = container_of(cmdq_cl, struct mtk_drm_crtc, cmdq_client);
310 struct mtk_crtc_state *state;
316 state = to_mtk_crtc_state(mtk_crtc->base.state);
318 state->pending_config = false;
320 if (mtk_crtc->pending_planes) {
321 for (i = 0; i < mtk_crtc->layer_nr; i++) {
322 struct drm_plane *plane = &mtk_crtc->planes[i];
323 struct mtk_plane_state *plane_state;
325 plane_state = to_mtk_plane_state(plane->state);
327 plane_state->pending.config = false;
329 mtk_crtc->pending_planes = false;
332 if (mtk_crtc->pending_async_planes) {
333 for (i = 0; i < mtk_crtc->layer_nr; i++) {
334 struct drm_plane *plane = &mtk_crtc->planes[i];
335 struct mtk_plane_state *plane_state;
337 plane_state = to_mtk_plane_state(plane->state);
339 plane_state->pending.async_config = false;
341 mtk_crtc->pending_async_planes = false;
344 mtk_crtc->cmdq_vblank_cnt = 0;
345 wake_up(&mtk_crtc->cb_blocking_queue);
349 static int mtk_crtc_ddp_hw_init(struct mtk_drm_crtc *mtk_crtc)
351 struct drm_crtc *crtc = &mtk_crtc->base;
352 struct drm_connector *connector;
353 struct drm_encoder *encoder;
354 struct drm_connector_list_iter conn_iter;
355 unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC;
359 if (WARN_ON(!crtc->state))
362 width = crtc->state->adjusted_mode.hdisplay;
363 height = crtc->state->adjusted_mode.vdisplay;
364 vrefresh = drm_mode_vrefresh(&crtc->state->adjusted_mode);
366 drm_for_each_encoder(encoder, crtc->dev) {
367 if (encoder->crtc != crtc)
370 drm_connector_list_iter_begin(crtc->dev, &conn_iter);
371 drm_for_each_connector_iter(connector, &conn_iter) {
372 if (connector->encoder != encoder)
374 if (connector->display_info.bpc != 0 &&
375 bpc > connector->display_info.bpc)
376 bpc = connector->display_info.bpc;
378 drm_connector_list_iter_end(&conn_iter);
381 ret = pm_runtime_resume_and_get(crtc->dev->dev);
383 DRM_ERROR("Failed to enable power domain: %d\n", ret);
387 ret = mtk_mutex_prepare(mtk_crtc->mutex);
389 DRM_ERROR("Failed to enable mutex clock: %d\n", ret);
390 goto err_pm_runtime_put;
393 ret = mtk_crtc_ddp_clk_enable(mtk_crtc);
395 DRM_ERROR("Failed to enable component clocks: %d\n", ret);
396 goto err_mutex_unprepare;
399 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
400 if (!mtk_ddp_comp_connect(mtk_crtc->ddp_comp[i], mtk_crtc->mmsys_dev,
401 mtk_crtc->ddp_comp[i + 1]->id))
402 mtk_mmsys_ddp_connect(mtk_crtc->mmsys_dev,
403 mtk_crtc->ddp_comp[i]->id,
404 mtk_crtc->ddp_comp[i + 1]->id);
405 if (!mtk_ddp_comp_add(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
406 mtk_mutex_add_comp(mtk_crtc->mutex,
407 mtk_crtc->ddp_comp[i]->id);
409 if (!mtk_ddp_comp_add(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
410 mtk_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
411 mtk_mutex_enable(mtk_crtc->mutex);
413 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
414 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i];
417 mtk_ddp_comp_bgclr_in_on(comp);
419 mtk_ddp_comp_config(comp, width, height, vrefresh, bpc, NULL);
420 mtk_ddp_comp_start(comp);
423 /* Initially configure all planes */
424 for (i = 0; i < mtk_crtc->layer_nr; i++) {
425 struct drm_plane *plane = &mtk_crtc->planes[i];
426 struct mtk_plane_state *plane_state;
427 struct mtk_ddp_comp *comp;
428 unsigned int local_layer;
430 plane_state = to_mtk_plane_state(plane->state);
432 /* should not enable layer before crtc enabled */
433 plane_state->pending.enable = false;
434 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer);
436 mtk_ddp_comp_layer_config(comp, local_layer,
443 mtk_mutex_unprepare(mtk_crtc->mutex);
445 pm_runtime_put(crtc->dev->dev);
449 static void mtk_crtc_ddp_hw_fini(struct mtk_drm_crtc *mtk_crtc)
451 struct drm_device *drm = mtk_crtc->base.dev;
452 struct drm_crtc *crtc = &mtk_crtc->base;
455 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
456 mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]);
458 mtk_ddp_comp_bgclr_in_off(mtk_crtc->ddp_comp[i]);
461 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
462 if (!mtk_ddp_comp_remove(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
463 mtk_mutex_remove_comp(mtk_crtc->mutex,
464 mtk_crtc->ddp_comp[i]->id);
465 mtk_mutex_disable(mtk_crtc->mutex);
466 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
467 if (!mtk_ddp_comp_disconnect(mtk_crtc->ddp_comp[i], mtk_crtc->mmsys_dev,
468 mtk_crtc->ddp_comp[i + 1]->id))
469 mtk_mmsys_ddp_disconnect(mtk_crtc->mmsys_dev,
470 mtk_crtc->ddp_comp[i]->id,
471 mtk_crtc->ddp_comp[i + 1]->id);
472 if (!mtk_ddp_comp_remove(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
473 mtk_mutex_remove_comp(mtk_crtc->mutex,
474 mtk_crtc->ddp_comp[i]->id);
476 if (!mtk_ddp_comp_remove(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
477 mtk_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
478 mtk_crtc_ddp_clk_disable(mtk_crtc);
479 mtk_mutex_unprepare(mtk_crtc->mutex);
481 pm_runtime_put(drm->dev);
483 if (crtc->state->event && !crtc->state->active) {
484 spin_lock_irq(&crtc->dev->event_lock);
485 drm_crtc_send_vblank_event(crtc, crtc->state->event);
486 crtc->state->event = NULL;
487 spin_unlock_irq(&crtc->dev->event_lock);
491 static void mtk_crtc_ddp_config(struct drm_crtc *crtc,
492 struct cmdq_pkt *cmdq_handle)
494 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
495 struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state);
496 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
498 unsigned int local_layer;
501 * TODO: instead of updating the registers here, we should prepare
502 * working registers in atomic_commit and let the hardware command
503 * queue update module registers on vblank.
505 if (state->pending_config) {
506 mtk_ddp_comp_config(comp, state->pending_width,
507 state->pending_height,
508 state->pending_vrefresh, 0,
512 state->pending_config = false;
515 if (mtk_crtc->pending_planes) {
516 for (i = 0; i < mtk_crtc->layer_nr; i++) {
517 struct drm_plane *plane = &mtk_crtc->planes[i];
518 struct mtk_plane_state *plane_state;
520 plane_state = to_mtk_plane_state(plane->state);
522 if (!plane_state->pending.config)
525 comp = mtk_drm_ddp_comp_for_plane(crtc, plane,
529 mtk_ddp_comp_layer_config(comp, local_layer,
533 plane_state->pending.config = false;
537 mtk_crtc->pending_planes = false;
540 if (mtk_crtc->pending_async_planes) {
541 for (i = 0; i < mtk_crtc->layer_nr; i++) {
542 struct drm_plane *plane = &mtk_crtc->planes[i];
543 struct mtk_plane_state *plane_state;
545 plane_state = to_mtk_plane_state(plane->state);
547 if (!plane_state->pending.async_config)
550 comp = mtk_drm_ddp_comp_for_plane(crtc, plane,
554 mtk_ddp_comp_layer_config(comp, local_layer,
558 plane_state->pending.async_config = false;
562 mtk_crtc->pending_async_planes = false;
566 static void mtk_drm_crtc_update_config(struct mtk_drm_crtc *mtk_crtc,
569 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
570 struct cmdq_pkt *cmdq_handle = &mtk_crtc->cmdq_handle;
572 struct drm_crtc *crtc = &mtk_crtc->base;
573 struct mtk_drm_private *priv = crtc->dev->dev_private;
574 unsigned int pending_planes = 0, pending_async_planes = 0;
577 mutex_lock(&mtk_crtc->hw_lock);
578 mtk_crtc->config_updating = true;
580 mtk_crtc->pending_needs_vblank = true;
582 for (i = 0; i < mtk_crtc->layer_nr; i++) {
583 struct drm_plane *plane = &mtk_crtc->planes[i];
584 struct mtk_plane_state *plane_state;
586 plane_state = to_mtk_plane_state(plane->state);
587 if (plane_state->pending.dirty) {
588 plane_state->pending.config = true;
589 plane_state->pending.dirty = false;
590 pending_planes |= BIT(i);
591 } else if (plane_state->pending.async_dirty) {
592 plane_state->pending.async_config = true;
593 plane_state->pending.async_dirty = false;
594 pending_async_planes |= BIT(i);
598 mtk_crtc->pending_planes = true;
599 if (pending_async_planes)
600 mtk_crtc->pending_async_planes = true;
602 if (priv->data->shadow_register) {
603 mtk_mutex_acquire(mtk_crtc->mutex);
604 mtk_crtc_ddp_config(crtc, NULL);
605 mtk_mutex_release(mtk_crtc->mutex);
607 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
608 if (mtk_crtc->cmdq_client.chan) {
609 mbox_flush(mtk_crtc->cmdq_client.chan, 2000);
610 cmdq_handle->cmd_buf_size = 0;
611 cmdq_pkt_clear_event(cmdq_handle, mtk_crtc->cmdq_event);
612 cmdq_pkt_wfe(cmdq_handle, mtk_crtc->cmdq_event, false);
613 mtk_crtc_ddp_config(crtc, cmdq_handle);
614 cmdq_pkt_finalize(cmdq_handle);
615 dma_sync_single_for_device(mtk_crtc->cmdq_client.chan->mbox->dev,
616 cmdq_handle->pa_base,
617 cmdq_handle->cmd_buf_size,
620 * CMDQ command should execute in next 3 vblank.
621 * One vblank interrupt before send message (occasionally)
622 * and one vblank interrupt after cmdq done,
623 * so it's timeout after 3 vblank interrupt.
624 * If it fail to execute in next 3 vblank, timeout happen.
626 mtk_crtc->cmdq_vblank_cnt = 3;
628 mbox_send_message(mtk_crtc->cmdq_client.chan, cmdq_handle);
629 mbox_client_txdone(mtk_crtc->cmdq_client.chan, 0);
632 mtk_crtc->config_updating = false;
633 mutex_unlock(&mtk_crtc->hw_lock);
636 static void mtk_crtc_ddp_irq(void *data)
638 struct drm_crtc *crtc = data;
639 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
640 struct mtk_drm_private *priv = crtc->dev->dev_private;
642 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
643 if (!priv->data->shadow_register && !mtk_crtc->cmdq_client.chan)
644 mtk_crtc_ddp_config(crtc, NULL);
645 else if (mtk_crtc->cmdq_vblank_cnt > 0 && --mtk_crtc->cmdq_vblank_cnt == 0)
646 DRM_ERROR("mtk_crtc %d CMDQ execute command timeout!\n",
647 drm_crtc_index(&mtk_crtc->base));
649 if (!priv->data->shadow_register)
650 mtk_crtc_ddp_config(crtc, NULL);
652 mtk_drm_finish_page_flip(mtk_crtc);
655 static int mtk_drm_crtc_enable_vblank(struct drm_crtc *crtc)
657 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
658 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
660 mtk_ddp_comp_enable_vblank(comp);
665 static void mtk_drm_crtc_disable_vblank(struct drm_crtc *crtc)
667 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
668 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
670 mtk_ddp_comp_disable_vblank(comp);
673 static void mtk_drm_crtc_update_output(struct drm_crtc *crtc,
674 struct drm_atomic_state *state)
676 int crtc_index = drm_crtc_index(crtc);
679 struct drm_crtc_state *crtc_state = state->crtcs[crtc_index].new_state;
680 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
681 struct mtk_drm_private *priv;
682 unsigned int encoder_mask = crtc_state->encoder_mask;
684 if (!crtc_state->connectors_changed)
687 if (!mtk_crtc->num_conn_routes)
690 priv = ((struct mtk_drm_private *)crtc->dev->dev_private)->all_drm_private[crtc_index];
693 dev_dbg(dev, "connector change:%d, encoder mask:0x%x for crtc:%d\n",
694 crtc_state->connectors_changed, encoder_mask, crtc_index);
696 for (i = 0; i < mtk_crtc->num_conn_routes; i++) {
697 unsigned int comp_id = mtk_crtc->conn_routes[i].route_ddp;
698 struct mtk_ddp_comp *comp = &priv->ddp_comp[comp_id];
700 if (comp->encoder_index >= 0 &&
701 (encoder_mask & BIT(comp->encoder_index))) {
702 mtk_crtc->ddp_comp[mtk_crtc->ddp_comp_nr - 1] = comp;
703 dev_dbg(dev, "Add comp_id: %d at path index %d\n",
704 comp->id, mtk_crtc->ddp_comp_nr - 1);
710 int mtk_drm_crtc_plane_check(struct drm_crtc *crtc, struct drm_plane *plane,
711 struct mtk_plane_state *state)
713 unsigned int local_layer;
714 struct mtk_ddp_comp *comp;
716 comp = mtk_drm_ddp_comp_for_plane(crtc, plane, &local_layer);
718 return mtk_ddp_comp_layer_check(comp, local_layer, state);
722 void mtk_drm_crtc_async_update(struct drm_crtc *crtc, struct drm_plane *plane,
723 struct drm_atomic_state *state)
725 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
727 if (!mtk_crtc->enabled)
730 mtk_drm_crtc_update_config(mtk_crtc, false);
733 static void mtk_drm_crtc_atomic_enable(struct drm_crtc *crtc,
734 struct drm_atomic_state *state)
736 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
737 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
740 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
742 ret = mtk_ddp_comp_power_on(comp);
744 DRM_DEV_ERROR(comp->dev, "Failed to enable power domain: %d\n", ret);
748 mtk_drm_crtc_update_output(crtc, state);
750 ret = mtk_crtc_ddp_hw_init(mtk_crtc);
752 mtk_ddp_comp_power_off(comp);
756 drm_crtc_vblank_on(crtc);
757 mtk_crtc->enabled = true;
760 static void mtk_drm_crtc_atomic_disable(struct drm_crtc *crtc,
761 struct drm_atomic_state *state)
763 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
764 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
767 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
768 if (!mtk_crtc->enabled)
771 /* Set all pending plane state to disabled */
772 for (i = 0; i < mtk_crtc->layer_nr; i++) {
773 struct drm_plane *plane = &mtk_crtc->planes[i];
774 struct mtk_plane_state *plane_state;
776 plane_state = to_mtk_plane_state(plane->state);
777 plane_state->pending.enable = false;
778 plane_state->pending.config = true;
780 mtk_crtc->pending_planes = true;
782 mtk_drm_crtc_update_config(mtk_crtc, false);
783 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
784 /* Wait for planes to be disabled by cmdq */
785 if (mtk_crtc->cmdq_client.chan)
786 wait_event_timeout(mtk_crtc->cb_blocking_queue,
787 mtk_crtc->cmdq_vblank_cnt == 0,
788 msecs_to_jiffies(500));
790 /* Wait for planes to be disabled */
791 drm_crtc_wait_one_vblank(crtc);
793 drm_crtc_vblank_off(crtc);
794 mtk_crtc_ddp_hw_fini(mtk_crtc);
795 mtk_ddp_comp_power_off(comp);
797 mtk_crtc->enabled = false;
800 static void mtk_drm_crtc_atomic_begin(struct drm_crtc *crtc,
801 struct drm_atomic_state *state)
803 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
805 struct mtk_crtc_state *mtk_crtc_state = to_mtk_crtc_state(crtc_state);
806 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
809 if (mtk_crtc->event && mtk_crtc_state->base.event)
810 DRM_ERROR("new event while there is still a pending event\n");
812 if (mtk_crtc_state->base.event) {
813 mtk_crtc_state->base.event->pipe = drm_crtc_index(crtc);
814 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
816 spin_lock_irqsave(&crtc->dev->event_lock, flags);
817 mtk_crtc->event = mtk_crtc_state->base.event;
818 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
820 mtk_crtc_state->base.event = NULL;
824 static void mtk_drm_crtc_atomic_flush(struct drm_crtc *crtc,
825 struct drm_atomic_state *state)
827 struct mtk_drm_crtc *mtk_crtc = to_mtk_crtc(crtc);
830 if (crtc->state->color_mgmt_changed)
831 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
832 mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state);
833 mtk_ddp_ctm_set(mtk_crtc->ddp_comp[i], crtc->state);
835 mtk_drm_crtc_update_config(mtk_crtc, !!mtk_crtc->event);
838 static const struct drm_crtc_funcs mtk_crtc_funcs = {
839 .set_config = drm_atomic_helper_set_config,
840 .page_flip = drm_atomic_helper_page_flip,
841 .destroy = mtk_drm_crtc_destroy,
842 .reset = mtk_drm_crtc_reset,
843 .atomic_duplicate_state = mtk_drm_crtc_duplicate_state,
844 .atomic_destroy_state = mtk_drm_crtc_destroy_state,
845 .enable_vblank = mtk_drm_crtc_enable_vblank,
846 .disable_vblank = mtk_drm_crtc_disable_vblank,
849 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = {
850 .mode_fixup = mtk_drm_crtc_mode_fixup,
851 .mode_set_nofb = mtk_drm_crtc_mode_set_nofb,
852 .mode_valid = mtk_drm_crtc_mode_valid,
853 .atomic_begin = mtk_drm_crtc_atomic_begin,
854 .atomic_flush = mtk_drm_crtc_atomic_flush,
855 .atomic_enable = mtk_drm_crtc_atomic_enable,
856 .atomic_disable = mtk_drm_crtc_atomic_disable,
859 static int mtk_drm_crtc_init(struct drm_device *drm,
860 struct mtk_drm_crtc *mtk_crtc,
863 struct drm_plane *primary = NULL;
864 struct drm_plane *cursor = NULL;
867 for (i = 0; i < mtk_crtc->layer_nr; i++) {
868 if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_PRIMARY)
869 primary = &mtk_crtc->planes[i];
870 else if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_CURSOR)
871 cursor = &mtk_crtc->planes[i];
874 ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor,
875 &mtk_crtc_funcs, NULL);
877 goto err_cleanup_crtc;
879 drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs);
884 drm_crtc_cleanup(&mtk_crtc->base);
888 static int mtk_drm_crtc_num_comp_planes(struct mtk_drm_crtc *mtk_crtc,
891 struct mtk_ddp_comp *comp;
896 comp = mtk_crtc->ddp_comp[comp_idx];
900 if (comp_idx == 1 && !comp->funcs->bgclr_in_on)
903 return mtk_ddp_comp_layer_nr(comp);
907 enum drm_plane_type mtk_drm_crtc_plane_type(unsigned int plane_idx,
908 unsigned int num_planes)
911 return DRM_PLANE_TYPE_PRIMARY;
912 else if (plane_idx == (num_planes - 1))
913 return DRM_PLANE_TYPE_CURSOR;
915 return DRM_PLANE_TYPE_OVERLAY;
919 static int mtk_drm_crtc_init_comp_planes(struct drm_device *drm_dev,
920 struct mtk_drm_crtc *mtk_crtc,
921 int comp_idx, int pipe)
923 int num_planes = mtk_drm_crtc_num_comp_planes(mtk_crtc, comp_idx);
924 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[comp_idx];
927 for (i = 0; i < num_planes; i++) {
928 ret = mtk_plane_init(drm_dev,
929 &mtk_crtc->planes[mtk_crtc->layer_nr],
931 mtk_drm_crtc_plane_type(mtk_crtc->layer_nr,
933 mtk_ddp_comp_supported_rotations(comp),
934 mtk_ddp_comp_get_formats(comp),
935 mtk_ddp_comp_get_num_formats(comp));
939 mtk_crtc->layer_nr++;
944 struct device *mtk_drm_crtc_dma_dev_get(struct drm_crtc *crtc)
946 struct mtk_drm_crtc *mtk_crtc = NULL;
951 mtk_crtc = to_mtk_crtc(crtc);
955 return mtk_crtc->dma_dev;
958 int mtk_drm_crtc_create(struct drm_device *drm_dev,
959 const unsigned int *path, unsigned int path_len,
960 int priv_data_index, const struct mtk_drm_route *conn_routes,
961 unsigned int num_conn_routes)
963 struct mtk_drm_private *priv = drm_dev->dev_private;
964 struct device *dev = drm_dev->dev;
965 struct mtk_drm_crtc *mtk_crtc;
966 unsigned int num_comp_planes = 0;
969 bool has_ctm = false;
970 uint gamma_lut_size = 0;
971 struct drm_crtc *tmp;
977 priv = priv->all_drm_private[priv_data_index];
979 drm_for_each_crtc(tmp, drm_dev)
982 for (i = 0; i < path_len; i++) {
983 enum mtk_ddp_comp_id comp_id = path[i];
984 struct device_node *node;
985 struct mtk_ddp_comp *comp;
987 node = priv->comp_node[comp_id];
988 comp = &priv->ddp_comp[comp_id];
990 /* Not all drm components have a DTS device node, such as ovl_adaptor,
991 * which is the drm bring up sub driver
993 if (!node && comp_id != DDP_COMPONENT_DRM_OVL_ADAPTOR) {
995 "Not creating crtc %d because component %d is disabled or missing\n",
1001 dev_err(dev, "Component %pOF not initialized\n", node);
1006 mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL);
1010 mtk_crtc->mmsys_dev = priv->mmsys_dev;
1011 mtk_crtc->ddp_comp_nr = path_len;
1012 mtk_crtc->ddp_comp = devm_kmalloc_array(dev,
1013 mtk_crtc->ddp_comp_nr + (conn_routes ? 1 : 0),
1014 sizeof(*mtk_crtc->ddp_comp),
1016 if (!mtk_crtc->ddp_comp)
1019 mtk_crtc->mutex = mtk_mutex_get(priv->mutex_dev);
1020 if (IS_ERR(mtk_crtc->mutex)) {
1021 ret = PTR_ERR(mtk_crtc->mutex);
1022 dev_err(dev, "Failed to get mutex: %d\n", ret);
1026 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
1027 unsigned int comp_id = path[i];
1028 struct mtk_ddp_comp *comp;
1030 comp = &priv->ddp_comp[comp_id];
1031 mtk_crtc->ddp_comp[i] = comp;
1034 if (comp->funcs->gamma_set && comp->funcs->gamma_get_lut_size) {
1035 unsigned int lut_sz = mtk_ddp_gamma_get_lut_size(comp);
1038 gamma_lut_size = lut_sz;
1041 if (comp->funcs->ctm_set)
1045 mtk_ddp_comp_register_vblank_cb(comp, mtk_crtc_ddp_irq,
1049 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
1050 num_comp_planes += mtk_drm_crtc_num_comp_planes(mtk_crtc, i);
1052 mtk_crtc->planes = devm_kcalloc(dev, num_comp_planes,
1053 sizeof(struct drm_plane), GFP_KERNEL);
1054 if (!mtk_crtc->planes)
1057 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
1058 ret = mtk_drm_crtc_init_comp_planes(drm_dev, mtk_crtc, i,
1065 * Default to use the first component as the dma dev.
1066 * In the case of ovl_adaptor sub driver, it needs to use the
1067 * dma_dev_get function to get representative dma dev.
1069 mtk_crtc->dma_dev = mtk_ddp_comp_dma_dev_get(&priv->ddp_comp[path[0]]);
1071 ret = mtk_drm_crtc_init(drm_dev, mtk_crtc, crtc_i);
1076 drm_mode_crtc_set_gamma_size(&mtk_crtc->base, gamma_lut_size);
1077 drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, has_ctm, gamma_lut_size);
1078 mutex_init(&mtk_crtc->hw_lock);
1080 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
1081 i = priv->mbox_index++;
1082 mtk_crtc->cmdq_client.client.dev = mtk_crtc->mmsys_dev;
1083 mtk_crtc->cmdq_client.client.tx_block = false;
1084 mtk_crtc->cmdq_client.client.knows_txdone = true;
1085 mtk_crtc->cmdq_client.client.rx_callback = ddp_cmdq_cb;
1086 mtk_crtc->cmdq_client.chan =
1087 mbox_request_channel(&mtk_crtc->cmdq_client.client, i);
1088 if (IS_ERR(mtk_crtc->cmdq_client.chan)) {
1089 dev_dbg(dev, "mtk_crtc %d failed to create mailbox client, writing register by CPU now\n",
1090 drm_crtc_index(&mtk_crtc->base));
1091 mtk_crtc->cmdq_client.chan = NULL;
1094 if (mtk_crtc->cmdq_client.chan) {
1095 ret = of_property_read_u32_index(priv->mutex_node,
1096 "mediatek,gce-events",
1098 &mtk_crtc->cmdq_event);
1100 dev_dbg(dev, "mtk_crtc %d failed to get mediatek,gce-events property\n",
1101 drm_crtc_index(&mtk_crtc->base));
1102 mbox_free_channel(mtk_crtc->cmdq_client.chan);
1103 mtk_crtc->cmdq_client.chan = NULL;
1105 ret = mtk_drm_cmdq_pkt_create(&mtk_crtc->cmdq_client,
1106 &mtk_crtc->cmdq_handle,
1109 dev_dbg(dev, "mtk_crtc %d failed to create cmdq packet\n",
1110 drm_crtc_index(&mtk_crtc->base));
1111 mbox_free_channel(mtk_crtc->cmdq_client.chan);
1112 mtk_crtc->cmdq_client.chan = NULL;
1116 /* for sending blocking cmd in crtc disable */
1117 init_waitqueue_head(&mtk_crtc->cb_blocking_queue);
1122 for (i = 0; i < num_conn_routes; i++) {
1123 unsigned int comp_id = conn_routes[i].route_ddp;
1124 struct device_node *node = priv->comp_node[comp_id];
1125 struct mtk_ddp_comp *comp = &priv->ddp_comp[comp_id];
1128 dev_dbg(dev, "comp_id:%d, Component %pOF not initialized\n",
1130 /* mark encoder_index to -1, if route comp device is not enabled */
1131 comp->encoder_index = -1;
1135 mtk_ddp_comp_encoder_index_set(&priv->ddp_comp[comp_id]);
1138 mtk_crtc->num_conn_routes = num_conn_routes;
1139 mtk_crtc->conn_routes = conn_routes;
1141 /* increase ddp_comp_nr at the end of mtk_drm_crtc_create */
1142 mtk_crtc->ddp_comp_nr++;