1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
4 * Author: YT SHEN <yt.shen@mediatek.com>
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,
189 DDP_COMPONENT_DP_INTF0,
192 static const unsigned int mt8192_mtk_ddp_main[] = {
194 DDP_COMPONENT_OVL_2L0,
196 DDP_COMPONENT_COLOR0,
200 DDP_COMPONENT_POSTMASK0,
201 DDP_COMPONENT_DITHER0,
205 static const unsigned int mt8192_mtk_ddp_ext[] = {
206 DDP_COMPONENT_OVL_2L2,
211 static const unsigned int mt8195_mtk_ddp_main[] = {
214 DDP_COMPONENT_COLOR0,
218 DDP_COMPONENT_DITHER0,
220 DDP_COMPONENT_MERGE0,
221 DDP_COMPONENT_DP_INTF0,
224 static const unsigned int mt8195_mtk_ddp_ext[] = {
225 DDP_COMPONENT_DRM_OVL_ADAPTOR,
226 DDP_COMPONENT_MERGE5,
227 DDP_COMPONENT_DP_INTF1,
230 static const struct mtk_mmsys_driver_data mt2701_mmsys_driver_data = {
231 .main_path = mt2701_mtk_ddp_main,
232 .main_len = ARRAY_SIZE(mt2701_mtk_ddp_main),
233 .ext_path = mt2701_mtk_ddp_ext,
234 .ext_len = ARRAY_SIZE(mt2701_mtk_ddp_ext),
235 .shadow_register = true,
239 static const struct mtk_mmsys_driver_data mt7623_mmsys_driver_data = {
240 .main_path = mt7623_mtk_ddp_main,
241 .main_len = ARRAY_SIZE(mt7623_mtk_ddp_main),
242 .ext_path = mt7623_mtk_ddp_ext,
243 .ext_len = ARRAY_SIZE(mt7623_mtk_ddp_ext),
244 .shadow_register = true,
248 static const struct mtk_mmsys_driver_data mt2712_mmsys_driver_data = {
249 .main_path = mt2712_mtk_ddp_main,
250 .main_len = ARRAY_SIZE(mt2712_mtk_ddp_main),
251 .ext_path = mt2712_mtk_ddp_ext,
252 .ext_len = ARRAY_SIZE(mt2712_mtk_ddp_ext),
253 .third_path = mt2712_mtk_ddp_third,
254 .third_len = ARRAY_SIZE(mt2712_mtk_ddp_third),
258 static const struct mtk_mmsys_driver_data mt8167_mmsys_driver_data = {
259 .main_path = mt8167_mtk_ddp_main,
260 .main_len = ARRAY_SIZE(mt8167_mtk_ddp_main),
264 static const struct mtk_mmsys_driver_data mt8173_mmsys_driver_data = {
265 .main_path = mt8173_mtk_ddp_main,
266 .main_len = ARRAY_SIZE(mt8173_mtk_ddp_main),
267 .ext_path = mt8173_mtk_ddp_ext,
268 .ext_len = ARRAY_SIZE(mt8173_mtk_ddp_ext),
272 static const struct mtk_mmsys_driver_data mt8183_mmsys_driver_data = {
273 .main_path = mt8183_mtk_ddp_main,
274 .main_len = ARRAY_SIZE(mt8183_mtk_ddp_main),
275 .ext_path = mt8183_mtk_ddp_ext,
276 .ext_len = ARRAY_SIZE(mt8183_mtk_ddp_ext),
280 static const struct mtk_mmsys_driver_data mt8186_mmsys_driver_data = {
281 .main_path = mt8186_mtk_ddp_main,
282 .main_len = ARRAY_SIZE(mt8186_mtk_ddp_main),
283 .ext_path = mt8186_mtk_ddp_ext,
284 .ext_len = ARRAY_SIZE(mt8186_mtk_ddp_ext),
288 static const struct mtk_mmsys_driver_data mt8188_vdosys0_driver_data = {
289 .main_path = mt8188_mtk_ddp_main,
290 .main_len = ARRAY_SIZE(mt8188_mtk_ddp_main),
293 static const struct mtk_mmsys_driver_data mt8192_mmsys_driver_data = {
294 .main_path = mt8192_mtk_ddp_main,
295 .main_len = ARRAY_SIZE(mt8192_mtk_ddp_main),
296 .ext_path = mt8192_mtk_ddp_ext,
297 .ext_len = ARRAY_SIZE(mt8192_mtk_ddp_ext),
301 static const struct mtk_mmsys_driver_data mt8195_vdosys0_driver_data = {
302 .main_path = mt8195_mtk_ddp_main,
303 .main_len = ARRAY_SIZE(mt8195_mtk_ddp_main),
307 static const struct mtk_mmsys_driver_data mt8195_vdosys1_driver_data = {
308 .ext_path = mt8195_mtk_ddp_ext,
309 .ext_len = ARRAY_SIZE(mt8195_mtk_ddp_ext),
314 static const struct of_device_id mtk_drm_of_ids[] = {
315 { .compatible = "mediatek,mt2701-mmsys",
316 .data = &mt2701_mmsys_driver_data},
317 { .compatible = "mediatek,mt7623-mmsys",
318 .data = &mt7623_mmsys_driver_data},
319 { .compatible = "mediatek,mt2712-mmsys",
320 .data = &mt2712_mmsys_driver_data},
321 { .compatible = "mediatek,mt8167-mmsys",
322 .data = &mt8167_mmsys_driver_data},
323 { .compatible = "mediatek,mt8173-mmsys",
324 .data = &mt8173_mmsys_driver_data},
325 { .compatible = "mediatek,mt8183-mmsys",
326 .data = &mt8183_mmsys_driver_data},
327 { .compatible = "mediatek,mt8186-mmsys",
328 .data = &mt8186_mmsys_driver_data},
329 { .compatible = "mediatek,mt8188-vdosys0",
330 .data = &mt8188_vdosys0_driver_data},
331 { .compatible = "mediatek,mt8192-mmsys",
332 .data = &mt8192_mmsys_driver_data},
333 { .compatible = "mediatek,mt8195-mmsys",
334 .data = &mt8195_vdosys0_driver_data},
335 { .compatible = "mediatek,mt8195-vdosys0",
336 .data = &mt8195_vdosys0_driver_data},
337 { .compatible = "mediatek,mt8195-vdosys1",
338 .data = &mt8195_vdosys1_driver_data},
341 MODULE_DEVICE_TABLE(of, mtk_drm_of_ids);
343 static int mtk_drm_match(struct device *dev, void *data)
345 if (!strncmp(dev_name(dev), "mediatek-drm", sizeof("mediatek-drm") - 1))
350 static bool mtk_drm_get_all_drm_priv(struct device *dev)
352 struct mtk_drm_private *drm_priv = dev_get_drvdata(dev);
353 struct mtk_drm_private *all_drm_priv[MAX_CRTC];
354 struct device_node *phandle = dev->parent->of_node;
355 const struct of_device_id *of_id;
356 struct device_node *node;
357 struct device *drm_dev;
358 unsigned int cnt = 0;
361 for_each_child_of_node(phandle->parent, node) {
362 struct platform_device *pdev;
364 of_id = of_match_node(mtk_drm_of_ids, node);
368 pdev = of_find_device_by_node(node);
372 drm_dev = device_find_child(&pdev->dev, NULL, mtk_drm_match);
373 if (!drm_dev || !dev_get_drvdata(drm_dev))
376 all_drm_priv[cnt] = dev_get_drvdata(drm_dev);
377 if (all_drm_priv[cnt] && all_drm_priv[cnt]->mtk_drm_bound)
384 if (drm_priv->data->mmsys_dev_num == cnt) {
385 for (i = 0; i < cnt; i++)
386 for (j = 0; j < cnt; j++)
387 all_drm_priv[j]->all_drm_private[i] = all_drm_priv[i];
395 static bool mtk_drm_find_mmsys_comp(struct mtk_drm_private *private, int comp_id)
397 const struct mtk_mmsys_driver_data *drv_data = private->data;
400 if (drv_data->main_path)
401 for (i = 0; i < drv_data->main_len; i++)
402 if (drv_data->main_path[i] == comp_id)
405 if (drv_data->ext_path)
406 for (i = 0; i < drv_data->ext_len; i++)
407 if (drv_data->ext_path[i] == comp_id)
410 if (drv_data->third_path)
411 for (i = 0; i < drv_data->third_len; i++)
412 if (drv_data->third_path[i] == comp_id)
418 static int mtk_drm_kms_init(struct drm_device *drm)
420 struct mtk_drm_private *private = drm->dev_private;
421 struct mtk_drm_private *priv_n;
422 struct device *dma_dev = NULL;
425 if (drm_firmware_drivers_only())
428 ret = drmm_mode_config_init(drm);
432 drm->mode_config.min_width = 64;
433 drm->mode_config.min_height = 64;
436 * set max width and height as default value(4096x4096).
437 * this value would be used to check framebuffer size limitation
438 * at drm_mode_addfb().
440 drm->mode_config.max_width = 4096;
441 drm->mode_config.max_height = 4096;
442 drm->mode_config.funcs = &mtk_drm_mode_config_funcs;
443 drm->mode_config.helper_private = &mtk_drm_mode_config_helpers;
445 for (i = 0; i < private->data->mmsys_dev_num; i++) {
446 drm->dev_private = private->all_drm_private[i];
447 ret = component_bind_all(private->all_drm_private[i]->dev, drm);
453 * Ensure internal panels are at the top of the connector list before
456 drm_helper_move_panel_connectors_to_head(drm);
459 * 1. We currently support two fixed data streams, each optional,
460 * and each statically assigned to a crtc:
461 * OVL0 -> COLOR0 -> AAL -> OD -> RDMA0 -> UFOE -> DSI0 ...
462 * 2. For multi mmsys architecture, crtc path data are located in
463 * different drm private data structures. Loop through crtc index to
464 * create crtc from the main path and then ext_path and finally the
467 for (i = 0; i < MAX_CRTC; i++) {
468 for (j = 0; j < private->data->mmsys_dev_num; j++) {
469 priv_n = private->all_drm_private[j];
471 if (i == 0 && priv_n->data->main_len) {
472 ret = mtk_drm_crtc_create(drm, priv_n->data->main_path,
473 priv_n->data->main_len, j);
475 goto err_component_unbind;
478 } else if (i == 1 && priv_n->data->ext_len) {
479 ret = mtk_drm_crtc_create(drm, priv_n->data->ext_path,
480 priv_n->data->ext_len, j);
482 goto err_component_unbind;
485 } else if (i == 2 && priv_n->data->third_len) {
486 ret = mtk_drm_crtc_create(drm, priv_n->data->third_path,
487 priv_n->data->third_len, j);
489 goto err_component_unbind;
496 /* Use OVL device for all DMA memory allocations */
497 dma_dev = mtk_drm_crtc_dma_dev_get(drm_crtc_from_index(drm, 0));
500 dev_err(drm->dev, "Need at least one OVL device\n");
501 goto err_component_unbind;
504 for (i = 0; i < private->data->mmsys_dev_num; i++)
505 private->all_drm_private[i]->dma_dev = dma_dev;
508 * Configure the DMA segment size to make sure we get contiguous IOVA
509 * when importing PRIME buffers.
511 ret = dma_set_max_seg_size(dma_dev, UINT_MAX);
513 dev_err(dma_dev, "Failed to set DMA segment size\n");
514 goto err_component_unbind;
517 ret = drm_vblank_init(drm, MAX_CRTC);
519 goto err_component_unbind;
521 drm_kms_helper_poll_init(drm);
522 drm_mode_config_reset(drm);
526 err_component_unbind:
527 for (i = 0; i < private->data->mmsys_dev_num; i++)
528 component_unbind_all(private->all_drm_private[i]->dev, drm);
530 for (i = 0; i < private->data->mmsys_dev_num; i++)
531 put_device(private->all_drm_private[i]->mutex_dev);
536 static void mtk_drm_kms_deinit(struct drm_device *drm)
538 drm_kms_helper_poll_fini(drm);
539 drm_atomic_helper_shutdown(drm);
541 component_unbind_all(drm->dev, drm);
544 DEFINE_DRM_GEM_FOPS(mtk_drm_fops);
547 * We need to override this because the device used to import the memory is
548 * not dev->dev, as drm_gem_prime_import() expects.
550 static struct drm_gem_object *mtk_drm_gem_prime_import(struct drm_device *dev,
551 struct dma_buf *dma_buf)
553 struct mtk_drm_private *private = dev->dev_private;
555 return drm_gem_prime_import_dev(dev, dma_buf, private->dma_dev);
558 static const struct drm_driver mtk_drm_driver = {
559 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
561 .dumb_create = mtk_drm_gem_dumb_create,
563 .gem_prime_import = mtk_drm_gem_prime_import,
564 .gem_prime_import_sg_table = mtk_gem_prime_import_sg_table,
565 .fops = &mtk_drm_fops,
570 .major = DRIVER_MAJOR,
571 .minor = DRIVER_MINOR,
574 static int compare_dev(struct device *dev, void *data)
576 return dev == (struct device *)data;
579 static int mtk_drm_bind(struct device *dev)
581 struct mtk_drm_private *private = dev_get_drvdata(dev);
582 struct platform_device *pdev;
583 struct drm_device *drm;
586 if (!iommu_present(&platform_bus_type))
587 return -EPROBE_DEFER;
589 pdev = of_find_device_by_node(private->mutex_node);
591 dev_err(dev, "Waiting for disp-mutex device %pOF\n",
592 private->mutex_node);
593 of_node_put(private->mutex_node);
594 return -EPROBE_DEFER;
597 private->mutex_dev = &pdev->dev;
598 private->mtk_drm_bound = true;
601 if (!mtk_drm_get_all_drm_priv(dev))
604 drm = drm_dev_alloc(&mtk_drm_driver, dev);
608 private->drm_master = true;
609 drm->dev_private = private;
610 for (i = 0; i < private->data->mmsys_dev_num; i++)
611 private->all_drm_private[i]->drm = drm;
613 ret = mtk_drm_kms_init(drm);
617 ret = drm_dev_register(drm, 0);
621 drm_fbdev_generic_setup(drm, 32);
626 mtk_drm_kms_deinit(drm);
633 static void mtk_drm_unbind(struct device *dev)
635 struct mtk_drm_private *private = dev_get_drvdata(dev);
637 /* for multi mmsys dev, unregister drm dev in mmsys master */
638 if (private->drm_master) {
639 drm_dev_unregister(private->drm);
640 mtk_drm_kms_deinit(private->drm);
641 drm_dev_put(private->drm);
643 private->mtk_drm_bound = false;
644 private->drm_master = false;
648 static const struct component_master_ops mtk_drm_ops = {
649 .bind = mtk_drm_bind,
650 .unbind = mtk_drm_unbind,
653 static const struct of_device_id mtk_ddp_comp_dt_ids[] = {
654 { .compatible = "mediatek,mt8167-disp-aal",
655 .data = (void *)MTK_DISP_AAL},
656 { .compatible = "mediatek,mt8173-disp-aal",
657 .data = (void *)MTK_DISP_AAL},
658 { .compatible = "mediatek,mt8183-disp-aal",
659 .data = (void *)MTK_DISP_AAL},
660 { .compatible = "mediatek,mt8192-disp-aal",
661 .data = (void *)MTK_DISP_AAL},
662 { .compatible = "mediatek,mt8167-disp-ccorr",
663 .data = (void *)MTK_DISP_CCORR },
664 { .compatible = "mediatek,mt8183-disp-ccorr",
665 .data = (void *)MTK_DISP_CCORR },
666 { .compatible = "mediatek,mt8192-disp-ccorr",
667 .data = (void *)MTK_DISP_CCORR },
668 { .compatible = "mediatek,mt2701-disp-color",
669 .data = (void *)MTK_DISP_COLOR },
670 { .compatible = "mediatek,mt8167-disp-color",
671 .data = (void *)MTK_DISP_COLOR },
672 { .compatible = "mediatek,mt8173-disp-color",
673 .data = (void *)MTK_DISP_COLOR },
674 { .compatible = "mediatek,mt8167-disp-dither",
675 .data = (void *)MTK_DISP_DITHER },
676 { .compatible = "mediatek,mt8183-disp-dither",
677 .data = (void *)MTK_DISP_DITHER },
678 { .compatible = "mediatek,mt8195-disp-dsc",
679 .data = (void *)MTK_DISP_DSC },
680 { .compatible = "mediatek,mt8167-disp-gamma",
681 .data = (void *)MTK_DISP_GAMMA, },
682 { .compatible = "mediatek,mt8173-disp-gamma",
683 .data = (void *)MTK_DISP_GAMMA, },
684 { .compatible = "mediatek,mt8183-disp-gamma",
685 .data = (void *)MTK_DISP_GAMMA, },
686 { .compatible = "mediatek,mt8195-disp-merge",
687 .data = (void *)MTK_DISP_MERGE },
688 { .compatible = "mediatek,mt2701-disp-mutex",
689 .data = (void *)MTK_DISP_MUTEX },
690 { .compatible = "mediatek,mt2712-disp-mutex",
691 .data = (void *)MTK_DISP_MUTEX },
692 { .compatible = "mediatek,mt8167-disp-mutex",
693 .data = (void *)MTK_DISP_MUTEX },
694 { .compatible = "mediatek,mt8173-disp-mutex",
695 .data = (void *)MTK_DISP_MUTEX },
696 { .compatible = "mediatek,mt8183-disp-mutex",
697 .data = (void *)MTK_DISP_MUTEX },
698 { .compatible = "mediatek,mt8186-disp-mutex",
699 .data = (void *)MTK_DISP_MUTEX },
700 { .compatible = "mediatek,mt8188-disp-mutex",
701 .data = (void *)MTK_DISP_MUTEX },
702 { .compatible = "mediatek,mt8192-disp-mutex",
703 .data = (void *)MTK_DISP_MUTEX },
704 { .compatible = "mediatek,mt8195-disp-mutex",
705 .data = (void *)MTK_DISP_MUTEX },
706 { .compatible = "mediatek,mt8173-disp-od",
707 .data = (void *)MTK_DISP_OD },
708 { .compatible = "mediatek,mt2701-disp-ovl",
709 .data = (void *)MTK_DISP_OVL },
710 { .compatible = "mediatek,mt8167-disp-ovl",
711 .data = (void *)MTK_DISP_OVL },
712 { .compatible = "mediatek,mt8173-disp-ovl",
713 .data = (void *)MTK_DISP_OVL },
714 { .compatible = "mediatek,mt8183-disp-ovl",
715 .data = (void *)MTK_DISP_OVL },
716 { .compatible = "mediatek,mt8192-disp-ovl",
717 .data = (void *)MTK_DISP_OVL },
718 { .compatible = "mediatek,mt8183-disp-ovl-2l",
719 .data = (void *)MTK_DISP_OVL_2L },
720 { .compatible = "mediatek,mt8192-disp-ovl-2l",
721 .data = (void *)MTK_DISP_OVL_2L },
722 { .compatible = "mediatek,mt8192-disp-postmask",
723 .data = (void *)MTK_DISP_POSTMASK },
724 { .compatible = "mediatek,mt2701-disp-pwm",
725 .data = (void *)MTK_DISP_BLS },
726 { .compatible = "mediatek,mt8167-disp-pwm",
727 .data = (void *)MTK_DISP_PWM },
728 { .compatible = "mediatek,mt8173-disp-pwm",
729 .data = (void *)MTK_DISP_PWM },
730 { .compatible = "mediatek,mt2701-disp-rdma",
731 .data = (void *)MTK_DISP_RDMA },
732 { .compatible = "mediatek,mt8167-disp-rdma",
733 .data = (void *)MTK_DISP_RDMA },
734 { .compatible = "mediatek,mt8173-disp-rdma",
735 .data = (void *)MTK_DISP_RDMA },
736 { .compatible = "mediatek,mt8183-disp-rdma",
737 .data = (void *)MTK_DISP_RDMA },
738 { .compatible = "mediatek,mt8195-disp-rdma",
739 .data = (void *)MTK_DISP_RDMA },
740 { .compatible = "mediatek,mt8173-disp-ufoe",
741 .data = (void *)MTK_DISP_UFOE },
742 { .compatible = "mediatek,mt8173-disp-wdma",
743 .data = (void *)MTK_DISP_WDMA },
744 { .compatible = "mediatek,mt2701-dpi",
745 .data = (void *)MTK_DPI },
746 { .compatible = "mediatek,mt8167-dsi",
747 .data = (void *)MTK_DSI },
748 { .compatible = "mediatek,mt8173-dpi",
749 .data = (void *)MTK_DPI },
750 { .compatible = "mediatek,mt8183-dpi",
751 .data = (void *)MTK_DPI },
752 { .compatible = "mediatek,mt8186-dpi",
753 .data = (void *)MTK_DPI },
754 { .compatible = "mediatek,mt8188-dp-intf",
755 .data = (void *)MTK_DP_INTF },
756 { .compatible = "mediatek,mt8192-dpi",
757 .data = (void *)MTK_DPI },
758 { .compatible = "mediatek,mt8195-dp-intf",
759 .data = (void *)MTK_DP_INTF },
760 { .compatible = "mediatek,mt2701-dsi",
761 .data = (void *)MTK_DSI },
762 { .compatible = "mediatek,mt8173-dsi",
763 .data = (void *)MTK_DSI },
764 { .compatible = "mediatek,mt8183-dsi",
765 .data = (void *)MTK_DSI },
766 { .compatible = "mediatek,mt8186-dsi",
767 .data = (void *)MTK_DSI },
771 static int mtk_drm_probe(struct platform_device *pdev)
773 struct device *dev = &pdev->dev;
774 struct device_node *phandle = dev->parent->of_node;
775 const struct of_device_id *of_id;
776 struct mtk_drm_private *private;
777 struct device_node *node;
778 struct component_match *match = NULL;
779 struct platform_device *ovl_adaptor;
783 private = devm_kzalloc(dev, sizeof(*private), GFP_KERNEL);
787 private->mmsys_dev = dev->parent;
788 if (!private->mmsys_dev) {
789 dev_err(dev, "Failed to get MMSYS device\n");
793 of_id = of_match_node(mtk_drm_of_ids, phandle);
797 private->data = of_id->data;
799 private->all_drm_private = devm_kmalloc_array(dev, private->data->mmsys_dev_num,
800 sizeof(*private->all_drm_private),
802 if (!private->all_drm_private)
805 /* Bringup ovl_adaptor */
806 if (mtk_drm_find_mmsys_comp(private, DDP_COMPONENT_DRM_OVL_ADAPTOR)) {
807 ovl_adaptor = platform_device_register_data(dev, "mediatek-disp-ovl-adaptor",
809 (void *)private->mmsys_dev,
810 sizeof(*private->mmsys_dev));
811 private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR].dev = &ovl_adaptor->dev;
812 mtk_ddp_comp_init(NULL, &private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR],
813 DDP_COMPONENT_DRM_OVL_ADAPTOR);
814 component_match_add(dev, &match, compare_dev, &ovl_adaptor->dev);
817 /* Iterate over sibling DISP function blocks */
818 for_each_child_of_node(phandle->parent, node) {
819 const struct of_device_id *of_id;
820 enum mtk_ddp_comp_type comp_type;
823 of_id = of_match_node(mtk_ddp_comp_dt_ids, node);
827 if (!of_device_is_available(node)) {
828 dev_dbg(dev, "Skipping disabled component %pOF\n",
833 comp_type = (enum mtk_ddp_comp_type)(uintptr_t)of_id->data;
835 if (comp_type == MTK_DISP_MUTEX) {
838 id = of_alias_get_id(node, "mutex");
839 if (id < 0 || id == private->data->mmsys_id) {
840 private->mutex_node = of_node_get(node);
841 dev_dbg(dev, "get mutex for mmsys %d", private->data->mmsys_id);
846 comp_id = mtk_ddp_comp_get_id(node, comp_type);
848 dev_warn(dev, "Skipping unknown component %pOF\n",
853 if (!mtk_drm_find_mmsys_comp(private, comp_id))
856 private->comp_node[comp_id] = of_node_get(node);
859 * Currently only the AAL, CCORR, COLOR, GAMMA, MERGE, OVL, RDMA, DSI, and DPI
860 * blocks have separate component platform drivers and initialize their own
861 * DDP component structure. The others are initialized here.
863 if (comp_type == MTK_DISP_AAL ||
864 comp_type == MTK_DISP_CCORR ||
865 comp_type == MTK_DISP_COLOR ||
866 comp_type == MTK_DISP_GAMMA ||
867 comp_type == MTK_DISP_MERGE ||
868 comp_type == MTK_DISP_OVL ||
869 comp_type == MTK_DISP_OVL_2L ||
870 comp_type == MTK_DISP_OVL_ADAPTOR ||
871 comp_type == MTK_DISP_RDMA ||
872 comp_type == MTK_DP_INTF ||
873 comp_type == MTK_DPI ||
874 comp_type == MTK_DSI) {
875 dev_info(dev, "Adding component match for %pOF\n",
877 drm_of_component_match_add(dev, &match, component_compare_of,
881 ret = mtk_ddp_comp_init(node, &private->ddp_comp[comp_id], comp_id);
888 if (!private->mutex_node) {
889 dev_err(dev, "Failed to find disp-mutex node\n");
894 pm_runtime_enable(dev);
896 platform_set_drvdata(pdev, private);
898 ret = component_master_add_with_match(dev, &mtk_drm_ops, match);
905 pm_runtime_disable(dev);
907 of_node_put(private->mutex_node);
908 for (i = 0; i < DDP_COMPONENT_DRM_ID_MAX; i++)
909 of_node_put(private->comp_node[i]);
913 static void mtk_drm_remove(struct platform_device *pdev)
915 struct mtk_drm_private *private = platform_get_drvdata(pdev);
918 component_master_del(&pdev->dev, &mtk_drm_ops);
919 pm_runtime_disable(&pdev->dev);
920 of_node_put(private->mutex_node);
921 for (i = 0; i < DDP_COMPONENT_DRM_ID_MAX; i++)
922 of_node_put(private->comp_node[i]);
925 static int mtk_drm_sys_prepare(struct device *dev)
927 struct mtk_drm_private *private = dev_get_drvdata(dev);
928 struct drm_device *drm = private->drm;
930 if (private->drm_master)
931 return drm_mode_config_helper_suspend(drm);
936 static void mtk_drm_sys_complete(struct device *dev)
938 struct mtk_drm_private *private = dev_get_drvdata(dev);
939 struct drm_device *drm = private->drm;
942 if (private->drm_master)
943 ret = drm_mode_config_helper_resume(drm);
945 dev_err(dev, "Failed to resume\n");
948 static const struct dev_pm_ops mtk_drm_pm_ops = {
949 .prepare = mtk_drm_sys_prepare,
950 .complete = mtk_drm_sys_complete,
953 static struct platform_driver mtk_drm_platform_driver = {
954 .probe = mtk_drm_probe,
955 .remove_new = mtk_drm_remove,
957 .name = "mediatek-drm",
958 .pm = &mtk_drm_pm_ops,
962 static struct platform_driver * const mtk_drm_drivers[] = {
963 &mtk_disp_aal_driver,
964 &mtk_disp_ccorr_driver,
965 &mtk_disp_color_driver,
966 &mtk_disp_gamma_driver,
967 &mtk_disp_merge_driver,
968 &mtk_disp_ovl_adaptor_driver,
969 &mtk_disp_ovl_driver,
970 &mtk_disp_rdma_driver,
972 &mtk_drm_platform_driver,
975 &mtk_mdp_rdma_driver,
978 static int __init mtk_drm_init(void)
980 return platform_register_drivers(mtk_drm_drivers,
981 ARRAY_SIZE(mtk_drm_drivers));
984 static void __exit mtk_drm_exit(void)
986 platform_unregister_drivers(mtk_drm_drivers,
987 ARRAY_SIZE(mtk_drm_drivers));
990 module_init(mtk_drm_init);
991 module_exit(mtk_drm_exit);
993 MODULE_AUTHOR("YT SHEN <yt.shen@mediatek.com>");
994 MODULE_DESCRIPTION("Mediatek SoC DRM driver");
995 MODULE_LICENSE("GPL v2");