1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
7 #include <linux/component.h>
8 #include <linux/iommu.h>
9 #include <linux/module.h>
11 #include <linux/of_platform.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/dma-mapping.h>
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_fbdev_generic.h>
20 #include <drm/drm_fourcc.h>
21 #include <drm/drm_gem.h>
22 #include <drm/drm_gem_framebuffer_helper.h>
23 #include <drm/drm_ioctl.h>
24 #include <drm/drm_of.h>
25 #include <drm/drm_probe_helper.h>
26 #include <drm/drm_vblank.h>
28 #include "mtk_drm_crtc.h"
29 #include "mtk_drm_ddp_comp.h"
30 #include "mtk_drm_drv.h"
31 #include "mtk_drm_gem.h"
33 #define DRIVER_NAME "mediatek"
34 #define DRIVER_DESC "Mediatek SoC DRM"
35 #define DRIVER_DATE "20150513"
36 #define DRIVER_MAJOR 1
37 #define DRIVER_MINOR 0
39 static const struct drm_mode_config_helper_funcs mtk_drm_mode_config_helpers = {
40 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
43 static struct drm_framebuffer *
44 mtk_drm_mode_fb_create(struct drm_device *dev,
45 struct drm_file *file,
46 const struct drm_mode_fb_cmd2 *cmd)
48 const struct drm_format_info *info = drm_get_format_info(dev, cmd);
50 if (info->num_planes != 1)
51 return ERR_PTR(-EINVAL);
53 return drm_gem_fb_create(dev, file, cmd);
56 static const struct drm_mode_config_funcs mtk_drm_mode_config_funcs = {
57 .fb_create = mtk_drm_mode_fb_create,
58 .atomic_check = drm_atomic_helper_check,
59 .atomic_commit = drm_atomic_helper_commit,
62 static const unsigned int mt2701_mtk_ddp_main[] = {
70 static const unsigned int mt2701_mtk_ddp_ext[] = {
75 static const unsigned int mt7623_mtk_ddp_main[] = {
83 static const unsigned int mt7623_mtk_ddp_ext[] = {
88 static const unsigned int mt2712_mtk_ddp_main[] = {
98 static const unsigned int mt2712_mtk_ddp_ext[] = {
100 DDP_COMPONENT_COLOR1,
108 static const unsigned int mt2712_mtk_ddp_third[] = {
114 static unsigned int mt8167_mtk_ddp_main[] = {
116 DDP_COMPONENT_COLOR0,
120 DDP_COMPONENT_DITHER0,
125 static const unsigned int mt8173_mtk_ddp_main[] = {
127 DDP_COMPONENT_COLOR0,
136 static const unsigned int mt8173_mtk_ddp_ext[] = {
138 DDP_COMPONENT_COLOR1,
144 static const unsigned int mt8183_mtk_ddp_main[] = {
146 DDP_COMPONENT_OVL_2L0,
148 DDP_COMPONENT_COLOR0,
152 DDP_COMPONENT_DITHER0,
156 static const unsigned int mt8183_mtk_ddp_ext[] = {
157 DDP_COMPONENT_OVL_2L1,
162 static const unsigned int mt8186_mtk_ddp_main[] = {
165 DDP_COMPONENT_COLOR0,
169 DDP_COMPONENT_POSTMASK0,
170 DDP_COMPONENT_DITHER0,
174 static const unsigned int mt8186_mtk_ddp_ext[] = {
175 DDP_COMPONENT_OVL_2L0,
180 static const unsigned int mt8188_mtk_ddp_main[] = {
183 DDP_COMPONENT_COLOR0,
187 DDP_COMPONENT_POSTMASK0,
188 DDP_COMPONENT_DITHER0,
191 static const struct mtk_drm_route mt8188_mtk_ddp_main_routes[] = {
192 {0, DDP_COMPONENT_DP_INTF0},
193 {0, DDP_COMPONENT_DSI0},
196 static const unsigned int mt8192_mtk_ddp_main[] = {
198 DDP_COMPONENT_OVL_2L0,
200 DDP_COMPONENT_COLOR0,
204 DDP_COMPONENT_POSTMASK0,
205 DDP_COMPONENT_DITHER0,
209 static const unsigned int mt8192_mtk_ddp_ext[] = {
210 DDP_COMPONENT_OVL_2L2,
215 static const unsigned int mt8195_mtk_ddp_main[] = {
218 DDP_COMPONENT_COLOR0,
222 DDP_COMPONENT_DITHER0,
224 DDP_COMPONENT_MERGE0,
225 DDP_COMPONENT_DP_INTF0,
228 static const unsigned int mt8195_mtk_ddp_ext[] = {
229 DDP_COMPONENT_DRM_OVL_ADAPTOR,
230 DDP_COMPONENT_MERGE5,
231 DDP_COMPONENT_DP_INTF1,
234 static const struct mtk_mmsys_driver_data mt2701_mmsys_driver_data = {
235 .main_path = mt2701_mtk_ddp_main,
236 .main_len = ARRAY_SIZE(mt2701_mtk_ddp_main),
237 .ext_path = mt2701_mtk_ddp_ext,
238 .ext_len = ARRAY_SIZE(mt2701_mtk_ddp_ext),
239 .shadow_register = true,
243 static const struct mtk_mmsys_driver_data mt7623_mmsys_driver_data = {
244 .main_path = mt7623_mtk_ddp_main,
245 .main_len = ARRAY_SIZE(mt7623_mtk_ddp_main),
246 .ext_path = mt7623_mtk_ddp_ext,
247 .ext_len = ARRAY_SIZE(mt7623_mtk_ddp_ext),
248 .shadow_register = true,
252 static const struct mtk_mmsys_driver_data mt2712_mmsys_driver_data = {
253 .main_path = mt2712_mtk_ddp_main,
254 .main_len = ARRAY_SIZE(mt2712_mtk_ddp_main),
255 .ext_path = mt2712_mtk_ddp_ext,
256 .ext_len = ARRAY_SIZE(mt2712_mtk_ddp_ext),
257 .third_path = mt2712_mtk_ddp_third,
258 .third_len = ARRAY_SIZE(mt2712_mtk_ddp_third),
262 static const struct mtk_mmsys_driver_data mt8167_mmsys_driver_data = {
263 .main_path = mt8167_mtk_ddp_main,
264 .main_len = ARRAY_SIZE(mt8167_mtk_ddp_main),
268 static const struct mtk_mmsys_driver_data mt8173_mmsys_driver_data = {
269 .main_path = mt8173_mtk_ddp_main,
270 .main_len = ARRAY_SIZE(mt8173_mtk_ddp_main),
271 .ext_path = mt8173_mtk_ddp_ext,
272 .ext_len = ARRAY_SIZE(mt8173_mtk_ddp_ext),
276 static const struct mtk_mmsys_driver_data mt8183_mmsys_driver_data = {
277 .main_path = mt8183_mtk_ddp_main,
278 .main_len = ARRAY_SIZE(mt8183_mtk_ddp_main),
279 .ext_path = mt8183_mtk_ddp_ext,
280 .ext_len = ARRAY_SIZE(mt8183_mtk_ddp_ext),
284 static const struct mtk_mmsys_driver_data mt8186_mmsys_driver_data = {
285 .main_path = mt8186_mtk_ddp_main,
286 .main_len = ARRAY_SIZE(mt8186_mtk_ddp_main),
287 .ext_path = mt8186_mtk_ddp_ext,
288 .ext_len = ARRAY_SIZE(mt8186_mtk_ddp_ext),
292 static const struct mtk_mmsys_driver_data mt8188_vdosys0_driver_data = {
293 .main_path = mt8188_mtk_ddp_main,
294 .main_len = ARRAY_SIZE(mt8188_mtk_ddp_main),
295 .conn_routes = mt8188_mtk_ddp_main_routes,
296 .num_conn_routes = ARRAY_SIZE(mt8188_mtk_ddp_main_routes),
300 static const struct mtk_mmsys_driver_data mt8192_mmsys_driver_data = {
301 .main_path = mt8192_mtk_ddp_main,
302 .main_len = ARRAY_SIZE(mt8192_mtk_ddp_main),
303 .ext_path = mt8192_mtk_ddp_ext,
304 .ext_len = ARRAY_SIZE(mt8192_mtk_ddp_ext),
308 static const struct mtk_mmsys_driver_data mt8195_vdosys0_driver_data = {
309 .main_path = mt8195_mtk_ddp_main,
310 .main_len = ARRAY_SIZE(mt8195_mtk_ddp_main),
314 static const struct mtk_mmsys_driver_data mt8195_vdosys1_driver_data = {
315 .ext_path = mt8195_mtk_ddp_ext,
316 .ext_len = ARRAY_SIZE(mt8195_mtk_ddp_ext),
321 static const struct of_device_id mtk_drm_of_ids[] = {
322 { .compatible = "mediatek,mt2701-mmsys",
323 .data = &mt2701_mmsys_driver_data},
324 { .compatible = "mediatek,mt7623-mmsys",
325 .data = &mt7623_mmsys_driver_data},
326 { .compatible = "mediatek,mt2712-mmsys",
327 .data = &mt2712_mmsys_driver_data},
328 { .compatible = "mediatek,mt8167-mmsys",
329 .data = &mt8167_mmsys_driver_data},
330 { .compatible = "mediatek,mt8173-mmsys",
331 .data = &mt8173_mmsys_driver_data},
332 { .compatible = "mediatek,mt8183-mmsys",
333 .data = &mt8183_mmsys_driver_data},
334 { .compatible = "mediatek,mt8186-mmsys",
335 .data = &mt8186_mmsys_driver_data},
336 { .compatible = "mediatek,mt8188-vdosys0",
337 .data = &mt8188_vdosys0_driver_data},
338 { .compatible = "mediatek,mt8192-mmsys",
339 .data = &mt8192_mmsys_driver_data},
340 { .compatible = "mediatek,mt8195-mmsys",
341 .data = &mt8195_vdosys0_driver_data},
342 { .compatible = "mediatek,mt8195-vdosys0",
343 .data = &mt8195_vdosys0_driver_data},
344 { .compatible = "mediatek,mt8195-vdosys1",
345 .data = &mt8195_vdosys1_driver_data},
348 MODULE_DEVICE_TABLE(of, mtk_drm_of_ids);
350 static int mtk_drm_match(struct device *dev, void *data)
352 if (!strncmp(dev_name(dev), "mediatek-drm", sizeof("mediatek-drm") - 1))
357 static bool mtk_drm_get_all_drm_priv(struct device *dev)
359 struct mtk_drm_private *drm_priv = dev_get_drvdata(dev);
360 struct mtk_drm_private *all_drm_priv[MAX_CRTC];
361 struct mtk_drm_private *temp_drm_priv;
362 struct device_node *phandle = dev->parent->of_node;
363 const struct of_device_id *of_id;
364 struct device_node *node;
365 struct device *drm_dev;
366 unsigned int cnt = 0;
369 for_each_child_of_node(phandle->parent, node) {
370 struct platform_device *pdev;
372 of_id = of_match_node(mtk_drm_of_ids, node);
376 pdev = of_find_device_by_node(node);
380 drm_dev = device_find_child(&pdev->dev, NULL, mtk_drm_match);
384 temp_drm_priv = dev_get_drvdata(drm_dev);
388 if (temp_drm_priv->data->main_len)
389 all_drm_priv[CRTC_MAIN] = temp_drm_priv;
390 else if (temp_drm_priv->data->ext_len)
391 all_drm_priv[CRTC_EXT] = temp_drm_priv;
392 else if (temp_drm_priv->data->third_len)
393 all_drm_priv[CRTC_THIRD] = temp_drm_priv;
395 if (temp_drm_priv->mtk_drm_bound)
402 if (drm_priv->data->mmsys_dev_num == cnt) {
403 for (i = 0; i < cnt; i++)
404 for (j = 0; j < cnt; j++)
405 all_drm_priv[j]->all_drm_private[i] = all_drm_priv[i];
413 static bool mtk_drm_find_mmsys_comp(struct mtk_drm_private *private, int comp_id)
415 const struct mtk_mmsys_driver_data *drv_data = private->data;
418 if (drv_data->main_path)
419 for (i = 0; i < drv_data->main_len; i++)
420 if (drv_data->main_path[i] == comp_id)
423 if (drv_data->ext_path)
424 for (i = 0; i < drv_data->ext_len; i++)
425 if (drv_data->ext_path[i] == comp_id)
428 if (drv_data->third_path)
429 for (i = 0; i < drv_data->third_len; i++)
430 if (drv_data->third_path[i] == comp_id)
433 if (drv_data->num_conn_routes)
434 for (i = 0; i < drv_data->num_conn_routes; i++)
435 if (drv_data->conn_routes[i].route_ddp == comp_id)
441 static int mtk_drm_kms_init(struct drm_device *drm)
443 struct mtk_drm_private *private = drm->dev_private;
444 struct mtk_drm_private *priv_n;
445 struct device *dma_dev = NULL;
446 struct drm_crtc *crtc;
449 if (drm_firmware_drivers_only())
452 ret = drmm_mode_config_init(drm);
456 drm->mode_config.min_width = 64;
457 drm->mode_config.min_height = 64;
460 * set max width and height as default value(4096x4096).
461 * this value would be used to check framebuffer size limitation
462 * at drm_mode_addfb().
464 drm->mode_config.max_width = 4096;
465 drm->mode_config.max_height = 4096;
466 drm->mode_config.funcs = &mtk_drm_mode_config_funcs;
467 drm->mode_config.helper_private = &mtk_drm_mode_config_helpers;
469 for (i = 0; i < private->data->mmsys_dev_num; i++) {
470 drm->dev_private = private->all_drm_private[i];
471 ret = component_bind_all(private->all_drm_private[i]->dev, drm);
477 * Ensure internal panels are at the top of the connector list before
480 drm_helper_move_panel_connectors_to_head(drm);
483 * 1. We currently support two fixed data streams, each optional,
484 * and each statically assigned to a crtc:
485 * OVL0 -> COLOR0 -> AAL -> OD -> RDMA0 -> UFOE -> DSI0 ...
486 * 2. For multi mmsys architecture, crtc path data are located in
487 * different drm private data structures. Loop through crtc index to
488 * create crtc from the main path and then ext_path and finally the
491 for (i = 0; i < MAX_CRTC; i++) {
492 for (j = 0; j < private->data->mmsys_dev_num; j++) {
493 priv_n = private->all_drm_private[j];
495 if (i == CRTC_MAIN && priv_n->data->main_len) {
496 ret = mtk_drm_crtc_create(drm, priv_n->data->main_path,
497 priv_n->data->main_len, j,
498 priv_n->data->conn_routes,
499 priv_n->data->num_conn_routes);
501 goto err_component_unbind;
504 } else if (i == CRTC_EXT && priv_n->data->ext_len) {
505 ret = mtk_drm_crtc_create(drm, priv_n->data->ext_path,
506 priv_n->data->ext_len, j, NULL, 0);
508 goto err_component_unbind;
511 } else if (i == CRTC_THIRD && priv_n->data->third_len) {
512 ret = mtk_drm_crtc_create(drm, priv_n->data->third_path,
513 priv_n->data->third_len, j, NULL, 0);
515 goto err_component_unbind;
522 /* Use OVL device for all DMA memory allocations */
523 crtc = drm_crtc_from_index(drm, 0);
525 dma_dev = mtk_drm_crtc_dma_dev_get(crtc);
528 dev_err(drm->dev, "Need at least one OVL device\n");
529 goto err_component_unbind;
532 for (i = 0; i < private->data->mmsys_dev_num; i++)
533 private->all_drm_private[i]->dma_dev = dma_dev;
536 * Configure the DMA segment size to make sure we get contiguous IOVA
537 * when importing PRIME buffers.
539 ret = dma_set_max_seg_size(dma_dev, UINT_MAX);
541 dev_err(dma_dev, "Failed to set DMA segment size\n");
542 goto err_component_unbind;
545 ret = drm_vblank_init(drm, MAX_CRTC);
547 goto err_component_unbind;
549 drm_kms_helper_poll_init(drm);
550 drm_mode_config_reset(drm);
554 err_component_unbind:
555 for (i = 0; i < private->data->mmsys_dev_num; i++)
556 component_unbind_all(private->all_drm_private[i]->dev, drm);
558 for (i = 0; i < private->data->mmsys_dev_num; i++)
559 put_device(private->all_drm_private[i]->mutex_dev);
564 static void mtk_drm_kms_deinit(struct drm_device *drm)
566 drm_kms_helper_poll_fini(drm);
567 drm_atomic_helper_shutdown(drm);
569 component_unbind_all(drm->dev, drm);
572 DEFINE_DRM_GEM_FOPS(mtk_drm_fops);
575 * We need to override this because the device used to import the memory is
576 * not dev->dev, as drm_gem_prime_import() expects.
578 static struct drm_gem_object *mtk_drm_gem_prime_import(struct drm_device *dev,
579 struct dma_buf *dma_buf)
581 struct mtk_drm_private *private = dev->dev_private;
583 return drm_gem_prime_import_dev(dev, dma_buf, private->dma_dev);
586 static const struct drm_driver mtk_drm_driver = {
587 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
589 .dumb_create = mtk_drm_gem_dumb_create,
591 .gem_prime_import = mtk_drm_gem_prime_import,
592 .gem_prime_import_sg_table = mtk_gem_prime_import_sg_table,
593 .fops = &mtk_drm_fops,
598 .major = DRIVER_MAJOR,
599 .minor = DRIVER_MINOR,
602 static int compare_dev(struct device *dev, void *data)
604 return dev == (struct device *)data;
607 static int mtk_drm_bind(struct device *dev)
609 struct mtk_drm_private *private = dev_get_drvdata(dev);
610 struct platform_device *pdev;
611 struct drm_device *drm;
614 if (!iommu_present(&platform_bus_type))
615 return -EPROBE_DEFER;
617 pdev = of_find_device_by_node(private->mutex_node);
619 dev_err(dev, "Waiting for disp-mutex device %pOF\n",
620 private->mutex_node);
621 of_node_put(private->mutex_node);
622 return -EPROBE_DEFER;
625 private->mutex_dev = &pdev->dev;
626 private->mtk_drm_bound = true;
629 if (!mtk_drm_get_all_drm_priv(dev))
632 drm = drm_dev_alloc(&mtk_drm_driver, dev);
636 private->drm_master = true;
637 drm->dev_private = private;
638 for (i = 0; i < private->data->mmsys_dev_num; i++)
639 private->all_drm_private[i]->drm = drm;
641 ret = mtk_drm_kms_init(drm);
645 ret = drm_dev_register(drm, 0);
649 drm_fbdev_generic_setup(drm, 32);
654 mtk_drm_kms_deinit(drm);
661 static void mtk_drm_unbind(struct device *dev)
663 struct mtk_drm_private *private = dev_get_drvdata(dev);
665 /* for multi mmsys dev, unregister drm dev in mmsys master */
666 if (private->drm_master) {
667 drm_dev_unregister(private->drm);
668 mtk_drm_kms_deinit(private->drm);
669 drm_dev_put(private->drm);
671 private->mtk_drm_bound = false;
672 private->drm_master = false;
676 static const struct component_master_ops mtk_drm_ops = {
677 .bind = mtk_drm_bind,
678 .unbind = mtk_drm_unbind,
681 static const struct of_device_id mtk_ddp_comp_dt_ids[] = {
682 { .compatible = "mediatek,mt8167-disp-aal",
683 .data = (void *)MTK_DISP_AAL},
684 { .compatible = "mediatek,mt8173-disp-aal",
685 .data = (void *)MTK_DISP_AAL},
686 { .compatible = "mediatek,mt8183-disp-aal",
687 .data = (void *)MTK_DISP_AAL},
688 { .compatible = "mediatek,mt8192-disp-aal",
689 .data = (void *)MTK_DISP_AAL},
690 { .compatible = "mediatek,mt8167-disp-ccorr",
691 .data = (void *)MTK_DISP_CCORR },
692 { .compatible = "mediatek,mt8183-disp-ccorr",
693 .data = (void *)MTK_DISP_CCORR },
694 { .compatible = "mediatek,mt8192-disp-ccorr",
695 .data = (void *)MTK_DISP_CCORR },
696 { .compatible = "mediatek,mt2701-disp-color",
697 .data = (void *)MTK_DISP_COLOR },
698 { .compatible = "mediatek,mt8167-disp-color",
699 .data = (void *)MTK_DISP_COLOR },
700 { .compatible = "mediatek,mt8173-disp-color",
701 .data = (void *)MTK_DISP_COLOR },
702 { .compatible = "mediatek,mt8167-disp-dither",
703 .data = (void *)MTK_DISP_DITHER },
704 { .compatible = "mediatek,mt8183-disp-dither",
705 .data = (void *)MTK_DISP_DITHER },
706 { .compatible = "mediatek,mt8195-disp-dsc",
707 .data = (void *)MTK_DISP_DSC },
708 { .compatible = "mediatek,mt8167-disp-gamma",
709 .data = (void *)MTK_DISP_GAMMA, },
710 { .compatible = "mediatek,mt8173-disp-gamma",
711 .data = (void *)MTK_DISP_GAMMA, },
712 { .compatible = "mediatek,mt8183-disp-gamma",
713 .data = (void *)MTK_DISP_GAMMA, },
714 { .compatible = "mediatek,mt8195-disp-merge",
715 .data = (void *)MTK_DISP_MERGE },
716 { .compatible = "mediatek,mt2701-disp-mutex",
717 .data = (void *)MTK_DISP_MUTEX },
718 { .compatible = "mediatek,mt2712-disp-mutex",
719 .data = (void *)MTK_DISP_MUTEX },
720 { .compatible = "mediatek,mt8167-disp-mutex",
721 .data = (void *)MTK_DISP_MUTEX },
722 { .compatible = "mediatek,mt8173-disp-mutex",
723 .data = (void *)MTK_DISP_MUTEX },
724 { .compatible = "mediatek,mt8183-disp-mutex",
725 .data = (void *)MTK_DISP_MUTEX },
726 { .compatible = "mediatek,mt8186-disp-mutex",
727 .data = (void *)MTK_DISP_MUTEX },
728 { .compatible = "mediatek,mt8188-disp-mutex",
729 .data = (void *)MTK_DISP_MUTEX },
730 { .compatible = "mediatek,mt8192-disp-mutex",
731 .data = (void *)MTK_DISP_MUTEX },
732 { .compatible = "mediatek,mt8195-disp-mutex",
733 .data = (void *)MTK_DISP_MUTEX },
734 { .compatible = "mediatek,mt8173-disp-od",
735 .data = (void *)MTK_DISP_OD },
736 { .compatible = "mediatek,mt2701-disp-ovl",
737 .data = (void *)MTK_DISP_OVL },
738 { .compatible = "mediatek,mt8167-disp-ovl",
739 .data = (void *)MTK_DISP_OVL },
740 { .compatible = "mediatek,mt8173-disp-ovl",
741 .data = (void *)MTK_DISP_OVL },
742 { .compatible = "mediatek,mt8183-disp-ovl",
743 .data = (void *)MTK_DISP_OVL },
744 { .compatible = "mediatek,mt8192-disp-ovl",
745 .data = (void *)MTK_DISP_OVL },
746 { .compatible = "mediatek,mt8183-disp-ovl-2l",
747 .data = (void *)MTK_DISP_OVL_2L },
748 { .compatible = "mediatek,mt8192-disp-ovl-2l",
749 .data = (void *)MTK_DISP_OVL_2L },
750 { .compatible = "mediatek,mt8192-disp-postmask",
751 .data = (void *)MTK_DISP_POSTMASK },
752 { .compatible = "mediatek,mt2701-disp-pwm",
753 .data = (void *)MTK_DISP_BLS },
754 { .compatible = "mediatek,mt8167-disp-pwm",
755 .data = (void *)MTK_DISP_PWM },
756 { .compatible = "mediatek,mt8173-disp-pwm",
757 .data = (void *)MTK_DISP_PWM },
758 { .compatible = "mediatek,mt2701-disp-rdma",
759 .data = (void *)MTK_DISP_RDMA },
760 { .compatible = "mediatek,mt8167-disp-rdma",
761 .data = (void *)MTK_DISP_RDMA },
762 { .compatible = "mediatek,mt8173-disp-rdma",
763 .data = (void *)MTK_DISP_RDMA },
764 { .compatible = "mediatek,mt8183-disp-rdma",
765 .data = (void *)MTK_DISP_RDMA },
766 { .compatible = "mediatek,mt8195-disp-rdma",
767 .data = (void *)MTK_DISP_RDMA },
768 { .compatible = "mediatek,mt8173-disp-ufoe",
769 .data = (void *)MTK_DISP_UFOE },
770 { .compatible = "mediatek,mt8173-disp-wdma",
771 .data = (void *)MTK_DISP_WDMA },
772 { .compatible = "mediatek,mt2701-dpi",
773 .data = (void *)MTK_DPI },
774 { .compatible = "mediatek,mt8167-dsi",
775 .data = (void *)MTK_DSI },
776 { .compatible = "mediatek,mt8173-dpi",
777 .data = (void *)MTK_DPI },
778 { .compatible = "mediatek,mt8183-dpi",
779 .data = (void *)MTK_DPI },
780 { .compatible = "mediatek,mt8186-dpi",
781 .data = (void *)MTK_DPI },
782 { .compatible = "mediatek,mt8188-dp-intf",
783 .data = (void *)MTK_DP_INTF },
784 { .compatible = "mediatek,mt8192-dpi",
785 .data = (void *)MTK_DPI },
786 { .compatible = "mediatek,mt8195-dp-intf",
787 .data = (void *)MTK_DP_INTF },
788 { .compatible = "mediatek,mt2701-dsi",
789 .data = (void *)MTK_DSI },
790 { .compatible = "mediatek,mt8173-dsi",
791 .data = (void *)MTK_DSI },
792 { .compatible = "mediatek,mt8183-dsi",
793 .data = (void *)MTK_DSI },
794 { .compatible = "mediatek,mt8186-dsi",
795 .data = (void *)MTK_DSI },
796 { .compatible = "mediatek,mt8188-dsi",
797 .data = (void *)MTK_DSI },
801 static int mtk_drm_probe(struct platform_device *pdev)
803 struct device *dev = &pdev->dev;
804 struct device_node *phandle = dev->parent->of_node;
805 const struct of_device_id *of_id;
806 struct mtk_drm_private *private;
807 struct device_node *node;
808 struct component_match *match = NULL;
809 struct platform_device *ovl_adaptor;
813 private = devm_kzalloc(dev, sizeof(*private), GFP_KERNEL);
817 private->mmsys_dev = dev->parent;
818 if (!private->mmsys_dev) {
819 dev_err(dev, "Failed to get MMSYS device\n");
823 of_id = of_match_node(mtk_drm_of_ids, phandle);
827 private->data = of_id->data;
829 private->all_drm_private = devm_kmalloc_array(dev, private->data->mmsys_dev_num,
830 sizeof(*private->all_drm_private),
832 if (!private->all_drm_private)
835 /* Bringup ovl_adaptor */
836 if (mtk_drm_find_mmsys_comp(private, DDP_COMPONENT_DRM_OVL_ADAPTOR)) {
837 ovl_adaptor = platform_device_register_data(dev, "mediatek-disp-ovl-adaptor",
839 (void *)private->mmsys_dev,
840 sizeof(*private->mmsys_dev));
841 private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR].dev = &ovl_adaptor->dev;
842 mtk_ddp_comp_init(NULL, &private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR],
843 DDP_COMPONENT_DRM_OVL_ADAPTOR);
844 component_match_add(dev, &match, compare_dev, &ovl_adaptor->dev);
847 /* Iterate over sibling DISP function blocks */
848 for_each_child_of_node(phandle->parent, node) {
849 const struct of_device_id *of_id;
850 enum mtk_ddp_comp_type comp_type;
853 of_id = of_match_node(mtk_ddp_comp_dt_ids, node);
857 if (!of_device_is_available(node)) {
858 dev_dbg(dev, "Skipping disabled component %pOF\n",
863 comp_type = (enum mtk_ddp_comp_type)(uintptr_t)of_id->data;
865 if (comp_type == MTK_DISP_MUTEX) {
868 id = of_alias_get_id(node, "mutex");
869 if (id < 0 || id == private->data->mmsys_id) {
870 private->mutex_node = of_node_get(node);
871 dev_dbg(dev, "get mutex for mmsys %d", private->data->mmsys_id);
876 comp_id = mtk_ddp_comp_get_id(node, comp_type);
878 dev_warn(dev, "Skipping unknown component %pOF\n",
883 if (!mtk_drm_find_mmsys_comp(private, comp_id))
886 private->comp_node[comp_id] = of_node_get(node);
889 * Currently only the AAL, CCORR, COLOR, GAMMA, MERGE, OVL, RDMA, DSI, and DPI
890 * blocks have separate component platform drivers and initialize their own
891 * DDP component structure. The others are initialized here.
893 if (comp_type == MTK_DISP_AAL ||
894 comp_type == MTK_DISP_CCORR ||
895 comp_type == MTK_DISP_COLOR ||
896 comp_type == MTK_DISP_GAMMA ||
897 comp_type == MTK_DISP_MERGE ||
898 comp_type == MTK_DISP_OVL ||
899 comp_type == MTK_DISP_OVL_2L ||
900 comp_type == MTK_DISP_OVL_ADAPTOR ||
901 comp_type == MTK_DISP_RDMA ||
902 comp_type == MTK_DP_INTF ||
903 comp_type == MTK_DPI ||
904 comp_type == MTK_DSI) {
905 dev_info(dev, "Adding component match for %pOF\n",
907 drm_of_component_match_add(dev, &match, component_compare_of,
911 ret = mtk_ddp_comp_init(node, &private->ddp_comp[comp_id], comp_id);
918 if (!private->mutex_node) {
919 dev_err(dev, "Failed to find disp-mutex node\n");
924 pm_runtime_enable(dev);
926 platform_set_drvdata(pdev, private);
928 ret = component_master_add_with_match(dev, &mtk_drm_ops, match);
935 pm_runtime_disable(dev);
937 of_node_put(private->mutex_node);
938 for (i = 0; i < DDP_COMPONENT_DRM_ID_MAX; i++)
939 of_node_put(private->comp_node[i]);
943 static void mtk_drm_remove(struct platform_device *pdev)
945 struct mtk_drm_private *private = platform_get_drvdata(pdev);
948 component_master_del(&pdev->dev, &mtk_drm_ops);
949 pm_runtime_disable(&pdev->dev);
950 of_node_put(private->mutex_node);
951 for (i = 0; i < DDP_COMPONENT_DRM_ID_MAX; i++)
952 of_node_put(private->comp_node[i]);
955 static int mtk_drm_sys_prepare(struct device *dev)
957 struct mtk_drm_private *private = dev_get_drvdata(dev);
958 struct drm_device *drm = private->drm;
960 if (private->drm_master)
961 return drm_mode_config_helper_suspend(drm);
966 static void mtk_drm_sys_complete(struct device *dev)
968 struct mtk_drm_private *private = dev_get_drvdata(dev);
969 struct drm_device *drm = private->drm;
972 if (private->drm_master)
973 ret = drm_mode_config_helper_resume(drm);
975 dev_err(dev, "Failed to resume\n");
978 static const struct dev_pm_ops mtk_drm_pm_ops = {
979 .prepare = mtk_drm_sys_prepare,
980 .complete = mtk_drm_sys_complete,
983 static struct platform_driver mtk_drm_platform_driver = {
984 .probe = mtk_drm_probe,
985 .remove_new = mtk_drm_remove,
987 .name = "mediatek-drm",
988 .pm = &mtk_drm_pm_ops,
992 static struct platform_driver * const mtk_drm_drivers[] = {
993 &mtk_disp_aal_driver,
994 &mtk_disp_ccorr_driver,
995 &mtk_disp_color_driver,
996 &mtk_disp_gamma_driver,
997 &mtk_disp_merge_driver,
998 &mtk_disp_ovl_adaptor_driver,
999 &mtk_disp_ovl_driver,
1000 &mtk_disp_rdma_driver,
1002 &mtk_drm_platform_driver,
1005 &mtk_mdp_rdma_driver,
1008 static int __init mtk_drm_init(void)
1010 return platform_register_drivers(mtk_drm_drivers,
1011 ARRAY_SIZE(mtk_drm_drivers));
1014 static void __exit mtk_drm_exit(void)
1016 platform_unregister_drivers(mtk_drm_drivers,
1017 ARRAY_SIZE(mtk_drm_drivers));
1020 module_init(mtk_drm_init);
1021 module_exit(mtk_drm_exit);
1024 MODULE_DESCRIPTION("Mediatek SoC DRM driver");
1025 MODULE_LICENSE("GPL v2");