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

Merge tag 'mediatek-drm-next-20251120' of https://git.kernel.org/pub/scm/linux/kernel/git/chunkuang.hu/linux into drm-next

Mediatek DRM Next - 20251120

1. Fix probe resource leaks
2. Add support for MT8195/88 HDMIv2 and DDCv2
3. Fix CCORR mtk_ctm_s31_32_to_s1_n function issue
4. Fix device node reference leak in mtk_dp_dt_parse()

Signed-off-by: Dave Airlie <airlied@redhat.com>

From: Chun-Kuang Hu <chunkuang.hu@kernel.org>
Link: https://patch.msgid.link/20251119233202.10034-1-chunkuang.hu@kernel.org

+2925 -549
+22 -1
drivers/gpu/drm/mediatek/Kconfig
··· 30 30 help 31 31 DRM/KMS Display Port driver for MediaTek SoCs. 32 32 33 + config DRM_MEDIATEK_HDMI_COMMON 34 + tristate 35 + depends on DRM_MEDIATEK 36 + select DRM_DISPLAY_HDMI_HELPER 37 + select DRM_DISPLAY_HELPER 38 + select SND_SOC_HDMI_CODEC if SND_SOC 39 + help 40 + MediaTek SoC HDMI common library 41 + 33 42 config DRM_MEDIATEK_HDMI 34 43 tristate "DRM HDMI Support for Mediatek SoCs" 35 44 depends on DRM_MEDIATEK 36 - select SND_SOC_HDMI_CODEC if SND_SOC 45 + select DRM_MEDIATEK_HDMI_COMMON 37 46 help 38 47 DRM/KMS HDMI driver for Mediatek SoCs 48 + 49 + config DRM_MEDIATEK_HDMI_V2 50 + tristate "DRM HDMI v2 IP support for MediaTek SoCs" 51 + depends on DRM_MEDIATEK 52 + select DRM_MEDIATEK_HDMI_COMMON 53 + help 54 + Say yes here to enable support for the HDMIv2 IP and related 55 + DDCv2 as found in the MediaTek MT8195, MT8188 SoCs and other 56 + variants. 57 + This driver can also be built as a module. If so, the HDMIv2 58 + module will be called "mtk_hdmi_v2", and the DDCv2 module 59 + will be called "mtk_hdmi_ddc_v2".
+3
drivers/gpu/drm/mediatek/Makefile
··· 21 21 22 22 obj-$(CONFIG_DRM_MEDIATEK) += mediatek-drm.o 23 23 24 + obj-$(CONFIG_DRM_MEDIATEK_HDMI_COMMON) += mtk_hdmi_common.o 24 25 obj-$(CONFIG_DRM_MEDIATEK_HDMI) += mtk_cec.o 25 26 obj-$(CONFIG_DRM_MEDIATEK_HDMI) += mtk_hdmi.o 26 27 obj-$(CONFIG_DRM_MEDIATEK_HDMI) += mtk_hdmi_ddc.o 28 + obj-$(CONFIG_DRM_MEDIATEK_HDMI_V2) += mtk_hdmi_v2.o 29 + obj-$(CONFIG_DRM_MEDIATEK_HDMI_V2) += mtk_hdmi_ddc_v2.o 27 30 28 31 obj-$(CONFIG_DRM_MEDIATEK_DP) += mtk_dp.o
+28 -5
drivers/gpu/drm/mediatek/mtk_ddp_comp.c
··· 621 621 return ret; 622 622 } 623 623 624 - int mtk_ddp_comp_init(struct device_node *node, struct mtk_ddp_comp *comp, 624 + static void mtk_ddp_comp_put_device(void *_dev) 625 + { 626 + struct device *dev = _dev; 627 + 628 + put_device(dev); 629 + } 630 + 631 + static void mtk_ddp_comp_clk_put(void *_clk) 632 + { 633 + struct clk *clk = _clk; 634 + 635 + clk_put(clk); 636 + } 637 + 638 + int mtk_ddp_comp_init(struct device *dev, struct device_node *node, struct mtk_ddp_comp *comp, 625 639 unsigned int comp_id) 626 640 { 627 641 struct platform_device *comp_pdev; 628 642 enum mtk_ddp_comp_type type; 629 643 struct mtk_ddp_comp_dev *priv; 630 - #if IS_REACHABLE(CONFIG_MTK_CMDQ) 631 644 int ret; 632 - #endif 633 645 634 646 if (comp_id >= DDP_COMPONENT_DRM_ID_MAX) 635 647 return -EINVAL; ··· 663 651 } 664 652 comp->dev = &comp_pdev->dev; 665 653 654 + ret = devm_add_action_or_reset(dev, mtk_ddp_comp_put_device, comp->dev); 655 + if (ret) 656 + return ret; 657 + 666 658 if (type == MTK_DISP_AAL || 667 659 type == MTK_DISP_BLS || 668 660 type == MTK_DISP_CCORR || ··· 682 666 type == MTK_DSI) 683 667 return 0; 684 668 685 - priv = devm_kzalloc(comp->dev, sizeof(*priv), GFP_KERNEL); 669 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 686 670 if (!priv) 687 671 return -ENOMEM; 688 672 689 - priv->regs = of_iomap(node, 0); 673 + priv->regs = devm_of_iomap(dev, node, 0, NULL); 674 + if (IS_ERR(priv->regs)) 675 + return PTR_ERR(priv->regs); 676 + 690 677 priv->clk = of_clk_get(node, 0); 691 678 if (IS_ERR(priv->clk)) 692 679 return PTR_ERR(priv->clk); 680 + 681 + ret = devm_add_action_or_reset(dev, mtk_ddp_comp_clk_put, priv->clk); 682 + if (ret) 683 + return ret; 693 684 694 685 #if IS_REACHABLE(CONFIG_MTK_CMDQ) 695 686 ret = cmdq_dev_get_client_reg(comp->dev, &priv->cmdq_reg, 0);
+1 -1
drivers/gpu/drm/mediatek/mtk_ddp_comp.h
··· 350 350 int mtk_ddp_comp_get_id(struct device_node *node, 351 351 enum mtk_ddp_comp_type comp_type); 352 352 int mtk_find_possible_crtcs(struct drm_device *drm, struct device *dev); 353 - int mtk_ddp_comp_init(struct device_node *comp_node, struct mtk_ddp_comp *comp, 353 + int mtk_ddp_comp_init(struct device *dev, struct device_node *comp_node, struct mtk_ddp_comp *comp, 354 354 unsigned int comp_id); 355 355 enum mtk_ddp_comp_type mtk_ddp_comp_get_type(unsigned int comp_id); 356 356 void mtk_ddp_write(struct cmdq_pkt *cmdq_pkt, unsigned int value,
+1 -22
drivers/gpu/drm/mediatek/mtk_disp_ccorr.c
··· 80 80 writel_relaxed(0x0, ccorr->regs + DISP_CCORR_EN); 81 81 } 82 82 83 - /* Converts a DRM S31.32 value to the HW S1.n format. */ 84 - static u16 mtk_ctm_s31_32_to_s1_n(u64 in, u32 n) 85 - { 86 - u16 r; 87 - 88 - /* Sign bit. */ 89 - r = in & BIT_ULL(63) ? BIT(n + 1) : 0; 90 - 91 - if ((in & GENMASK_ULL(62, 33)) > 0) { 92 - /* identity value 0x100000000 -> 0x400(mt8183), */ 93 - /* identity value 0x100000000 -> 0x800(mt8192), */ 94 - /* if bigger this, set it to max 0x7ff. */ 95 - r |= GENMASK(n, 0); 96 - } else { 97 - /* take the n+1 most important bits. */ 98 - r |= (in >> (32 - n)) & GENMASK(n, 0); 99 - } 100 - 101 - return r; 102 - } 103 - 104 83 void mtk_ccorr_ctm_set(struct device *dev, struct drm_crtc_state *state) 105 84 { 106 85 struct mtk_disp_ccorr *ccorr = dev_get_drvdata(dev); ··· 98 119 input = ctm->matrix; 99 120 100 121 for (i = 0; i < ARRAY_SIZE(coeffs); i++) 101 - coeffs[i] = mtk_ctm_s31_32_to_s1_n(input[i], matrix_bits); 122 + coeffs[i] = drm_color_ctm_s31_32_to_qm_n(input[i], 2, matrix_bits); 102 123 103 124 mtk_ddp_write(cmdq_pkt, coeffs[0] << 16 | coeffs[1], 104 125 &ccorr->cmdq_reg, ccorr->regs, DISP_CCORR_COEF_0);
+12
drivers/gpu/drm/mediatek/mtk_disp_ovl_adaptor.c
··· 527 527 type == OVL_ADAPTOR_TYPE_PADDING; 528 528 } 529 529 530 + static void ovl_adaptor_put_device(void *_dev) 531 + { 532 + struct device *dev = _dev; 533 + 534 + put_device(dev); 535 + } 536 + 530 537 static int ovl_adaptor_comp_init(struct device *dev, struct component_match **match) 531 538 { 532 539 struct mtk_disp_ovl_adaptor *priv = dev_get_drvdata(dev); ··· 566 559 comp_pdev = of_find_device_by_node(node); 567 560 if (!comp_pdev) 568 561 return -EPROBE_DEFER; 562 + 563 + ret = devm_add_action_or_reset(dev, ovl_adaptor_put_device, 564 + &comp_pdev->dev); 565 + if (ret) 566 + return ret; 569 567 570 568 priv->ovl_adaptor_comp[id] = &comp_pdev->dev; 571 569
+1
drivers/gpu/drm/mediatek/mtk_dp.c
··· 2087 2087 endpoint = of_graph_get_endpoint_by_regs(pdev->dev.of_node, 1, -1); 2088 2088 len = of_property_count_elems_of_size(endpoint, 2089 2089 "data-lanes", sizeof(u32)); 2090 + of_node_put(endpoint); 2090 2091 if (len < 0 || len > 4 || len == 3) { 2091 2092 dev_err(dev, "invalid data lane size: %d\n", len); 2092 2093 return -EINVAL;
+2 -2
drivers/gpu/drm/mediatek/mtk_drm_drv.c
··· 1123 1123 (void *)private->mmsys_dev, 1124 1124 sizeof(*private->mmsys_dev)); 1125 1125 private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR].dev = &ovl_adaptor->dev; 1126 - mtk_ddp_comp_init(NULL, &private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR], 1126 + mtk_ddp_comp_init(dev, NULL, &private->ddp_comp[DDP_COMPONENT_DRM_OVL_ADAPTOR], 1127 1127 DDP_COMPONENT_DRM_OVL_ADAPTOR); 1128 1128 component_match_add(dev, &match, compare_dev, &ovl_adaptor->dev); 1129 1129 } ··· 1189 1189 node); 1190 1190 } 1191 1191 1192 - ret = mtk_ddp_comp_init(node, &private->ddp_comp[comp_id], comp_id); 1192 + ret = mtk_ddp_comp_init(dev, node, &private->ddp_comp[comp_id], comp_id); 1193 1193 if (ret) { 1194 1194 of_node_put(node); 1195 1195 goto err_node;
+21 -518
drivers/gpu/drm/mediatek/mtk_hdmi.c
··· 31 31 #include <drm/drm_probe_helper.h> 32 32 33 33 #include "mtk_cec.h" 34 + #include "mtk_hdmi_common.h" 34 35 #include "mtk_hdmi_regs.h" 35 36 36 37 #define NCTS_BYTES 7 ··· 43 42 MTK_HDMI_CLK_AUD_SPDIF, 44 43 MTK_HDMI_CLK_COUNT 45 44 }; 46 - 47 - enum hdmi_aud_input_type { 48 - HDMI_AUD_INPUT_I2S = 0, 49 - HDMI_AUD_INPUT_SPDIF, 50 - }; 51 - 52 - enum hdmi_aud_i2s_fmt { 53 - HDMI_I2S_MODE_RJT_24BIT = 0, 54 - HDMI_I2S_MODE_RJT_16BIT, 55 - HDMI_I2S_MODE_LJT_24BIT, 56 - HDMI_I2S_MODE_LJT_16BIT, 57 - HDMI_I2S_MODE_I2S_24BIT, 58 - HDMI_I2S_MODE_I2S_16BIT 59 - }; 60 - 61 - enum hdmi_aud_mclk { 62 - HDMI_AUD_MCLK_128FS, 63 - HDMI_AUD_MCLK_192FS, 64 - HDMI_AUD_MCLK_256FS, 65 - HDMI_AUD_MCLK_384FS, 66 - HDMI_AUD_MCLK_512FS, 67 - HDMI_AUD_MCLK_768FS, 68 - HDMI_AUD_MCLK_1152FS, 69 - }; 70 - 71 - enum hdmi_aud_channel_type { 72 - HDMI_AUD_CHAN_TYPE_1_0 = 0, 73 - HDMI_AUD_CHAN_TYPE_1_1, 74 - HDMI_AUD_CHAN_TYPE_2_0, 75 - HDMI_AUD_CHAN_TYPE_2_1, 76 - HDMI_AUD_CHAN_TYPE_3_0, 77 - HDMI_AUD_CHAN_TYPE_3_1, 78 - HDMI_AUD_CHAN_TYPE_4_0, 79 - HDMI_AUD_CHAN_TYPE_4_1, 80 - HDMI_AUD_CHAN_TYPE_5_0, 81 - HDMI_AUD_CHAN_TYPE_5_1, 82 - HDMI_AUD_CHAN_TYPE_6_0, 83 - HDMI_AUD_CHAN_TYPE_6_1, 84 - HDMI_AUD_CHAN_TYPE_7_0, 85 - HDMI_AUD_CHAN_TYPE_7_1, 86 - HDMI_AUD_CHAN_TYPE_3_0_LRS, 87 - HDMI_AUD_CHAN_TYPE_3_1_LRS, 88 - HDMI_AUD_CHAN_TYPE_4_0_CLRS, 89 - HDMI_AUD_CHAN_TYPE_4_1_CLRS, 90 - HDMI_AUD_CHAN_TYPE_6_1_CS, 91 - HDMI_AUD_CHAN_TYPE_6_1_CH, 92 - HDMI_AUD_CHAN_TYPE_6_1_OH, 93 - HDMI_AUD_CHAN_TYPE_6_1_CHR, 94 - HDMI_AUD_CHAN_TYPE_7_1_LH_RH, 95 - HDMI_AUD_CHAN_TYPE_7_1_LSR_RSR, 96 - HDMI_AUD_CHAN_TYPE_7_1_LC_RC, 97 - HDMI_AUD_CHAN_TYPE_7_1_LW_RW, 98 - HDMI_AUD_CHAN_TYPE_7_1_LSD_RSD, 99 - HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS, 100 - HDMI_AUD_CHAN_TYPE_7_1_LHS_RHS, 101 - HDMI_AUD_CHAN_TYPE_7_1_CS_CH, 102 - HDMI_AUD_CHAN_TYPE_7_1_CS_OH, 103 - HDMI_AUD_CHAN_TYPE_7_1_CS_CHR, 104 - HDMI_AUD_CHAN_TYPE_7_1_CH_OH, 105 - HDMI_AUD_CHAN_TYPE_7_1_CH_CHR, 106 - HDMI_AUD_CHAN_TYPE_7_1_OH_CHR, 107 - HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS_LSR_RSR, 108 - HDMI_AUD_CHAN_TYPE_6_0_CS, 109 - HDMI_AUD_CHAN_TYPE_6_0_CH, 110 - HDMI_AUD_CHAN_TYPE_6_0_OH, 111 - HDMI_AUD_CHAN_TYPE_6_0_CHR, 112 - HDMI_AUD_CHAN_TYPE_7_0_LH_RH, 113 - HDMI_AUD_CHAN_TYPE_7_0_LSR_RSR, 114 - HDMI_AUD_CHAN_TYPE_7_0_LC_RC, 115 - HDMI_AUD_CHAN_TYPE_7_0_LW_RW, 116 - HDMI_AUD_CHAN_TYPE_7_0_LSD_RSD, 117 - HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS, 118 - HDMI_AUD_CHAN_TYPE_7_0_LHS_RHS, 119 - HDMI_AUD_CHAN_TYPE_7_0_CS_CH, 120 - HDMI_AUD_CHAN_TYPE_7_0_CS_OH, 121 - HDMI_AUD_CHAN_TYPE_7_0_CS_CHR, 122 - HDMI_AUD_CHAN_TYPE_7_0_CH_OH, 123 - HDMI_AUD_CHAN_TYPE_7_0_CH_CHR, 124 - HDMI_AUD_CHAN_TYPE_7_0_OH_CHR, 125 - HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS_LSR_RSR, 126 - HDMI_AUD_CHAN_TYPE_8_0_LH_RH_CS, 127 - HDMI_AUD_CHAN_TYPE_UNKNOWN = 0xFF 128 - }; 129 - 130 - enum hdmi_aud_channel_swap_type { 131 - HDMI_AUD_SWAP_LR, 132 - HDMI_AUD_SWAP_LFE_CC, 133 - HDMI_AUD_SWAP_LSRS, 134 - HDMI_AUD_SWAP_RLS_RRS, 135 - HDMI_AUD_SWAP_LR_STATUS, 136 - }; 137 - 138 - struct hdmi_audio_param { 139 - enum hdmi_audio_coding_type aud_codec; 140 - enum hdmi_audio_sample_size aud_sample_size; 141 - enum hdmi_aud_input_type aud_input_type; 142 - enum hdmi_aud_i2s_fmt aud_i2s_fmt; 143 - enum hdmi_aud_mclk aud_mclk; 144 - enum hdmi_aud_channel_type aud_input_chan_type; 145 - struct hdmi_codec_params codec_params; 146 - }; 147 - 148 - struct mtk_hdmi_conf { 149 - bool tz_disabled; 150 - bool cea_modes_only; 151 - unsigned long max_mode_clock; 152 - }; 153 - 154 - struct mtk_hdmi { 155 - struct drm_bridge bridge; 156 - struct drm_bridge *next_bridge; 157 - struct drm_connector *curr_conn;/* current connector (only valid when 'enabled') */ 158 - struct device *dev; 159 - const struct mtk_hdmi_conf *conf; 160 - struct phy *phy; 161 - struct device *cec_dev; 162 - struct i2c_adapter *ddc_adpt; 163 - struct clk *clk[MTK_HDMI_CLK_COUNT]; 164 - struct drm_display_mode mode; 165 - bool dvi_mode; 166 - struct regmap *sys_regmap; 167 - unsigned int sys_offset; 168 - struct regmap *regs; 169 - struct platform_device *audio_pdev; 170 - struct hdmi_audio_param aud_param; 171 - bool audio_enable; 172 - bool powered; 173 - bool enabled; 174 - hdmi_codec_plugged_cb plugged_cb; 175 - struct device *codec_dev; 176 - struct mutex update_plugged_status_lock; 177 - }; 178 - 179 - static inline struct mtk_hdmi *hdmi_ctx_from_bridge(struct drm_bridge *b) 180 - { 181 - return container_of(b, struct mtk_hdmi, bridge); 182 - } 183 45 184 46 static void mtk_hdmi_hw_vid_black(struct mtk_hdmi *hdmi, bool black) 185 47 { ··· 464 600 regmap_write(hdmi->regs, GRL_CFG5, val); 465 601 } 466 602 467 - struct hdmi_acr_n { 468 - unsigned int clock; 469 - unsigned int n[3]; 470 - }; 471 - 472 - /* Recommended N values from HDMI specification, tables 7-1 to 7-3 */ 473 - static const struct hdmi_acr_n hdmi_rec_n_table[] = { 474 - /* Clock, N: 32kHz 44.1kHz 48kHz */ 475 - { 25175, { 4576, 7007, 6864 } }, 476 - { 74176, { 11648, 17836, 11648 } }, 477 - { 148352, { 11648, 8918, 5824 } }, 478 - { 296703, { 5824, 4459, 5824 } }, 479 - { 297000, { 3072, 4704, 5120 } }, 480 - { 0, { 4096, 6272, 6144 } }, /* all other TMDS clocks */ 481 - }; 482 - 483 - /** 484 - * hdmi_recommended_n() - Return N value recommended by HDMI specification 485 - * @freq: audio sample rate in Hz 486 - * @clock: rounded TMDS clock in kHz 487 - */ 488 - static unsigned int hdmi_recommended_n(unsigned int freq, unsigned int clock) 489 - { 490 - const struct hdmi_acr_n *recommended; 491 - unsigned int i; 492 - 493 - for (i = 0; i < ARRAY_SIZE(hdmi_rec_n_table) - 1; i++) { 494 - if (clock == hdmi_rec_n_table[i].clock) 495 - break; 496 - } 497 - recommended = hdmi_rec_n_table + i; 498 - 499 - switch (freq) { 500 - case 32000: 501 - return recommended->n[0]; 502 - case 44100: 503 - return recommended->n[1]; 504 - case 48000: 505 - return recommended->n[2]; 506 - case 88200: 507 - return recommended->n[1] * 2; 508 - case 96000: 509 - return recommended->n[2] * 2; 510 - case 176400: 511 - return recommended->n[1] * 4; 512 - case 192000: 513 - return recommended->n[2] * 4; 514 - default: 515 - return (128 * freq) / 1000; 516 - } 517 - } 518 - 519 - static unsigned int hdmi_mode_clock_to_hz(unsigned int clock) 520 - { 521 - switch (clock) { 522 - case 25175: 523 - return 25174825; /* 25.2/1.001 MHz */ 524 - case 74176: 525 - return 74175824; /* 74.25/1.001 MHz */ 526 - case 148352: 527 - return 148351648; /* 148.5/1.001 MHz */ 528 - case 296703: 529 - return 296703297; /* 297/1.001 MHz */ 530 - default: 531 - return clock * 1000; 532 - } 533 - } 534 - 535 - static unsigned int hdmi_expected_cts(unsigned int audio_sample_rate, 536 - unsigned int tmds_clock, unsigned int n) 537 - { 538 - return DIV_ROUND_CLOSEST_ULL((u64)hdmi_mode_clock_to_hz(tmds_clock) * n, 539 - 128 * audio_sample_rate); 540 - } 541 - 542 - static void mtk_hdmi_get_ncts(unsigned int sample_rate, unsigned int clock, 543 - unsigned int *n, unsigned int *cts) 544 - { 545 - *n = hdmi_recommended_n(sample_rate, clock); 546 - *cts = hdmi_expected_cts(sample_rate, clock, *n); 547 - } 548 - 549 603 static void do_hdmi_hw_aud_set_ncts(struct mtk_hdmi *hdmi, unsigned int n, 550 604 unsigned int cts) 551 605 { ··· 854 1072 [MTK_HDMI_CLK_AUD_SPDIF] = "spdif", 855 1073 }; 856 1074 857 - static int mtk_hdmi_get_all_clk(struct mtk_hdmi *hdmi, 858 - struct device_node *np) 859 - { 860 - int i; 861 - 862 - for (i = 0; i < ARRAY_SIZE(mtk_hdmi_clk_names); i++) { 863 - hdmi->clk[i] = of_clk_get_by_name(np, 864 - mtk_hdmi_clk_names[i]); 865 - if (IS_ERR(hdmi->clk[i])) 866 - return PTR_ERR(hdmi->clk[i]); 867 - } 868 - return 0; 869 - } 870 - 871 1075 static int mtk_hdmi_clk_enable_audio(struct mtk_hdmi *hdmi) 872 1076 { 873 1077 int ret; ··· 998 1230 return 0; 999 1231 } 1000 1232 1001 - static bool mtk_hdmi_bridge_mode_fixup(struct drm_bridge *bridge, 1002 - const struct drm_display_mode *mode, 1003 - struct drm_display_mode *adjusted_mode) 1004 - { 1005 - return true; 1006 - } 1007 - 1008 1233 static void mtk_hdmi_bridge_atomic_disable(struct drm_bridge *bridge, 1009 1234 struct drm_atomic_state *state) 1010 1235 { ··· 1027 1266 mtk_hdmi_hw_make_reg_writable(hdmi, false); 1028 1267 1029 1268 hdmi->powered = false; 1030 - } 1031 - 1032 - static void mtk_hdmi_bridge_mode_set(struct drm_bridge *bridge, 1033 - const struct drm_display_mode *mode, 1034 - const struct drm_display_mode *adjusted_mode) 1035 - { 1036 - struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge); 1037 - 1038 - dev_dbg(hdmi->dev, "cur info: name:%s, hdisplay:%d\n", 1039 - adjusted_mode->name, adjusted_mode->hdisplay); 1040 - dev_dbg(hdmi->dev, "hsync_start:%d,hsync_end:%d, htotal:%d", 1041 - adjusted_mode->hsync_start, adjusted_mode->hsync_end, 1042 - adjusted_mode->htotal); 1043 - dev_dbg(hdmi->dev, "hskew:%d, vdisplay:%d\n", 1044 - adjusted_mode->hskew, adjusted_mode->vdisplay); 1045 - dev_dbg(hdmi->dev, "vsync_start:%d, vsync_end:%d, vtotal:%d", 1046 - adjusted_mode->vsync_start, adjusted_mode->vsync_end, 1047 - adjusted_mode->vtotal); 1048 - dev_dbg(hdmi->dev, "vscan:%d, flag:%d\n", 1049 - adjusted_mode->vscan, adjusted_mode->flags); 1050 - 1051 - drm_mode_copy(&hdmi->mode, adjusted_mode); 1052 1269 } 1053 1270 1054 1271 static void mtk_hdmi_bridge_atomic_pre_enable(struct drm_bridge *bridge, ··· 1084 1345 .edid_read = mtk_hdmi_bridge_edid_read, 1085 1346 }; 1086 1347 1087 - static int mtk_hdmi_get_cec_dev(struct mtk_hdmi *hdmi, struct device *dev, struct device_node *np) 1088 - { 1089 - struct platform_device *cec_pdev; 1090 - struct device_node *cec_np; 1091 - int ret; 1092 - 1093 - ret = mtk_hdmi_get_all_clk(hdmi, np); 1094 - if (ret) 1095 - return dev_err_probe(dev, ret, "Failed to get clocks\n"); 1096 - 1097 - /* The CEC module handles HDMI hotplug detection */ 1098 - cec_np = of_get_compatible_child(np->parent, "mediatek,mt8173-cec"); 1099 - if (!cec_np) 1100 - return dev_err_probe(dev, -EINVAL, "Failed to find CEC node\n"); 1101 - 1102 - cec_pdev = of_find_device_by_node(cec_np); 1103 - if (!cec_pdev) { 1104 - dev_err(hdmi->dev, "Waiting for CEC device %pOF\n", 1105 - cec_np); 1106 - of_node_put(cec_np); 1107 - return -EPROBE_DEFER; 1108 - } 1109 - of_node_put(cec_np); 1110 - 1111 - /* 1112 - * The mediatek,syscon-hdmi property contains a phandle link to the 1113 - * MMSYS_CONFIG device and the register offset of the HDMI_SYS_CFG 1114 - * registers it contains. 1115 - */ 1116 - hdmi->sys_regmap = syscon_regmap_lookup_by_phandle_args(np, "mediatek,syscon-hdmi", 1117 - 1, &hdmi->sys_offset); 1118 - if (IS_ERR(hdmi->sys_regmap)) 1119 - return dev_err_probe(dev, PTR_ERR(hdmi->sys_regmap), 1120 - "Failed to get system configuration registers\n"); 1121 - 1122 - hdmi->cec_dev = &cec_pdev->dev; 1123 - return 0; 1124 - } 1125 - 1126 - static int mtk_hdmi_dt_parse_pdata(struct mtk_hdmi *hdmi, 1127 - struct platform_device *pdev) 1128 - { 1129 - struct device *dev = &pdev->dev; 1130 - struct device_node *np = dev->of_node; 1131 - struct device_node *remote, *i2c_np; 1132 - int ret; 1133 - 1134 - ret = mtk_hdmi_get_all_clk(hdmi, np); 1135 - if (ret) 1136 - return dev_err_probe(dev, ret, "Failed to get clocks\n"); 1137 - 1138 - hdmi->regs = device_node_to_regmap(dev->of_node); 1139 - if (IS_ERR(hdmi->regs)) 1140 - return PTR_ERR(hdmi->regs); 1141 - 1142 - remote = of_graph_get_remote_node(np, 1, 0); 1143 - if (!remote) 1144 - return -EINVAL; 1145 - 1146 - if (!of_device_is_compatible(remote, "hdmi-connector")) { 1147 - hdmi->next_bridge = of_drm_find_bridge(remote); 1148 - if (!hdmi->next_bridge) { 1149 - dev_err(dev, "Waiting for external bridge\n"); 1150 - of_node_put(remote); 1151 - return -EPROBE_DEFER; 1152 - } 1153 - } 1154 - 1155 - i2c_np = of_parse_phandle(remote, "ddc-i2c-bus", 0); 1156 - of_node_put(remote); 1157 - if (!i2c_np) 1158 - return dev_err_probe(dev, -EINVAL, "No ddc-i2c-bus in connector\n"); 1159 - 1160 - hdmi->ddc_adpt = of_find_i2c_adapter_by_node(i2c_np); 1161 - of_node_put(i2c_np); 1162 - if (!hdmi->ddc_adpt) 1163 - return dev_err_probe(dev, -EINVAL, "Failed to get ddc i2c adapter by node\n"); 1164 - 1165 - ret = mtk_hdmi_get_cec_dev(hdmi, dev, np); 1166 - if (ret) 1167 - return ret; 1168 - 1169 - return 0; 1170 - } 1171 - 1172 1348 /* 1173 1349 * HDMI audio codec callbacks 1174 1350 */ 1175 - 1176 - static int mtk_hdmi_audio_params(struct mtk_hdmi *hdmi, 1177 - struct hdmi_codec_daifmt *daifmt, 1178 - struct hdmi_codec_params *params) 1179 - { 1180 - struct hdmi_audio_param aud_params = { 0 }; 1181 - unsigned int chan = params->cea.channels; 1182 - 1183 - dev_dbg(hdmi->dev, "%s: %u Hz, %d bit, %d channels\n", __func__, 1184 - params->sample_rate, params->sample_width, chan); 1185 - 1186 - if (!hdmi->bridge.encoder) 1187 - return -ENODEV; 1188 - 1189 - switch (chan) { 1190 - case 2: 1191 - aud_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_2_0; 1192 - break; 1193 - case 4: 1194 - aud_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_4_0; 1195 - break; 1196 - case 6: 1197 - aud_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_5_1; 1198 - break; 1199 - case 8: 1200 - aud_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_7_1; 1201 - break; 1202 - default: 1203 - dev_err(hdmi->dev, "channel[%d] not supported!\n", chan); 1204 - return -EINVAL; 1205 - } 1206 - 1207 - switch (params->sample_rate) { 1208 - case 32000: 1209 - case 44100: 1210 - case 48000: 1211 - case 88200: 1212 - case 96000: 1213 - case 176400: 1214 - case 192000: 1215 - break; 1216 - default: 1217 - dev_err(hdmi->dev, "rate[%d] not supported!\n", 1218 - params->sample_rate); 1219 - return -EINVAL; 1220 - } 1221 - 1222 - switch (daifmt->fmt) { 1223 - case HDMI_I2S: 1224 - aud_params.aud_codec = HDMI_AUDIO_CODING_TYPE_PCM; 1225 - aud_params.aud_sample_size = HDMI_AUDIO_SAMPLE_SIZE_16; 1226 - aud_params.aud_input_type = HDMI_AUD_INPUT_I2S; 1227 - aud_params.aud_i2s_fmt = HDMI_I2S_MODE_I2S_24BIT; 1228 - aud_params.aud_mclk = HDMI_AUD_MCLK_128FS; 1229 - break; 1230 - case HDMI_SPDIF: 1231 - aud_params.aud_codec = HDMI_AUDIO_CODING_TYPE_PCM; 1232 - aud_params.aud_sample_size = HDMI_AUDIO_SAMPLE_SIZE_16; 1233 - aud_params.aud_input_type = HDMI_AUD_INPUT_SPDIF; 1234 - break; 1235 - default: 1236 - dev_err(hdmi->dev, "%s: Invalid DAI format %d\n", __func__, 1237 - daifmt->fmt); 1238 - return -EINVAL; 1239 - } 1240 - memcpy(&aud_params.codec_params, params, sizeof(aud_params.codec_params)); 1241 - memcpy(&hdmi->aud_param, &aud_params, sizeof(aud_params)); 1242 - 1243 - dev_dbg(hdmi->dev, "codec:%d, input:%d, channel:%d, fs:%d\n", 1244 - aud_params.aud_codec, aud_params.aud_input_type, 1245 - aud_params.aud_input_chan_type, aud_params.codec_params.sample_rate); 1246 - 1247 - return 0; 1248 - } 1249 1351 1250 1352 static int mtk_hdmi_audio_hw_params(struct device *dev, void *data, 1251 1353 struct hdmi_codec_daifmt *daifmt, ··· 1135 1555 return 0; 1136 1556 } 1137 1557 1138 - static int mtk_hdmi_audio_get_eld(struct device *dev, void *data, uint8_t *buf, size_t len) 1139 - { 1140 - struct mtk_hdmi *hdmi = dev_get_drvdata(dev); 1141 - 1142 - if (hdmi->enabled) 1143 - memcpy(buf, hdmi->curr_conn->eld, min(sizeof(hdmi->curr_conn->eld), len)); 1144 - else 1145 - memset(buf, 0, len); 1146 - return 0; 1147 - } 1148 - 1149 - static void mtk_hdmi_audio_set_plugged_cb(struct mtk_hdmi *hdmi, hdmi_codec_plugged_cb fn, 1150 - struct device *codec_dev) 1151 - { 1152 - mutex_lock(&hdmi->update_plugged_status_lock); 1153 - hdmi->plugged_cb = fn; 1154 - hdmi->codec_dev = codec_dev; 1155 - mutex_unlock(&hdmi->update_plugged_status_lock); 1156 - } 1157 - 1158 1558 static int mtk_hdmi_audio_hook_plugged_cb(struct device *dev, void *data, 1159 1559 hdmi_codec_plugged_cb fn, 1160 1560 struct device *codec_dev) ··· 1156 1596 .hook_plugged_cb = mtk_hdmi_audio_hook_plugged_cb, 1157 1597 }; 1158 1598 1159 - static void mtk_hdmi_unregister_audio_driver(void *data) 1160 - { 1161 - platform_device_unregister(data); 1162 - } 1163 - 1164 - static int mtk_hdmi_register_audio_driver(struct device *dev) 1165 - { 1166 - struct mtk_hdmi *hdmi = dev_get_drvdata(dev); 1167 - struct hdmi_audio_param *aud_param = &hdmi->aud_param; 1168 - struct hdmi_codec_pdata codec_data = { 1169 - .ops = &mtk_hdmi_audio_codec_ops, 1170 - .max_i2s_channels = 2, 1171 - .i2s = 1, 1172 - .data = hdmi, 1173 - .no_capture_mute = 1, 1174 - }; 1175 - int ret; 1176 - 1177 - aud_param->aud_codec = HDMI_AUDIO_CODING_TYPE_PCM; 1178 - aud_param->aud_sample_size = HDMI_AUDIO_SAMPLE_SIZE_16; 1179 - aud_param->aud_input_type = HDMI_AUD_INPUT_I2S; 1180 - aud_param->aud_i2s_fmt = HDMI_I2S_MODE_I2S_24BIT; 1181 - aud_param->aud_mclk = HDMI_AUD_MCLK_128FS; 1182 - aud_param->aud_input_chan_type = HDMI_AUD_CHAN_TYPE_2_0; 1183 - 1184 - hdmi->audio_pdev = platform_device_register_data(dev, 1185 - HDMI_CODEC_DRV_NAME, 1186 - PLATFORM_DEVID_AUTO, 1187 - &codec_data, 1188 - sizeof(codec_data)); 1189 - if (IS_ERR(hdmi->audio_pdev)) 1190 - return PTR_ERR(hdmi->audio_pdev); 1191 - 1192 - ret = devm_add_action_or_reset(dev, mtk_hdmi_unregister_audio_driver, 1193 - hdmi->audio_pdev); 1194 - if (ret) 1195 - return ret; 1196 - 1197 - return 0; 1198 - } 1199 - 1200 1599 static int mtk_hdmi_probe(struct platform_device *pdev) 1201 1600 { 1202 1601 struct mtk_hdmi *hdmi; 1203 - struct device *dev = &pdev->dev; 1204 1602 int ret; 1205 1603 1206 - hdmi = devm_drm_bridge_alloc(dev, struct mtk_hdmi, bridge, 1207 - &mtk_hdmi_bridge_funcs); 1604 + hdmi = mtk_hdmi_common_probe(pdev); 1208 1605 if (IS_ERR(hdmi)) 1209 1606 return PTR_ERR(hdmi); 1210 1607 1211 - hdmi->dev = dev; 1212 - hdmi->conf = of_device_get_match_data(dev); 1213 - 1214 - ret = mtk_hdmi_dt_parse_pdata(hdmi, pdev); 1215 - if (ret) 1216 - return ret; 1217 - 1218 - hdmi->phy = devm_phy_get(dev, "hdmi"); 1219 - if (IS_ERR(hdmi->phy)) 1220 - return dev_err_probe(dev, PTR_ERR(hdmi->phy), 1221 - "Failed to get HDMI PHY\n"); 1222 - 1223 - mutex_init(&hdmi->update_plugged_status_lock); 1224 - platform_set_drvdata(pdev, hdmi); 1225 - 1226 - ret = mtk_hdmi_register_audio_driver(dev); 1227 - if (ret) 1228 - return dev_err_probe(dev, ret, 1229 - "Failed to register audio driver\n"); 1230 - 1231 - hdmi->bridge.of_node = pdev->dev.of_node; 1232 - hdmi->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID 1233 - | DRM_BRIDGE_OP_HPD; 1234 - hdmi->bridge.type = DRM_MODE_CONNECTOR_HDMIA; 1235 - hdmi->bridge.vendor = "MediaTek"; 1236 - hdmi->bridge.product = "On-Chip HDMI"; 1237 - 1238 - ret = devm_drm_bridge_add(dev, &hdmi->bridge); 1239 - if (ret) 1240 - return dev_err_probe(dev, ret, "Failed to add bridge\n"); 1608 + if (!hdmi->cec_dev) 1609 + return dev_err_probe(hdmi->dev, -ENODEV, "CEC is required by HDMIv1\n"); 1241 1610 1242 1611 ret = mtk_hdmi_clk_enable_audio(hdmi); 1243 1612 if (ret) 1244 - return dev_err_probe(dev, ret, 1613 + return dev_err_probe(hdmi->dev, ret, 1245 1614 "Failed to enable audio clocks\n"); 1246 1615 1247 1616 return 0; ··· 1201 1712 1202 1713 static SIMPLE_DEV_PM_OPS(mtk_hdmi_pm_ops, mtk_hdmi_suspend, mtk_hdmi_resume); 1203 1714 1715 + static const struct mtk_hdmi_ver_conf mtk_hdmi_v1_ver_conf = { 1716 + .bridge_funcs = &mtk_hdmi_bridge_funcs, 1717 + .codec_ops = &mtk_hdmi_audio_codec_ops, 1718 + .mtk_hdmi_clock_names = mtk_hdmi_clk_names, 1719 + .num_clocks = ARRAY_SIZE(mtk_hdmi_clk_names) 1720 + }; 1721 + 1204 1722 static const struct mtk_hdmi_conf mtk_hdmi_conf_mt2701 = { 1205 1723 .tz_disabled = true, 1724 + .ver_conf = &mtk_hdmi_v1_ver_conf 1206 1725 }; 1207 1726 1208 1727 static const struct mtk_hdmi_conf mtk_hdmi_conf_mt8167 = { 1209 - .max_mode_clock = 148500, 1210 1728 .cea_modes_only = true, 1729 + .max_mode_clock = 148500, 1730 + .ver_conf = &mtk_hdmi_v1_ver_conf 1731 + }; 1732 + 1733 + static const struct mtk_hdmi_conf mtk_hdmi_conf_mt8173 = { 1734 + .ver_conf = &mtk_hdmi_v1_ver_conf 1211 1735 }; 1212 1736 1213 1737 static const struct of_device_id mtk_hdmi_of_ids[] = { 1214 1738 { .compatible = "mediatek,mt2701-hdmi", .data = &mtk_hdmi_conf_mt2701 }, 1215 1739 { .compatible = "mediatek,mt8167-hdmi", .data = &mtk_hdmi_conf_mt8167 }, 1216 - { .compatible = "mediatek,mt8173-hdmi" }, 1740 + { .compatible = "mediatek,mt8173-hdmi", .data = &mtk_hdmi_conf_mt8173 }, 1217 1741 { /* sentinel */ } 1218 1742 }; 1219 1743 MODULE_DEVICE_TABLE(of, mtk_hdmi_of_ids); ··· 1246 1744 MODULE_DESCRIPTION("MediaTek HDMI Driver"); 1247 1745 MODULE_LICENSE("GPL v2"); 1248 1746 MODULE_IMPORT_NS("DRM_MTK_HDMI_V1"); 1747 + MODULE_IMPORT_NS("DRM_MTK_HDMI");
+456
drivers/gpu/drm/mediatek/mtk_hdmi_common.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Copyright (c) 2014 MediaTek Inc. 4 + * Copyright (c) 2024 Collabora Ltd. 5 + * AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> 6 + */ 7 + 8 + #include <drm/drm_modes.h> 9 + #include <linux/device.h> 10 + #include <linux/hdmi.h> 11 + #include <linux/i2c.h> 12 + #include <linux/math.h> 13 + #include <linux/of.h> 14 + #include <linux/of_platform.h> 15 + #include <linux/platform_device.h> 16 + #include <linux/mfd/syscon.h> 17 + #include <sound/hdmi-codec.h> 18 + 19 + #include "mtk_hdmi_common.h" 20 + 21 + struct hdmi_acr_n { 22 + unsigned int clock; 23 + unsigned int n[3]; 24 + }; 25 + 26 + /* Recommended N values from HDMI specification, tables 7-1 to 7-3 */ 27 + static const struct hdmi_acr_n hdmi_rec_n_table[] = { 28 + /* Clock, N: 32kHz 44.1kHz 48kHz */ 29 + { 25175, { 4576, 7007, 6864 } }, 30 + { 74176, { 11648, 17836, 11648 } }, 31 + { 148352, { 11648, 8918, 5824 } }, 32 + { 296703, { 5824, 4459, 5824 } }, 33 + { 297000, { 3072, 4704, 5120 } }, 34 + { 0, { 4096, 6272, 6144 } }, /* all other TMDS clocks */ 35 + }; 36 + 37 + /** 38 + * hdmi_recommended_n() - Return N value recommended by HDMI specification 39 + * @freq: audio sample rate in Hz 40 + * @clock: rounded TMDS clock in kHz 41 + */ 42 + static unsigned int hdmi_recommended_n(unsigned int freq, unsigned int clock) 43 + { 44 + const struct hdmi_acr_n *recommended; 45 + unsigned int i; 46 + 47 + for (i = 0; i < ARRAY_SIZE(hdmi_rec_n_table) - 1; i++) { 48 + if (clock == hdmi_rec_n_table[i].clock) 49 + break; 50 + } 51 + recommended = hdmi_rec_n_table + i; 52 + 53 + switch (freq) { 54 + case 32000: 55 + return recommended->n[0]; 56 + case 44100: 57 + return recommended->n[1]; 58 + case 48000: 59 + return recommended->n[2]; 60 + case 88200: 61 + return recommended->n[1] * 2; 62 + case 96000: 63 + return recommended->n[2] * 2; 64 + case 176400: 65 + return recommended->n[1] * 4; 66 + case 192000: 67 + return recommended->n[2] * 4; 68 + default: 69 + return (128 * freq) / 1000; 70 + } 71 + } 72 + 73 + static unsigned int hdmi_mode_clock_to_hz(unsigned int clock) 74 + { 75 + switch (clock) { 76 + case 25175: 77 + return 25174825; /* 25.2/1.001 MHz */ 78 + case 74176: 79 + return 74175824; /* 74.25/1.001 MHz */ 80 + case 148352: 81 + return 148351648; /* 148.5/1.001 MHz */ 82 + case 296703: 83 + return 296703297; /* 297/1.001 MHz */ 84 + default: 85 + return clock * 1000; 86 + } 87 + } 88 + 89 + static unsigned int hdmi_expected_cts(unsigned int audio_sample_rate, 90 + unsigned int tmds_clock, unsigned int n) 91 + { 92 + return DIV_ROUND_CLOSEST_ULL((u64)hdmi_mode_clock_to_hz(tmds_clock) * n, 93 + 128 * audio_sample_rate); 94 + } 95 + 96 + void mtk_hdmi_get_ncts(unsigned int sample_rate, unsigned int clock, 97 + unsigned int *n, unsigned int *cts) 98 + { 99 + *n = hdmi_recommended_n(sample_rate, clock); 100 + *cts = hdmi_expected_cts(sample_rate, clock, *n); 101 + } 102 + EXPORT_SYMBOL_NS_GPL(mtk_hdmi_get_ncts, "DRM_MTK_HDMI"); 103 + 104 + int mtk_hdmi_audio_params(struct mtk_hdmi *hdmi, 105 + struct hdmi_codec_daifmt *daifmt, 106 + struct hdmi_codec_params *params) 107 + { 108 + struct hdmi_audio_param aud_params = { 0 }; 109 + unsigned int chan = params->cea.channels; 110 + 111 + dev_dbg(hdmi->dev, "%s: %u Hz, %d bit, %d channels\n", __func__, 112 + params->sample_rate, params->sample_width, chan); 113 + 114 + if (!hdmi->bridge.encoder) 115 + return -ENODEV; 116 + 117 + switch (chan) { 118 + case 2: 119 + aud_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_2_0; 120 + break; 121 + case 4: 122 + aud_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_4_0; 123 + break; 124 + case 6: 125 + aud_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_5_1; 126 + break; 127 + case 8: 128 + aud_params.aud_input_chan_type = HDMI_AUD_CHAN_TYPE_7_1; 129 + break; 130 + default: 131 + dev_err(hdmi->dev, "channel[%d] not supported!\n", chan); 132 + return -EINVAL; 133 + } 134 + 135 + switch (params->sample_rate) { 136 + case 32000: 137 + case 44100: 138 + case 48000: 139 + case 88200: 140 + case 96000: 141 + case 176400: 142 + case 192000: 143 + break; 144 + default: 145 + dev_err(hdmi->dev, "rate[%d] not supported!\n", 146 + params->sample_rate); 147 + return -EINVAL; 148 + } 149 + 150 + switch (daifmt->fmt) { 151 + case HDMI_I2S: 152 + aud_params.aud_codec = HDMI_AUDIO_CODING_TYPE_PCM; 153 + aud_params.aud_sample_size = HDMI_AUDIO_SAMPLE_SIZE_16; 154 + aud_params.aud_input_type = HDMI_AUD_INPUT_I2S; 155 + aud_params.aud_i2s_fmt = HDMI_I2S_MODE_I2S_24BIT; 156 + aud_params.aud_mclk = HDMI_AUD_MCLK_128FS; 157 + break; 158 + case HDMI_SPDIF: 159 + aud_params.aud_codec = HDMI_AUDIO_CODING_TYPE_PCM; 160 + aud_params.aud_sample_size = HDMI_AUDIO_SAMPLE_SIZE_16; 161 + aud_params.aud_input_type = HDMI_AUD_INPUT_SPDIF; 162 + break; 163 + default: 164 + dev_err(hdmi->dev, "%s: Invalid DAI format %d\n", __func__, 165 + daifmt->fmt); 166 + return -EINVAL; 167 + } 168 + memcpy(&aud_params.codec_params, params, sizeof(aud_params.codec_params)); 169 + memcpy(&hdmi->aud_param, &aud_params, sizeof(aud_params)); 170 + 171 + dev_dbg(hdmi->dev, "codec:%d, input:%d, channel:%d, fs:%d\n", 172 + aud_params.aud_codec, aud_params.aud_input_type, 173 + aud_params.aud_input_chan_type, aud_params.codec_params.sample_rate); 174 + 175 + return 0; 176 + } 177 + EXPORT_SYMBOL_NS_GPL(mtk_hdmi_audio_params, "DRM_MTK_HDMI"); 178 + 179 + int mtk_hdmi_audio_get_eld(struct device *dev, void *data, uint8_t *buf, size_t len) 180 + { 181 + struct mtk_hdmi *hdmi = dev_get_drvdata(dev); 182 + 183 + if (hdmi->enabled) 184 + memcpy(buf, hdmi->curr_conn->eld, min(sizeof(hdmi->curr_conn->eld), len)); 185 + else 186 + memset(buf, 0, len); 187 + 188 + return 0; 189 + } 190 + EXPORT_SYMBOL_NS_GPL(mtk_hdmi_audio_get_eld, "DRM_MTK_HDMI"); 191 + 192 + void mtk_hdmi_audio_set_plugged_cb(struct mtk_hdmi *hdmi, hdmi_codec_plugged_cb fn, 193 + struct device *codec_dev) 194 + { 195 + mutex_lock(&hdmi->update_plugged_status_lock); 196 + hdmi->plugged_cb = fn; 197 + hdmi->codec_dev = codec_dev; 198 + mutex_unlock(&hdmi->update_plugged_status_lock); 199 + } 200 + EXPORT_SYMBOL_NS_GPL(mtk_hdmi_audio_set_plugged_cb, "DRM_MTK_HDMI"); 201 + 202 + static int mtk_hdmi_get_all_clk(struct mtk_hdmi *hdmi, struct device_node *np, 203 + const char * const *clock_names, size_t num_clocks) 204 + { 205 + int i; 206 + 207 + for (i = 0; i < num_clocks; i++) { 208 + hdmi->clk[i] = of_clk_get_by_name(np, clock_names[i]); 209 + 210 + if (IS_ERR(hdmi->clk[i])) 211 + return PTR_ERR(hdmi->clk[i]); 212 + } 213 + 214 + return 0; 215 + } 216 + 217 + bool mtk_hdmi_bridge_mode_fixup(struct drm_bridge *bridge, 218 + const struct drm_display_mode *mode, 219 + struct drm_display_mode *adjusted_mode) 220 + { 221 + return true; 222 + } 223 + EXPORT_SYMBOL_NS_GPL(mtk_hdmi_bridge_mode_fixup, "DRM_MTK_HDMI"); 224 + 225 + void mtk_hdmi_bridge_mode_set(struct drm_bridge *bridge, 226 + const struct drm_display_mode *mode, 227 + const struct drm_display_mode *adjusted_mode) 228 + { 229 + struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge); 230 + 231 + dev_dbg(hdmi->dev, "cur info: name:%s, hdisplay:%d\n", 232 + adjusted_mode->name, adjusted_mode->hdisplay); 233 + dev_dbg(hdmi->dev, "hsync_start:%d,hsync_end:%d, htotal:%d", 234 + adjusted_mode->hsync_start, adjusted_mode->hsync_end, 235 + adjusted_mode->htotal); 236 + dev_dbg(hdmi->dev, "hskew:%d, vdisplay:%d\n", 237 + adjusted_mode->hskew, adjusted_mode->vdisplay); 238 + dev_dbg(hdmi->dev, "vsync_start:%d, vsync_end:%d, vtotal:%d", 239 + adjusted_mode->vsync_start, adjusted_mode->vsync_end, 240 + adjusted_mode->vtotal); 241 + dev_dbg(hdmi->dev, "vscan:%d, flag:%d\n", 242 + adjusted_mode->vscan, adjusted_mode->flags); 243 + 244 + drm_mode_copy(&hdmi->mode, adjusted_mode); 245 + } 246 + EXPORT_SYMBOL_NS_GPL(mtk_hdmi_bridge_mode_set, "DRM_MTK_HDMI"); 247 + 248 + static void mtk_hdmi_put_device(void *_dev) 249 + { 250 + struct device *dev = _dev; 251 + 252 + put_device(dev); 253 + } 254 + 255 + static int mtk_hdmi_get_cec_dev(struct mtk_hdmi *hdmi, struct device *dev, struct device_node *np) 256 + { 257 + struct platform_device *cec_pdev; 258 + struct device_node *cec_np; 259 + int ret; 260 + 261 + /* The CEC module handles HDMI hotplug detection */ 262 + cec_np = of_get_compatible_child(np->parent, "mediatek,mt8173-cec"); 263 + if (!cec_np) 264 + return dev_err_probe(dev, -EOPNOTSUPP, "Failed to find CEC node\n"); 265 + 266 + cec_pdev = of_find_device_by_node(cec_np); 267 + if (!cec_pdev) { 268 + dev_err(hdmi->dev, "Waiting for CEC device %pOF\n", cec_np); 269 + of_node_put(cec_np); 270 + return -EPROBE_DEFER; 271 + } 272 + of_node_put(cec_np); 273 + 274 + ret = devm_add_action_or_reset(dev, mtk_hdmi_put_device, &cec_pdev->dev); 275 + if (ret) 276 + return ret; 277 + 278 + /* 279 + * The mediatek,syscon-hdmi property contains a phandle link to the 280 + * MMSYS_CONFIG device and the register offset of the HDMI_SYS_CFG 281 + * registers it contains. 282 + */ 283 + hdmi->sys_regmap = syscon_regmap_lookup_by_phandle_args(np, "mediatek,syscon-hdmi", 284 + 1, &hdmi->sys_offset); 285 + if (IS_ERR(hdmi->sys_regmap)) 286 + return dev_err_probe(dev, PTR_ERR(hdmi->sys_regmap), 287 + "Failed to get system configuration registers\n"); 288 + 289 + hdmi->cec_dev = &cec_pdev->dev; 290 + return 0; 291 + } 292 + 293 + static int mtk_hdmi_dt_parse_pdata(struct mtk_hdmi *hdmi, struct platform_device *pdev, 294 + const char * const *clk_names, size_t num_clocks) 295 + { 296 + struct device *dev = &pdev->dev; 297 + struct device_node *np = dev->of_node; 298 + struct device_node *remote, *i2c_np; 299 + int ret; 300 + 301 + ret = mtk_hdmi_get_all_clk(hdmi, np, clk_names, num_clocks); 302 + if (ret) 303 + return dev_err_probe(dev, ret, "Failed to get clocks\n"); 304 + 305 + hdmi->irq = platform_get_irq(pdev, 0); 306 + if (!hdmi->irq) 307 + return hdmi->irq; 308 + 309 + hdmi->regs = device_node_to_regmap(dev->of_node); 310 + if (IS_ERR(hdmi->regs)) 311 + return PTR_ERR(hdmi->regs); 312 + 313 + remote = of_graph_get_remote_node(np, 1, 0); 314 + if (!remote) 315 + return -EINVAL; 316 + 317 + if (!of_device_is_compatible(remote, "hdmi-connector")) { 318 + hdmi->next_bridge = of_drm_find_bridge(remote); 319 + if (!hdmi->next_bridge) { 320 + dev_err(dev, "Waiting for external bridge\n"); 321 + of_node_put(remote); 322 + return -EPROBE_DEFER; 323 + } 324 + } 325 + 326 + i2c_np = of_parse_phandle(remote, "ddc-i2c-bus", 0); 327 + of_node_put(remote); 328 + if (!i2c_np) 329 + return dev_err_probe(dev, -EINVAL, "No ddc-i2c-bus in connector\n"); 330 + 331 + hdmi->ddc_adpt = of_find_i2c_adapter_by_node(i2c_np); 332 + of_node_put(i2c_np); 333 + if (!hdmi->ddc_adpt) 334 + return dev_err_probe(dev, -EPROBE_DEFER, "Failed to get ddc i2c adapter by node\n"); 335 + 336 + ret = devm_add_action_or_reset(dev, mtk_hdmi_put_device, &hdmi->ddc_adpt->dev); 337 + if (ret) 338 + return ret; 339 + 340 + ret = mtk_hdmi_get_cec_dev(hdmi, dev, np); 341 + if (ret == -EOPNOTSUPP) 342 + dev_info(dev, "CEC support unavailable: node not found\n"); 343 + else if (ret) 344 + return ret; 345 + 346 + return 0; 347 + } 348 + 349 + static void mtk_hdmi_unregister_audio_driver(void *data) 350 + { 351 + platform_device_unregister(data); 352 + } 353 + 354 + static int mtk_hdmi_register_audio_driver(struct device *dev) 355 + { 356 + struct mtk_hdmi *hdmi = dev_get_drvdata(dev); 357 + struct hdmi_audio_param *aud_param = &hdmi->aud_param; 358 + struct hdmi_codec_pdata codec_data = { 359 + .ops = hdmi->conf->ver_conf->codec_ops, 360 + .max_i2s_channels = 2, 361 + .i2s = 1, 362 + .data = hdmi, 363 + .no_capture_mute = 1, 364 + }; 365 + int ret; 366 + 367 + aud_param->aud_codec = HDMI_AUDIO_CODING_TYPE_PCM; 368 + aud_param->aud_sample_size = HDMI_AUDIO_SAMPLE_SIZE_16; 369 + aud_param->aud_input_type = HDMI_AUD_INPUT_I2S; 370 + aud_param->aud_i2s_fmt = HDMI_I2S_MODE_I2S_24BIT; 371 + aud_param->aud_mclk = HDMI_AUD_MCLK_128FS; 372 + aud_param->aud_input_chan_type = HDMI_AUD_CHAN_TYPE_2_0; 373 + 374 + hdmi->audio_pdev = platform_device_register_data(dev, 375 + HDMI_CODEC_DRV_NAME, 376 + PLATFORM_DEVID_AUTO, 377 + &codec_data, 378 + sizeof(codec_data)); 379 + if (IS_ERR(hdmi->audio_pdev)) 380 + return PTR_ERR(hdmi->audio_pdev); 381 + 382 + ret = devm_add_action_or_reset(dev, mtk_hdmi_unregister_audio_driver, 383 + hdmi->audio_pdev); 384 + if (ret) 385 + return ret; 386 + 387 + return 0; 388 + } 389 + 390 + struct mtk_hdmi *mtk_hdmi_common_probe(struct platform_device *pdev) 391 + { 392 + const struct mtk_hdmi_ver_conf *ver_conf; 393 + const struct mtk_hdmi_conf *hdmi_conf; 394 + struct device *dev = &pdev->dev; 395 + struct mtk_hdmi *hdmi; 396 + int ret; 397 + 398 + hdmi_conf = of_device_get_match_data(dev); 399 + if (!hdmi_conf) 400 + return ERR_PTR(-ENODEV); 401 + 402 + ver_conf = hdmi_conf->ver_conf; 403 + 404 + hdmi = devm_drm_bridge_alloc(dev, struct mtk_hdmi, bridge, 405 + ver_conf->bridge_funcs); 406 + if (IS_ERR(hdmi)) 407 + return hdmi; 408 + 409 + hdmi->dev = dev; 410 + hdmi->conf = hdmi_conf; 411 + 412 + hdmi->clk = devm_kcalloc(dev, ver_conf->num_clocks, sizeof(*hdmi->clk), GFP_KERNEL); 413 + if (!hdmi->clk) 414 + return ERR_PTR(-ENOMEM); 415 + 416 + ret = mtk_hdmi_dt_parse_pdata(hdmi, pdev, ver_conf->mtk_hdmi_clock_names, 417 + ver_conf->num_clocks); 418 + if (ret) 419 + return ERR_PTR(ret); 420 + 421 + hdmi->phy = devm_phy_get(dev, "hdmi"); 422 + if (IS_ERR(hdmi->phy)) 423 + return dev_err_cast_probe(dev, hdmi->phy, "Failed to get HDMI PHY\n"); 424 + 425 + mutex_init(&hdmi->update_plugged_status_lock); 426 + platform_set_drvdata(pdev, hdmi); 427 + 428 + ret = mtk_hdmi_register_audio_driver(dev); 429 + if (ret) 430 + return dev_err_ptr_probe(dev, ret, "Cannot register HDMI Audio driver\n"); 431 + 432 + hdmi->bridge.of_node = pdev->dev.of_node; 433 + hdmi->bridge.ops = DRM_BRIDGE_OP_DETECT | DRM_BRIDGE_OP_EDID 434 + | DRM_BRIDGE_OP_HPD; 435 + 436 + if (ver_conf->bridge_funcs->hdmi_write_infoframe && 437 + ver_conf->bridge_funcs->hdmi_clear_infoframe) 438 + hdmi->bridge.ops |= DRM_BRIDGE_OP_HDMI; 439 + 440 + hdmi->bridge.type = DRM_MODE_CONNECTOR_HDMIA; 441 + hdmi->bridge.ddc = hdmi->ddc_adpt; 442 + hdmi->bridge.vendor = "MediaTek"; 443 + hdmi->bridge.product = "On-Chip HDMI"; 444 + hdmi->bridge.interlace_allowed = ver_conf->interlace_allowed; 445 + 446 + ret = devm_drm_bridge_add(dev, &hdmi->bridge); 447 + if (ret) 448 + return dev_err_ptr_probe(dev, ret, "Failed to add bridge\n"); 449 + 450 + return hdmi; 451 + } 452 + EXPORT_SYMBOL_NS_GPL(mtk_hdmi_common_probe, "DRM_MTK_HDMI"); 453 + 454 + MODULE_AUTHOR("AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>"); 455 + MODULE_DESCRIPTION("MediaTek HDMI Common Library"); 456 + MODULE_LICENSE("GPL");
+198
drivers/gpu/drm/mediatek/mtk_hdmi_common.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* 3 + * Copyright (c) 2021 MediaTek Inc. 4 + * Copyright (c) 2024 Collabora Ltd. 5 + */ 6 + 7 + #ifndef _MTK_HDMI_COMMON_H 8 + #define _MTK_HDMI_COMMON_H 9 + 10 + #include <drm/drm_atomic_helper.h> 11 + #include <drm/drm_bridge.h> 12 + #include <drm/drm_crtc.h> 13 + #include <drm/drm_crtc_helper.h> 14 + #include <drm/drm_edid.h> 15 + #include <drm/drm_print.h> 16 + 17 + #include <linux/clk.h> 18 + #include <linux/device.h> 19 + #include <linux/hdmi.h> 20 + #include <linux/i2c.h> 21 + #include <linux/mfd/syscon.h> 22 + #include <linux/mutex.h> 23 + #include <linux/phy/phy.h> 24 + #include <linux/platform_device.h> 25 + 26 + #include <sound/hdmi-codec.h> 27 + 28 + enum hdmi_aud_input_type { 29 + HDMI_AUD_INPUT_I2S = 0, 30 + HDMI_AUD_INPUT_SPDIF, 31 + }; 32 + 33 + enum hdmi_aud_i2s_fmt { 34 + HDMI_I2S_MODE_RJT_24BIT = 0, 35 + HDMI_I2S_MODE_RJT_16BIT, 36 + HDMI_I2S_MODE_LJT_24BIT, 37 + HDMI_I2S_MODE_LJT_16BIT, 38 + HDMI_I2S_MODE_I2S_24BIT, 39 + HDMI_I2S_MODE_I2S_16BIT 40 + }; 41 + 42 + enum hdmi_aud_mclk { 43 + HDMI_AUD_MCLK_128FS, 44 + HDMI_AUD_MCLK_192FS, 45 + HDMI_AUD_MCLK_256FS, 46 + HDMI_AUD_MCLK_384FS, 47 + HDMI_AUD_MCLK_512FS, 48 + HDMI_AUD_MCLK_768FS, 49 + HDMI_AUD_MCLK_1152FS, 50 + }; 51 + 52 + enum hdmi_aud_channel_type { 53 + HDMI_AUD_CHAN_TYPE_1_0 = 0, 54 + HDMI_AUD_CHAN_TYPE_1_1, 55 + HDMI_AUD_CHAN_TYPE_2_0, 56 + HDMI_AUD_CHAN_TYPE_2_1, 57 + HDMI_AUD_CHAN_TYPE_3_0, 58 + HDMI_AUD_CHAN_TYPE_3_1, 59 + HDMI_AUD_CHAN_TYPE_4_0, 60 + HDMI_AUD_CHAN_TYPE_4_1, 61 + HDMI_AUD_CHAN_TYPE_5_0, 62 + HDMI_AUD_CHAN_TYPE_5_1, 63 + HDMI_AUD_CHAN_TYPE_6_0, 64 + HDMI_AUD_CHAN_TYPE_6_1, 65 + HDMI_AUD_CHAN_TYPE_7_0, 66 + HDMI_AUD_CHAN_TYPE_7_1, 67 + HDMI_AUD_CHAN_TYPE_3_0_LRS, 68 + HDMI_AUD_CHAN_TYPE_3_1_LRS, 69 + HDMI_AUD_CHAN_TYPE_4_0_CLRS, 70 + HDMI_AUD_CHAN_TYPE_4_1_CLRS, 71 + HDMI_AUD_CHAN_TYPE_6_1_CS, 72 + HDMI_AUD_CHAN_TYPE_6_1_CH, 73 + HDMI_AUD_CHAN_TYPE_6_1_OH, 74 + HDMI_AUD_CHAN_TYPE_6_1_CHR, 75 + HDMI_AUD_CHAN_TYPE_7_1_LH_RH, 76 + HDMI_AUD_CHAN_TYPE_7_1_LSR_RSR, 77 + HDMI_AUD_CHAN_TYPE_7_1_LC_RC, 78 + HDMI_AUD_CHAN_TYPE_7_1_LW_RW, 79 + HDMI_AUD_CHAN_TYPE_7_1_LSD_RSD, 80 + HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS, 81 + HDMI_AUD_CHAN_TYPE_7_1_LHS_RHS, 82 + HDMI_AUD_CHAN_TYPE_7_1_CS_CH, 83 + HDMI_AUD_CHAN_TYPE_7_1_CS_OH, 84 + HDMI_AUD_CHAN_TYPE_7_1_CS_CHR, 85 + HDMI_AUD_CHAN_TYPE_7_1_CH_OH, 86 + HDMI_AUD_CHAN_TYPE_7_1_CH_CHR, 87 + HDMI_AUD_CHAN_TYPE_7_1_OH_CHR, 88 + HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS_LSR_RSR, 89 + HDMI_AUD_CHAN_TYPE_6_0_CS, 90 + HDMI_AUD_CHAN_TYPE_6_0_CH, 91 + HDMI_AUD_CHAN_TYPE_6_0_OH, 92 + HDMI_AUD_CHAN_TYPE_6_0_CHR, 93 + HDMI_AUD_CHAN_TYPE_7_0_LH_RH, 94 + HDMI_AUD_CHAN_TYPE_7_0_LSR_RSR, 95 + HDMI_AUD_CHAN_TYPE_7_0_LC_RC, 96 + HDMI_AUD_CHAN_TYPE_7_0_LW_RW, 97 + HDMI_AUD_CHAN_TYPE_7_0_LSD_RSD, 98 + HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS, 99 + HDMI_AUD_CHAN_TYPE_7_0_LHS_RHS, 100 + HDMI_AUD_CHAN_TYPE_7_0_CS_CH, 101 + HDMI_AUD_CHAN_TYPE_7_0_CS_OH, 102 + HDMI_AUD_CHAN_TYPE_7_0_CS_CHR, 103 + HDMI_AUD_CHAN_TYPE_7_0_CH_OH, 104 + HDMI_AUD_CHAN_TYPE_7_0_CH_CHR, 105 + HDMI_AUD_CHAN_TYPE_7_0_OH_CHR, 106 + HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS_LSR_RSR, 107 + HDMI_AUD_CHAN_TYPE_8_0_LH_RH_CS, 108 + HDMI_AUD_CHAN_TYPE_UNKNOWN = 0xFF 109 + }; 110 + 111 + enum hdmi_aud_channel_swap_type { 112 + HDMI_AUD_SWAP_LR, 113 + HDMI_AUD_SWAP_LFE_CC, 114 + HDMI_AUD_SWAP_LSRS, 115 + HDMI_AUD_SWAP_RLS_RRS, 116 + HDMI_AUD_SWAP_LR_STATUS, 117 + }; 118 + 119 + struct hdmi_audio_param { 120 + enum hdmi_audio_coding_type aud_codec; 121 + enum hdmi_audio_sample_size aud_sample_size; 122 + enum hdmi_aud_input_type aud_input_type; 123 + enum hdmi_aud_i2s_fmt aud_i2s_fmt; 124 + enum hdmi_aud_mclk aud_mclk; 125 + enum hdmi_aud_channel_type aud_input_chan_type; 126 + struct hdmi_codec_params codec_params; 127 + }; 128 + 129 + enum hdmi_hpd_state { 130 + HDMI_PLUG_OUT = 0, 131 + HDMI_PLUG_IN_AND_SINK_POWER_ON, 132 + HDMI_PLUG_IN_ONLY, 133 + }; 134 + 135 + struct mtk_hdmi_ver_conf { 136 + const struct drm_bridge_funcs *bridge_funcs; 137 + const struct hdmi_codec_ops *codec_ops; 138 + const char * const *mtk_hdmi_clock_names; 139 + int num_clocks; 140 + bool interlace_allowed; 141 + }; 142 + 143 + struct mtk_hdmi_conf { 144 + const struct mtk_hdmi_ver_conf *ver_conf; 145 + bool tz_disabled; 146 + bool cea_modes_only; 147 + unsigned long max_mode_clock; 148 + u32 reg_hdmi_tx_cfg; 149 + }; 150 + 151 + struct mtk_hdmi { 152 + struct drm_bridge bridge; 153 + struct drm_bridge *next_bridge; 154 + struct drm_connector *curr_conn;/* current connector (only valid when 'enabled') */ 155 + struct device *dev; 156 + const struct mtk_hdmi_conf *conf; 157 + struct phy *phy; 158 + struct device *cec_dev; 159 + struct i2c_adapter *ddc_adpt; 160 + struct clk **clk; 161 + struct drm_display_mode mode; 162 + bool dvi_mode; 163 + struct regmap *sys_regmap; 164 + unsigned int sys_offset; 165 + struct regmap *regs; 166 + struct platform_device *audio_pdev; 167 + struct hdmi_audio_param aud_param; 168 + bool audio_enable; 169 + bool powered; 170 + bool enabled; 171 + unsigned int irq; 172 + enum hdmi_hpd_state hpd; 173 + hdmi_codec_plugged_cb plugged_cb; 174 + struct device *codec_dev; 175 + struct mutex update_plugged_status_lock; 176 + }; 177 + 178 + static inline struct mtk_hdmi *hdmi_ctx_from_bridge(struct drm_bridge *b) 179 + { 180 + return container_of(b, struct mtk_hdmi, bridge); 181 + } 182 + 183 + 184 + int mtk_hdmi_audio_get_eld(struct device *dev, void *data, uint8_t *buf, size_t len); 185 + void mtk_hdmi_audio_set_plugged_cb(struct mtk_hdmi *hdmi, hdmi_codec_plugged_cb fn, 186 + struct device *codec_dev); 187 + int mtk_hdmi_audio_params(struct mtk_hdmi *hdmi, struct hdmi_codec_daifmt *daifmt, 188 + struct hdmi_codec_params *params); 189 + void mtk_hdmi_get_ncts(unsigned int sample_rate, unsigned int clock, 190 + unsigned int *n, unsigned int *cts); 191 + bool mtk_hdmi_bridge_mode_fixup(struct drm_bridge *bridge, 192 + const struct drm_display_mode *mode, 193 + struct drm_display_mode *adjusted_mode); 194 + void mtk_hdmi_bridge_mode_set(struct drm_bridge *bridge, 195 + const struct drm_display_mode *mode, 196 + const struct drm_display_mode *adjusted_mode); 197 + struct mtk_hdmi *mtk_hdmi_common_probe(struct platform_device *pdev); 198 + #endif /* _MTK_HDMI_COMMON_H */
+396
drivers/gpu/drm/mediatek/mtk_hdmi_ddc_v2.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * MediaTek HDMI v2 Display Data Channel Driver 4 + * 5 + * Copyright (c) 2021 MediaTek Inc. 6 + * Copyright (c) 2021 BayLibre, SAS 7 + * Copyright (c) 2024 Collabora Ltd. 8 + * AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> 9 + */ 10 + 11 + #include <linux/bitfield.h> 12 + #include <linux/clk.h> 13 + #include <linux/delay.h> 14 + #include <linux/device.h> 15 + #include <linux/err.h> 16 + #include <linux/i2c.h> 17 + #include <linux/kernel.h> 18 + #include <linux/mfd/syscon.h> 19 + #include <linux/module.h> 20 + #include <linux/mutex.h> 21 + #include <linux/of_platform.h> 22 + #include <linux/platform_device.h> 23 + #include <linux/pm_runtime.h> 24 + #include <linux/regmap.h> 25 + #include <linux/types.h> 26 + 27 + #include <drm/drm_edid.h> 28 + 29 + #include "mtk_hdmi_common.h" 30 + #include "mtk_hdmi_regs_v2.h" 31 + 32 + #define DDC2_DLY_CNT 572 /* BIM=208M/(v*4) = 90Khz */ 33 + #define DDC2_DLY_CNT_EDID 832 /* BIM=208M/(v*4) = 62.5Khz */ 34 + #define SI2C_ADDR_READ 0xf4 35 + #define SCDC_I2C_SLAVE_ADDRESS 0x54 36 + 37 + struct mtk_hdmi_ddc { 38 + struct device *dev; 39 + struct regmap *regs; 40 + struct clk *clk; 41 + struct i2c_adapter adap; 42 + }; 43 + 44 + static int mtk_ddc_check_and_rise_low_bus(struct mtk_hdmi_ddc *ddc) 45 + { 46 + u32 val; 47 + 48 + regmap_read(ddc->regs, HDCP2X_DDCM_STATUS, &val); 49 + if (val & DDC_I2C_BUS_LOW) { 50 + regmap_update_bits(ddc->regs, DDC_CTRL, DDC_CTRL_CMD, 51 + FIELD_PREP(DDC_CTRL_CMD, DDC_CMD_CLOCK_SCL)); 52 + usleep_range(250, 300); 53 + } 54 + 55 + if (val & DDC_I2C_NO_ACK) { 56 + u32 ddc_ctrl, hpd_ddc_ctrl, hpd_ddc_status; 57 + 58 + regmap_read(ddc->regs, DDC_CTRL, &ddc_ctrl); 59 + regmap_read(ddc->regs, HPD_DDC_CTRL, &hpd_ddc_ctrl); 60 + regmap_read(ddc->regs, HPD_DDC_STATUS, &hpd_ddc_status); 61 + } 62 + 63 + if (val & DDC_I2C_NO_ACK) 64 + return -EIO; 65 + 66 + return 0; 67 + } 68 + 69 + static int mtk_ddc_wr_one(struct mtk_hdmi_ddc *ddc, u16 addr_id, 70 + u16 offset_id, u8 *wr_data) 71 + { 72 + u32 val; 73 + int ret; 74 + 75 + /* If down, rise bus for write operation */ 76 + mtk_ddc_check_and_rise_low_bus(ddc); 77 + 78 + regmap_update_bits(ddc->regs, HPD_DDC_CTRL, HPD_DDC_DELAY_CNT, 79 + FIELD_PREP(HPD_DDC_DELAY_CNT, DDC2_DLY_CNT)); 80 + 81 + if (wr_data) { 82 + regmap_write(ddc->regs, SI2C_CTRL, 83 + FIELD_PREP(SI2C_ADDR, SI2C_ADDR_READ) | 84 + FIELD_PREP(SI2C_WDATA, *wr_data) | 85 + SI2C_WR); 86 + } 87 + 88 + regmap_write(ddc->regs, DDC_CTRL, 89 + FIELD_PREP(DDC_CTRL_CMD, DDC_CMD_SEQ_WRITE) | 90 + FIELD_PREP(DDC_CTRL_DIN_CNT, wr_data == NULL ? 0 : 1) | 91 + FIELD_PREP(DDC_CTRL_OFFSET, offset_id) | 92 + FIELD_PREP(DDC_CTRL_ADDR, addr_id)); 93 + usleep_range(1000, 1250); 94 + 95 + ret = regmap_read_poll_timeout(ddc->regs, HPD_DDC_STATUS, val, 96 + !(val & DDC_I2C_IN_PROG), 500, 1000); 97 + if (ret) { 98 + dev_err(ddc->dev, "DDC I2C write timeout\n"); 99 + return ret; 100 + } 101 + 102 + /* The I2C bus might be down after WR operation: rise it again */ 103 + ret = mtk_ddc_check_and_rise_low_bus(ddc); 104 + if (ret) { 105 + dev_err(ddc->dev, "Error during write operation: No ACK\n"); 106 + return ret; 107 + } 108 + 109 + return 0; 110 + } 111 + 112 + static int mtk_ddcm_read_hdmi(struct mtk_hdmi_ddc *ddc, u16 uc_dev, 113 + u8 addr, u8 *puc_value, u16 data_cnt) 114 + { 115 + u16 dly_cnt, i, uc_idx; 116 + u32 rem, temp_length, uc_read_count, val; 117 + u64 loop_counter; 118 + int ret; 119 + 120 + mtk_ddc_check_and_rise_low_bus(ddc); 121 + 122 + regmap_update_bits(ddc->regs, DDC_CTRL, DDC_CTRL_CMD, 123 + FIELD_PREP(DDC_CTRL_CMD, DDC_CMD_CLEAR_FIFO)); 124 + 125 + if (data_cnt >= 16) { 126 + temp_length = 16; 127 + loop_counter = data_cnt; 128 + 129 + rem = do_div(loop_counter, temp_length); 130 + if (rem) 131 + loop_counter++; 132 + } else { 133 + temp_length = data_cnt; 134 + loop_counter = 1; 135 + } 136 + 137 + if (uc_dev >= DDC_ADDR) 138 + dly_cnt = DDC2_DLY_CNT_EDID; 139 + else 140 + dly_cnt = DDC2_DLY_CNT; 141 + 142 + regmap_update_bits(ddc->regs, HPD_DDC_CTRL, HPD_DDC_DELAY_CNT, 143 + FIELD_PREP(HPD_DDC_DELAY_CNT, dly_cnt)); 144 + 145 + for (i = 0; i < loop_counter; i++) { 146 + rem = data_cnt % 16; 147 + 148 + if (i > 0 && i == (loop_counter - 1) && rem) 149 + temp_length = rem; 150 + 151 + /* 0x51 - 0x53: Flow control */ 152 + if (uc_dev > DDC_ADDR && uc_dev <= 0x53) { 153 + regmap_update_bits(ddc->regs, SCDC_CTRL, SCDC_DDC_SEGMENT, 154 + FIELD_PREP(SCDC_DDC_SEGMENT, uc_dev - DDC_ADDR)); 155 + 156 + regmap_write(ddc->regs, DDC_CTRL, 157 + FIELD_PREP(DDC_CTRL_CMD, DDC_CMD_ENH_READ_NOACK) | 158 + FIELD_PREP(DDC_CTRL_DIN_CNT, temp_length) | 159 + FIELD_PREP(DDC_CTRL_OFFSET, addr + i * temp_length) | 160 + FIELD_PREP(DDC_CTRL_ADDR, DDC_ADDR)); 161 + } else { 162 + u16 offset; 163 + 164 + if (addr != 0x43) 165 + offset = i * 16; 166 + else 167 + offset = 0; 168 + 169 + regmap_write(ddc->regs, DDC_CTRL, 170 + FIELD_PREP(DDC_CTRL_CMD, DDC_CMD_SEQ_READ_NOACK) | 171 + FIELD_PREP(DDC_CTRL_DIN_CNT, temp_length) | 172 + FIELD_PREP(DDC_CTRL_OFFSET, addr + offset) | 173 + FIELD_PREP(DDC_CTRL_ADDR, uc_dev)); 174 + } 175 + usleep_range(5000, 5500); 176 + 177 + ret = regmap_read_poll_timeout(ddc->regs, HPD_DDC_STATUS, val, 178 + !(val & DDC_I2C_IN_PROG), 1000, 179 + 500 * (temp_length + 5)); 180 + if (ret) { 181 + dev_err(ddc->dev, "Timeout waiting for DDC I2C\n"); 182 + return ret; 183 + } 184 + 185 + ret = mtk_ddc_check_and_rise_low_bus(ddc); 186 + if (ret) { 187 + dev_err(ddc->dev, "Error during read operation: No ACK\n"); 188 + return ret; 189 + } 190 + 191 + for (uc_idx = 0; uc_idx < temp_length; uc_idx++) { 192 + unsigned int read_idx = i * 16 + uc_idx; 193 + 194 + regmap_write(ddc->regs, SI2C_CTRL, 195 + FIELD_PREP(SI2C_ADDR, SI2C_ADDR_READ) | 196 + SI2C_RD); 197 + 198 + regmap_read(ddc->regs, HPD_DDC_STATUS, &val); 199 + puc_value[read_idx] = FIELD_GET(DDC_DATA_OUT, val); 200 + 201 + regmap_write(ddc->regs, SI2C_CTRL, 202 + FIELD_PREP(SI2C_ADDR, SI2C_ADDR_READ) | 203 + SI2C_CONFIRM_READ); 204 + 205 + /* 206 + * If HDMI IP gets reset during EDID read, DDC read 207 + * operation will fail and its delay counter will be 208 + * reset to 400. 209 + */ 210 + regmap_read(ddc->regs, HPD_DDC_CTRL, &val); 211 + if (FIELD_GET(HPD_DDC_DELAY_CNT, val) < DDC2_DLY_CNT) 212 + return 0; 213 + 214 + uc_read_count = read_idx + 1; 215 + } 216 + } 217 + if (uc_read_count > U8_MAX) 218 + dev_warn(ddc->dev, "Invalid read data count %u\n", uc_read_count); 219 + 220 + return uc_read_count; 221 + } 222 + 223 + static int mtk_hdmi_fg_ddc_data_read(struct mtk_hdmi_ddc *ddc, u16 b_dev, 224 + u8 data_addr, u16 data_cnt, u8 *pr_data) 225 + { 226 + int read_data_cnt; 227 + u16 req_data_cnt; 228 + 229 + if (!data_cnt) { 230 + dev_err(ddc->dev, "Invalid DDCM read request\n"); 231 + return -EINVAL; 232 + } 233 + 234 + req_data_cnt = U8_MAX - data_addr + 1; 235 + if (req_data_cnt > data_cnt) 236 + req_data_cnt = data_cnt; 237 + 238 + regmap_set_bits(ddc->regs, HDCP2X_POL_CTRL, HDCP2X_DIS_POLL_EN); 239 + 240 + read_data_cnt = mtk_ddcm_read_hdmi(ddc, b_dev, data_addr, pr_data, req_data_cnt); 241 + 242 + if (read_data_cnt < 0) 243 + return read_data_cnt; 244 + else if (read_data_cnt != req_data_cnt) 245 + return -EINVAL; 246 + 247 + return 0; 248 + } 249 + 250 + static int mtk_hdmi_ddc_fg_data_write(struct mtk_hdmi_ddc *ddc, u16 b_dev, 251 + u8 data_addr, u16 data_cnt, u8 *pr_data) 252 + { 253 + int i, ret; 254 + 255 + regmap_set_bits(ddc->regs, HDCP2X_POL_CTRL, HDCP2X_DIS_POLL_EN); 256 + /* 257 + * In case there is no payload data, just do a single write for the 258 + * address only 259 + */ 260 + if (data_cnt == 0) 261 + return mtk_ddc_wr_one(ddc, b_dev, data_addr, NULL); 262 + 263 + i = 0; 264 + do { 265 + ret = mtk_ddc_wr_one(ddc, b_dev, data_addr + i, pr_data + i); 266 + if (ret) 267 + return ret; 268 + } while (++i < data_cnt); 269 + 270 + return 0; 271 + } 272 + 273 + static int mtk_hdmi_ddc_v2_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num) 274 + { 275 + struct mtk_hdmi_ddc *ddc; 276 + u8 offset = 0; 277 + int i, ret; 278 + 279 + ddc = adapter->algo_data; 280 + 281 + for (i = 0; i < num; i++) { 282 + struct i2c_msg *msg = &msgs[i]; 283 + 284 + if (!msg->buf) { 285 + dev_err(ddc->dev, "No message buffer\n"); 286 + return -EINVAL; 287 + } 288 + 289 + if (msg->flags & I2C_M_RD) { 290 + /* 291 + * The underlying DDC hardware always issues a write request 292 + * that assigns the read offset as part of the read operation, 293 + * therefore, use the `offset` value assigned in the previous 294 + * write request from drm_edid 295 + */ 296 + ret = mtk_hdmi_fg_ddc_data_read(ddc, msg->addr, offset, 297 + msg->len, &msg->buf[0]); 298 + if (ret) 299 + return ret; 300 + } else { 301 + /* 302 + * The HW needs the data offset, found in buf[0], in the 303 + * DDC_CTRL register, and each byte of data, starting at 304 + * buf[1], goes in the SI2C_WDATA register. 305 + */ 306 + ret = mtk_hdmi_ddc_fg_data_write(ddc, msg->addr, msg->buf[0], 307 + msg->len - 1, &msg->buf[1]); 308 + if (ret) 309 + return ret; 310 + 311 + /* 312 + * Store the offset value requested by drm_edid or by 313 + * scdc to use in subsequent read requests. 314 + */ 315 + if ((msg->addr == DDC_ADDR || msg->addr == SCDC_I2C_SLAVE_ADDRESS) && 316 + msg->len == 1) { 317 + offset = msg->buf[0]; 318 + } 319 + } 320 + } 321 + 322 + return i; 323 + } 324 + 325 + static u32 mtk_hdmi_ddc_v2_func(struct i2c_adapter *adapter) 326 + { 327 + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; 328 + } 329 + 330 + static const struct i2c_algorithm mtk_hdmi_ddc_v2_algorithm = { 331 + .master_xfer = mtk_hdmi_ddc_v2_xfer, 332 + .functionality = mtk_hdmi_ddc_v2_func, 333 + }; 334 + 335 + static int mtk_hdmi_ddc_v2_probe(struct platform_device *pdev) 336 + { 337 + struct device *dev = &pdev->dev; 338 + struct mtk_hdmi_ddc *ddc; 339 + int ret; 340 + 341 + ddc = devm_kzalloc(dev, sizeof(*ddc), GFP_KERNEL); 342 + if (!ddc) 343 + return -ENOMEM; 344 + 345 + ddc->dev = dev; 346 + ddc->regs = device_node_to_regmap(dev->parent->of_node); 347 + if (IS_ERR_OR_NULL(ddc->regs)) 348 + return dev_err_probe(dev, 349 + IS_ERR(ddc->regs) ? PTR_ERR(ddc->regs) : -EINVAL, 350 + "Cannot get regmap\n"); 351 + 352 + ddc->clk = devm_clk_get_enabled(dev, NULL); 353 + if (IS_ERR(ddc->clk)) 354 + return dev_err_probe(dev, PTR_ERR(ddc->clk), "Cannot get DDC clock\n"); 355 + 356 + strscpy(ddc->adap.name, "mediatek-hdmi-ddc-v2", sizeof(ddc->adap.name)); 357 + ddc->adap.owner = THIS_MODULE; 358 + ddc->adap.algo = &mtk_hdmi_ddc_v2_algorithm; 359 + ddc->adap.retries = 3; 360 + ddc->adap.dev.of_node = dev->of_node; 361 + ddc->adap.algo_data = ddc; 362 + ddc->adap.dev.parent = &pdev->dev; 363 + 364 + ret = devm_pm_runtime_enable(&pdev->dev); 365 + if (ret) 366 + return dev_err_probe(&pdev->dev, ret, "Cannot enable Runtime PM\n"); 367 + 368 + pm_runtime_get_sync(dev); 369 + 370 + ret = devm_i2c_add_adapter(dev, &ddc->adap); 371 + if (ret < 0) 372 + return dev_err_probe(dev, ret, "Cannot add DDC I2C adapter\n"); 373 + 374 + platform_set_drvdata(pdev, ddc); 375 + return 0; 376 + } 377 + 378 + static const struct of_device_id mtk_hdmi_ddc_v2_match[] = { 379 + { .compatible = "mediatek,mt8195-hdmi-ddc" }, 380 + { /* sentinel */ } 381 + }; 382 + MODULE_DEVICE_TABLE(of, mtk_hdmi_ddc_v2_match); 383 + 384 + struct platform_driver mtk_hdmi_ddc_v2_driver = { 385 + .probe = mtk_hdmi_ddc_v2_probe, 386 + .driver = { 387 + .name = "mediatek-hdmi-ddc-v2", 388 + .of_match_table = mtk_hdmi_ddc_v2_match, 389 + }, 390 + }; 391 + module_platform_driver(mtk_hdmi_ddc_v2_driver); 392 + 393 + MODULE_AUTHOR("AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>"); 394 + MODULE_AUTHOR("Can Zeng <can.zeng@mediatek.com>"); 395 + MODULE_DESCRIPTION("MediaTek HDMIv2 DDC Driver"); 396 + MODULE_LICENSE("GPL");
+263
drivers/gpu/drm/mediatek/mtk_hdmi_regs_v2.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + /* 3 + * Copyright (c) 2021 MediaTek Inc. 4 + * Copyright (c) 2021 BayLibre, SAS 5 + * Copyright (c) 2024 Collabora Ltd. 6 + * AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> 7 + */ 8 + 9 + #ifndef _MTK_HDMI_REGS_H 10 + #define _MTK_HDMI_REGS_H 11 + 12 + /* HDMI_TOP Config */ 13 + #define TOP_CFG00 0x000 14 + #define HDMI2_ON BIT(2) 15 + #define HDMI_MODE_HDMI BIT(3) 16 + #define SCR_ON BIT(4) 17 + #define TMDS_PACK_MODE GENMASK(9, 8) 18 + #define TMDS_PACK_MODE_8BPP 0 19 + #define TMDS_PACK_MODE_10BPP 1 20 + #define TMDS_PACK_MODE_12BPP 2 21 + #define TMDS_PACK_MODE_16BPP 3 22 + #define DEEPCOLOR_PKT_EN BIT(12) 23 + #define HDMI_ABIST_VIDEO_FORMAT GENMASK(21, 16) 24 + #define HDMI_ABIST_ENABLE BIT(31) 25 + #define TOP_CFG01 0x004 26 + #define CP_SET_MUTE_EN BIT(0) 27 + #define CP_CLR_MUTE_EN BIT(1) 28 + #define NULL_PKT_EN BIT(2) 29 + #define NULL_PKT_VSYNC_HIGH_EN BIT(3) 30 + 31 + /* HDMI_TOP Audio: Channel Mapping */ 32 + #define TOP_AUD_MAP 0x00c 33 + #define SD0_MAP GENMASK(2, 0) 34 + #define SD1_MAP GENMASK(6, 4) 35 + #define SD2_MAP GENMASK(10, 8) 36 + #define SD3_MAP GENMASK(14, 12) 37 + #define SD4_MAP GENMASK(18, 16) 38 + #define SD5_MAP GENMASK(22, 20) 39 + #define SD6_MAP GENMASK(26, 24) 40 + #define SD7_MAP GENMASK(30, 28) 41 + 42 + /* Auxiliary Video Information (AVI) Infoframe */ 43 + #define TOP_AVI_HEADER 0x024 44 + #define TOP_AVI_PKT00 0x028 45 + #define TOP_AVI_PKT01 0x02C 46 + #define TOP_AVI_PKT02 0x030 47 + #define TOP_AVI_PKT03 0x034 48 + #define TOP_AVI_PKT04 0x038 49 + #define TOP_AVI_PKT05 0x03C 50 + 51 + /* Audio Interface Infoframe */ 52 + #define TOP_AIF_HEADER 0x040 53 + #define TOP_AIF_PKT00 0x044 54 + #define TOP_AIF_PKT01 0x048 55 + #define TOP_AIF_PKT02 0x04c 56 + #define TOP_AIF_PKT03 0x050 57 + 58 + /* Audio SPDIF Infoframe */ 59 + #define TOP_SPDIF_HEADER 0x054 60 + #define TOP_SPDIF_PKT00 0x058 61 + #define TOP_SPDIF_PKT01 0x05c 62 + #define TOP_SPDIF_PKT02 0x060 63 + #define TOP_SPDIF_PKT03 0x064 64 + #define TOP_SPDIF_PKT04 0x068 65 + #define TOP_SPDIF_PKT05 0x06c 66 + #define TOP_SPDIF_PKT06 0x070 67 + #define TOP_SPDIF_PKT07 0x074 68 + 69 + /* Infoframes Configuration */ 70 + #define TOP_INFO_EN 0x01c 71 + #define AVI_EN BIT(0) 72 + #define SPD_EN BIT(1) 73 + #define AUD_EN BIT(2) 74 + #define CP_EN BIT(5) 75 + #define VSIF_EN BIT(11) 76 + #define AVI_EN_WR BIT(16) 77 + #define SPD_EN_WR BIT(17) 78 + #define AUD_EN_WR BIT(18) 79 + #define CP_EN_WR BIT(21) 80 + #define VSIF_EN_WR BIT(27) 81 + #define TOP_INFO_RPT 0x020 82 + #define AVI_RPT_EN BIT(0) 83 + #define SPD_RPT_EN BIT(1) 84 + #define AUD_RPT_EN BIT(2) 85 + #define CP_RPT_EN BIT(5) 86 + #define VSIF_RPT_EN BIT(11) 87 + 88 + /* Vendor Specific Infoframe */ 89 + #define TOP_VSIF_HEADER 0x174 90 + #define TOP_VSIF_PKT00 0x178 91 + #define TOP_VSIF_PKT01 0x17c 92 + #define TOP_VSIF_PKT02 0x180 93 + #define TOP_VSIF_PKT03 0x184 94 + #define TOP_VSIF_PKT04 0x188 95 + #define TOP_VSIF_PKT05 0x18c 96 + #define TOP_VSIF_PKT06 0x190 97 + #define TOP_VSIF_PKT07 0x194 98 + 99 + /* HDMI_TOP Misc */ 100 + #define TOP_MISC_CTLR 0x1a4 101 + #define DEEP_COLOR_ADD BIT(4) 102 + 103 + /* Hardware interrupts */ 104 + #define TOP_INT_STA00 0x1a8 105 + #define TOP_INT_ENABLE00 0x1b0 106 + #define HTPLG_R_INT BIT(0) 107 + #define HTPLG_F_INT BIT(1) 108 + #define PORD_R_INT BIT(2) 109 + #define PORD_F_INT BIT(3) 110 + #define HDMI_VSYNC_INT BIT(4) 111 + #define HDMI_AUDIO_INT BIT(5) 112 + #define HDCP2X_RX_REAUTH_REQ_DDCM_INT BIT(25) 113 + #define TOP_INT_ENABLE01 0x1b4 114 + #define TOP_INT_CLR00 0x1b8 115 + #define TOP_INT_CLR01 0x1bc 116 + 117 + 118 + /* Video Mute */ 119 + #define TOP_VMUTE_CFG1 0x1c8 120 + #define REG_VMUTE_EN BIT(16) 121 + 122 + /* HDMI Audio IP */ 123 + #define AIP_CTRL 0x400 124 + #define CTS_SW_SEL BIT(0) 125 + #define CTS_REQ_EN BIT(1) 126 + #define MCLK_EN BIT(2) 127 + #define NO_MCLK_CTSGEN_SEL BIT(3) 128 + #define AUD_IN_EN BIT(8) 129 + #define AUD_SEL_OWRT BIT(9) 130 + #define SPDIF_EN BIT(13) 131 + #define HBRA_ON BIT(14) 132 + #define DSD_EN BIT(15) 133 + #define I2S_EN GENMASK(19, 16) 134 + #define HBR_FROM_SPDIF BIT(20) 135 + #define CTS_CAL_N4 BIT(23) 136 + #define SPDIF_INTERNAL_MODULE BIT(24) 137 + #define AIP_N_VAL 0x404 138 + #define AIP_CTS_SVAL 0x408 139 + #define AIP_SPDIF_CTRL 0x40c 140 + #define WR_1UI_LOCK BIT(0) 141 + #define FS_OVERRIDE_WRITE BIT(1) 142 + #define WR_2UI_LOCK BIT(2) 143 + #define MAX_1UI_WRITE GENMASK(15, 8) 144 + #define MAX_2UI_SPDIF_WRITE GENMASK(23, 16) 145 + #define MAX_2UI_I2S_HI_WRITE GENMASK(23, 20) 146 + #define MAX_2UI_I2S_LFE_CC_SWAP BIT(1) 147 + #define MAX_2UI_I2S_LO_WRITE GENMASK(19, 16) 148 + #define AUD_ERR_THRESH GENMASK(29, 24) 149 + #define I2S2DSD_EN BIT(30) 150 + #define AIP_I2S_CTRL 0x410 151 + #define FIFO0_MAP GENMASK(1, 0) 152 + #define FIFO1_MAP GENMASK(3, 2) 153 + #define FIFO2_MAP GENMASK(5, 4) 154 + #define FIFO3_MAP GENMASK(7, 6) 155 + #define I2S_1ST_BIT_NOSHIFT BIT(8) 156 + #define I2S_DATA_DIR_LSB BIT(9) 157 + #define JUSTIFY_RIGHT BIT(10) 158 + #define WS_HIGH BIT(11) 159 + #define VBIT_COMPRESSED BIT(12) 160 + #define CBIT_ORDER_SAME BIT(13) 161 + #define SCK_EDGE_RISE BIT(14) 162 + #define AIP_I2S_CHST0 0x414 163 + #define AIP_I2S_CHST1 0x418 164 + #define AIP_TXCTRL 0x424 165 + #define RST4AUDIO BIT(0) 166 + #define RST4AUDIO_FIFO BIT(1) 167 + #define RST4AUDIO_ACR BIT(2) 168 + #define AUD_LAYOUT_1 BIT(4) 169 + #define AUD_MUTE_FIFO_EN BIT(5) 170 + #define AUD_PACKET_DROP BIT(6) 171 + #define DSD_MUTE_EN BIT(7) 172 + #define AIP_TPI_CTRL 0x428 173 + #define TPI_AUDIO_LOOKUP_EN BIT(2) 174 + 175 + /* Video downsampling configuration */ 176 + #define VID_DOWNSAMPLE_CONFIG 0x8d0 177 + #define C444_C422_CONFIG_ENABLE BIT(0) 178 + #define C422_C420_CONFIG_ENABLE BIT(4) 179 + #define C422_C420_CONFIG_BYPASS BIT(5) 180 + #define C422_C420_CONFIG_OUT_CB_OR_CR BIT(6) 181 + #define VID_OUT_FORMAT 0x8fc 182 + #define OUTPUT_FORMAT_DEMUX_420_ENABLE BIT(10) 183 + 184 + /* HDCP registers */ 185 + #define HDCP_TOP_CTRL 0xc00 186 + #define HDCP2X_CTRL_0 0xc20 187 + #define HDCP2X_EN BIT(0) 188 + #define HDCP2X_ENCRYPT_EN BIT(7) 189 + #define HDCP2X_HPD_OVR BIT(10) 190 + #define HDCP2X_HPD_SW BIT(11) 191 + #define HDCP2X_POL_CTRL 0xc54 192 + #define HDCP2X_DIS_POLL_EN BIT(16) 193 + #define HDCP1X_CTRL 0xcd0 194 + #define HDCP1X_ENC_EN BIT(6) 195 + 196 + /* HDMI DDC registers */ 197 + #define HPD_DDC_CTRL 0xc08 198 + #define HPD_DDC_DELAY_CNT GENMASK(31, 16) 199 + #define HPD_DDC_HPD_DBNC_EN BIT(2) 200 + #define HPD_DDC_PORD_DBNC_EN BIT(3) 201 + #define DDC_CTRL 0xc10 202 + #define DDC_CTRL_ADDR GENMASK(7, 1) 203 + #define DDC_CTRL_OFFSET GENMASK(15, 8) 204 + #define DDC_CTRL_DIN_CNT GENMASK(25, 16) 205 + #define DDC_CTRL_CMD GENMASK(31, 28) 206 + #define SCDC_CTRL 0xc18 207 + #define SCDC_DDC_SEGMENT GENMASK(15, 8) 208 + #define HPD_DDC_STATUS 0xc60 209 + #define HPD_STATE GENMASK(1, 0) 210 + #define HPD_STATE_CONNECTED 2 211 + #define HPD_PIN_STA BIT(4) 212 + #define PORD_PIN_STA BIT(5) 213 + #define DDC_I2C_IN_PROG BIT(13) 214 + #define DDC_DATA_OUT GENMASK(23, 16) 215 + #define SI2C_CTRL 0xcac 216 + #define SI2C_WR BIT(0) 217 + #define SI2C_RD BIT(1) 218 + #define SI2C_CONFIRM_READ BIT(2) 219 + #define SI2C_WDATA GENMASK(15, 8) 220 + #define SI2C_ADDR GENMASK(23, 16) 221 + 222 + /* HDCP DDC registers */ 223 + #define HDCP2X_DDCM_STATUS 0xc68 224 + #define DDC_I2C_NO_ACK BIT(10) 225 + #define DDC_I2C_BUS_LOW BIT(11) 226 + 227 + /* HDMI TX registers */ 228 + #define HDMITX_CONFIG_MT8188 0xea0 229 + #define HDMITX_CONFIG_MT8195 0x900 230 + #define HDMI_YUV420_MODE BIT(10) 231 + #define HDMITX_SW_HPD BIT(29) 232 + #define HDMITX_SW_RSTB BIT(31) 233 + 234 + /** 235 + * enum mtk_hdmi_ddc_v2_cmds - DDC_CMD register commands 236 + * @DDC_CMD_READ_NOACK: Current address read with no ACK on last byte 237 + * @DDC_CMD_READ: Current address read with ACK on last byte 238 + * @DDC_CMD_SEQ_READ_NOACK: Sequential read with no ACK on last byte 239 + * @DDC_CMD_SEQ_READ: Sequential read with ACK on last byte 240 + * @DDC_CMD_ENH_READ_NOACK: Enhanced read with no ACK on last byte 241 + * @DDC_CMD_ENH_READ: Enhanced read with ACK on last byte 242 + * @DDC_CMD_SEQ_WRITE_NOACK: Sequential write ignoring ACK on last byte 243 + * @DDC_CMD_SEQ_WRITE: Sequential write requiring ACK on last byte 244 + * @DDC_CMD_RSVD: Reserved for future use 245 + * @DDC_CMD_CLEAR_FIFO: Clear DDC I2C FIFO 246 + * @DDC_CMD_CLOCK_SCL: Start clocking DDC I2C SCL 247 + * @DDC_CMD_ABORT_XFER: Abort DDC I2C transaction 248 + */ 249 + enum mtk_hdmi_ddc_v2_cmds { 250 + DDC_CMD_READ_NOACK = 0x0, 251 + DDC_CMD_READ, 252 + DDC_CMD_SEQ_READ_NOACK, 253 + DDC_CMD_SEQ_READ, 254 + DDC_CMD_ENH_READ_NOACK, 255 + DDC_CMD_ENH_READ, 256 + DDC_CMD_SEQ_WRITE_NOACK, 257 + DDC_CMD_SEQ_WRITE = 0x07, 258 + DDC_CMD_CLEAR_FIFO = 0x09, 259 + DDC_CMD_CLOCK_SCL = 0x0a, 260 + DDC_CMD_ABORT_XFER = 0x0f 261 + }; 262 + 263 + #endif /* _MTK_HDMI_REGS_H */
+1521
drivers/gpu/drm/mediatek/mtk_hdmi_v2.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * MediaTek HDMI v2 IP driver 4 + * 5 + * Copyright (c) 2022 MediaTek Inc. 6 + * Copyright (c) 2022 BayLibre, SAS 7 + * Copyright (c) 2024 Collabora Ltd. 8 + * AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com> 9 + */ 10 + 11 + #include <linux/bitfield.h> 12 + #include <linux/clk.h> 13 + #include <linux/debugfs.h> 14 + #include <linux/delay.h> 15 + #include <linux/device.h> 16 + #include <linux/err.h> 17 + #include <linux/interrupt.h> 18 + #include <linux/irq.h> 19 + #include <linux/kernel.h> 20 + #include <linux/mutex.h> 21 + #include <linux/of.h> 22 + #include <linux/of_platform.h> 23 + #include <linux/pm_runtime.h> 24 + #include <linux/regmap.h> 25 + #include <linux/suspend.h> 26 + #include <linux/units.h> 27 + #include <linux/phy/phy.h> 28 + 29 + #include <drm/display/drm_hdmi_helper.h> 30 + #include <drm/display/drm_hdmi_state_helper.h> 31 + #include <drm/display/drm_scdc_helper.h> 32 + #include <drm/drm_drv.h> 33 + #include <drm/drm_edid.h> 34 + #include <drm/drm_print.h> 35 + #include <drm/drm_probe_helper.h> 36 + 37 + #include "mtk_hdmi_common.h" 38 + #include "mtk_hdmi_regs_v2.h" 39 + 40 + #define MTK_HDMI_V2_CLOCK_MIN 27000 41 + #define MTK_HDMI_V2_CLOCK_MAX 594000 42 + 43 + #define HPD_PORD_HWIRQS (HTPLG_R_INT | HTPLG_F_INT | PORD_F_INT | PORD_R_INT) 44 + 45 + enum mtk_hdmi_v2_clk_id { 46 + MTK_HDMI_V2_CLK_HDCP_SEL, 47 + MTK_HDMI_V2_CLK_HDCP_24M_SEL, 48 + MTK_HDMI_V2_CLK_VPP_SPLIT_HDMI, 49 + MTK_HDMI_V2_CLK_HDMI_APB_SEL, 50 + MTK_HDMI_V2_CLK_COUNT, 51 + }; 52 + 53 + const char *const mtk_hdmi_v2_clk_names[MTK_HDMI_V2_CLK_COUNT] = { 54 + [MTK_HDMI_V2_CLK_HDMI_APB_SEL] = "bus", 55 + [MTK_HDMI_V2_CLK_HDCP_SEL] = "hdcp", 56 + [MTK_HDMI_V2_CLK_HDCP_24M_SEL] = "hdcp24m", 57 + [MTK_HDMI_V2_CLK_VPP_SPLIT_HDMI] = "hdmi-split", 58 + }; 59 + 60 + static inline void mtk_hdmi_v2_hwirq_disable(struct mtk_hdmi *hdmi) 61 + { 62 + regmap_write(hdmi->regs, TOP_INT_ENABLE00, 0); 63 + regmap_write(hdmi->regs, TOP_INT_ENABLE01, 0); 64 + } 65 + 66 + static inline void mtk_hdmi_v2_enable_hpd_pord_irq(struct mtk_hdmi *hdmi, bool enable) 67 + { 68 + if (enable) 69 + regmap_set_bits(hdmi->regs, TOP_INT_ENABLE00, HPD_PORD_HWIRQS); 70 + else 71 + regmap_clear_bits(hdmi->regs, TOP_INT_ENABLE00, HPD_PORD_HWIRQS); 72 + } 73 + 74 + static inline void mtk_hdmi_v2_set_sw_hpd(struct mtk_hdmi *hdmi, bool enable) 75 + { 76 + if (enable) { 77 + regmap_set_bits(hdmi->regs, hdmi->conf->reg_hdmi_tx_cfg, HDMITX_SW_HPD); 78 + regmap_set_bits(hdmi->regs, HDCP2X_CTRL_0, HDCP2X_HPD_OVR); 79 + regmap_set_bits(hdmi->regs, HDCP2X_CTRL_0, HDCP2X_HPD_SW); 80 + } else { 81 + regmap_clear_bits(hdmi->regs, HDCP2X_CTRL_0, HDCP2X_HPD_OVR); 82 + regmap_clear_bits(hdmi->regs, HDCP2X_CTRL_0, HDCP2X_HPD_SW); 83 + regmap_clear_bits(hdmi->regs, hdmi->conf->reg_hdmi_tx_cfg, HDMITX_SW_HPD); 84 + } 85 + } 86 + 87 + static inline void mtk_hdmi_v2_enable_scrambling(struct mtk_hdmi *hdmi, bool enable) 88 + { 89 + struct drm_scdc *scdc = &hdmi->curr_conn->display_info.hdmi.scdc; 90 + 91 + if (enable) 92 + regmap_set_bits(hdmi->regs, TOP_CFG00, SCR_ON | HDMI2_ON); 93 + else 94 + regmap_clear_bits(hdmi->regs, TOP_CFG00, SCR_ON | HDMI2_ON); 95 + 96 + if (scdc->supported) { 97 + if (scdc->scrambling.supported) 98 + drm_scdc_set_scrambling(hdmi->curr_conn, enable); 99 + drm_scdc_set_high_tmds_clock_ratio(hdmi->curr_conn, enable); 100 + } 101 + } 102 + 103 + static void mtk_hdmi_v2_hw_vid_mute(struct mtk_hdmi *hdmi, bool enable) 104 + { 105 + /* If enabled, sends a black image */ 106 + if (enable) 107 + regmap_set_bits(hdmi->regs, TOP_VMUTE_CFG1, REG_VMUTE_EN); 108 + else 109 + regmap_clear_bits(hdmi->regs, TOP_VMUTE_CFG1, REG_VMUTE_EN); 110 + } 111 + 112 + static void mtk_hdmi_v2_hw_aud_mute(struct mtk_hdmi *hdmi, bool enable) 113 + { 114 + u32 aip, val; 115 + 116 + if (!enable) { 117 + regmap_clear_bits(hdmi->regs, AIP_TXCTRL, AUD_MUTE_FIFO_EN); 118 + return; 119 + } 120 + 121 + regmap_read(hdmi->regs, AIP_CTRL, &aip); 122 + 123 + val = AUD_MUTE_FIFO_EN; 124 + if (aip & DSD_EN) 125 + val |= DSD_MUTE_EN; 126 + 127 + regmap_update_bits(hdmi->regs, AIP_TXCTRL, val, val); 128 + } 129 + 130 + static void mtk_hdmi_v2_hw_reset(struct mtk_hdmi *hdmi) 131 + { 132 + regmap_clear_bits(hdmi->regs, hdmi->conf->reg_hdmi_tx_cfg, HDMITX_SW_RSTB); 133 + udelay(5); 134 + regmap_set_bits(hdmi->regs, hdmi->conf->reg_hdmi_tx_cfg, HDMITX_SW_RSTB); 135 + } 136 + 137 + static inline u32 mtk_hdmi_v2_format_hw_packet(const u8 *buffer, u8 len) 138 + { 139 + unsigned short i; 140 + u32 val = 0; 141 + 142 + for (i = 0; i < len; i++) 143 + val |= buffer[i] << (i * 8); 144 + 145 + return val; 146 + } 147 + 148 + static void mtk_hdmi_v2_hw_write_audio_infoframe(struct mtk_hdmi *hdmi, const u8 *buffer) 149 + { 150 + regmap_clear_bits(hdmi->regs, TOP_INFO_EN, AUD_EN | AUD_EN_WR); 151 + regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, AUD_RPT_EN); 152 + 153 + regmap_write(hdmi->regs, TOP_AIF_HEADER, mtk_hdmi_v2_format_hw_packet(&buffer[0], 3)); 154 + regmap_write(hdmi->regs, TOP_AIF_PKT00, mtk_hdmi_v2_format_hw_packet(&buffer[3], 3)); 155 + regmap_write(hdmi->regs, TOP_AIF_PKT01, mtk_hdmi_v2_format_hw_packet(&buffer[7], 2)); 156 + regmap_write(hdmi->regs, TOP_AIF_PKT02, 0); 157 + regmap_write(hdmi->regs, TOP_AIF_PKT03, 0); 158 + 159 + regmap_set_bits(hdmi->regs, TOP_INFO_RPT, AUD_RPT_EN); 160 + regmap_set_bits(hdmi->regs, TOP_INFO_EN, AUD_EN | AUD_EN_WR); 161 + } 162 + 163 + static void mtk_hdmi_v2_hw_write_avi_infoframe(struct mtk_hdmi *hdmi, const u8 *buffer) 164 + { 165 + regmap_clear_bits(hdmi->regs, TOP_INFO_EN, AVI_EN_WR | AVI_EN); 166 + regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, AVI_RPT_EN); 167 + 168 + regmap_write(hdmi->regs, TOP_AVI_HEADER, mtk_hdmi_v2_format_hw_packet(&buffer[0], 3)); 169 + regmap_write(hdmi->regs, TOP_AVI_PKT00, mtk_hdmi_v2_format_hw_packet(&buffer[3], 4)); 170 + regmap_write(hdmi->regs, TOP_AVI_PKT01, mtk_hdmi_v2_format_hw_packet(&buffer[7], 3)); 171 + regmap_write(hdmi->regs, TOP_AVI_PKT02, mtk_hdmi_v2_format_hw_packet(&buffer[10], 4)); 172 + regmap_write(hdmi->regs, TOP_AVI_PKT03, mtk_hdmi_v2_format_hw_packet(&buffer[14], 3)); 173 + regmap_write(hdmi->regs, TOP_AVI_PKT04, 0); 174 + regmap_write(hdmi->regs, TOP_AVI_PKT05, 0); 175 + 176 + regmap_set_bits(hdmi->regs, TOP_INFO_RPT, AVI_RPT_EN); 177 + regmap_set_bits(hdmi->regs, TOP_INFO_EN, AVI_EN_WR | AVI_EN); 178 + } 179 + 180 + static void mtk_hdmi_v2_hw_write_spd_infoframe(struct mtk_hdmi *hdmi, const u8 *buffer) 181 + { 182 + regmap_clear_bits(hdmi->regs, TOP_INFO_EN, SPD_EN_WR | SPD_EN); 183 + regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, SPD_RPT_EN); 184 + 185 + regmap_write(hdmi->regs, TOP_SPDIF_HEADER, mtk_hdmi_v2_format_hw_packet(&buffer[0], 3)); 186 + regmap_write(hdmi->regs, TOP_SPDIF_PKT00, mtk_hdmi_v2_format_hw_packet(&buffer[3], 4)); 187 + regmap_write(hdmi->regs, TOP_SPDIF_PKT01, mtk_hdmi_v2_format_hw_packet(&buffer[7], 3)); 188 + regmap_write(hdmi->regs, TOP_SPDIF_PKT02, mtk_hdmi_v2_format_hw_packet(&buffer[10], 4)); 189 + regmap_write(hdmi->regs, TOP_SPDIF_PKT03, mtk_hdmi_v2_format_hw_packet(&buffer[14], 3)); 190 + regmap_write(hdmi->regs, TOP_SPDIF_PKT04, mtk_hdmi_v2_format_hw_packet(&buffer[17], 4)); 191 + regmap_write(hdmi->regs, TOP_SPDIF_PKT05, mtk_hdmi_v2_format_hw_packet(&buffer[21], 3)); 192 + regmap_write(hdmi->regs, TOP_SPDIF_PKT06, mtk_hdmi_v2_format_hw_packet(&buffer[24], 4)); 193 + regmap_write(hdmi->regs, TOP_SPDIF_PKT07, buffer[28]); 194 + 195 + regmap_set_bits(hdmi->regs, TOP_INFO_EN, SPD_EN_WR | SPD_EN); 196 + regmap_set_bits(hdmi->regs, TOP_INFO_RPT, SPD_RPT_EN); 197 + } 198 + 199 + static void mtk_hdmi_v2_hw_write_vendor_infoframe(struct mtk_hdmi *hdmi, const u8 *buffer) 200 + { 201 + regmap_clear_bits(hdmi->regs, TOP_INFO_EN, VSIF_EN_WR | VSIF_EN); 202 + regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, VSIF_RPT_EN); 203 + 204 + regmap_write(hdmi->regs, TOP_VSIF_HEADER, mtk_hdmi_v2_format_hw_packet(&buffer[0], 3)); 205 + regmap_write(hdmi->regs, TOP_VSIF_PKT00, mtk_hdmi_v2_format_hw_packet(&buffer[3], 4)); 206 + regmap_write(hdmi->regs, TOP_VSIF_PKT01, mtk_hdmi_v2_format_hw_packet(&buffer[7], 2)); 207 + regmap_write(hdmi->regs, TOP_VSIF_PKT02, 0); 208 + regmap_write(hdmi->regs, TOP_VSIF_PKT03, 0); 209 + regmap_write(hdmi->regs, TOP_VSIF_PKT04, 0); 210 + regmap_write(hdmi->regs, TOP_VSIF_PKT05, 0); 211 + regmap_write(hdmi->regs, TOP_VSIF_PKT06, 0); 212 + regmap_write(hdmi->regs, TOP_VSIF_PKT07, 0); 213 + 214 + regmap_set_bits(hdmi->regs, TOP_INFO_EN, VSIF_EN_WR | VSIF_EN); 215 + regmap_set_bits(hdmi->regs, TOP_INFO_RPT, VSIF_RPT_EN); 216 + } 217 + 218 + static void mtk_hdmi_yuv420_downsampling(struct mtk_hdmi *hdmi, bool enable) 219 + { 220 + u32 val; 221 + 222 + regmap_read(hdmi->regs, VID_DOWNSAMPLE_CONFIG, &val); 223 + 224 + if (enable) { 225 + regmap_set_bits(hdmi->regs, hdmi->conf->reg_hdmi_tx_cfg, HDMI_YUV420_MODE); 226 + 227 + val |= C444_C422_CONFIG_ENABLE | C422_C420_CONFIG_ENABLE; 228 + val |= C422_C420_CONFIG_OUT_CB_OR_CR; 229 + val &= ~C422_C420_CONFIG_BYPASS; 230 + regmap_write(hdmi->regs, VID_DOWNSAMPLE_CONFIG, val); 231 + 232 + regmap_set_bits(hdmi->regs, VID_OUT_FORMAT, OUTPUT_FORMAT_DEMUX_420_ENABLE); 233 + } else { 234 + regmap_clear_bits(hdmi->regs, hdmi->conf->reg_hdmi_tx_cfg, HDMI_YUV420_MODE); 235 + 236 + val &= ~(C444_C422_CONFIG_ENABLE | C422_C420_CONFIG_ENABLE); 237 + val &= ~C422_C420_CONFIG_OUT_CB_OR_CR; 238 + val |= C422_C420_CONFIG_BYPASS; 239 + regmap_write(hdmi->regs, VID_DOWNSAMPLE_CONFIG, val); 240 + 241 + regmap_clear_bits(hdmi->regs, VID_OUT_FORMAT, OUTPUT_FORMAT_DEMUX_420_ENABLE); 242 + } 243 + } 244 + 245 + static int mtk_hdmi_v2_setup_audio_infoframe(struct mtk_hdmi *hdmi) 246 + { 247 + struct hdmi_codec_params *params = &hdmi->aud_param.codec_params; 248 + struct hdmi_audio_infoframe frame; 249 + u8 buffer[14]; 250 + ssize_t ret; 251 + 252 + memcpy(&frame, &params->cea, sizeof(frame)); 253 + 254 + ret = hdmi_audio_infoframe_pack(&frame, buffer, sizeof(buffer)); 255 + if (ret < 0) 256 + return ret; 257 + 258 + mtk_hdmi_v2_hw_write_audio_infoframe(hdmi, buffer); 259 + 260 + return 0; 261 + } 262 + 263 + static inline void mtk_hdmi_v2_hw_gcp_avmute(struct mtk_hdmi *hdmi, bool mute) 264 + { 265 + u32 val; 266 + 267 + regmap_read(hdmi->regs, TOP_CFG01, &val); 268 + val &= ~(CP_CLR_MUTE_EN | CP_SET_MUTE_EN); 269 + 270 + if (mute) { 271 + val |= CP_SET_MUTE_EN; 272 + val &= ~CP_CLR_MUTE_EN; 273 + } else { 274 + val |= CP_CLR_MUTE_EN; 275 + val &= ~CP_SET_MUTE_EN; 276 + } 277 + regmap_write(hdmi->regs, TOP_CFG01, val); 278 + 279 + regmap_set_bits(hdmi->regs, TOP_INFO_RPT, CP_RPT_EN); 280 + regmap_set_bits(hdmi->regs, TOP_INFO_EN, CP_EN | CP_EN_WR); 281 + } 282 + 283 + static void mtk_hdmi_v2_hw_ncts_enable(struct mtk_hdmi *hdmi, bool enable) 284 + { 285 + if (enable) 286 + regmap_set_bits(hdmi->regs, AIP_CTRL, CTS_SW_SEL); 287 + else 288 + regmap_clear_bits(hdmi->regs, AIP_CTRL, CTS_SW_SEL); 289 + } 290 + 291 + static void mtk_hdmi_v2_hw_aud_set_channel_status(struct mtk_hdmi *hdmi) 292 + { 293 + u8 *ch_status = hdmi->aud_param.codec_params.iec.status; 294 + 295 + /* Only the first 5 to 7 bytes of Channel Status contain useful information */ 296 + regmap_write(hdmi->regs, AIP_I2S_CHST0, mtk_hdmi_v2_format_hw_packet(&ch_status[0], 4)); 297 + regmap_write(hdmi->regs, AIP_I2S_CHST1, mtk_hdmi_v2_format_hw_packet(&ch_status[4], 3)); 298 + } 299 + 300 + static void mtk_hdmi_v2_hw_aud_set_ncts(struct mtk_hdmi *hdmi, 301 + unsigned int sample_rate, 302 + unsigned int clock) 303 + { 304 + unsigned int n, cts; 305 + 306 + mtk_hdmi_get_ncts(sample_rate, clock, &n, &cts); 307 + 308 + regmap_write(hdmi->regs, AIP_N_VAL, n); 309 + regmap_write(hdmi->regs, AIP_CTS_SVAL, cts); 310 + } 311 + 312 + static void mtk_hdmi_v2_hw_aud_enable(struct mtk_hdmi *hdmi, bool enable) 313 + { 314 + if (enable) 315 + regmap_clear_bits(hdmi->regs, AIP_TXCTRL, AUD_PACKET_DROP); 316 + else 317 + regmap_set_bits(hdmi->regs, AIP_TXCTRL, AUD_PACKET_DROP); 318 + } 319 + 320 + static u32 mtk_hdmi_v2_aud_output_channel_map(u8 sd0, u8 sd1, u8 sd2, u8 sd3, 321 + u8 sd4, u8 sd5, u8 sd6, u8 sd7) 322 + { 323 + u32 val; 324 + 325 + /* 326 + * Each of the Output Channels (0-7) can be mapped to get their input 327 + * from any of the available Input Channels (0-7): this function 328 + * takes input channel numbers and formats a value that must then 329 + * be written to the TOP_AUD_MAP hardware register by the caller. 330 + */ 331 + val = FIELD_PREP(SD0_MAP, sd0) | FIELD_PREP(SD1_MAP, sd1); 332 + val |= FIELD_PREP(SD2_MAP, sd2) | FIELD_PREP(SD3_MAP, sd3); 333 + val |= FIELD_PREP(SD4_MAP, sd4) | FIELD_PREP(SD5_MAP, sd5); 334 + val |= FIELD_PREP(SD6_MAP, sd6) | FIELD_PREP(SD7_MAP, sd7); 335 + 336 + return val; 337 + } 338 + 339 + static void mtk_hdmi_audio_dsd_config(struct mtk_hdmi *hdmi, 340 + unsigned char chnum, bool dsd_bypass) 341 + { 342 + u32 channel_map; 343 + 344 + regmap_update_bits(hdmi->regs, AIP_CTRL, SPDIF_EN | DSD_EN | HBRA_ON, DSD_EN); 345 + regmap_set_bits(hdmi->regs, AIP_TXCTRL, DSD_MUTE_EN); 346 + 347 + if (dsd_bypass) 348 + channel_map = mtk_hdmi_v2_aud_output_channel_map(0, 2, 4, 6, 1, 3, 5, 7); 349 + else 350 + channel_map = mtk_hdmi_v2_aud_output_channel_map(0, 5, 1, 0, 3, 2, 4, 0); 351 + 352 + regmap_write(hdmi->regs, TOP_AUD_MAP, channel_map); 353 + regmap_clear_bits(hdmi->regs, AIP_SPDIF_CTRL, I2S2DSD_EN); 354 + } 355 + 356 + static inline void mtk_hdmi_v2_hw_i2s_fifo_map(struct mtk_hdmi *hdmi, u32 fifo_mapping) 357 + { 358 + regmap_update_bits(hdmi->regs, AIP_I2S_CTRL, 359 + FIFO0_MAP | FIFO1_MAP | FIFO2_MAP | FIFO3_MAP, fifo_mapping); 360 + } 361 + 362 + static inline void mtk_hdmi_v2_hw_i2s_ch_number(struct mtk_hdmi *hdmi, u8 chnum) 363 + { 364 + regmap_update_bits(hdmi->regs, AIP_CTRL, I2S_EN, FIELD_PREP(I2S_EN, chnum)); 365 + } 366 + 367 + static void mtk_hdmi_v2_hw_i2s_ch_mapping(struct mtk_hdmi *hdmi, u8 chnum, u8 mapping) 368 + { 369 + u32 fifo_map; 370 + u8 bdata; 371 + 372 + switch (chnum) { 373 + default: 374 + case 2: 375 + bdata = 0x1; 376 + break; 377 + case 3: 378 + bdata = 0x3; 379 + break; 380 + case 6: 381 + if (mapping == 0x0e) { 382 + bdata = 0xf; 383 + break; 384 + } 385 + fallthrough; 386 + case 5: 387 + bdata = 0x7; 388 + break; 389 + case 7: 390 + case 8: 391 + bdata = 0xf; 392 + break; 393 + } 394 + 395 + /* Assign default FIFO mapping: SD0 to FIFO0, SD1 to FIFO1, etc. */ 396 + fifo_map = FIELD_PREP(FIFO0_MAP, 0) | FIELD_PREP(FIFO1_MAP, 1); 397 + fifo_map |= FIELD_PREP(FIFO2_MAP, 2) | FIELD_PREP(FIFO3_MAP, 3); 398 + mtk_hdmi_v2_hw_i2s_fifo_map(hdmi, fifo_map); 399 + mtk_hdmi_v2_hw_i2s_ch_number(hdmi, bdata); 400 + 401 + /* 402 + * Set HDMI Audio packet layout indicator: 403 + * Layout 0 is for two channels 404 + * Layout 1 is for up to eight channels 405 + */ 406 + if (chnum == 2) 407 + regmap_set_bits(hdmi->regs, AIP_TXCTRL, AUD_LAYOUT_1); 408 + else 409 + regmap_clear_bits(hdmi->regs, AIP_TXCTRL, AUD_LAYOUT_1); 410 + } 411 + 412 + static void mtk_hdmi_i2s_data_fmt(struct mtk_hdmi *hdmi, unsigned char fmt) 413 + { 414 + u32 val; 415 + 416 + regmap_read(hdmi->regs, AIP_I2S_CTRL, &val); 417 + val &= ~(WS_HIGH | I2S_1ST_BIT_NOSHIFT | JUSTIFY_RIGHT); 418 + 419 + switch (fmt) { 420 + case HDMI_I2S_MODE_RJT_24BIT: 421 + case HDMI_I2S_MODE_RJT_16BIT: 422 + val |= (WS_HIGH | I2S_1ST_BIT_NOSHIFT | JUSTIFY_RIGHT); 423 + break; 424 + case HDMI_I2S_MODE_LJT_24BIT: 425 + case HDMI_I2S_MODE_LJT_16BIT: 426 + val |= (WS_HIGH | I2S_1ST_BIT_NOSHIFT); 427 + break; 428 + case HDMI_I2S_MODE_I2S_24BIT: 429 + case HDMI_I2S_MODE_I2S_16BIT: 430 + default: 431 + break; 432 + } 433 + 434 + regmap_write(hdmi->regs, AIP_I2S_CTRL, val); 435 + } 436 + 437 + static inline void mtk_hdmi_i2s_sck_edge_rise(struct mtk_hdmi *hdmi, bool rise) 438 + { 439 + if (rise) 440 + regmap_set_bits(hdmi->regs, AIP_I2S_CTRL, SCK_EDGE_RISE); 441 + else 442 + regmap_clear_bits(hdmi->regs, AIP_I2S_CTRL, SCK_EDGE_RISE); 443 + } 444 + 445 + static inline void mtk_hdmi_i2s_cbit_order(struct mtk_hdmi *hdmi, unsigned int cbit) 446 + { 447 + regmap_update_bits(hdmi->regs, AIP_I2S_CTRL, CBIT_ORDER_SAME, cbit); 448 + } 449 + 450 + static inline void mtk_hdmi_i2s_vbit(struct mtk_hdmi *hdmi, unsigned int vbit) 451 + { 452 + /* V bit: 0 for PCM, 1 for Compressed data */ 453 + regmap_update_bits(hdmi->regs, AIP_I2S_CTRL, VBIT_COMPRESSED, vbit); 454 + } 455 + 456 + static inline void mtk_hdmi_i2s_data_direction(struct mtk_hdmi *hdmi, unsigned int is_lsb) 457 + { 458 + regmap_update_bits(hdmi->regs, AIP_I2S_CTRL, I2S_DATA_DIR_LSB, is_lsb); 459 + } 460 + 461 + static inline void mtk_hdmi_v2_hw_audio_type(struct mtk_hdmi *hdmi, unsigned int spdif_i2s) 462 + { 463 + regmap_update_bits(hdmi->regs, AIP_CTRL, SPDIF_EN, FIELD_PREP(SPDIF_EN, spdif_i2s)); 464 + } 465 + 466 + static u8 mtk_hdmi_v2_get_i2s_ch_mapping(struct mtk_hdmi *hdmi, u8 channel_type) 467 + { 468 + switch (channel_type) { 469 + case HDMI_AUD_CHAN_TYPE_1_1: 470 + case HDMI_AUD_CHAN_TYPE_2_1: 471 + return 0x01; 472 + case HDMI_AUD_CHAN_TYPE_3_0: 473 + return 0x02; 474 + case HDMI_AUD_CHAN_TYPE_3_1: 475 + return 0x03; 476 + case HDMI_AUD_CHAN_TYPE_3_0_LRS: 477 + case HDMI_AUD_CHAN_TYPE_4_0: 478 + return 0x08; 479 + case HDMI_AUD_CHAN_TYPE_5_1: 480 + return 0x0b; 481 + case HDMI_AUD_CHAN_TYPE_4_1_CLRS: 482 + case HDMI_AUD_CHAN_TYPE_6_0: 483 + case HDMI_AUD_CHAN_TYPE_6_0_CS: 484 + case HDMI_AUD_CHAN_TYPE_6_0_CH: 485 + case HDMI_AUD_CHAN_TYPE_6_0_OH: 486 + case HDMI_AUD_CHAN_TYPE_6_0_CHR: 487 + return 0x0e; 488 + case HDMI_AUD_CHAN_TYPE_1_0: 489 + case HDMI_AUD_CHAN_TYPE_2_0: 490 + case HDMI_AUD_CHAN_TYPE_3_1_LRS: 491 + case HDMI_AUD_CHAN_TYPE_4_1: 492 + case HDMI_AUD_CHAN_TYPE_5_0: 493 + case HDMI_AUD_CHAN_TYPE_4_0_CLRS: 494 + case HDMI_AUD_CHAN_TYPE_6_1: 495 + case HDMI_AUD_CHAN_TYPE_6_1_CS: 496 + case HDMI_AUD_CHAN_TYPE_6_1_CH: 497 + case HDMI_AUD_CHAN_TYPE_6_1_OH: 498 + case HDMI_AUD_CHAN_TYPE_6_1_CHR: 499 + case HDMI_AUD_CHAN_TYPE_7_0: 500 + case HDMI_AUD_CHAN_TYPE_7_0_LH_RH: 501 + case HDMI_AUD_CHAN_TYPE_7_0_LSR_RSR: 502 + case HDMI_AUD_CHAN_TYPE_7_0_LC_RC: 503 + case HDMI_AUD_CHAN_TYPE_7_0_LW_RW: 504 + case HDMI_AUD_CHAN_TYPE_7_0_LSD_RSD: 505 + case HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS: 506 + case HDMI_AUD_CHAN_TYPE_7_0_LHS_RHS: 507 + case HDMI_AUD_CHAN_TYPE_7_0_CS_CH: 508 + case HDMI_AUD_CHAN_TYPE_7_0_CS_OH: 509 + case HDMI_AUD_CHAN_TYPE_7_0_CS_CHR: 510 + case HDMI_AUD_CHAN_TYPE_7_0_CH_OH: 511 + case HDMI_AUD_CHAN_TYPE_7_0_CH_CHR: 512 + case HDMI_AUD_CHAN_TYPE_7_0_OH_CHR: 513 + case HDMI_AUD_CHAN_TYPE_7_0_LSS_RSS_LSR_RSR: 514 + case HDMI_AUD_CHAN_TYPE_8_0_LH_RH_CS: 515 + case HDMI_AUD_CHAN_TYPE_7_1: 516 + case HDMI_AUD_CHAN_TYPE_7_1_LH_RH: 517 + case HDMI_AUD_CHAN_TYPE_7_1_LSR_RSR: 518 + case HDMI_AUD_CHAN_TYPE_7_1_LC_RC: 519 + case HDMI_AUD_CHAN_TYPE_7_1_LW_RW: 520 + case HDMI_AUD_CHAN_TYPE_7_1_LSD_RSD: 521 + case HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS: 522 + case HDMI_AUD_CHAN_TYPE_7_1_LHS_RHS: 523 + case HDMI_AUD_CHAN_TYPE_7_1_CS_CH: 524 + case HDMI_AUD_CHAN_TYPE_7_1_CS_OH: 525 + case HDMI_AUD_CHAN_TYPE_7_1_CS_CHR: 526 + case HDMI_AUD_CHAN_TYPE_7_1_CH_OH: 527 + case HDMI_AUD_CHAN_TYPE_7_1_CH_CHR: 528 + case HDMI_AUD_CHAN_TYPE_7_1_OH_CHR: 529 + case HDMI_AUD_CHAN_TYPE_7_1_LSS_RSS_LSR_RSR: 530 + default: 531 + return 0; 532 + } 533 + 534 + return 0; 535 + } 536 + 537 + static inline void mtk_hdmi_v2_hw_i2s_ch_swap(struct mtk_hdmi *hdmi) 538 + { 539 + regmap_update_bits(hdmi->regs, AIP_SPDIF_CTRL, MAX_2UI_I2S_HI_WRITE, 540 + FIELD_PREP(MAX_2UI_I2S_HI_WRITE, MAX_2UI_I2S_LFE_CC_SWAP)); 541 + } 542 + 543 + static void mtk_hdmi_hbr_config(struct mtk_hdmi *hdmi, bool dsd_bypass) 544 + { 545 + const u32 hbr_mask = SPDIF_EN | DSD_EN | HBRA_ON; 546 + 547 + if (dsd_bypass) { 548 + regmap_update_bits(hdmi->regs, AIP_CTRL, hbr_mask, HBRA_ON); 549 + regmap_set_bits(hdmi->regs, AIP_CTRL, I2S_EN); 550 + } else { 551 + regmap_update_bits(hdmi->regs, AIP_CTRL, hbr_mask, SPDIF_EN); 552 + regmap_set_bits(hdmi->regs, AIP_CTRL, SPDIF_INTERNAL_MODULE); 553 + regmap_set_bits(hdmi->regs, AIP_CTRL, HBR_FROM_SPDIF); 554 + regmap_set_bits(hdmi->regs, AIP_CTRL, CTS_CAL_N4); 555 + } 556 + } 557 + 558 + static inline void mtk_hdmi_v2_hw_spdif_config(struct mtk_hdmi *hdmi) 559 + { 560 + regmap_clear_bits(hdmi->regs, AIP_SPDIF_CTRL, WR_1UI_LOCK); 561 + regmap_clear_bits(hdmi->regs, AIP_SPDIF_CTRL, FS_OVERRIDE_WRITE); 562 + regmap_clear_bits(hdmi->regs, AIP_SPDIF_CTRL, WR_2UI_LOCK); 563 + 564 + regmap_update_bits(hdmi->regs, AIP_SPDIF_CTRL, MAX_1UI_WRITE, 565 + FIELD_PREP(MAX_1UI_WRITE, 4)); 566 + regmap_update_bits(hdmi->regs, AIP_SPDIF_CTRL, MAX_2UI_SPDIF_WRITE, 567 + FIELD_PREP(MAX_2UI_SPDIF_WRITE, 9)); 568 + regmap_update_bits(hdmi->regs, AIP_SPDIF_CTRL, AUD_ERR_THRESH, 569 + FIELD_PREP(AUD_ERR_THRESH, 4)); 570 + 571 + regmap_set_bits(hdmi->regs, AIP_SPDIF_CTRL, I2S2DSD_EN); 572 + } 573 + 574 + static void mtk_hdmi_v2_aud_set_input(struct mtk_hdmi *hdmi) 575 + { 576 + struct hdmi_audio_param *aud_param = &hdmi->aud_param; 577 + struct hdmi_codec_params *codec_params = &aud_param->codec_params; 578 + u8 i2s_ch_map; 579 + u32 out_ch_map; 580 + 581 + /* Write the default output channel map. CH0 maps to SD0, CH1 maps to SD1, etc */ 582 + out_ch_map = mtk_hdmi_v2_aud_output_channel_map(0, 1, 2, 3, 4, 5, 6, 7); 583 + regmap_write(hdmi->regs, TOP_AUD_MAP, out_ch_map); 584 + 585 + regmap_update_bits(hdmi->regs, AIP_SPDIF_CTRL, MAX_2UI_I2S_HI_WRITE, 0); 586 + regmap_clear_bits(hdmi->regs, AIP_CTRL, 587 + SPDIF_EN | DSD_EN | HBRA_ON | CTS_CAL_N4 | 588 + HBR_FROM_SPDIF | SPDIF_INTERNAL_MODULE); 589 + regmap_clear_bits(hdmi->regs, AIP_TXCTRL, DSD_MUTE_EN | AUD_LAYOUT_1); 590 + 591 + if (aud_param->aud_input_type == HDMI_AUD_INPUT_I2S) { 592 + switch (aud_param->aud_codec) { 593 + case HDMI_AUDIO_CODING_TYPE_DTS_HD: 594 + case HDMI_AUDIO_CODING_TYPE_MLP: 595 + mtk_hdmi_i2s_data_fmt(hdmi, aud_param->aud_i2s_fmt); 596 + mtk_hdmi_hbr_config(hdmi, true); 597 + break; 598 + case HDMI_AUDIO_CODING_TYPE_DSD: 599 + mtk_hdmi_audio_dsd_config(hdmi, codec_params->channels, 0); 600 + mtk_hdmi_v2_hw_i2s_ch_mapping(hdmi, codec_params->channels, 1); 601 + break; 602 + default: 603 + mtk_hdmi_i2s_data_fmt(hdmi, aud_param->aud_i2s_fmt); 604 + mtk_hdmi_i2s_sck_edge_rise(hdmi, true); 605 + mtk_hdmi_i2s_cbit_order(hdmi, CBIT_ORDER_SAME); 606 + mtk_hdmi_i2s_vbit(hdmi, 0); /* PCM data */ 607 + mtk_hdmi_i2s_data_direction(hdmi, 0); /* MSB first */ 608 + mtk_hdmi_v2_hw_audio_type(hdmi, HDMI_AUD_INPUT_I2S); 609 + i2s_ch_map = mtk_hdmi_v2_get_i2s_ch_mapping(hdmi, 610 + aud_param->aud_input_chan_type); 611 + mtk_hdmi_v2_hw_i2s_ch_mapping(hdmi, codec_params->channels, i2s_ch_map); 612 + mtk_hdmi_v2_hw_i2s_ch_swap(hdmi); 613 + } 614 + } else { 615 + if (codec_params->sample_rate == 768000 && 616 + (aud_param->aud_codec == HDMI_AUDIO_CODING_TYPE_DTS_HD || 617 + aud_param->aud_codec == HDMI_AUDIO_CODING_TYPE_MLP)) { 618 + mtk_hdmi_hbr_config(hdmi, false); 619 + } else { 620 + mtk_hdmi_v2_hw_spdif_config(hdmi); 621 + mtk_hdmi_v2_hw_i2s_ch_mapping(hdmi, 2, 0); 622 + } 623 + } 624 + } 625 + 626 + static inline void mtk_hdmi_v2_hw_audio_input_enable(struct mtk_hdmi *hdmi, bool ena) 627 + { 628 + if (ena) 629 + regmap_set_bits(hdmi->regs, AIP_CTRL, AUD_IN_EN); 630 + else 631 + regmap_clear_bits(hdmi->regs, AIP_CTRL, AUD_IN_EN); 632 + } 633 + 634 + static void mtk_hdmi_v2_aip_ctrl_init(struct mtk_hdmi *hdmi) 635 + { 636 + regmap_set_bits(hdmi->regs, AIP_CTRL, 637 + AUD_SEL_OWRT | NO_MCLK_CTSGEN_SEL | MCLK_EN | CTS_REQ_EN); 638 + regmap_clear_bits(hdmi->regs, AIP_TPI_CTRL, TPI_AUDIO_LOOKUP_EN); 639 + } 640 + 641 + static void mtk_hdmi_v2_audio_reset(struct mtk_hdmi *hdmi, bool reset) 642 + { 643 + const u32 arst_bits = RST4AUDIO | RST4AUDIO_FIFO | RST4AUDIO_ACR; 644 + 645 + if (reset) 646 + regmap_set_bits(hdmi->regs, AIP_TXCTRL, arst_bits); 647 + else 648 + regmap_clear_bits(hdmi->regs, AIP_TXCTRL, arst_bits); 649 + } 650 + 651 + static void mtk_hdmi_v2_aud_output_config(struct mtk_hdmi *hdmi, 652 + struct drm_display_mode *display_mode) 653 + { 654 + /* Shut down and reset the HDMI Audio HW to avoid glitching */ 655 + mtk_hdmi_v2_hw_aud_mute(hdmi, true); 656 + mtk_hdmi_v2_hw_aud_enable(hdmi, false); 657 + mtk_hdmi_v2_audio_reset(hdmi, true); 658 + 659 + /* Configure the main hardware params and get out of reset */ 660 + mtk_hdmi_v2_aip_ctrl_init(hdmi); 661 + mtk_hdmi_v2_aud_set_input(hdmi); 662 + mtk_hdmi_v2_hw_aud_set_channel_status(hdmi); 663 + mtk_hdmi_v2_setup_audio_infoframe(hdmi); 664 + mtk_hdmi_v2_hw_audio_input_enable(hdmi, true); 665 + mtk_hdmi_v2_audio_reset(hdmi, false); 666 + 667 + /* Ignore N/CTS packet transmission requests and configure */ 668 + mtk_hdmi_v2_hw_ncts_enable(hdmi, false); 669 + mtk_hdmi_v2_hw_aud_set_ncts(hdmi, hdmi->aud_param.codec_params.sample_rate, 670 + display_mode->clock); 671 + 672 + /* Wait for the HW to apply settings */ 673 + usleep_range(25, 50); 674 + 675 + /* Hardware is fully configured: enable TX of N/CTS pkts and unmute */ 676 + mtk_hdmi_v2_hw_ncts_enable(hdmi, true); 677 + mtk_hdmi_v2_hw_aud_enable(hdmi, true); 678 + mtk_hdmi_v2_hw_aud_mute(hdmi, false); 679 + } 680 + 681 + static void mtk_hdmi_v2_change_video_resolution(struct mtk_hdmi *hdmi, 682 + struct drm_connector_state *conn_state) 683 + { 684 + mtk_hdmi_v2_hw_reset(hdmi); 685 + mtk_hdmi_v2_set_sw_hpd(hdmi, true); 686 + udelay(2); 687 + 688 + regmap_write(hdmi->regs, HDCP_TOP_CTRL, 0); 689 + 690 + /* 691 + * Enable HDCP reauthentication interrupt: the HW uses this internally 692 + * for the HPD state machine even if HDCP encryption is not enabled. 693 + */ 694 + regmap_set_bits(hdmi->regs, TOP_INT_ENABLE00, HDCP2X_RX_REAUTH_REQ_DDCM_INT); 695 + 696 + /* Enable hotplug and pord interrupts */ 697 + mtk_hdmi_v2_enable_hpd_pord_irq(hdmi, true); 698 + 699 + /* Force enabling HDCP HPD */ 700 + regmap_set_bits(hdmi->regs, HDCP2X_CTRL_0, HDCP2X_HPD_OVR); 701 + regmap_set_bits(hdmi->regs, HDCP2X_CTRL_0, HDCP2X_HPD_SW); 702 + 703 + /* Set 8 bits per pixel */ 704 + regmap_update_bits(hdmi->regs, TOP_CFG00, TMDS_PACK_MODE, 705 + FIELD_PREP(TMDS_PACK_MODE, TMDS_PACK_MODE_8BPP)); 706 + /* Disable generating deepcolor packets */ 707 + regmap_clear_bits(hdmi->regs, TOP_CFG00, DEEPCOLOR_PKT_EN); 708 + /* Disable adding deepcolor information to the general packet */ 709 + regmap_clear_bits(hdmi->regs, TOP_MISC_CTLR, DEEP_COLOR_ADD); 710 + 711 + if (hdmi->curr_conn->display_info.is_hdmi) 712 + regmap_set_bits(hdmi->regs, TOP_CFG00, HDMI_MODE_HDMI); 713 + else 714 + regmap_clear_bits(hdmi->regs, TOP_CFG00, HDMI_MODE_HDMI); 715 + 716 + udelay(5); 717 + mtk_hdmi_v2_hw_vid_mute(hdmi, true); 718 + mtk_hdmi_v2_hw_aud_mute(hdmi, true); 719 + mtk_hdmi_v2_hw_gcp_avmute(hdmi, false); 720 + 721 + regmap_update_bits(hdmi->regs, TOP_CFG01, 722 + NULL_PKT_VSYNC_HIGH_EN | NULL_PKT_EN, NULL_PKT_VSYNC_HIGH_EN); 723 + usleep_range(100, 150); 724 + 725 + /* Enable scrambling if tmds clock is 340MHz or more */ 726 + mtk_hdmi_v2_enable_scrambling(hdmi, hdmi->mode.clock >= 340 * KILO); 727 + 728 + switch (conn_state->hdmi.output_format) { 729 + default: 730 + case HDMI_COLORSPACE_RGB: 731 + case HDMI_COLORSPACE_YUV444: 732 + /* Disable YUV420 downsampling for RGB and YUV444 */ 733 + mtk_hdmi_yuv420_downsampling(hdmi, false); 734 + break; 735 + case HDMI_COLORSPACE_YUV422: 736 + /* 737 + * YUV420 downsampling is special and needs a bit of setup 738 + * so we disable everything there before doing anything else. 739 + * 740 + * YUV422 downsampling instead just needs one bit to be set. 741 + */ 742 + mtk_hdmi_yuv420_downsampling(hdmi, false); 743 + regmap_set_bits(hdmi->regs, VID_DOWNSAMPLE_CONFIG, 744 + C444_C422_CONFIG_ENABLE); 745 + break; 746 + case HDMI_COLORSPACE_YUV420: 747 + mtk_hdmi_yuv420_downsampling(hdmi, true); 748 + break; 749 + }; 750 + } 751 + 752 + static void mtk_hdmi_v2_output_set_display_mode(struct mtk_hdmi *hdmi, 753 + struct drm_connector_state *conn_state, 754 + struct drm_display_mode *mode) 755 + { 756 + union phy_configure_opts opts = { 757 + .dp = { .link_rate = hdmi->mode.clock * KILO } 758 + }; 759 + int ret; 760 + 761 + ret = phy_configure(hdmi->phy, &opts); 762 + if (ret) 763 + dev_err(hdmi->dev, "Setting clock=%d failed: %d", mode->clock, ret); 764 + 765 + mtk_hdmi_v2_change_video_resolution(hdmi, conn_state); 766 + mtk_hdmi_v2_aud_output_config(hdmi, mode); 767 + } 768 + 769 + static int mtk_hdmi_v2_clk_enable(struct mtk_hdmi *hdmi) 770 + { 771 + int ret; 772 + 773 + ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_V2_CLK_HDCP_SEL]); 774 + if (ret) 775 + return ret; 776 + 777 + ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_V2_CLK_HDCP_24M_SEL]); 778 + if (ret) 779 + goto disable_hdcp_clk; 780 + 781 + ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_V2_CLK_HDMI_APB_SEL]); 782 + if (ret) 783 + goto disable_hdcp_24m_clk; 784 + 785 + ret = clk_prepare_enable(hdmi->clk[MTK_HDMI_V2_CLK_VPP_SPLIT_HDMI]); 786 + if (ret) 787 + goto disable_bus_clk; 788 + 789 + return 0; 790 + 791 + disable_bus_clk: 792 + clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_HDMI_APB_SEL]); 793 + disable_hdcp_24m_clk: 794 + clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_HDCP_24M_SEL]); 795 + disable_hdcp_clk: 796 + clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_HDCP_SEL]); 797 + 798 + return ret; 799 + } 800 + 801 + static void mtk_hdmi_v2_clk_disable(struct mtk_hdmi *hdmi) 802 + { 803 + clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_VPP_SPLIT_HDMI]); 804 + clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_HDMI_APB_SEL]); 805 + clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_HDCP_24M_SEL]); 806 + clk_disable_unprepare(hdmi->clk[MTK_HDMI_V2_CLK_HDCP_SEL]); 807 + } 808 + 809 + static enum hdmi_hpd_state mtk_hdmi_v2_hpd_pord_status(struct mtk_hdmi *hdmi) 810 + { 811 + u8 hpd_pin_sta, pord_pin_sta; 812 + u32 hpd_status; 813 + 814 + regmap_read(hdmi->regs, HPD_DDC_STATUS, &hpd_status); 815 + hpd_pin_sta = FIELD_GET(HPD_PIN_STA, hpd_status); 816 + pord_pin_sta = FIELD_GET(PORD_PIN_STA, hpd_status); 817 + 818 + /* 819 + * Inform that the cable is plugged in (hpd_pin_sta) so that the 820 + * sink can be powered on by switching the 5V VBUS as required by 821 + * the HDMI spec for reading EDID and for HDMI Audio registers to 822 + * be accessible. 823 + * 824 + * PORD detection succeeds only when the cable is plugged in and 825 + * the sink is powered on: reaching that state means that the 826 + * communication with the sink can be started. 827 + * 828 + * Please note that when the cable is plugged out the HPD pin will 829 + * be the first one to fall, while PORD may still be in rise state 830 + * for a few more milliseconds, so we decide HDMI_PLUG_OUT without 831 + * checking PORD at all (we check only HPD falling for that). 832 + */ 833 + if (hpd_pin_sta && pord_pin_sta) 834 + return HDMI_PLUG_IN_AND_SINK_POWER_ON; 835 + else if (hpd_pin_sta) 836 + return HDMI_PLUG_IN_ONLY; 837 + else 838 + return HDMI_PLUG_OUT; 839 + } 840 + 841 + static irqreturn_t mtk_hdmi_v2_isr(int irq, void *arg) 842 + { 843 + struct mtk_hdmi *hdmi = arg; 844 + unsigned int irq_sta; 845 + int ret = IRQ_HANDLED; 846 + 847 + regmap_read(hdmi->regs, TOP_INT_STA00, &irq_sta); 848 + 849 + /* Handle Hotplug Detection interrupts */ 850 + if (irq_sta & HPD_PORD_HWIRQS) { 851 + /* 852 + * Disable the HPD/PORD IRQs now and until thread done to 853 + * avoid interrupt storm that could happen with bad cables 854 + */ 855 + mtk_hdmi_v2_enable_hpd_pord_irq(hdmi, false); 856 + ret = IRQ_WAKE_THREAD; 857 + 858 + /* Clear HPD/PORD irqs to avoid unwanted retriggering */ 859 + regmap_write(hdmi->regs, TOP_INT_CLR00, HPD_PORD_HWIRQS); 860 + regmap_write(hdmi->regs, TOP_INT_CLR00, 0); 861 + } 862 + 863 + return ret; 864 + } 865 + 866 + static irqreturn_t __mtk_hdmi_v2_isr_thread(struct mtk_hdmi *hdmi) 867 + { 868 + enum hdmi_hpd_state hpd; 869 + 870 + hpd = mtk_hdmi_v2_hpd_pord_status(hdmi); 871 + if (hpd != hdmi->hpd) { 872 + struct drm_encoder *encoder = hdmi->bridge.encoder; 873 + 874 + hdmi->hpd = hpd; 875 + 876 + if (encoder && encoder->dev) 877 + drm_helper_hpd_irq_event(hdmi->bridge.encoder->dev); 878 + } 879 + 880 + mtk_hdmi_v2_enable_hpd_pord_irq(hdmi, true); 881 + return IRQ_HANDLED; 882 + } 883 + 884 + static irqreturn_t mtk_hdmi_v2_isr_thread(int irq, void *arg) 885 + { 886 + struct mtk_hdmi *hdmi = arg; 887 + 888 + /* 889 + * Debounce HDMI monitor HPD status. 890 + * Empirical testing shows that 30ms is enough wait 891 + */ 892 + msleep(30); 893 + 894 + return __mtk_hdmi_v2_isr_thread(hdmi); 895 + } 896 + 897 + static int mtk_hdmi_v2_enable(struct mtk_hdmi *hdmi) 898 + { 899 + bool was_active = pm_runtime_active(hdmi->dev); 900 + int ret; 901 + 902 + ret = pm_runtime_resume_and_get(hdmi->dev); 903 + if (ret) { 904 + dev_err(hdmi->dev, "Cannot resume HDMI\n"); 905 + return ret; 906 + } 907 + 908 + ret = mtk_hdmi_v2_clk_enable(hdmi); 909 + if (ret) { 910 + pm_runtime_put(hdmi->dev); 911 + return ret; 912 + } 913 + 914 + if (!was_active) { 915 + mtk_hdmi_v2_hw_reset(hdmi); 916 + mtk_hdmi_v2_set_sw_hpd(hdmi, true); 917 + } 918 + 919 + return 0; 920 + } 921 + 922 + static void mtk_hdmi_v2_disable(struct mtk_hdmi *hdmi) 923 + { 924 + mtk_hdmi_v2_clk_disable(hdmi); 925 + pm_runtime_put_sync(hdmi->dev); 926 + } 927 + 928 + /* 929 + * Bridge callbacks 930 + */ 931 + 932 + static int mtk_hdmi_v2_bridge_attach(struct drm_bridge *bridge, 933 + struct drm_encoder *encoder, 934 + enum drm_bridge_attach_flags flags) 935 + { 936 + struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge); 937 + int ret; 938 + 939 + if (!(flags & DRM_BRIDGE_ATTACH_NO_CONNECTOR)) { 940 + DRM_ERROR("The flag DRM_BRIDGE_ATTACH_NO_CONNECTOR must be supplied\n"); 941 + return -EINVAL; 942 + } 943 + if (hdmi->next_bridge) { 944 + ret = drm_bridge_attach(encoder, hdmi->next_bridge, bridge, flags); 945 + if (ret) 946 + return ret; 947 + } 948 + 949 + ret = mtk_hdmi_v2_enable(hdmi); 950 + if (ret) 951 + return ret; 952 + 953 + /* Enable Hotplug and Pord pins internal debouncing */ 954 + regmap_set_bits(hdmi->regs, HPD_DDC_CTRL, 955 + HPD_DDC_HPD_DBNC_EN | HPD_DDC_PORD_DBNC_EN); 956 + 957 + irq_clear_status_flags(hdmi->irq, IRQ_NOAUTOEN); 958 + enable_irq(hdmi->irq); 959 + 960 + /* 961 + * Check if any HDMI monitor was connected before probing this driver 962 + * and/or attaching the bridge, without debouncing: if so, we want to 963 + * notify the DRM so that we start outputting an image ASAP. 964 + * Note that calling the ISR thread function will also perform a HW 965 + * registers write that enables both the HPD and Pord interrupts. 966 + */ 967 + __mtk_hdmi_v2_isr_thread(hdmi); 968 + 969 + mtk_hdmi_v2_disable(hdmi); 970 + 971 + return 0; 972 + } 973 + 974 + static void mtk_hdmi_v2_bridge_detach(struct drm_bridge *bridge) 975 + { 976 + struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge); 977 + 978 + WARN_ON(pm_runtime_active(hdmi->dev)); 979 + 980 + /* The controller is already powered off, just disable irq here */ 981 + disable_irq(hdmi->irq); 982 + } 983 + 984 + static void mtk_hdmi_v2_handle_plugged_change(struct mtk_hdmi *hdmi, bool plugged) 985 + { 986 + mutex_lock(&hdmi->update_plugged_status_lock); 987 + if (hdmi->plugged_cb && hdmi->codec_dev) 988 + hdmi->plugged_cb(hdmi->codec_dev, plugged); 989 + mutex_unlock(&hdmi->update_plugged_status_lock); 990 + } 991 + 992 + static void mtk_hdmi_v2_bridge_pre_enable(struct drm_bridge *bridge, 993 + struct drm_atomic_state *state) 994 + { 995 + struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge); 996 + struct drm_connector_state *conn_state; 997 + union phy_configure_opts opts = { 998 + .dp = { .link_rate = hdmi->mode.clock * KILO } 999 + }; 1000 + int ret; 1001 + 1002 + /* Power on the controller before trying to write to registers */ 1003 + ret = mtk_hdmi_v2_enable(hdmi); 1004 + if (WARN_ON(ret)) 1005 + return; 1006 + 1007 + /* Retrieve the connector through the atomic state */ 1008 + hdmi->curr_conn = drm_atomic_get_new_connector_for_encoder(state, bridge->encoder); 1009 + 1010 + conn_state = drm_atomic_get_new_connector_state(state, hdmi->curr_conn); 1011 + if (WARN_ON(!conn_state)) 1012 + return; 1013 + 1014 + /* 1015 + * Preconfigure the HDMI controller and the HDMI PHY at pre_enable 1016 + * stage to make sure that this IP is ready and clocked before the 1017 + * mtk_dpi gets powered on and before it enables the output. 1018 + */ 1019 + mtk_hdmi_v2_output_set_display_mode(hdmi, conn_state, &hdmi->mode); 1020 + 1021 + /* Reconfigure phy clock link with appropriate rate */ 1022 + phy_configure(hdmi->phy, &opts); 1023 + 1024 + /* Power on the PHY here to make sure that DPI_HDMI is clocked */ 1025 + phy_power_on(hdmi->phy); 1026 + 1027 + hdmi->powered = true; 1028 + } 1029 + 1030 + static void mtk_hdmi_v2_bridge_enable(struct drm_bridge *bridge, 1031 + struct drm_atomic_state *state) 1032 + { 1033 + struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge); 1034 + int ret; 1035 + 1036 + if (WARN_ON(!hdmi->powered)) 1037 + return; 1038 + 1039 + ret = drm_atomic_helper_connector_hdmi_update_infoframes(hdmi->curr_conn, state); 1040 + if (ret) 1041 + dev_err(hdmi->dev, "Could not update infoframes: %d\n", ret); 1042 + 1043 + mtk_hdmi_v2_hw_vid_mute(hdmi, false); 1044 + 1045 + /* signal the connect event to audio codec */ 1046 + mtk_hdmi_v2_handle_plugged_change(hdmi, true); 1047 + 1048 + hdmi->enabled = true; 1049 + } 1050 + 1051 + static void mtk_hdmi_v2_bridge_disable(struct drm_bridge *bridge, 1052 + struct drm_atomic_state *state) 1053 + { 1054 + struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge); 1055 + 1056 + if (!hdmi->enabled) 1057 + return; 1058 + 1059 + mtk_hdmi_v2_hw_gcp_avmute(hdmi, true); 1060 + msleep(50); 1061 + mtk_hdmi_v2_hw_vid_mute(hdmi, true); 1062 + mtk_hdmi_v2_hw_aud_mute(hdmi, true); 1063 + msleep(50); 1064 + 1065 + hdmi->enabled = false; 1066 + } 1067 + 1068 + static void mtk_hdmi_v2_bridge_post_disable(struct drm_bridge *bridge, 1069 + struct drm_atomic_state *state) 1070 + { 1071 + struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge); 1072 + 1073 + if (!hdmi->powered) 1074 + return; 1075 + 1076 + phy_power_off(hdmi->phy); 1077 + hdmi->powered = false; 1078 + 1079 + /* signal the disconnect event to audio codec */ 1080 + mtk_hdmi_v2_handle_plugged_change(hdmi, false); 1081 + 1082 + /* Power off */ 1083 + mtk_hdmi_v2_disable(hdmi); 1084 + } 1085 + 1086 + static enum drm_connector_status mtk_hdmi_v2_bridge_detect(struct drm_bridge *bridge, 1087 + struct drm_connector *connector) 1088 + { 1089 + struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge); 1090 + 1091 + return hdmi->hpd != HDMI_PLUG_OUT ? 1092 + connector_status_connected : connector_status_disconnected; 1093 + } 1094 + 1095 + static const struct drm_edid *mtk_hdmi_v2_bridge_edid_read(struct drm_bridge *bridge, 1096 + struct drm_connector *connector) 1097 + { 1098 + return drm_edid_read(connector); 1099 + } 1100 + 1101 + static void mtk_hdmi_v2_hpd_enable(struct drm_bridge *bridge) 1102 + { 1103 + struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge); 1104 + int ret; 1105 + 1106 + ret = mtk_hdmi_v2_enable(hdmi); 1107 + if (ret) { 1108 + dev_err(hdmi->dev, "Cannot power on controller for HPD: %d\n", ret); 1109 + return; 1110 + } 1111 + 1112 + mtk_hdmi_v2_enable_hpd_pord_irq(hdmi, true); 1113 + } 1114 + 1115 + static void mtk_hdmi_v2_hpd_disable(struct drm_bridge *bridge) 1116 + { 1117 + struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge); 1118 + 1119 + mtk_hdmi_v2_enable_hpd_pord_irq(hdmi, false); 1120 + mtk_hdmi_v2_disable(hdmi); 1121 + } 1122 + 1123 + static int mtk_hdmi_v2_hdmi_tmds_char_rate_valid(const struct drm_bridge *bridge, 1124 + const struct drm_display_mode *mode, 1125 + unsigned long long tmds_rate) 1126 + { 1127 + if (mode->clock < MTK_HDMI_V2_CLOCK_MIN) 1128 + return MODE_CLOCK_LOW; 1129 + else if (mode->clock > MTK_HDMI_V2_CLOCK_MAX) 1130 + return MODE_CLOCK_HIGH; 1131 + else 1132 + return MODE_OK; 1133 + } 1134 + 1135 + static int mtk_hdmi_v2_hdmi_clear_infoframe(struct drm_bridge *bridge, 1136 + enum hdmi_infoframe_type type) 1137 + { 1138 + struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge); 1139 + 1140 + switch (type) { 1141 + case HDMI_INFOFRAME_TYPE_AUDIO: 1142 + regmap_clear_bits(hdmi->regs, TOP_INFO_EN, AUD_EN_WR | AUD_EN); 1143 + regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, AUD_RPT_EN); 1144 + break; 1145 + case HDMI_INFOFRAME_TYPE_AVI: 1146 + regmap_clear_bits(hdmi->regs, TOP_INFO_EN, AVI_EN_WR | AVI_EN); 1147 + regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, AVI_RPT_EN); 1148 + break; 1149 + case HDMI_INFOFRAME_TYPE_SPD: 1150 + regmap_clear_bits(hdmi->regs, TOP_INFO_EN, SPD_EN_WR | SPD_EN); 1151 + regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, SPD_RPT_EN); 1152 + break; 1153 + case HDMI_INFOFRAME_TYPE_VENDOR: 1154 + regmap_clear_bits(hdmi->regs, TOP_INFO_EN, VSIF_EN_WR | VSIF_EN); 1155 + regmap_clear_bits(hdmi->regs, TOP_INFO_RPT, VSIF_RPT_EN); 1156 + break; 1157 + case HDMI_INFOFRAME_TYPE_DRM: 1158 + default: 1159 + break; 1160 + }; 1161 + 1162 + return 0; 1163 + } 1164 + 1165 + static int mtk_hdmi_v2_hdmi_write_infoframe(struct drm_bridge *bridge, 1166 + enum hdmi_infoframe_type type, 1167 + const u8 *buffer, size_t len) 1168 + { 1169 + struct mtk_hdmi *hdmi = hdmi_ctx_from_bridge(bridge); 1170 + 1171 + switch (type) { 1172 + case HDMI_INFOFRAME_TYPE_AUDIO: 1173 + mtk_hdmi_v2_hw_write_audio_infoframe(hdmi, buffer); 1174 + break; 1175 + case HDMI_INFOFRAME_TYPE_AVI: 1176 + mtk_hdmi_v2_hw_write_avi_infoframe(hdmi, buffer); 1177 + break; 1178 + case HDMI_INFOFRAME_TYPE_SPD: 1179 + mtk_hdmi_v2_hw_write_spd_infoframe(hdmi, buffer); 1180 + break; 1181 + case HDMI_INFOFRAME_TYPE_VENDOR: 1182 + mtk_hdmi_v2_hw_write_vendor_infoframe(hdmi, buffer); 1183 + break; 1184 + case HDMI_INFOFRAME_TYPE_DRM: 1185 + default: 1186 + dev_err(hdmi->dev, "Unsupported HDMI infoframe type %u\n", type); 1187 + break; 1188 + }; 1189 + 1190 + return 0; 1191 + } 1192 + 1193 + static int mtk_hdmi_v2_set_abist(struct mtk_hdmi *hdmi, bool enable) 1194 + { 1195 + struct drm_display_mode *mode = &hdmi->mode; 1196 + int abist_format = -EINVAL; 1197 + bool interlaced; 1198 + 1199 + if (!enable) { 1200 + regmap_clear_bits(hdmi->regs, TOP_CFG00, HDMI_ABIST_ENABLE); 1201 + return 0; 1202 + } 1203 + 1204 + if (!mode->hdisplay || !mode->vdisplay) 1205 + return -EINVAL; 1206 + 1207 + interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE; 1208 + 1209 + switch (mode->hdisplay) { 1210 + case 720: 1211 + if (mode->vdisplay == 480) 1212 + abist_format = 2; 1213 + else if (mode->vdisplay == 576) 1214 + abist_format = 11; 1215 + break; 1216 + case 1280: 1217 + if (mode->vdisplay == 720) 1218 + abist_format = 3; 1219 + break; 1220 + case 1440: 1221 + if (mode->vdisplay == 480) 1222 + abist_format = interlaced ? 5 : 9; 1223 + else if (mode->vdisplay == 576) 1224 + abist_format = interlaced ? 14 : 18; 1225 + break; 1226 + case 1920: 1227 + if (mode->vdisplay == 1080) 1228 + abist_format = interlaced ? 4 : 10; 1229 + break; 1230 + case 3840: 1231 + if (mode->vdisplay == 2160) 1232 + abist_format = 25; 1233 + break; 1234 + case 4096: 1235 + if (mode->vdisplay == 2160) 1236 + abist_format = 26; 1237 + break; 1238 + default: 1239 + break; 1240 + } 1241 + if (abist_format < 0) 1242 + return abist_format; 1243 + 1244 + regmap_update_bits(hdmi->regs, TOP_CFG00, HDMI_ABIST_VIDEO_FORMAT, 1245 + FIELD_PREP(HDMI_ABIST_VIDEO_FORMAT, abist_format)); 1246 + regmap_set_bits(hdmi->regs, TOP_CFG00, HDMI_ABIST_ENABLE); 1247 + return 0; 1248 + } 1249 + 1250 + static int mtk_hdmi_v2_debug_abist_show(struct seq_file *m, void *arg) 1251 + { 1252 + struct mtk_hdmi *hdmi = m->private; 1253 + bool en; 1254 + u32 val; 1255 + int ret; 1256 + 1257 + if (!hdmi) 1258 + return -EINVAL; 1259 + 1260 + ret = regmap_read(hdmi->regs, TOP_CFG00, &val); 1261 + if (ret) 1262 + return ret; 1263 + 1264 + en = FIELD_GET(HDMI_ABIST_ENABLE, val); 1265 + 1266 + seq_printf(m, "HDMI Automated Built-In Self Test: %s\n", 1267 + en ? "Enabled" : "Disabled"); 1268 + 1269 + return 0; 1270 + } 1271 + 1272 + static ssize_t mtk_hdmi_v2_debug_abist_write(struct file *file, 1273 + const char __user *ubuf, 1274 + size_t len, loff_t *offp) 1275 + { 1276 + struct seq_file *m = file->private_data; 1277 + int ret; 1278 + u32 en; 1279 + 1280 + if (!m || !m->private || *offp) 1281 + return -EINVAL; 1282 + 1283 + ret = kstrtouint_from_user(ubuf, len, 0, &en); 1284 + if (ret) 1285 + return ret; 1286 + 1287 + if (en < 0 || en > 1) 1288 + return -EINVAL; 1289 + 1290 + mtk_hdmi_v2_set_abist((struct mtk_hdmi *)m->private, en); 1291 + return len; 1292 + } 1293 + 1294 + static int mtk_hdmi_v2_debug_abist_open(struct inode *inode, struct file *file) 1295 + { 1296 + return single_open(file, mtk_hdmi_v2_debug_abist_show, inode->i_private); 1297 + } 1298 + 1299 + static const struct file_operations mtk_hdmi_debug_abist_fops = { 1300 + .owner = THIS_MODULE, 1301 + .open = mtk_hdmi_v2_debug_abist_open, 1302 + .read = seq_read, 1303 + .write = mtk_hdmi_v2_debug_abist_write, 1304 + .llseek = seq_lseek, 1305 + .release = single_release, 1306 + }; 1307 + 1308 + static void mtk_hdmi_v2_debugfs_init(struct drm_bridge *bridge, struct dentry *root) 1309 + { 1310 + struct mtk_hdmi *dpi = hdmi_ctx_from_bridge(bridge); 1311 + 1312 + debugfs_create_file("hdmi_abist", 0640, root, dpi, &mtk_hdmi_debug_abist_fops); 1313 + } 1314 + 1315 + static const struct drm_bridge_funcs mtk_v2_hdmi_bridge_funcs = { 1316 + .attach = mtk_hdmi_v2_bridge_attach, 1317 + .detach = mtk_hdmi_v2_bridge_detach, 1318 + .mode_fixup = mtk_hdmi_bridge_mode_fixup, 1319 + .mode_set = mtk_hdmi_bridge_mode_set, 1320 + .atomic_pre_enable = mtk_hdmi_v2_bridge_pre_enable, 1321 + .atomic_enable = mtk_hdmi_v2_bridge_enable, 1322 + .atomic_disable = mtk_hdmi_v2_bridge_disable, 1323 + .atomic_post_disable = mtk_hdmi_v2_bridge_post_disable, 1324 + .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state, 1325 + .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state, 1326 + .atomic_reset = drm_atomic_helper_bridge_reset, 1327 + .detect = mtk_hdmi_v2_bridge_detect, 1328 + .edid_read = mtk_hdmi_v2_bridge_edid_read, 1329 + .hpd_enable = mtk_hdmi_v2_hpd_enable, 1330 + .hpd_disable = mtk_hdmi_v2_hpd_disable, 1331 + .hdmi_tmds_char_rate_valid = mtk_hdmi_v2_hdmi_tmds_char_rate_valid, 1332 + .hdmi_clear_infoframe = mtk_hdmi_v2_hdmi_clear_infoframe, 1333 + .hdmi_write_infoframe = mtk_hdmi_v2_hdmi_write_infoframe, 1334 + .debugfs_init = mtk_hdmi_v2_debugfs_init, 1335 + }; 1336 + 1337 + /* 1338 + * HDMI audio codec callbacks 1339 + */ 1340 + static int mtk_hdmi_v2_audio_hook_plugged_cb(struct device *dev, void *data, 1341 + hdmi_codec_plugged_cb fn, 1342 + struct device *codec_dev) 1343 + { 1344 + struct mtk_hdmi *hdmi = dev_get_drvdata(dev); 1345 + bool plugged; 1346 + 1347 + if (!hdmi) 1348 + return -ENODEV; 1349 + 1350 + mtk_hdmi_audio_set_plugged_cb(hdmi, fn, codec_dev); 1351 + plugged = (hdmi->hpd == HDMI_PLUG_IN_AND_SINK_POWER_ON); 1352 + mtk_hdmi_v2_handle_plugged_change(hdmi, plugged); 1353 + 1354 + return 0; 1355 + } 1356 + 1357 + static int mtk_hdmi_v2_audio_hw_params(struct device *dev, void *data, 1358 + struct hdmi_codec_daifmt *daifmt, 1359 + struct hdmi_codec_params *params) 1360 + { 1361 + struct mtk_hdmi *hdmi = dev_get_drvdata(dev); 1362 + 1363 + if (hdmi->audio_enable) { 1364 + mtk_hdmi_audio_params(hdmi, daifmt, params); 1365 + mtk_hdmi_v2_aud_output_config(hdmi, &hdmi->mode); 1366 + } 1367 + return 0; 1368 + } 1369 + 1370 + static int mtk_hdmi_v2_audio_startup(struct device *dev, void *data) 1371 + { 1372 + struct mtk_hdmi *hdmi = dev_get_drvdata(dev); 1373 + 1374 + mtk_hdmi_v2_hw_aud_enable(hdmi, true); 1375 + hdmi->audio_enable = true; 1376 + 1377 + return 0; 1378 + } 1379 + 1380 + static void mtk_hdmi_v2_audio_shutdown(struct device *dev, void *data) 1381 + { 1382 + struct mtk_hdmi *hdmi = dev_get_drvdata(dev); 1383 + 1384 + hdmi->audio_enable = false; 1385 + mtk_hdmi_v2_hw_aud_enable(hdmi, false); 1386 + } 1387 + 1388 + static int mtk_hdmi_v2_audio_mute(struct device *dev, void *data, bool enable, int dir) 1389 + { 1390 + struct mtk_hdmi *hdmi = dev_get_drvdata(dev); 1391 + 1392 + mtk_hdmi_v2_hw_aud_mute(hdmi, enable); 1393 + 1394 + return 0; 1395 + } 1396 + 1397 + static const struct hdmi_codec_ops mtk_hdmi_v2_audio_codec_ops = { 1398 + .hw_params = mtk_hdmi_v2_audio_hw_params, 1399 + .audio_startup = mtk_hdmi_v2_audio_startup, 1400 + .audio_shutdown = mtk_hdmi_v2_audio_shutdown, 1401 + .mute_stream = mtk_hdmi_v2_audio_mute, 1402 + .get_eld = mtk_hdmi_audio_get_eld, 1403 + .hook_plugged_cb = mtk_hdmi_v2_audio_hook_plugged_cb, 1404 + }; 1405 + 1406 + static __maybe_unused int mtk_hdmi_v2_suspend(struct device *dev) 1407 + { 1408 + struct mtk_hdmi *hdmi = dev_get_drvdata(dev); 1409 + 1410 + mtk_hdmi_v2_disable(hdmi); 1411 + 1412 + return 0; 1413 + } 1414 + 1415 + static __maybe_unused int mtk_hdmi_v2_resume(struct device *dev) 1416 + { 1417 + struct mtk_hdmi *hdmi = dev_get_drvdata(dev); 1418 + 1419 + return mtk_hdmi_v2_enable(hdmi); 1420 + } 1421 + 1422 + static SIMPLE_DEV_PM_OPS(mtk_hdmi_v2_pm_ops, mtk_hdmi_v2_suspend, mtk_hdmi_v2_resume); 1423 + 1424 + static const struct mtk_hdmi_ver_conf mtk_hdmi_conf_v2 = { 1425 + .bridge_funcs = &mtk_v2_hdmi_bridge_funcs, 1426 + .codec_ops = &mtk_hdmi_v2_audio_codec_ops, 1427 + .mtk_hdmi_clock_names = mtk_hdmi_v2_clk_names, 1428 + .num_clocks = MTK_HDMI_V2_CLK_COUNT, 1429 + .interlace_allowed = true, 1430 + }; 1431 + 1432 + static const struct mtk_hdmi_conf mtk_hdmi_conf_mt8188 = { 1433 + .ver_conf = &mtk_hdmi_conf_v2, 1434 + .reg_hdmi_tx_cfg = HDMITX_CONFIG_MT8188 1435 + }; 1436 + 1437 + static const struct mtk_hdmi_conf mtk_hdmi_conf_mt8195 = { 1438 + .ver_conf = &mtk_hdmi_conf_v2, 1439 + .reg_hdmi_tx_cfg = HDMITX_CONFIG_MT8195 1440 + }; 1441 + 1442 + static int mtk_hdmi_v2_probe(struct platform_device *pdev) 1443 + { 1444 + struct mtk_hdmi *hdmi; 1445 + int ret; 1446 + 1447 + /* Populate HDMI sub-devices if present */ 1448 + ret = devm_of_platform_populate(&pdev->dev); 1449 + if (ret) 1450 + return ret; 1451 + 1452 + hdmi = mtk_hdmi_common_probe(pdev); 1453 + if (IS_ERR(hdmi)) 1454 + return PTR_ERR(hdmi); 1455 + 1456 + hdmi->hpd = HDMI_PLUG_OUT; 1457 + 1458 + /* Disable all HW interrupts at probe stage */ 1459 + mtk_hdmi_v2_hwirq_disable(hdmi); 1460 + 1461 + /* 1462 + * In case bootloader leaves HDMI enabled before booting, make 1463 + * sure that any interrupt that was left is cleared by setting 1464 + * all bits in the INT_CLR registers for all 32+19 interrupts. 1465 + */ 1466 + regmap_write(hdmi->regs, TOP_INT_CLR00, GENMASK(31, 0)); 1467 + regmap_write(hdmi->regs, TOP_INT_CLR01, GENMASK(18, 0)); 1468 + 1469 + /* Restore interrupt clearing registers to zero */ 1470 + regmap_write(hdmi->regs, TOP_INT_CLR00, 0); 1471 + regmap_write(hdmi->regs, TOP_INT_CLR01, 0); 1472 + 1473 + /* 1474 + * Install the ISR but keep it disabled: as the interrupts are 1475 + * being set up in the .bridge_attach() callback which will 1476 + * enable both the right HW IRQs and the ISR. 1477 + */ 1478 + irq_set_status_flags(hdmi->irq, IRQ_NOAUTOEN); 1479 + ret = devm_request_threaded_irq(&pdev->dev, hdmi->irq, mtk_hdmi_v2_isr, 1480 + mtk_hdmi_v2_isr_thread, 1481 + IRQ_TYPE_LEVEL_HIGH, 1482 + dev_name(&pdev->dev), hdmi); 1483 + if (ret) 1484 + return dev_err_probe(&pdev->dev, ret, "Cannot request IRQ\n"); 1485 + 1486 + ret = devm_pm_runtime_enable(&pdev->dev); 1487 + if (ret) 1488 + return dev_err_probe(&pdev->dev, ret, "Cannot enable Runtime PM\n"); 1489 + 1490 + return 0; 1491 + } 1492 + 1493 + static void mtk_hdmi_v2_remove(struct platform_device *pdev) 1494 + { 1495 + struct mtk_hdmi *hdmi = platform_get_drvdata(pdev); 1496 + 1497 + i2c_put_adapter(hdmi->ddc_adpt); 1498 + } 1499 + 1500 + static const struct of_device_id mtk_drm_hdmi_v2_of_ids[] = { 1501 + { .compatible = "mediatek,mt8188-hdmi-tx", .data = &mtk_hdmi_conf_mt8188 }, 1502 + { .compatible = "mediatek,mt8195-hdmi-tx", .data = &mtk_hdmi_conf_mt8195 }, 1503 + { /* sentinel */ } 1504 + }; 1505 + MODULE_DEVICE_TABLE(of, mtk_drm_hdmi_v2_of_ids); 1506 + 1507 + static struct platform_driver mtk_hdmi_v2_driver = { 1508 + .probe = mtk_hdmi_v2_probe, 1509 + .remove = mtk_hdmi_v2_remove, 1510 + .driver = { 1511 + .name = "mediatek-drm-hdmi-v2", 1512 + .of_match_table = mtk_drm_hdmi_v2_of_ids, 1513 + .pm = &mtk_hdmi_v2_pm_ops, 1514 + }, 1515 + }; 1516 + module_platform_driver(mtk_hdmi_v2_driver); 1517 + 1518 + MODULE_AUTHOR("AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>>"); 1519 + MODULE_DESCRIPTION("MediaTek HDMIv2 Driver"); 1520 + MODULE_LICENSE("GPL"); 1521 + MODULE_IMPORT_NS("DRM_MTK_HDMI");