Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm/mediatek: Implement OF graphs support for display paths

It is impossible to add each and every possible DDP path combination
for each and every possible combination of SoC and board: right now,
this driver hardcodes configuration for 10 SoCs and this is going to
grow larger and larger, and with new hacks like the introduction of
mtk_drm_route which is anyway not enough for all final routes as the
DSI cannot be connected to MERGE if it's not a dual-DSI, or enabling
DSC preventively doesn't work if the display doesn't support it, or
others.

Since practically all display IPs in MediaTek SoCs support being
interconnected with different instances of other, or the same, IPs
or with different IPs and in different combinations, the final DDP
pipeline is effectively a board specific configuration.

Implement OF graphs support to the mediatek-drm drivers, allowing to
stop hardcoding the paths, and preventing this driver to get a huge
amount of arrays for each board and SoC combination, also paving the
way to share the same mtk_mmsys_driver_data between multiple SoCs,
making it more straightforward to add support for new chips.

Note that the OVL_ADAPTOR software component driver needs relatively
big changes in order to fully support OF Graphs (and more SoCs anyway)
and such changes will come at a later time.
As of now, the mtk_disp_ovl_adaptor driver takes the MERGE components
(for example, on mt8195, merge 1 to 4) dynamically so, even though
later updates to the ovl-adaptor driver will *not* require bindings
changes, the merge1-4 will be temporarily omitted in the graph for the
MT8195 SoC.

This means that an example graph for this SoC looks like:

mdp_rdma (0 ~ 7) -> padding (0 ~ 7) -> ethdr -> merge5

and the resulting path in this driver will be `ovl_adaptor -> merge5`

Later updates to the ovl adaptor will expand it to support more SoCs
and, in turn, to also fully support graphs.

Reviewed-by: Alexandre Mergnat <amergnat@baylibre.com>
Tested-by: Alexandre Mergnat <amergnat@baylibre.com>
Acked-by: Sui Jingfeng <sui.jingfeng@linux.dev>
Tested-by: Michael Walle <mwalle@kernel.org> # on kontron-sbc-i1200
Reviewed-by: CK Hu <ck.hu@mediatek.com>
Signed-off-by: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Link: https://patchwork.kernel.org/project/dri-devel/patch/20241017103809.156056-4-angelogioacchino.delregno@collabora.com/
Signed-off-by: Chun-Kuang Hu <chunkuang.hu@kernel.org>

authored by

AngeloGioacchino Del Regno and committed by
Chun-Kuang Hu
4c932840 2b6433f3

