1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
7 #include <linux/component.h>
8 #include <linux/module.h>
10 #include <linux/of_platform.h>
11 #include <linux/platform_device.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/dma-mapping.h>
15 #include <drm/drm_atomic.h>
16 #include <drm/drm_atomic_helper.h>
17 #include <drm/drm_drv.h>
18 #include <drm/drm_fbdev_generic.h>
19 #include <drm/drm_fourcc.h>
20 #include <drm/drm_gem.h>
21 #include <drm/drm_gem_framebuffer_helper.h>
22 #include <drm/drm_ioctl.h>
23 #include <drm/drm_of.h>
24 #include <drm/drm_probe_helper.h>
25 #include <drm/drm_vblank.h>
27 #include "mtk_drm_crtc.h"
28 #include "mtk_drm_ddp_comp.h"
29 #include "mtk_drm_drv.h"
30 #include "mtk_drm_gem.h"
32 #define DRIVER_NAME "mediatek"
33 #define DRIVER_DESC "Mediatek SoC DRM"
34 #define DRIVER_DATE "20150513"
35 #define DRIVER_MAJOR 1
36 #define DRIVER_MINOR 0
38 static const struct drm_mode_config_helper_funcs mtk_drm_mode_config_helpers = {
39 .atomic_commit_tail = drm_atomic_helper_commit_tail_rpm,
42 static struct drm_framebuffer *
43 mtk_drm_mode_fb_create(struct drm_device *dev,
44 struct drm_file *file,
45 const struct drm_mode_fb_cmd2 *cmd)
47 const struct drm_format_info *info = drm_get_format_info(dev, cmd);
49 if (info->num_planes != 1)
50 return ERR_PTR(-EINVAL);
52 return drm_gem_fb_create(dev, file, cmd);
55 static const struct drm_mode_config_funcs mtk_drm_mode_config_funcs = {
56 .fb_create = mtk_drm_mode_fb_create,
57 .atomic_check = drm_atomic_helper_check,
58 .atomic_commit = drm_atomic_helper_commit,
61 static const unsigned int mt2701_mtk_ddp_main[] = {
69 static const unsigned int mt2701_mtk_ddp_ext[] = {
74 static const unsigned int mt7623_mtk_ddp_main[] = {
82 static const unsigned int mt7623_mtk_ddp_ext[] = {
87 static const unsigned int mt2712_mtk_ddp_main[] = {
97 static const unsigned int mt2712_mtk_ddp_ext[] = {
107 static const unsigned int mt2712_mtk_ddp_third[] = {
113 static unsigned int mt8167_mtk_ddp_main[] = {
115 DDP_COMPONENT_COLOR0,
119 DDP_COMPONENT_DITHER0,
124 static const unsigned int mt8173_mtk_ddp_main[] = {
126 DDP_COMPONENT_COLOR0,
135 static const unsigned int mt8173_mtk_ddp_ext[] = {
137 DDP_COMPONENT_COLOR1,
143 static const unsigned int mt8183_mtk_ddp_main[] = {
145 DDP_COMPONENT_OVL_2L0,
147 DDP_COMPONENT_COLOR0,
151 DDP_COMPONENT_DITHER0,
155 static const unsigned int mt8183_mtk_ddp_ext[] = {
156 DDP_COMPONENT_OVL_2L1,
161 static const unsigned int mt8186_mtk_ddp_main[] = {
164 DDP_COMPONENT_COLOR0,
168 DDP_COMPONENT_POSTMASK0,
169 DDP_COMPONENT_DITHER0,
173 static const unsigned int mt8186_mtk_ddp_ext[] = {
174 DDP_COMPONENT_OVL_2L0,
179 static const unsigned int mt8188_mtk_ddp_main[] = {
182 DDP_COMPONENT_COLOR0,
186 DDP_COMPONENT_POSTMASK0,
187 DDP_COMPONENT_DITHER0,
190 static const struct mtk_drm_route mt8188_mtk_ddp_main_routes[] = {
191 {0, DDP_COMPONENT_DP_INTF0},
192 {0, DDP_COMPONENT_DSI0},
195 static const unsigned int mt8192_mtk_ddp_main[] = {
197 DDP_COMPONENT_OVL_2L0,
199 DDP_COMPONENT_COLOR0,
203 DDP_COMPONENT_POSTMASK0,
204 DDP_COMPONENT_DITHER0,
208 static const unsigned int mt8192_mtk_ddp_ext[] = {
209 DDP_COMPONENT_OVL_2L2,
214 static const unsigned int mt8195_mtk_ddp_main[] = {
217 DDP_COMPONENT_COLOR0,
221 DDP_COMPONENT_DITHER0,
223 DDP_COMPONENT_MERGE0,
224 DDP_COMPONENT_DP_INTF0,
227 static const unsigned int mt8195_mtk_ddp_ext[] = {
228 DDP_COMPONENT_DRM_OVL_ADAPTOR,
229 DDP_COMPONENT_MERGE5,
230 DDP_COMPONENT_DP_INTF1,
233 static const struct mtk_mmsys_driver_data mt2701_mmsys_driver_data = {
234 .main_path = mt2701_mtk_ddp_main,
235 .main_len = ARRAY_SIZE(mt2701_mtk_ddp_main),
236 .ext_path = mt2701_mtk_ddp_ext,
237 .ext_len = ARRAY_SIZE(mt2701_mtk_ddp_ext),
238 .shadow_register = true,
242 static const struct mtk_mmsys_driver_data mt7623_mmsys_driver_data = {
243 .main_path = mt7623_mtk_ddp_main,
244 .main_len = ARRAY_SIZE(mt7623_mtk_ddp_main),
245 .ext_path = mt7623_mtk_ddp_ext,
246 .ext_len = ARRAY_SIZE(mt7623_mtk_ddp_ext),
247 .shadow_register = true,
251 static const struct mtk_mmsys_driver_data mt2712_mmsys_driver_data = {
252 .main_path = mt2712_mtk_ddp_main,
253 .main_len = ARRAY_SIZE(mt2712_mtk_ddp_main),
254 .ext_path = mt2712_mtk_ddp_ext,
255 .ext_len = ARRAY_SIZE(mt2712_mtk_ddp_ext),
256 .third_path = mt2712_mtk_ddp_third,
257 .third_len = ARRAY_SIZE(mt2712_mtk_ddp_third),
261 static const struct mtk_mmsys_driver_data mt8167_mmsys_driver_data = {
262 .main_path = mt8167_mtk_ddp_main,
263 .main_len = ARRAY_SIZE(mt8167_mtk_ddp_main),
267 static const struct mtk_mmsys_driver_data mt8173_mmsys_driver_data = {
268 .main_path = mt8173_mtk_ddp_main,
269 .main_len = ARRAY_SIZE(mt8173_mtk_ddp_main),
270 .ext_path = mt8173_mtk_ddp_ext,
271 .ext_len = ARRAY_SIZE(mt8173_mtk_ddp_ext),
275 static const struct mtk_mmsys_driver_data mt8183_mmsys_driver_data = {
276 .main_path = mt8183_mtk_ddp_main,
277 .main_len = ARRAY_SIZE(mt8183_mtk_ddp_main),
278 .ext_path = mt8183_mtk_ddp_ext,
279 .ext_len = ARRAY_SIZE(mt8183_mtk_ddp_ext),
283 static const struct mtk_mmsys_driver_data mt8186_mmsys_driver_data = {
284 .main_path = mt8186_mtk_ddp_main,
285 .main_len = ARRAY_SIZE(mt8186_mtk_ddp_main),
286 .ext_path = mt8186_mtk_ddp_ext,
287 .ext_len = ARRAY_SIZE(mt8186_mtk_ddp_ext),
291 static const struct mtk_mmsys_driver_data mt8188_vdosys0_driver_data = {
292 .main_path = mt8188_mtk_ddp_main,
293 .main_len = ARRAY_SIZE(mt8188_mtk_ddp_main),
294 .conn_routes = mt8188_mtk_ddp_main_routes,
295 .num_conn_routes = ARRAY_SIZE(mt8188_mtk_ddp_main_routes),
299 static const struct mtk_mmsys_driver_data mt8192_mmsys_driver_data = {
300 .main_path = mt8192_mtk_ddp_main,
301 .main_len = ARRAY_SIZE(mt8192_mtk_ddp_main),
302 .ext_path = mt8192_mtk_ddp_ext,
303 .ext_len = ARRAY_SIZE(mt8192_mtk_ddp_ext),
307 static const struct mtk_mmsys_driver_data mt8195_vdosys0_driver_data = {
308 .main_path = mt8195_mtk_ddp_main,
309 .main_len = ARRAY_SIZE(mt8195_mtk_ddp_main),
313 static const struct mtk_mmsys_driver_data mt8195_vdosys1_driver_data = {
314 .ext_path = mt8195_mtk_ddp_ext,
315 .ext_len = ARRAY_SIZE(mt8195_mtk_ddp_ext),
320 static const struct of_device_id mtk_drm_of_ids[] = {
321 { .compatible = "mediatek,mt2701-mmsys",
322 .data = &mt2701_mmsys_driver_data},
323 { .compatible = "mediatek,mt7623-mmsys",
324 .data = &mt7623_mmsys_driver_data},
325 { .compatible = "mediatek,mt2712-mmsys",
326 .data = &mt2712_mmsys_driver_data},
327 { .compatible = "mediatek,mt8167-mmsys",
328 .data = &mt8167_mmsys_driver_data},
329 { .compatible = "mediatek,mt8173-mmsys",
330 .data = &mt8173_mmsys_driver_data},
331 { .compatible = "mediatek,mt8183-mmsys",
332 .data = &mt8183_mmsys_driver_data},
333 { .compatible = "mediatek,mt8186-mmsys",
334 .data = &mt8186_mmsys_driver_data},
335 { .compatible = "mediatek,mt8188-vdosys0",
336 .data = &mt8188_vdosys0_driver_data},
337 { .compatible = "mediatek,mt8192-mmsys",
338 .data = &mt8192_mmsys_driver_data},
339 { .compatible = "mediatek,mt8195-mmsys",
340 .data = &mt8195_vdosys0_driver_data},
341 { .compatible = "mediatek,mt8195-vdosys0",
342 .data = &mt8195_vdosys0_driver_data},
343 { .compatible = "mediatek,mt8195-vdosys1",
344 .data = &mt8195_vdosys1_driver_data},
347 MODULE_DEVICE_TABLE(of, mtk_drm_of_ids);
349 static int mtk_drm_match(struct device *dev, void *data)
351 if (!strncmp(dev_name(dev), "mediatek-drm", sizeof("mediatek-drm") - 1))
356 static bool mtk_drm_get_all_drm_priv(struct device *dev)
358 struct mtk_drm_private *drm_priv = dev_get_drvdata(dev);
359 struct mtk_drm_private *all_drm_priv[MAX_CRTC];
360 struct mtk_drm_private *temp_drm_priv;
361 struct device_node *phandle = dev->parent->of_node;
362 const struct of_device_id *of_id;
363 struct device_node *node;
364 struct device *drm_dev;
365 unsigned int cnt = 0;
368 for_each_child_of_node(phandle->parent, node) {
369 struct platform_device *pdev;
371 of_id = of_match_node(mtk_drm_of_ids, node);
375 pdev = of_find_device_by_node(node);
379 drm_dev = device_find_child(&pdev->dev, NULL, mtk_drm_match);
383 temp_drm_priv = dev_get_drvdata(drm_dev);
387 if (temp_drm_priv->data->main_len)
388 all_drm_priv[CRTC_MAIN] = temp_drm_priv;
389 else if (temp_drm_priv->data->ext_len)
390 all_drm_priv[CRTC_EXT] = temp_drm_priv;
391 else if (temp_drm_priv->data->third_len)
392 all_drm_priv[CRTC_THIRD] = temp_drm_priv;
394 if (temp_drm_priv->mtk_drm_bound)
401 if (drm_priv->data->mmsys_dev_num == cnt) {
402 for (i = 0; i < cnt; i++)
403 for (j = 0; j < cnt; j++)
404 all_drm_priv[j]->all_drm_private[i] = all_drm_priv[i];
412 static bool mtk_drm_find_mmsys_comp(struct mtk_drm_private *private, int comp_id)
414 const struct mtk_mmsys_driver_data *drv_data = private->data;
417 if (drv_data->main_path)
418 for (i = 0; i < drv_data->main_len; i++)
419 if (drv_data->main_path[i] == comp_id)
422 if (drv_data->ext_path)
423 for (i = 0; i < drv_data->ext_len; i++)
424 if (drv_data->ext_path[i] == comp_id)
427 if (drv_data->third_path)
428 for (i = 0; i < drv_data->third_len; i++)
429 if (drv_data->third_path[i] == comp_id)
432 if (drv_data->num_conn_routes)
433 for (i = 0; i < drv_data->num_conn_routes; i++)
434 if (drv_data->conn_routes[i].route_ddp == comp_id)
440 static int mtk_drm_kms_init(struct drm_device *drm)
442 struct mtk_drm_private *private = drm->dev_private;
443 struct mtk_drm_private *priv_n;
444 struct device *dma_dev = NULL;
447 if (drm_firmware_drivers_only())
450 ret = drmm_mode_config_init(drm);
454 drm->mode_config.min_width = 64;
455 drm->mode_config.min_height = 64;
458 * set max width and height as default value(4096x4096).
459 * this value would be used to check framebuffer size limitation
460 * at drm_mode_addfb().
462 drm->mode_config.max_width = 4096;
463 drm->mode_config.max_height = 4096;
464 drm->mode_config.funcs = &mtk_drm_mode_config_funcs;
465 drm->mode_config.helper_private = &mtk_drm_mode_config_helpers;
467 for (i = 0; i < private->data->mmsys_dev_num; i++) {
468 drm->dev_private = private->all_drm_private[i];
469 ret = component_bind_all(private->all_drm_private[i]->dev, drm);
475 * Ensure internal panels are at the top of the connector list before
478 drm_helper_move_panel_connectors_to_head(drm);
481 * 1. We currently support two fixed data streams, each optional,
482 * and each statically assigned to a crtc:
483 * OVL0 -> COLOR0 -> AAL -> OD -> RDMA0 -> UFOE -> DSI0 ...
484 * 2. For multi mmsys architecture, crtc path data are located in
485 * different drm private data structures. Loop through crtc index to
486 * create crtc from the main path and then ext_path and finally the
489 for (i = 0; i < MAX_CRTC; i++) {
490 for (j = 0; j < private->data->mmsys_dev_num; j++) {
491 priv_n = private->all_drm_private[j];
493 if (i == CRTC_MAIN && priv_n->data->main_len) {
494 ret = mtk_drm_crtc_create(drm, priv_n->data->main_path,
495 priv_n->data->main_len, j,
496 priv_n->data->conn_routes,
497 priv_n->data->num_conn_routes);
499 goto err_component_unbind;
502 } else if (i == CRTC_EXT && priv_n->data->ext_len) {
503 ret = mtk_drm_crtc_create(drm, priv_n->data->ext_path,
504 priv_n->data->ext_len, j, NULL, 0);
506 goto err_component_unbind;
509 } else if (i == CRTC_THIRD && priv_n->data->third_len) {
510 ret = mtk_drm_crtc_create(drm, priv_n->data->third_path,
511 priv_n->data->third_len, j, NULL, 0);
513 goto err_component_unbind;
520 /* Use OVL device for all DMA memory allocations */
521 dma_dev = mtk_drm_crtc_dma_dev_get(drm_crtc_from_index(drm, 0));
524 dev_err(drm->dev, "Need at least one OVL device\n");
525 goto err_component_unbind;
528 for (i = 0; i < private->data->mmsys_dev_num; i++)
529 private->all_drm_private[i]->dma_dev = dma_dev;
532 * Configure the DMA segment size to make sure we get contiguous IOVA
533 * when importing PRIME buffers.
535 ret = dma_set_max_seg_size(dma_dev, UINT_MAX);
537 dev_err(dma_dev, "Failed to set DMA segment size\n");
538 goto err_component_unbind;
541 ret = drm_vblank_init(drm, MAX_CRTC);
543 goto err_component_unbind;
545 drm_kms_helper_poll_init(drm);
546 drm_mode_config_reset(drm);
550 err_component_unbind:
551 for (i = 0; i < private->data->mmsys_dev_num; i++)
552 component_unbind_all(private->all_drm_private[i]->dev, drm);
554 for (i = 0; i < private->data->mmsys_dev_num; i++)
555 put_device(private->all_drm_private[i]->mutex_dev);
560 static void mtk_drm_kms_deinit(struct drm_device *drm)
562 drm_kms_helper_poll_fini(drm);
563 drm_atomic_helper_shutdown(drm);
565 component_unbind_all(drm->dev, drm);
568 DEFINE_DRM_GEM_FOPS(mtk_drm_fops);
571 * We need to override this because the device used to import the memory is
572 * not dev->dev, as drm_gem_prime_import() expects.
574 static struct drm_gem_object *mtk_drm_gem_prime_import(struct drm_device *dev,
575 struct dma_buf *dma_buf)
577 struct mtk_drm_private *private = dev->dev_private;
579 return drm_gem_prime_import_dev(dev, dma_buf, private->dma_dev);
582 static const struct drm_driver mtk_drm_driver = {
583 .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC,
585 .dumb_create = mtk_drm_gem_dumb_create,
587 .gem_prime_import = mtk_drm_gem_prime_import,
588 .gem_prime_import_sg_table = mtk_gem_prime_import_sg_table,
589 .fops = &mtk_drm_fops,
594 .major = DRIVER_MAJOR,
595 .minor = DRIVER_MINOR,
598 static int compare_dev(struct device *dev, void *data)
600 return dev == (struct device *)data;
603 static int mtk_drm_bind(struct device *dev)
605 struct mtk_drm_private *private = dev_get_drvdata(dev);
606 struct platform_device *pdev;
607 struct drm_device *drm;
610 pdev = of_find_device_by_node(private->mutex_node);
612 dev_err(dev, "Waiting for disp-mutex device %pOF\n",
613 private->mutex_node);
614 of_node_put(private->mutex_node);
615 return -EPROBE_DEFER;
618 private->mutex_dev = &pdev->dev;
619 private->mtk_drm_bound = true;
622 if (!mtk_drm_get_all_drm_priv(dev))
625 drm = drm_dev_alloc(&mtk_drm_driver, dev);
629 private->drm_master = true;
630 drm->dev_private = private;
631 for (i = 0; i < private->data->mmsys_dev_num; i++)
632 private->all_drm_private[i]->drm = drm;
634 ret = mtk_drm_kms_init(drm);
638 ret = drm_dev_register(drm, 0);
642 drm_fbdev_generic_setup(drm, 32);
647 mtk_drm_kms_deinit(drm);
654 static void mtk_drm_unbind(struct device *dev)
656 struct mtk_drm_private *private = dev_get_drvdata(dev);
658 /* for multi mmsys dev, unregister drm dev in mmsys master */
659 if (private->drm_master) {
660 drm_dev_unregister(private->drm);
661 mtk_drm_kms_deinit(private->drm);
662 drm_dev_put(private->drm);
664 private->mtk_drm_bound = false;
665 private->drm_master = false;
669 static const struct component_master_ops mtk_drm_ops = {
670 .bind = mtk_drm_bind,
671 .unbind = mtk_drm_unbind,
674 static const struct of_device_id mtk_ddp_comp_dt_ids[] = {
675 { .compatible = "mediatek,mt8167-disp-aal",
676 .data = (void *)MTK_DISP_AAL},
677 { .compatible = "mediatek,mt8173-disp-aal",
678 .data = (void *)MTK_DISP_AAL},
679 { .compatible = "mediatek,mt8183-disp-aal",
680 .data = (void *)MTK_DISP_AAL},
681 { .compatible = "mediatek,mt8192-disp-aal",
682 .data = (void *)MTK_DISP_AAL},
683 { .compatible = "mediatek,mt8167-disp-ccorr",
684 .data = (void *)MTK_DISP_CCORR },
685 { .compatible = "mediatek,mt8183-disp-ccorr",
686 .data = (void *)MTK_DISP_CCORR },
687 { .compatible = "mediatek,mt8192-disp-ccorr",
688 .data = (void *)MTK_DISP_CCORR },
689 { .compatible = "mediatek,mt2701-disp-color",
690 .data = (void *)MTK_DISP_COLOR },
691 { .compatible = "mediatek,mt8167-disp-color",
692 .data = (void *)MTK_DISP_COLOR },
693 { .compatible = "mediatek,mt8173-disp-color",
694 .data = (void *)MTK_DISP_COLOR },
695 { .compatible = "mediatek,mt8167-disp-dither",
696 .data = (void *)MTK_DISP_DITHER },
697 { .compatible = "mediatek,mt8183-disp-dither",
698 .data = (void *)MTK_DISP_DITHER },
699 { .compatible = "mediatek,mt8195-disp-dsc",
700 .data = (void *)MTK_DISP_DSC },
701 { .compatible = "mediatek,mt8167-disp-gamma",
702 .data = (void *)MTK_DISP_GAMMA, },
703 { .compatible = "mediatek,mt8173-disp-gamma",
704 .data = (void *)MTK_DISP_GAMMA, },
705 { .compatible = "mediatek,mt8183-disp-gamma",
706 .data = (void *)MTK_DISP_GAMMA, },
707 { .compatible = "mediatek,mt8195-disp-merge",
708 .data = (void *)MTK_DISP_MERGE },
709 { .compatible = "mediatek,mt2701-disp-mutex",
710 .data = (void *)MTK_DISP_MUTEX },
711 { .compatible = "mediatek,mt2712-disp-mutex",
712 .data = (void *)MTK_DISP_MUTEX },
713 { .compatible = "mediatek,mt8167-disp-mutex",
714 .data = (void *)MTK_DISP_MUTEX },
715 { .compatible = "mediatek,mt8173-disp-mutex",
716 .data = (void *)MTK_DISP_MUTEX },
717 { .compatible = "mediatek,mt8183-disp-mutex",
718 .data = (void *)MTK_DISP_MUTEX },
719 { .compatible = "mediatek,mt8186-disp-mutex",
720 .data = (void *)MTK_DISP_MUTEX },
721 { .compatible = "mediatek,mt8188-disp-mutex",
722 .data = (void *)MTK_DISP_MUTEX },
723 { .compatible = "mediatek,mt8192-disp-mutex",
724 .data = (void *)MTK_DISP_MUTEX },
725 { .compatible = "mediatek,mt8195-disp-mutex",
726 .data = (void *)MTK_DISP_MUTEX },
727 { .compatible = "mediatek,mt8173-disp-od",
728 .data = (void *)MTK_DISP_OD },
729 { .compatible = "mediatek,mt2701-disp-ovl",
730 .data = (void *)MTK_DISP_OVL },
731 { .compatible = "mediatek,mt8167-disp-ovl",
732 .data = (void *)MTK_DISP_OVL },
733 { .compatible = "mediatek,mt8173-disp-ovl",
734 .data = (void *)MTK_DISP_OVL },
735 { .compatible = "mediatek,mt8183-disp-ovl",
736 .data = (void *)MTK_DISP_OVL },
737 { .compatible = "mediatek,mt8192-disp-ovl",
738 .data = (void *)MTK_DISP_OVL },
739 { .compatible = "mediatek,mt8183-disp-ovl-2l",
740 .data = (void *)MTK_DISP_OVL_2L },
741 { .compatible = "mediatek,mt8192-disp-ovl-2l",
742 .data = (void *)MTK_DISP_OVL_2L },
743 { .compatible = "mediatek,mt8192-disp-postmask",
744 .data = (void *)MTK_DISP_POSTMASK },
745 { .compatible = "mediatek,mt2701-disp-pwm",
746 .data = (void *)MTK_DISP_BLS },
747 { .compatible = "mediatek,mt8167-disp-pwm",
748 .data = (void *)MTK_DISP_PWM },
749 { .compatible = "mediatek,mt8173-disp-pwm",
750 .data = (void *)MTK_DISP_PWM },
751 { .compatible = "mediatek,mt2701-disp-rdma",
752 .data = (void *)MTK_DISP_RDMA },
753 { .compatible = "mediatek,mt8167-disp-rdma",
754 .data = (void *)MTK_DISP_RDMA },
755 { .compatible = "mediatek,mt8173-disp-rdma",
756 .data = (void *)MTK_DISP_RDMA },
757 { .compatible = "mediatek,mt8183-disp-rdma",
758 .data = (void *)MTK_DISP_RDMA },
759 { .compatible = "mediatek,mt8195-disp-rdma",
760 .data = (void *)MTK_DISP_RDMA },
761 { .compatible = "mediatek,mt8173-disp-ufoe",
762 .data = (void *)MTK_DISP_UFOE },
763 { .compatible = "mediatek,mt8173-disp-wdma",
764 .data = (void *)MTK_DISP_WDMA },
765 { .compatible = "mediatek,mt2701-dpi",
766 .data = (void *)MTK_DPI },
767 { .compatible = "mediatek,mt8167-dsi",
768 .data = (void *)MTK_DSI },
769 { .compatible = "mediatek,mt8173-dpi",
770 .data = (void *)MTK_DPI },
771 { .compatible = "mediatek,mt8183-dpi",
772 .data = (void *)MTK_DPI },
773 { .compatible = "mediatek,mt8186-dpi",
774 .data = (void *)MTK_DPI },
775 { .compatible = "mediatek,mt8188-dp-intf",
776 .data = (void *)MTK_DP_INTF },
777 { .compatible = "mediatek,mt8192-dpi",
778 .data = (void *)MTK_DPI },
779 { .compatible = "mediatek,mt8195-dp-intf",
780 .data = (void *)MTK_DP_INTF },
781 { .compatible = "mediatek,mt2701-dsi",
782 .data = (void *)MTK_DSI },
783 { .compatible = "mediatek,mt8173-dsi",
784 .data = (void *)MTK_DSI },
785 { .compatible = "mediatek,mt8183-dsi",
786 .data = (void *)MTK_DSI },
787 { .compatible = "mediatek,mt8186-dsi",
788 .data = (void *)MTK_DSI },
789 { .compatible = "mediatek,mt8188-dsi",
790 .data = (void *)MTK_DSI },
794 static int mtk_drm_probe(struct platform_device *pdev)
796 struct device *dev = &pdev->dev;
797 struct device_node *phandle = dev->parent->of_node;
798 const struct of_device_id *of_id;
799 struct mtk_drm_private *private;
800 struct device_node *node;
801 struct component_match *match = NULL;
802 struct platform_device *ovl_adaptor;
806 private = devm_kzalloc(dev, sizeof(*private), GFP_KERNEL);
810 private->mmsys_dev = dev->parent;
811 if (!private->mmsys_dev) {
812 dev_err(dev, "Failed to get MMSYS device\n");
816 of_id = of_match_node(mtk_drm_of_ids, phandle);
820 private->data = of_id->data;
822 private->all_drm_private = devm_kmalloc_array(dev, private->data->mmsys_dev_num,
823 sizeof(*private->all_drm_private),
825 if (!private->all_drm_private)
828 /* Bringup ovl_adaptor */
829 if (mtk_drm_find_mmsys_comp(private, DDP_COMPONENT_DRM_OVL_ADAPTOR)) {
830 ovl_adaptor = platform_device_register_data(dev, "mediatek-disp-ovl-adaptor",
832 (void *)private->mmsys_dev,
833 sizeof(*private->mmsys_dev));
834 private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR].dev = &ovl_adaptor->dev;
835 mtk_ddp_comp_init(NULL, &private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR],
836 DDP_COMPONENT_DRM_OVL_ADAPTOR);
837 component_match_add(dev, &match, compare_dev, &ovl_adaptor->dev);
840 /* Iterate over sibling DISP function blocks */
841 for_each_child_of_node(phandle->parent, node) {
842 const struct of_device_id *of_id;
843 enum mtk_ddp_comp_type comp_type;
846 of_id = of_match_node(mtk_ddp_comp_dt_ids, node);
850 if (!of_device_is_available(node)) {
851 dev_dbg(dev, "Skipping disabled component %pOF\n",
856 comp_type = (enum mtk_ddp_comp_type)(uintptr_t)of_id->data;
858 if (comp_type == MTK_DISP_MUTEX) {
861 id = of_alias_get_id(node, "mutex");
862 if (id < 0 || id == private->data->mmsys_id) {
863 private->mutex_node = of_node_get(node);
864 dev_dbg(dev, "get mutex for mmsys %d", private->data->mmsys_id);
869 comp_id = mtk_ddp_comp_get_id(node, comp_type);
871 dev_warn(dev, "Skipping unknown component %pOF\n",
876 if (!mtk_drm_find_mmsys_comp(private, comp_id))
879 private->comp_node[comp_id] = of_node_get(node);
882 * Currently only the AAL, CCORR, COLOR, GAMMA, MERGE, OVL, RDMA, DSI, and DPI
883 * blocks have separate component platform drivers and initialize their own
884 * DDP component structure. The others are initialized here.
886 if (comp_type == MTK_DISP_AAL ||
887 comp_type == MTK_DISP_CCORR ||
888 comp_type == MTK_DISP_COLOR ||
889 comp_type == MTK_DISP_GAMMA ||
890 comp_type == MTK_DISP_MERGE ||
891 comp_type == MTK_DISP_OVL ||
892 comp_type == MTK_DISP_OVL_2L ||
893 comp_type == MTK_DISP_OVL_ADAPTOR ||
894 comp_type == MTK_DISP_RDMA ||
895 comp_type == MTK_DP_INTF ||
896 comp_type == MTK_DPI ||
897 comp_type == MTK_DSI) {
898 dev_info(dev, "Adding component match for %pOF\n",
900 drm_of_component_match_add(dev, &match, component_compare_of,
904 ret = mtk_ddp_comp_init(node, &private->ddp_comp[comp_id], comp_id);
911 if (!private->mutex_node) {
912 dev_err(dev, "Failed to find disp-mutex node\n");
917 pm_runtime_enable(dev);
919 platform_set_drvdata(pdev, private);
921 ret = component_master_add_with_match(dev, &mtk_drm_ops, match);
928 pm_runtime_disable(dev);
930 of_node_put(private->mutex_node);
931 for (i = 0; i < DDP_COMPONENT_DRM_ID_MAX; i++)
932 of_node_put(private->comp_node[i]);
936 static void mtk_drm_remove(struct platform_device *pdev)
938 struct mtk_drm_private *private = platform_get_drvdata(pdev);
941 component_master_del(&pdev->dev, &mtk_drm_ops);
942 pm_runtime_disable(&pdev->dev);
943 of_node_put(private->mutex_node);
944 for (i = 0; i < DDP_COMPONENT_DRM_ID_MAX; i++)
945 of_node_put(private->comp_node[i]);
948 static int mtk_drm_sys_prepare(struct device *dev)
950 struct mtk_drm_private *private = dev_get_drvdata(dev);
951 struct drm_device *drm = private->drm;
953 if (private->drm_master)
954 return drm_mode_config_helper_suspend(drm);
959 static void mtk_drm_sys_complete(struct device *dev)
961 struct mtk_drm_private *private = dev_get_drvdata(dev);
962 struct drm_device *drm = private->drm;
965 if (private->drm_master)
966 ret = drm_mode_config_helper_resume(drm);
968 dev_err(dev, "Failed to resume\n");
971 static const struct dev_pm_ops mtk_drm_pm_ops = {
972 .prepare = mtk_drm_sys_prepare,
973 .complete = mtk_drm_sys_complete,
976 static struct platform_driver mtk_drm_platform_driver = {
977 .probe = mtk_drm_probe,
978 .remove_new = mtk_drm_remove,
980 .name = "mediatek-drm",
981 .pm = &mtk_drm_pm_ops,
985 static struct platform_driver * const mtk_drm_drivers[] = {
986 &mtk_disp_aal_driver,
987 &mtk_disp_ccorr_driver,
988 &mtk_disp_color_driver,
989 &mtk_disp_gamma_driver,
990 &mtk_disp_merge_driver,
991 &mtk_disp_ovl_adaptor_driver,
992 &mtk_disp_ovl_driver,
993 &mtk_disp_rdma_driver,
995 &mtk_drm_platform_driver,
998 &mtk_mdp_rdma_driver,
1002 static int __init mtk_drm_init(void)
1004 return platform_register_drivers(mtk_drm_drivers,
1005 ARRAY_SIZE(mtk_drm_drivers));
1008 static void __exit mtk_drm_exit(void)
1010 platform_unregister_drivers(mtk_drm_drivers,
1011 ARRAY_SIZE(mtk_drm_drivers));
1014 module_init(mtk_drm_init);
1015 module_exit(mtk_drm_exit);
1018 MODULE_DESCRIPTION("Mediatek SoC DRM driver");
1019 MODULE_LICENSE("GPL v2");