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>
23 #include "mtk_ddp_comp.h"
24 #include "mtk_drm_drv.h"
26 #include "mtk_plane.h"
29 * struct mtk_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_crtc *to_mtk_crtc(struct drm_crtc *c)
85 return container_of(c, struct mtk_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_crtc_finish_page_flip(struct mtk_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_crtc *mtk_crtc)
109 drm_crtc_handle_vblank(&mtk_crtc->base);
110 if (!mtk_crtc->config_updating && mtk_crtc->pending_needs_vblank) {
111 mtk_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_crtc_destroy(struct drm_crtc *crtc)
156 struct mtk_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_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_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_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_crtc_mode_valid(struct drm_crtc *crtc, const struct drm_display_mode *mode)
221 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
222 enum drm_mode_status status = MODE_OK;
225 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
226 status = mtk_ddp_comp_mode_valid(mtk_crtc->ddp_comp[i], mode);
227 if (status != MODE_OK)
233 static bool mtk_crtc_mode_fixup(struct drm_crtc *crtc,
234 const struct drm_display_mode *mode,
235 struct drm_display_mode *adjusted_mode)
237 /* Nothing to do here, but this callback is mandatory. */
241 static void mtk_crtc_mode_set_nofb(struct drm_crtc *crtc)
243 struct mtk_crtc_state *state = to_mtk_crtc_state(crtc->state);
245 state->pending_width = crtc->mode.hdisplay;
246 state->pending_height = crtc->mode.vdisplay;
247 state->pending_vrefresh = drm_mode_vrefresh(&crtc->mode);
248 wmb(); /* Make sure the above parameters are set before update */
249 state->pending_config = true;
252 static int mtk_crtc_ddp_clk_enable(struct mtk_crtc *mtk_crtc)
257 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
258 ret = mtk_ddp_comp_clk_enable(mtk_crtc->ddp_comp[i]);
260 DRM_ERROR("Failed to enable clock %d: %d\n", i, ret);
268 mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]);
272 static void mtk_crtc_ddp_clk_disable(struct mtk_crtc *mtk_crtc)
276 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
277 mtk_ddp_comp_clk_disable(mtk_crtc->ddp_comp[i]);
281 struct mtk_ddp_comp *mtk_ddp_comp_for_plane(struct drm_crtc *crtc,
282 struct drm_plane *plane,
283 unsigned int *local_layer)
285 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
286 struct mtk_ddp_comp *comp;
288 unsigned int local_index = plane - mtk_crtc->planes;
290 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
291 comp = mtk_crtc->ddp_comp[i];
292 if (local_index < (count + mtk_ddp_comp_layer_nr(comp))) {
293 *local_layer = local_index - count;
296 count += mtk_ddp_comp_layer_nr(comp);
299 WARN(1, "Failed to find component for plane %d\n", plane->index);
303 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
304 static void ddp_cmdq_cb(struct mbox_client *cl, void *mssg)
306 struct cmdq_cb_data *data = mssg;
307 struct cmdq_client *cmdq_cl = container_of(cl, struct cmdq_client, client);
308 struct mtk_crtc *mtk_crtc = container_of(cmdq_cl, struct mtk_crtc, cmdq_client);
309 struct mtk_crtc_state *state;
315 state = to_mtk_crtc_state(mtk_crtc->base.state);
317 state->pending_config = false;
319 if (mtk_crtc->pending_planes) {
320 for (i = 0; i < mtk_crtc->layer_nr; i++) {
321 struct drm_plane *plane = &mtk_crtc->planes[i];
322 struct mtk_plane_state *plane_state;
324 plane_state = to_mtk_plane_state(plane->state);
326 plane_state->pending.config = false;
328 mtk_crtc->pending_planes = false;
331 if (mtk_crtc->pending_async_planes) {
332 for (i = 0; i < mtk_crtc->layer_nr; i++) {
333 struct drm_plane *plane = &mtk_crtc->planes[i];
334 struct mtk_plane_state *plane_state;
336 plane_state = to_mtk_plane_state(plane->state);
338 plane_state->pending.async_config = false;
340 mtk_crtc->pending_async_planes = false;
343 mtk_crtc->cmdq_vblank_cnt = 0;
344 wake_up(&mtk_crtc->cb_blocking_queue);
348 static int mtk_crtc_ddp_hw_init(struct mtk_crtc *mtk_crtc)
350 struct drm_crtc *crtc = &mtk_crtc->base;
351 struct drm_connector *connector;
352 struct drm_encoder *encoder;
353 struct drm_connector_list_iter conn_iter;
354 unsigned int width, height, vrefresh, bpc = MTK_MAX_BPC;
358 if (WARN_ON(!crtc->state))
361 width = crtc->state->adjusted_mode.hdisplay;
362 height = crtc->state->adjusted_mode.vdisplay;
363 vrefresh = drm_mode_vrefresh(&crtc->state->adjusted_mode);
365 drm_for_each_encoder(encoder, crtc->dev) {
366 if (encoder->crtc != crtc)
369 drm_connector_list_iter_begin(crtc->dev, &conn_iter);
370 drm_for_each_connector_iter(connector, &conn_iter) {
371 if (connector->encoder != encoder)
373 if (connector->display_info.bpc != 0 &&
374 bpc > connector->display_info.bpc)
375 bpc = connector->display_info.bpc;
377 drm_connector_list_iter_end(&conn_iter);
380 ret = pm_runtime_resume_and_get(crtc->dev->dev);
382 DRM_ERROR("Failed to enable power domain: %d\n", ret);
386 ret = mtk_mutex_prepare(mtk_crtc->mutex);
388 DRM_ERROR("Failed to enable mutex clock: %d\n", ret);
389 goto err_pm_runtime_put;
392 ret = mtk_crtc_ddp_clk_enable(mtk_crtc);
394 DRM_ERROR("Failed to enable component clocks: %d\n", ret);
395 goto err_mutex_unprepare;
398 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
399 if (!mtk_ddp_comp_connect(mtk_crtc->ddp_comp[i], mtk_crtc->mmsys_dev,
400 mtk_crtc->ddp_comp[i + 1]->id))
401 mtk_mmsys_ddp_connect(mtk_crtc->mmsys_dev,
402 mtk_crtc->ddp_comp[i]->id,
403 mtk_crtc->ddp_comp[i + 1]->id);
404 if (!mtk_ddp_comp_add(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
405 mtk_mutex_add_comp(mtk_crtc->mutex,
406 mtk_crtc->ddp_comp[i]->id);
408 if (!mtk_ddp_comp_add(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
409 mtk_mutex_add_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
410 mtk_mutex_enable(mtk_crtc->mutex);
412 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
413 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[i];
416 mtk_ddp_comp_bgclr_in_on(comp);
418 mtk_ddp_comp_config(comp, width, height, vrefresh, bpc, NULL);
419 mtk_ddp_comp_start(comp);
422 /* Initially configure all planes */
423 for (i = 0; i < mtk_crtc->layer_nr; i++) {
424 struct drm_plane *plane = &mtk_crtc->planes[i];
425 struct mtk_plane_state *plane_state;
426 struct mtk_ddp_comp *comp;
427 unsigned int local_layer;
429 plane_state = to_mtk_plane_state(plane->state);
431 /* should not enable layer before crtc enabled */
432 plane_state->pending.enable = false;
433 comp = mtk_ddp_comp_for_plane(crtc, plane, &local_layer);
435 mtk_ddp_comp_layer_config(comp, local_layer,
442 mtk_mutex_unprepare(mtk_crtc->mutex);
444 pm_runtime_put(crtc->dev->dev);
448 static void mtk_crtc_ddp_hw_fini(struct mtk_crtc *mtk_crtc)
450 struct drm_device *drm = mtk_crtc->base.dev;
451 struct drm_crtc *crtc = &mtk_crtc->base;
454 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
455 mtk_ddp_comp_stop(mtk_crtc->ddp_comp[i]);
457 mtk_ddp_comp_bgclr_in_off(mtk_crtc->ddp_comp[i]);
460 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
461 if (!mtk_ddp_comp_remove(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
462 mtk_mutex_remove_comp(mtk_crtc->mutex,
463 mtk_crtc->ddp_comp[i]->id);
464 mtk_mutex_disable(mtk_crtc->mutex);
465 for (i = 0; i < mtk_crtc->ddp_comp_nr - 1; i++) {
466 if (!mtk_ddp_comp_disconnect(mtk_crtc->ddp_comp[i], mtk_crtc->mmsys_dev,
467 mtk_crtc->ddp_comp[i + 1]->id))
468 mtk_mmsys_ddp_disconnect(mtk_crtc->mmsys_dev,
469 mtk_crtc->ddp_comp[i]->id,
470 mtk_crtc->ddp_comp[i + 1]->id);
471 if (!mtk_ddp_comp_remove(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
472 mtk_mutex_remove_comp(mtk_crtc->mutex,
473 mtk_crtc->ddp_comp[i]->id);
475 if (!mtk_ddp_comp_remove(mtk_crtc->ddp_comp[i], mtk_crtc->mutex))
476 mtk_mutex_remove_comp(mtk_crtc->mutex, mtk_crtc->ddp_comp[i]->id);
477 mtk_crtc_ddp_clk_disable(mtk_crtc);
478 mtk_mutex_unprepare(mtk_crtc->mutex);
480 pm_runtime_put(drm->dev);
482 if (crtc->state->event && !crtc->state->active) {
483 spin_lock_irq(&crtc->dev->event_lock);
484 drm_crtc_send_vblank_event(crtc, crtc->state->event);
485 crtc->state->event = NULL;
486 spin_unlock_irq(&crtc->dev->event_lock);
490 static void mtk_crtc_ddp_config(struct drm_crtc *crtc,
491 struct cmdq_pkt *cmdq_handle)
493 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
494 struct mtk_crtc_state *state = to_mtk_crtc_state(mtk_crtc->base.state);
495 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
497 unsigned int local_layer;
500 * TODO: instead of updating the registers here, we should prepare
501 * working registers in atomic_commit and let the hardware command
502 * queue update module registers on vblank.
504 if (state->pending_config) {
505 mtk_ddp_comp_config(comp, state->pending_width,
506 state->pending_height,
507 state->pending_vrefresh, 0,
511 state->pending_config = false;
514 if (mtk_crtc->pending_planes) {
515 for (i = 0; i < mtk_crtc->layer_nr; i++) {
516 struct drm_plane *plane = &mtk_crtc->planes[i];
517 struct mtk_plane_state *plane_state;
519 plane_state = to_mtk_plane_state(plane->state);
521 if (!plane_state->pending.config)
524 comp = mtk_ddp_comp_for_plane(crtc, plane, &local_layer);
527 mtk_ddp_comp_layer_config(comp, local_layer,
531 plane_state->pending.config = false;
535 mtk_crtc->pending_planes = false;
538 if (mtk_crtc->pending_async_planes) {
539 for (i = 0; i < mtk_crtc->layer_nr; i++) {
540 struct drm_plane *plane = &mtk_crtc->planes[i];
541 struct mtk_plane_state *plane_state;
543 plane_state = to_mtk_plane_state(plane->state);
545 if (!plane_state->pending.async_config)
548 comp = mtk_ddp_comp_for_plane(crtc, plane, &local_layer);
551 mtk_ddp_comp_layer_config(comp, local_layer,
555 plane_state->pending.async_config = false;
559 mtk_crtc->pending_async_planes = false;
563 static void mtk_crtc_update_config(struct mtk_crtc *mtk_crtc, bool needs_vblank)
565 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
566 struct cmdq_pkt *cmdq_handle = &mtk_crtc->cmdq_handle;
568 struct drm_crtc *crtc = &mtk_crtc->base;
569 struct mtk_drm_private *priv = crtc->dev->dev_private;
570 unsigned int pending_planes = 0, pending_async_planes = 0;
573 mutex_lock(&mtk_crtc->hw_lock);
574 mtk_crtc->config_updating = true;
576 mtk_crtc->pending_needs_vblank = true;
578 for (i = 0; i < mtk_crtc->layer_nr; i++) {
579 struct drm_plane *plane = &mtk_crtc->planes[i];
580 struct mtk_plane_state *plane_state;
582 plane_state = to_mtk_plane_state(plane->state);
583 if (plane_state->pending.dirty) {
584 plane_state->pending.config = true;
585 plane_state->pending.dirty = false;
586 pending_planes |= BIT(i);
587 } else if (plane_state->pending.async_dirty) {
588 plane_state->pending.async_config = true;
589 plane_state->pending.async_dirty = false;
590 pending_async_planes |= BIT(i);
594 mtk_crtc->pending_planes = true;
595 if (pending_async_planes)
596 mtk_crtc->pending_async_planes = true;
598 if (priv->data->shadow_register) {
599 mtk_mutex_acquire(mtk_crtc->mutex);
600 mtk_crtc_ddp_config(crtc, NULL);
601 mtk_mutex_release(mtk_crtc->mutex);
603 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
604 if (mtk_crtc->cmdq_client.chan) {
605 mbox_flush(mtk_crtc->cmdq_client.chan, 2000);
606 cmdq_handle->cmd_buf_size = 0;
607 cmdq_pkt_clear_event(cmdq_handle, mtk_crtc->cmdq_event);
608 cmdq_pkt_wfe(cmdq_handle, mtk_crtc->cmdq_event, false);
609 mtk_crtc_ddp_config(crtc, cmdq_handle);
610 cmdq_pkt_finalize(cmdq_handle);
611 dma_sync_single_for_device(mtk_crtc->cmdq_client.chan->mbox->dev,
612 cmdq_handle->pa_base,
613 cmdq_handle->cmd_buf_size,
616 * CMDQ command should execute in next 3 vblank.
617 * One vblank interrupt before send message (occasionally)
618 * and one vblank interrupt after cmdq done,
619 * so it's timeout after 3 vblank interrupt.
620 * If it fail to execute in next 3 vblank, timeout happen.
622 mtk_crtc->cmdq_vblank_cnt = 3;
624 mbox_send_message(mtk_crtc->cmdq_client.chan, cmdq_handle);
625 mbox_client_txdone(mtk_crtc->cmdq_client.chan, 0);
628 mtk_crtc->config_updating = false;
629 mutex_unlock(&mtk_crtc->hw_lock);
632 static void mtk_crtc_ddp_irq(void *data)
634 struct drm_crtc *crtc = data;
635 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
636 struct mtk_drm_private *priv = crtc->dev->dev_private;
638 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
639 if (!priv->data->shadow_register && !mtk_crtc->cmdq_client.chan)
640 mtk_crtc_ddp_config(crtc, NULL);
641 else if (mtk_crtc->cmdq_vblank_cnt > 0 && --mtk_crtc->cmdq_vblank_cnt == 0)
642 DRM_ERROR("mtk_crtc %d CMDQ execute command timeout!\n",
643 drm_crtc_index(&mtk_crtc->base));
645 if (!priv->data->shadow_register)
646 mtk_crtc_ddp_config(crtc, NULL);
648 mtk_drm_finish_page_flip(mtk_crtc);
651 static int mtk_crtc_enable_vblank(struct drm_crtc *crtc)
653 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
654 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
656 mtk_ddp_comp_enable_vblank(comp);
661 static void mtk_crtc_disable_vblank(struct drm_crtc *crtc)
663 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
664 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
666 mtk_ddp_comp_disable_vblank(comp);
669 static void mtk_crtc_update_output(struct drm_crtc *crtc,
670 struct drm_atomic_state *state)
672 int crtc_index = drm_crtc_index(crtc);
675 struct drm_crtc_state *crtc_state = state->crtcs[crtc_index].new_state;
676 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
677 struct mtk_drm_private *priv;
678 unsigned int encoder_mask = crtc_state->encoder_mask;
680 if (!crtc_state->connectors_changed)
683 if (!mtk_crtc->num_conn_routes)
686 priv = ((struct mtk_drm_private *)crtc->dev->dev_private)->all_drm_private[crtc_index];
689 dev_dbg(dev, "connector change:%d, encoder mask:0x%x for crtc:%d\n",
690 crtc_state->connectors_changed, encoder_mask, crtc_index);
692 for (i = 0; i < mtk_crtc->num_conn_routes; i++) {
693 unsigned int comp_id = mtk_crtc->conn_routes[i].route_ddp;
694 struct mtk_ddp_comp *comp = &priv->ddp_comp[comp_id];
696 if (comp->encoder_index >= 0 &&
697 (encoder_mask & BIT(comp->encoder_index))) {
698 mtk_crtc->ddp_comp[mtk_crtc->ddp_comp_nr - 1] = comp;
699 dev_dbg(dev, "Add comp_id: %d at path index %d\n",
700 comp->id, mtk_crtc->ddp_comp_nr - 1);
706 int mtk_crtc_plane_check(struct drm_crtc *crtc, struct drm_plane *plane,
707 struct mtk_plane_state *state)
709 unsigned int local_layer;
710 struct mtk_ddp_comp *comp;
712 comp = mtk_ddp_comp_for_plane(crtc, plane, &local_layer);
714 return mtk_ddp_comp_layer_check(comp, local_layer, state);
718 void mtk_crtc_async_update(struct drm_crtc *crtc, struct drm_plane *plane,
719 struct drm_atomic_state *state)
721 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
723 if (!mtk_crtc->enabled)
726 mtk_crtc_update_config(mtk_crtc, false);
729 static void mtk_crtc_atomic_enable(struct drm_crtc *crtc,
730 struct drm_atomic_state *state)
732 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
733 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
736 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
738 ret = mtk_ddp_comp_power_on(comp);
740 DRM_DEV_ERROR(comp->dev, "Failed to enable power domain: %d\n", ret);
744 mtk_crtc_update_output(crtc, state);
746 ret = mtk_crtc_ddp_hw_init(mtk_crtc);
748 mtk_ddp_comp_power_off(comp);
752 drm_crtc_vblank_on(crtc);
753 mtk_crtc->enabled = true;
756 static void mtk_crtc_atomic_disable(struct drm_crtc *crtc,
757 struct drm_atomic_state *state)
759 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
760 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[0];
763 DRM_DEBUG_DRIVER("%s %d\n", __func__, crtc->base.id);
764 if (!mtk_crtc->enabled)
767 /* Set all pending plane state to disabled */
768 for (i = 0; i < mtk_crtc->layer_nr; i++) {
769 struct drm_plane *plane = &mtk_crtc->planes[i];
770 struct mtk_plane_state *plane_state;
772 plane_state = to_mtk_plane_state(plane->state);
773 plane_state->pending.enable = false;
774 plane_state->pending.config = true;
776 mtk_crtc->pending_planes = true;
778 mtk_crtc_update_config(mtk_crtc, false);
779 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
780 /* Wait for planes to be disabled by cmdq */
781 if (mtk_crtc->cmdq_client.chan)
782 wait_event_timeout(mtk_crtc->cb_blocking_queue,
783 mtk_crtc->cmdq_vblank_cnt == 0,
784 msecs_to_jiffies(500));
786 /* Wait for planes to be disabled */
787 drm_crtc_wait_one_vblank(crtc);
789 drm_crtc_vblank_off(crtc);
790 mtk_crtc_ddp_hw_fini(mtk_crtc);
791 mtk_ddp_comp_power_off(comp);
793 mtk_crtc->enabled = false;
796 static void mtk_crtc_atomic_begin(struct drm_crtc *crtc,
797 struct drm_atomic_state *state)
799 struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
801 struct mtk_crtc_state *mtk_crtc_state = to_mtk_crtc_state(crtc_state);
802 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
805 if (mtk_crtc->event && mtk_crtc_state->base.event)
806 DRM_ERROR("new event while there is still a pending event\n");
808 if (mtk_crtc_state->base.event) {
809 mtk_crtc_state->base.event->pipe = drm_crtc_index(crtc);
810 WARN_ON(drm_crtc_vblank_get(crtc) != 0);
812 spin_lock_irqsave(&crtc->dev->event_lock, flags);
813 mtk_crtc->event = mtk_crtc_state->base.event;
814 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
816 mtk_crtc_state->base.event = NULL;
820 static void mtk_crtc_atomic_flush(struct drm_crtc *crtc,
821 struct drm_atomic_state *state)
823 struct mtk_crtc *mtk_crtc = to_mtk_crtc(crtc);
826 if (crtc->state->color_mgmt_changed)
827 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
828 mtk_ddp_gamma_set(mtk_crtc->ddp_comp[i], crtc->state);
829 mtk_ddp_ctm_set(mtk_crtc->ddp_comp[i], crtc->state);
831 mtk_crtc_update_config(mtk_crtc, !!mtk_crtc->event);
834 static const struct drm_crtc_funcs mtk_crtc_funcs = {
835 .set_config = drm_atomic_helper_set_config,
836 .page_flip = drm_atomic_helper_page_flip,
837 .destroy = mtk_crtc_destroy,
838 .reset = mtk_crtc_reset,
839 .atomic_duplicate_state = mtk_crtc_duplicate_state,
840 .atomic_destroy_state = mtk_crtc_destroy_state,
841 .enable_vblank = mtk_crtc_enable_vblank,
842 .disable_vblank = mtk_crtc_disable_vblank,
845 static const struct drm_crtc_helper_funcs mtk_crtc_helper_funcs = {
846 .mode_fixup = mtk_crtc_mode_fixup,
847 .mode_set_nofb = mtk_crtc_mode_set_nofb,
848 .mode_valid = mtk_crtc_mode_valid,
849 .atomic_begin = mtk_crtc_atomic_begin,
850 .atomic_flush = mtk_crtc_atomic_flush,
851 .atomic_enable = mtk_crtc_atomic_enable,
852 .atomic_disable = mtk_crtc_atomic_disable,
855 static int mtk_crtc_init(struct drm_device *drm, struct mtk_crtc *mtk_crtc,
858 struct drm_plane *primary = NULL;
859 struct drm_plane *cursor = NULL;
862 for (i = 0; i < mtk_crtc->layer_nr; i++) {
863 if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_PRIMARY)
864 primary = &mtk_crtc->planes[i];
865 else if (mtk_crtc->planes[i].type == DRM_PLANE_TYPE_CURSOR)
866 cursor = &mtk_crtc->planes[i];
869 ret = drm_crtc_init_with_planes(drm, &mtk_crtc->base, primary, cursor,
870 &mtk_crtc_funcs, NULL);
872 goto err_cleanup_crtc;
874 drm_crtc_helper_add(&mtk_crtc->base, &mtk_crtc_helper_funcs);
879 drm_crtc_cleanup(&mtk_crtc->base);
883 static int mtk_crtc_num_comp_planes(struct mtk_crtc *mtk_crtc, int comp_idx)
885 struct mtk_ddp_comp *comp;
890 comp = mtk_crtc->ddp_comp[comp_idx];
894 if (comp_idx == 1 && !comp->funcs->bgclr_in_on)
897 return mtk_ddp_comp_layer_nr(comp);
901 enum drm_plane_type mtk_crtc_plane_type(unsigned int plane_idx,
902 unsigned int num_planes)
905 return DRM_PLANE_TYPE_PRIMARY;
906 else if (plane_idx == (num_planes - 1))
907 return DRM_PLANE_TYPE_CURSOR;
909 return DRM_PLANE_TYPE_OVERLAY;
913 static int mtk_crtc_init_comp_planes(struct drm_device *drm_dev,
914 struct mtk_crtc *mtk_crtc,
915 int comp_idx, int pipe)
917 int num_planes = mtk_crtc_num_comp_planes(mtk_crtc, comp_idx);
918 struct mtk_ddp_comp *comp = mtk_crtc->ddp_comp[comp_idx];
921 for (i = 0; i < num_planes; i++) {
922 ret = mtk_plane_init(drm_dev,
923 &mtk_crtc->planes[mtk_crtc->layer_nr],
925 mtk_crtc_plane_type(mtk_crtc->layer_nr, num_planes),
926 mtk_ddp_comp_supported_rotations(comp),
927 mtk_ddp_comp_get_formats(comp),
928 mtk_ddp_comp_get_num_formats(comp));
932 mtk_crtc->layer_nr++;
937 struct device *mtk_crtc_dma_dev_get(struct drm_crtc *crtc)
939 struct mtk_crtc *mtk_crtc = NULL;
944 mtk_crtc = to_mtk_crtc(crtc);
948 return mtk_crtc->dma_dev;
951 int mtk_crtc_create(struct drm_device *drm_dev, const unsigned int *path,
952 unsigned int path_len, int priv_data_index,
953 const struct mtk_drm_route *conn_routes,
954 unsigned int num_conn_routes)
956 struct mtk_drm_private *priv = drm_dev->dev_private;
957 struct device *dev = drm_dev->dev;
958 struct mtk_crtc *mtk_crtc;
959 unsigned int num_comp_planes = 0;
962 bool has_ctm = false;
963 uint gamma_lut_size = 0;
964 struct drm_crtc *tmp;
970 priv = priv->all_drm_private[priv_data_index];
972 drm_for_each_crtc(tmp, drm_dev)
975 for (i = 0; i < path_len; i++) {
976 enum mtk_ddp_comp_id comp_id = path[i];
977 struct device_node *node;
978 struct mtk_ddp_comp *comp;
980 node = priv->comp_node[comp_id];
981 comp = &priv->ddp_comp[comp_id];
983 /* Not all drm components have a DTS device node, such as ovl_adaptor,
984 * which is the drm bring up sub driver
986 if (!node && comp_id != DDP_COMPONENT_DRM_OVL_ADAPTOR) {
988 "Not creating crtc %d because component %d is disabled or missing\n",
994 dev_err(dev, "Component %pOF not initialized\n", node);
999 mtk_crtc = devm_kzalloc(dev, sizeof(*mtk_crtc), GFP_KERNEL);
1003 mtk_crtc->mmsys_dev = priv->mmsys_dev;
1004 mtk_crtc->ddp_comp_nr = path_len;
1005 mtk_crtc->ddp_comp = devm_kcalloc(dev,
1006 mtk_crtc->ddp_comp_nr + (conn_routes ? 1 : 0),
1007 sizeof(*mtk_crtc->ddp_comp),
1009 if (!mtk_crtc->ddp_comp)
1012 mtk_crtc->mutex = mtk_mutex_get(priv->mutex_dev);
1013 if (IS_ERR(mtk_crtc->mutex)) {
1014 ret = PTR_ERR(mtk_crtc->mutex);
1015 dev_err(dev, "Failed to get mutex: %d\n", ret);
1019 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
1020 unsigned int comp_id = path[i];
1021 struct mtk_ddp_comp *comp;
1023 comp = &priv->ddp_comp[comp_id];
1024 mtk_crtc->ddp_comp[i] = comp;
1027 if (comp->funcs->gamma_set && comp->funcs->gamma_get_lut_size) {
1028 unsigned int lut_sz = mtk_ddp_gamma_get_lut_size(comp);
1031 gamma_lut_size = lut_sz;
1034 if (comp->funcs->ctm_set)
1038 mtk_ddp_comp_register_vblank_cb(comp, mtk_crtc_ddp_irq,
1042 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++)
1043 num_comp_planes += mtk_crtc_num_comp_planes(mtk_crtc, i);
1045 mtk_crtc->planes = devm_kcalloc(dev, num_comp_planes,
1046 sizeof(struct drm_plane), GFP_KERNEL);
1047 if (!mtk_crtc->planes)
1050 for (i = 0; i < mtk_crtc->ddp_comp_nr; i++) {
1051 ret = mtk_crtc_init_comp_planes(drm_dev, mtk_crtc, i, crtc_i);
1057 * Default to use the first component as the dma dev.
1058 * In the case of ovl_adaptor sub driver, it needs to use the
1059 * dma_dev_get function to get representative dma dev.
1061 mtk_crtc->dma_dev = mtk_ddp_comp_dma_dev_get(&priv->ddp_comp[path[0]]);
1063 ret = mtk_crtc_init(drm_dev, mtk_crtc, crtc_i);
1068 drm_mode_crtc_set_gamma_size(&mtk_crtc->base, gamma_lut_size);
1069 drm_crtc_enable_color_mgmt(&mtk_crtc->base, 0, has_ctm, gamma_lut_size);
1070 mutex_init(&mtk_crtc->hw_lock);
1072 #if IS_REACHABLE(CONFIG_MTK_CMDQ)
1073 i = priv->mbox_index++;
1074 mtk_crtc->cmdq_client.client.dev = mtk_crtc->mmsys_dev;
1075 mtk_crtc->cmdq_client.client.tx_block = false;
1076 mtk_crtc->cmdq_client.client.knows_txdone = true;
1077 mtk_crtc->cmdq_client.client.rx_callback = ddp_cmdq_cb;
1078 mtk_crtc->cmdq_client.chan =
1079 mbox_request_channel(&mtk_crtc->cmdq_client.client, i);
1080 if (IS_ERR(mtk_crtc->cmdq_client.chan)) {
1081 dev_dbg(dev, "mtk_crtc %d failed to create mailbox client, writing register by CPU now\n",
1082 drm_crtc_index(&mtk_crtc->base));
1083 mtk_crtc->cmdq_client.chan = NULL;
1086 if (mtk_crtc->cmdq_client.chan) {
1087 ret = of_property_read_u32_index(priv->mutex_node,
1088 "mediatek,gce-events",
1090 &mtk_crtc->cmdq_event);
1092 dev_dbg(dev, "mtk_crtc %d failed to get mediatek,gce-events property\n",
1093 drm_crtc_index(&mtk_crtc->base));
1094 mbox_free_channel(mtk_crtc->cmdq_client.chan);
1095 mtk_crtc->cmdq_client.chan = NULL;
1097 ret = mtk_drm_cmdq_pkt_create(&mtk_crtc->cmdq_client,
1098 &mtk_crtc->cmdq_handle,
1101 dev_dbg(dev, "mtk_crtc %d failed to create cmdq packet\n",
1102 drm_crtc_index(&mtk_crtc->base));
1103 mbox_free_channel(mtk_crtc->cmdq_client.chan);
1104 mtk_crtc->cmdq_client.chan = NULL;
1108 /* for sending blocking cmd in crtc disable */
1109 init_waitqueue_head(&mtk_crtc->cb_blocking_queue);
1114 for (i = 0; i < num_conn_routes; i++) {
1115 unsigned int comp_id = conn_routes[i].route_ddp;
1116 struct device_node *node = priv->comp_node[comp_id];
1117 struct mtk_ddp_comp *comp = &priv->ddp_comp[comp_id];
1120 dev_dbg(dev, "comp_id:%d, Component %pOF not initialized\n",
1122 /* mark encoder_index to -1, if route comp device is not enabled */
1123 comp->encoder_index = -1;
1127 mtk_ddp_comp_encoder_index_set(&priv->ddp_comp[comp_id]);
1130 mtk_crtc->num_conn_routes = num_conn_routes;
1131 mtk_crtc->conn_routes = conn_routes;
1133 /* increase ddp_comp_nr at the end of mtk_crtc_create */
1134 mtk_crtc->ddp_comp_nr++;