+312 -22
+1
drivers/gpu/drm/mediatek/mtk_disp_drv.h
··· 108 108 109 109 void mtk_ovl_adaptor_add_comp(struct device *dev, struct mtk_mutex *mutex); 110 110 void mtk_ovl_adaptor_remove_comp(struct device *dev, struct mtk_mutex *mutex); 111 + bool mtk_ovl_adaptor_is_comp_present(struct device_node *node); 111 112 void mtk_ovl_adaptor_connect(struct device *dev, struct device *mmsys_dev, 112 113 unsigned int next); 113 114 void mtk_ovl_adaptor_disconnect(struct device *dev, struct device *mmsys_dev,
+38 -5
drivers/gpu/drm/mediatek/mtk_disp_ovl_adaptor.c
··· 490 490 return dev->of_node == data; 491 491 } 492 492 493 + static int ovl_adaptor_of_get_ddp_comp_type(struct device_node *node, 494 + enum mtk_ovl_adaptor_comp_type *ctype) 495 + { 496 + const struct of_device_id *of_id = of_match_node(mtk_ovl_adaptor_comp_dt_ids, node); 497 + 498 + if (!of_id) 499 + return -EINVAL; 500 + 501 + *ctype = (enum mtk_ovl_adaptor_comp_type)((uintptr_t)of_id->data); 502 + 503 + return 0; 504 + } 505 + 506 + bool mtk_ovl_adaptor_is_comp_present(struct device_node *node) 507 + { 508 + enum mtk_ovl_adaptor_comp_type type; 509 + int ret; 510 + 511 + ret = ovl_adaptor_of_get_ddp_comp_type(node, &type); 512 + if (ret) 513 + return false; 514 + 515 + if (type >= OVL_ADAPTOR_TYPE_NUM) 516 + return false; 517 + 518 + /* 519 + * In the context of mediatek-drm, ETHDR, MDP_RDMA and Padding are 520 + * used exclusively by OVL Adaptor: if this component is not one of 521 + * those, it's likely not an OVL Adaptor path. 522 + */ 523 + return type == OVL_ADAPTOR_TYPE_ETHDR || 524 + type == OVL_ADAPTOR_TYPE_MDP_RDMA || 525 + type == OVL_ADAPTOR_TYPE_PADDING; 526 + } 527 + 493 528 static int ovl_adaptor_comp_init(struct device *dev, struct component_match **match) 494 529 { 495 530 struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev); ··· 534 499 parent = dev->parent->parent->of_node->parent; 535 500 536 501 for_each_child_of_node_scoped(parent, node) { 537 - const struct of_device_id *of_id; 538 502 enum mtk_ovl_adaptor_comp_type type; 539 - int id; 503 + int id, ret; 540 504 541 - of_id = of_match_node(mtk_ovl_adaptor_comp_dt_ids, node); 542 - if (!of_id) 505 + ret = ovl_adaptor_of_get_ddp_comp_type(node, &type); 506 + if (ret) 543 507 continue; 544 508 545 509 if (!of_device_is_available(node)) { ··· 547 513 continue; 548 514 } 549 515 550 - type = (enum mtk_ovl_adaptor_comp_type)(uintptr_t)of_id->data; 551 516 id = ovl_adaptor_comp_get_id(dev, node, type); 552 517 if (id < 0) { 553 518 dev_warn(dev, "Skipping unknown component %pOF\n",
+14 -7
drivers/gpu/drm/mediatek/mtk_dpi.c
··· 704 704 enum drm_bridge_attach_flags flags) 705 705 { 706 706 struct mtk_dpi *dpi = bridge_to_dpi(bridge); 707 + int ret; 708 + 709 + dpi->next_bridge = devm_drm_of_get_bridge(dpi->dev, dpi->dev->of_node, 1, -1); 710 + if (IS_ERR(dpi->next_bridge)) { 711 + ret = PTR_ERR(dpi->next_bridge); 712 + if (ret == -EPROBE_DEFER) 713 + return ret; 714 + 715 + /* Old devicetree has only one endpoint */ 716 + dpi->next_bridge = devm_drm_of_get_bridge(dpi->dev, dpi->dev->of_node, 0, 0); 717 + if (IS_ERR(dpi->next_bridge)) 718 + return dev_err_probe(dpi->dev, PTR_ERR(dpi->next_bridge), 719 + "Failed to get bridge\n"); 720 + } 707 721 708 722 return drm_bridge_attach(bridge->encoder, dpi->next_bridge, 709 723 &dpi->bridge, flags); ··· 1071 1057 dpi->irq = platform_get_irq(pdev, 0); 1072 1058 if (dpi->irq < 0) 1073 1059 return dpi->irq; 1074 - 1075 - dpi->next_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 0, 0); 1076 - if (IS_ERR(dpi->next_bridge)) 1077 - return dev_err_probe(dev, PTR_ERR(dpi->next_bridge), 1078 - "Failed to get bridge\n"); 1079 - 1080 - dev_info(dev, "Found bridge node: %pOF\n", dpi->next_bridge->of_node); 1081 1060 1082 1061 platform_set_drvdata(pdev, dpi); 1083 1062
+247 -6
drivers/gpu/drm/mediatek/mtk_drm_drv.c
··· 26 26 27 27 #include "mtk_crtc.h" 28 28 #include "mtk_ddp_comp.h" 29 + #include "mtk_disp_drv.h" 29 30 #include "mtk_drm_drv.h" 30 31 #include "mtk_gem.h" 31 32 ··· 819 818 { } 820 819 }; 821 820 821 + static int mtk_drm_of_get_ddp_comp_type(struct device_node *node, enum mtk_ddp_comp_type *ctype) 822 + { 823 + const struct of_device_id *of_id = of_match_node(mtk_ddp_comp_dt_ids, node); 824 + 825 + if (!of_id) 826 + return -EINVAL; 827 + 828 + *ctype = (enum mtk_ddp_comp_type)((uintptr_t)of_id->data); 829 + 830 + return 0; 831 + } 832 + 833 + static int mtk_drm_of_get_ddp_ep_cid(struct device_node *node, 834 + int output_port, enum mtk_crtc_path crtc_path, 835 + struct device_node **next, unsigned int *cid) 836 + { 837 + struct device_node *ep_dev_node, *ep_out; 838 + enum mtk_ddp_comp_type comp_type; 839 + int ret; 840 + 841 + ep_out = of_graph_get_endpoint_by_regs(node, output_port, crtc_path); 842 + if (!ep_out) 843 + return -ENOENT; 844 + 845 + ep_dev_node = of_graph_get_remote_port_parent(ep_out); 846 + of_node_put(ep_out); 847 + if (!ep_dev_node) 848 + return -EINVAL; 849 + 850 + /* 851 + * Pass the next node pointer regardless of failures in the later code 852 + * so that if this function is called in a loop it will walk through all 853 + * of the subsequent endpoints anyway. 854 + */ 855 + *next = ep_dev_node; 856 + 857 + if (!of_device_is_available(ep_dev_node)) 858 + return -ENODEV; 859 + 860 + ret = mtk_drm_of_get_ddp_comp_type(ep_dev_node, &comp_type); 861 + if (ret) { 862 + if (mtk_ovl_adaptor_is_comp_present(ep_dev_node)) { 863 + *cid = (unsigned int)DDP_COMPONENT_DRM_OVL_ADAPTOR; 864 + return 0; 865 + } 866 + return ret; 867 + } 868 + 869 + ret = mtk_ddp_comp_get_id(ep_dev_node, comp_type); 870 + if (ret < 0) 871 + return ret; 872 + 873 + /* All ok! Pass the Component ID to the caller. */ 874 + *cid = (unsigned int)ret; 875 + 876 + return 0; 877 + } 878 + 879 + /** 880 + * mtk_drm_of_ddp_path_build_one - Build a Display HW Pipeline for a CRTC Path 881 + * @dev: The mediatek-drm device 882 + * @cpath: CRTC Path relative to a VDO or MMSYS 883 + * @out_path: Pointer to an array that will contain the new pipeline 884 + * @out_path_len: Number of entries in the pipeline array 885 + * 886 + * MediaTek SoCs can use different DDP hardware pipelines (or paths) depending 887 + * on the board-specific desired display configuration; this function walks 888 + * through all of the output endpoints starting from a VDO or MMSYS hardware 889 + * instance and builds the right pipeline as specified in device trees. 890 + * 891 + * Return: 892 + * * %0 - Display HW Pipeline successfully built and validated 893 + * * %-ENOENT - Display pipeline was not specified in device tree 894 + * * %-EINVAL - Display pipeline built but validation failed 895 + * * %-ENOMEM - Failure to allocate pipeline array to pass to the caller 896 + */ 897 + static int mtk_drm_of_ddp_path_build_one(struct device *dev, enum mtk_crtc_path cpath, 898 + const unsigned int **out_path, 899 + unsigned int *out_path_len) 900 + { 901 + struct device_node *next, *prev, *vdo = dev->parent->of_node; 902 + unsigned int temp_path[DDP_COMPONENT_DRM_ID_MAX] = { 0 }; 903 + unsigned int *final_ddp_path; 904 + unsigned short int idx = 0; 905 + bool ovl_adaptor_comp_added = false; 906 + int ret; 907 + 908 + /* Get the first entry for the temp_path array */ 909 + ret = mtk_drm_of_get_ddp_ep_cid(vdo, 0, cpath, &next, &temp_path[idx]); 910 + if (ret) { 911 + if (next && temp_path[idx] == DDP_COMPONENT_DRM_OVL_ADAPTOR) { 912 + dev_dbg(dev, "Adding OVL Adaptor for %pOF\n", next); 913 + ovl_adaptor_comp_added = true; 914 + } else { 915 + if (next) 916 + dev_err(dev, "Invalid component %pOF\n", next); 917 + else 918 + dev_err(dev, "Cannot find first endpoint for path %d\n", cpath); 919 + 920 + return ret; 921 + } 922 + } 923 + idx++; 924 + 925 + /* 926 + * Walk through port outputs until we reach the last valid mediatek-drm component. 927 + * To be valid, this must end with an "invalid" component that is a display node. 928 + */ 929 + do { 930 + prev = next; 931 + ret = mtk_drm_of_get_ddp_ep_cid(next, 1, cpath, &next, &temp_path[idx]); 932 + of_node_put(prev); 933 + if (ret) { 934 + of_node_put(next); 935 + break; 936 + } 937 + 938 + /* 939 + * If this is an OVL adaptor exclusive component and one of those 940 + * was already added, don't add another instance of the generic 941 + * DDP_COMPONENT_OVL_ADAPTOR, as this is used only to decide whether 942 + * to probe that component master driver of which only one instance 943 + * is needed and possible. 944 + */ 945 + if (temp_path[idx] == DDP_COMPONENT_DRM_OVL_ADAPTOR) { 946 + if (!ovl_adaptor_comp_added) 947 + ovl_adaptor_comp_added = true; 948 + else 949 + idx--; 950 + } 951 + } while (++idx < DDP_COMPONENT_DRM_ID_MAX); 952 + 953 + /* 954 + * The device component might not be enabled: in that case, don't 955 + * check the last entry and just report that the device is missing. 956 + */ 957 + if (ret == -ENODEV) 958 + return ret; 959 + 960 + /* If the last entry is not a final display output, the configuration is wrong */ 961 + switch (temp_path[idx - 1]) { 962 + case DDP_COMPONENT_DP_INTF0: 963 + case DDP_COMPONENT_DP_INTF1: 964 + case DDP_COMPONENT_DPI0: 965 + case DDP_COMPONENT_DPI1: 966 + case DDP_COMPONENT_DSI0: 967 + case DDP_COMPONENT_DSI1: 968 + case DDP_COMPONENT_DSI2: 969 + case DDP_COMPONENT_DSI3: 970 + break; 971 + default: 972 + dev_err(dev, "Invalid display hw pipeline. Last component: %d (ret=%d)\n", 973 + temp_path[idx - 1], ret); 974 + return -EINVAL; 975 + } 976 + 977 + final_ddp_path = devm_kmemdup(dev, temp_path, idx * sizeof(temp_path[0]), GFP_KERNEL); 978 + if (!final_ddp_path) 979 + return -ENOMEM; 980 + 981 + dev_dbg(dev, "Display HW Pipeline built with %d components.\n", idx); 982 + 983 + /* Pipeline built! */ 984 + *out_path = final_ddp_path; 985 + *out_path_len = idx; 986 + 987 + return 0; 988 + } 989 + 990 + static int mtk_drm_of_ddp_path_build(struct device *dev, struct device_node *node, 991 + struct mtk_mmsys_driver_data *data) 992 + { 993 + struct device_node *ep_node; 994 + struct of_endpoint of_ep; 995 + bool output_present[MAX_CRTC] = { false }; 996 + int ret; 997 + 998 + for_each_endpoint_of_node(node, ep_node) { 999 + ret = of_graph_parse_endpoint(ep_node, &of_ep); 1000 + if (ret) { 1001 + dev_err_probe(dev, ret, "Cannot parse endpoint\n"); 1002 + break; 1003 + } 1004 + 1005 + if (of_ep.id >= MAX_CRTC) { 1006 + ret = dev_err_probe(dev, -EINVAL, 1007 + "Invalid endpoint%u number\n", of_ep.port); 1008 + break; 1009 + } 1010 + 1011 + output_present[of_ep.id] = true; 1012 + } 1013 + 1014 + if (ret) { 1015 + of_node_put(ep_node); 1016 + return ret; 1017 + } 1018 + 1019 + if (output_present[CRTC_MAIN]) { 1020 + ret = mtk_drm_of_ddp_path_build_one(dev, CRTC_MAIN, 1021 + &data->main_path, &data->main_len); 1022 + if (ret && ret != -ENODEV) 1023 + return ret; 1024 + } 1025 + 1026 + if (output_present[CRTC_EXT]) { 1027 + ret = mtk_drm_of_ddp_path_build_one(dev, CRTC_EXT, 1028 + &data->ext_path, &data->ext_len); 1029 + if (ret && ret != -ENODEV) 1030 + return ret; 1031 + } 1032 + 1033 + if (output_present[CRTC_THIRD]) { 1034 + ret = mtk_drm_of_ddp_path_build_one(dev, CRTC_THIRD, 1035 + &data->third_path, &data->third_len); 1036 + if (ret && ret != -ENODEV) 1037 + return ret; 1038 + } 1039 + 1040 + return 0; 1041 + } 1042 + 822 1043 static int mtk_drm_probe(struct platform_device *pdev) 823 1044 { 824 1045 struct device *dev = &pdev->dev; 825 1046 struct device_node *phandle = dev->parent->of_node; 826 1047 const struct of_device_id *of_id; 827 1048 struct mtk_drm_private *private; 1049 + struct mtk_mmsys_driver_data *mtk_drm_data; 828 1050 struct device_node *node; 829 1051 struct component_match *match = NULL; 830 1052 struct platform_device *ovl_adaptor; ··· 1068 844 if (!of_id) 1069 845 return -ENODEV; 1070 846 1071 - private->data = of_id->data; 847 + mtk_drm_data = (struct mtk_mmsys_driver_data *)of_id->data; 848 + if (!mtk_drm_data) 849 + return -EINVAL; 850 + 851 + /* Try to build the display pipeline from devicetree graphs */ 852 + if (of_graph_is_present(phandle)) { 853 + dev_dbg(dev, "Building display pipeline for MMSYS %u\n", 854 + mtk_drm_data->mmsys_id); 855 + private->data = devm_kmemdup(dev, mtk_drm_data, 856 + sizeof(*mtk_drm_data), GFP_KERNEL); 857 + if (!private->data) 858 + return -ENOMEM; 859 + 860 + ret = mtk_drm_of_ddp_path_build(dev, phandle, private->data); 861 + if (ret) 862 + return ret; 863 + } else { 864 + /* No devicetree graphs support: go with hardcoded paths if present */ 865 + dev_dbg(dev, "Using hardcoded paths for MMSYS %u\n", mtk_drm_data->mmsys_id); 866 + private->data = mtk_drm_data; 867 + }; 1072 868 1073 869 private->all_drm_private = devm_kmalloc_array(dev, private->data->mmsys_dev_num, 1074 870 sizeof(*private->all_drm_private), ··· 1110 866 1111 867 /* Iterate over sibling DISP function blocks */ 1112 868 for_each_child_of_node(phandle->parent, node) { 1113 - const struct of_device_id *of_id; 1114 869 enum mtk_ddp_comp_type comp_type; 1115 870 int comp_id; 1116 871 1117 - of_id = of_match_node(mtk_ddp_comp_dt_ids, node); 1118 - if (!of_id) 872 + ret = mtk_drm_of_get_ddp_comp_type(node, &comp_type); 873 + if (ret) 1119 874 continue; 1120 875 1121 876 if (!of_device_is_available(node)) { ··· 1122 879 node); 1123 880 continue; 1124 881 } 1125 - 1126 - comp_type = (enum mtk_ddp_comp_type)(uintptr_t)of_id->data; 1127 882 1128 883 if (comp_type == MTK_DISP_MUTEX) { 1129 884 int id;
+1 -1
drivers/gpu/drm/mediatek/mtk_drm_drv.h
··· 63 63 struct device *mmsys_dev; 64 64 struct device_node *comp_node[DDP_COMPONENT_DRM_ID_MAX]; 65 65 struct mtk_ddp_comp ddp_comp[DDP_COMPONENT_DRM_ID_MAX]; 66 - const struct mtk_mmsys_driver_data *data; 66 + struct mtk_mmsys_driver_data *data; 67 67 struct drm_atomic_state *suspend_state; 68 68 unsigned int mbox_index; 69 69 struct mtk_drm_private **all_drm_private;
+11 -3
drivers/gpu/drm/mediatek/mtk_dsi.c
··· 988 988 dsi->lanes = device->lanes; 989 989 dsi->format = device->format; 990 990 dsi->mode_flags = device->mode_flags; 991 - dsi->next_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 0, 0); 992 - if (IS_ERR(dsi->next_bridge)) 993 - return PTR_ERR(dsi->next_bridge); 991 + dsi->next_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 1, 0); 992 + if (IS_ERR(dsi->next_bridge)) { 993 + ret = PTR_ERR(dsi->next_bridge); 994 + if (ret == -EPROBE_DEFER) 995 + return ret; 996 + 997 + /* Old devicetree has only one endpoint */ 998 + dsi->next_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 0, 0); 999 + if (IS_ERR(dsi->next_bridge)) 1000 + return PTR_ERR(dsi->next_bridge); 1001 + } 994 1002 995 1003 drm_bridge_add(&dsi->bridge); 996 1004