1*96f30ee0SIcenowy Zheng // SPDX-License-Identifier: GPL-2.0+ 2*96f30ee0SIcenowy Zheng /* 3*96f30ee0SIcenowy Zheng * Copyright (C) 2025 Icenowy Zheng <uwu@icenowy.me> 4*96f30ee0SIcenowy Zheng * 5*96f30ee0SIcenowy Zheng * Based on rcar_dw_hdmi.c, which is: 6*96f30ee0SIcenowy Zheng * Copyright (C) 2016 Renesas Electronics Corporation 7*96f30ee0SIcenowy Zheng * Based on imx8mp-hdmi-tx.c, which is: 8*96f30ee0SIcenowy Zheng * Copyright (C) 2022 Pengutronix, Lucas Stach <kernel@pengutronix.de> 9*96f30ee0SIcenowy Zheng */ 10*96f30ee0SIcenowy Zheng 11*96f30ee0SIcenowy Zheng #include <linux/clk.h> 12*96f30ee0SIcenowy Zheng #include <linux/mod_devicetable.h> 13*96f30ee0SIcenowy Zheng #include <linux/module.h> 14*96f30ee0SIcenowy Zheng #include <linux/platform_device.h> 15*96f30ee0SIcenowy Zheng #include <linux/reset.h> 16*96f30ee0SIcenowy Zheng 17*96f30ee0SIcenowy Zheng #include <drm/bridge/dw_hdmi.h> 18*96f30ee0SIcenowy Zheng #include <drm/drm_modes.h> 19*96f30ee0SIcenowy Zheng 20*96f30ee0SIcenowy Zheng #define TH1520_HDMI_PHY_OPMODE_PLLCFG 0x06 /* Mode of operation and PLL dividers */ 21*96f30ee0SIcenowy Zheng #define TH1520_HDMI_PHY_CKSYMTXCTRL 0x09 /* Clock Symbol and Transmitter Control Register */ 22*96f30ee0SIcenowy Zheng #define TH1520_HDMI_PHY_VLEVCTRL 0x0e /* Voltage Level Control Register */ 23*96f30ee0SIcenowy Zheng #define TH1520_HDMI_PHY_PLLCURRGMPCTRL 0x10 /* PLL current and Gmp (conductance) */ 24*96f30ee0SIcenowy Zheng #define TH1520_HDMI_PHY_PLLDIVCTRL 0x11 /* PLL dividers */ 25*96f30ee0SIcenowy Zheng #define TH1520_HDMI_PHY_TXTERM 0x19 /* Transmission Termination Register */ 26*96f30ee0SIcenowy Zheng 27*96f30ee0SIcenowy Zheng struct th1520_hdmi_phy_params { 28*96f30ee0SIcenowy Zheng unsigned long mpixelclock; 29*96f30ee0SIcenowy Zheng u16 opmode_pllcfg; 30*96f30ee0SIcenowy Zheng u16 pllcurrgmpctrl; 31*96f30ee0SIcenowy Zheng u16 plldivctrl; 32*96f30ee0SIcenowy Zheng u16 cksymtxctrl; 33*96f30ee0SIcenowy Zheng u16 vlevctrl; 34*96f30ee0SIcenowy Zheng u16 txterm; 35*96f30ee0SIcenowy Zheng }; 36*96f30ee0SIcenowy Zheng 37*96f30ee0SIcenowy Zheng static const struct th1520_hdmi_phy_params th1520_hdmi_phy_params[] = { 38*96f30ee0SIcenowy Zheng { 35500000, 0x0003, 0x0283, 0x0628, 0x8088, 0x01a0, 0x0007 }, 39*96f30ee0SIcenowy Zheng { 44900000, 0x0003, 0x0285, 0x0228, 0x8088, 0x01a0, 0x0007 }, 40*96f30ee0SIcenowy Zheng { 71000000, 0x0002, 0x1183, 0x0614, 0x8088, 0x01a0, 0x0007 }, 41*96f30ee0SIcenowy Zheng { 90000000, 0x0002, 0x1142, 0x0214, 0x8088, 0x01a0, 0x0007 }, 42*96f30ee0SIcenowy Zheng { 121750000, 0x0001, 0x20c0, 0x060a, 0x8088, 0x01a0, 0x0007 }, 43*96f30ee0SIcenowy Zheng { 165000000, 0x0001, 0x2080, 0x020a, 0x8088, 0x01a0, 0x0007 }, 44*96f30ee0SIcenowy Zheng { 198000000, 0x0000, 0x3040, 0x0605, 0x83c8, 0x0120, 0x0004 }, 45*96f30ee0SIcenowy Zheng { 297000000, 0x0000, 0x3041, 0x0205, 0x81dc, 0x0200, 0x0005 }, 46*96f30ee0SIcenowy Zheng { 371250000, 0x0640, 0x3041, 0x0205, 0x80f6, 0x0140, 0x0000 }, 47*96f30ee0SIcenowy Zheng { 495000000, 0x0640, 0x3080, 0x0005, 0x80f6, 0x0140, 0x0000 }, 48*96f30ee0SIcenowy Zheng { 594000000, 0x0640, 0x3080, 0x0005, 0x80fa, 0x01e0, 0x0004 }, 49*96f30ee0SIcenowy Zheng }; 50*96f30ee0SIcenowy Zheng 51*96f30ee0SIcenowy Zheng struct th1520_hdmi { 52*96f30ee0SIcenowy Zheng struct dw_hdmi_plat_data plat_data; 53*96f30ee0SIcenowy Zheng struct dw_hdmi *dw_hdmi; 54*96f30ee0SIcenowy Zheng struct clk *pixclk; 55*96f30ee0SIcenowy Zheng struct reset_control *mainrst, *prst; 56*96f30ee0SIcenowy Zheng }; 57*96f30ee0SIcenowy Zheng 58*96f30ee0SIcenowy Zheng static enum drm_mode_status 59*96f30ee0SIcenowy Zheng th1520_hdmi_mode_valid(struct dw_hdmi *hdmi, void *data, 60*96f30ee0SIcenowy Zheng const struct drm_display_info *info, 61*96f30ee0SIcenowy Zheng const struct drm_display_mode *mode) 62*96f30ee0SIcenowy Zheng { 63*96f30ee0SIcenowy Zheng /* 64*96f30ee0SIcenowy Zheng * The maximum supported clock frequency is 594 MHz, as shown in the PHY 65*96f30ee0SIcenowy Zheng * parameters table. 66*96f30ee0SIcenowy Zheng */ 67*96f30ee0SIcenowy Zheng if (mode->clock > 594000) 68*96f30ee0SIcenowy Zheng return MODE_CLOCK_HIGH; 69*96f30ee0SIcenowy Zheng 70*96f30ee0SIcenowy Zheng return MODE_OK; 71*96f30ee0SIcenowy Zheng } 72*96f30ee0SIcenowy Zheng 73*96f30ee0SIcenowy Zheng static void th1520_hdmi_phy_set_params(struct dw_hdmi *hdmi, 74*96f30ee0SIcenowy Zheng const struct th1520_hdmi_phy_params *params) 75*96f30ee0SIcenowy Zheng { 76*96f30ee0SIcenowy Zheng dw_hdmi_phy_i2c_write(hdmi, params->opmode_pllcfg, 77*96f30ee0SIcenowy Zheng TH1520_HDMI_PHY_OPMODE_PLLCFG); 78*96f30ee0SIcenowy Zheng dw_hdmi_phy_i2c_write(hdmi, params->pllcurrgmpctrl, 79*96f30ee0SIcenowy Zheng TH1520_HDMI_PHY_PLLCURRGMPCTRL); 80*96f30ee0SIcenowy Zheng dw_hdmi_phy_i2c_write(hdmi, params->plldivctrl, 81*96f30ee0SIcenowy Zheng TH1520_HDMI_PHY_PLLDIVCTRL); 82*96f30ee0SIcenowy Zheng dw_hdmi_phy_i2c_write(hdmi, params->vlevctrl, 83*96f30ee0SIcenowy Zheng TH1520_HDMI_PHY_VLEVCTRL); 84*96f30ee0SIcenowy Zheng dw_hdmi_phy_i2c_write(hdmi, params->cksymtxctrl, 85*96f30ee0SIcenowy Zheng TH1520_HDMI_PHY_CKSYMTXCTRL); 86*96f30ee0SIcenowy Zheng dw_hdmi_phy_i2c_write(hdmi, params->txterm, 87*96f30ee0SIcenowy Zheng TH1520_HDMI_PHY_TXTERM); 88*96f30ee0SIcenowy Zheng } 89*96f30ee0SIcenowy Zheng 90*96f30ee0SIcenowy Zheng static int th1520_hdmi_phy_configure(struct dw_hdmi *hdmi, void *data, 91*96f30ee0SIcenowy Zheng unsigned long mpixelclock) 92*96f30ee0SIcenowy Zheng { 93*96f30ee0SIcenowy Zheng unsigned int i; 94*96f30ee0SIcenowy Zheng 95*96f30ee0SIcenowy Zheng for (i = 0; i < ARRAY_SIZE(th1520_hdmi_phy_params); i++) { 96*96f30ee0SIcenowy Zheng if (mpixelclock <= th1520_hdmi_phy_params[i].mpixelclock) { 97*96f30ee0SIcenowy Zheng th1520_hdmi_phy_set_params(hdmi, 98*96f30ee0SIcenowy Zheng &th1520_hdmi_phy_params[i]); 99*96f30ee0SIcenowy Zheng return 0; 100*96f30ee0SIcenowy Zheng } 101*96f30ee0SIcenowy Zheng } 102*96f30ee0SIcenowy Zheng 103*96f30ee0SIcenowy Zheng return -EINVAL; 104*96f30ee0SIcenowy Zheng } 105*96f30ee0SIcenowy Zheng 106*96f30ee0SIcenowy Zheng static int th1520_dw_hdmi_probe(struct platform_device *pdev) 107*96f30ee0SIcenowy Zheng { 108*96f30ee0SIcenowy Zheng struct th1520_hdmi *hdmi; 109*96f30ee0SIcenowy Zheng struct dw_hdmi_plat_data *plat_data; 110*96f30ee0SIcenowy Zheng struct device *dev = &pdev->dev; 111*96f30ee0SIcenowy Zheng 112*96f30ee0SIcenowy Zheng hdmi = devm_kzalloc(dev, sizeof(*hdmi), GFP_KERNEL); 113*96f30ee0SIcenowy Zheng if (!hdmi) 114*96f30ee0SIcenowy Zheng return -ENOMEM; 115*96f30ee0SIcenowy Zheng 116*96f30ee0SIcenowy Zheng plat_data = &hdmi->plat_data; 117*96f30ee0SIcenowy Zheng 118*96f30ee0SIcenowy Zheng hdmi->pixclk = devm_clk_get_enabled(dev, "pix"); 119*96f30ee0SIcenowy Zheng if (IS_ERR(hdmi->pixclk)) 120*96f30ee0SIcenowy Zheng return dev_err_probe(dev, PTR_ERR(hdmi->pixclk), 121*96f30ee0SIcenowy Zheng "Unable to get pixel clock\n"); 122*96f30ee0SIcenowy Zheng 123*96f30ee0SIcenowy Zheng hdmi->mainrst = devm_reset_control_get_exclusive_deasserted(dev, "main"); 124*96f30ee0SIcenowy Zheng if (IS_ERR(hdmi->mainrst)) 125*96f30ee0SIcenowy Zheng return dev_err_probe(dev, PTR_ERR(hdmi->mainrst), 126*96f30ee0SIcenowy Zheng "Unable to get main reset\n"); 127*96f30ee0SIcenowy Zheng 128*96f30ee0SIcenowy Zheng hdmi->prst = devm_reset_control_get_exclusive_deasserted(dev, "apb"); 129*96f30ee0SIcenowy Zheng if (IS_ERR(hdmi->prst)) 130*96f30ee0SIcenowy Zheng return dev_err_probe(dev, PTR_ERR(hdmi->prst), 131*96f30ee0SIcenowy Zheng "Unable to get apb reset\n"); 132*96f30ee0SIcenowy Zheng 133*96f30ee0SIcenowy Zheng plat_data->output_port = 1; 134*96f30ee0SIcenowy Zheng plat_data->mode_valid = th1520_hdmi_mode_valid; 135*96f30ee0SIcenowy Zheng plat_data->configure_phy = th1520_hdmi_phy_configure; 136*96f30ee0SIcenowy Zheng plat_data->priv_data = hdmi; 137*96f30ee0SIcenowy Zheng 138*96f30ee0SIcenowy Zheng hdmi->dw_hdmi = dw_hdmi_probe(pdev, plat_data); 139*96f30ee0SIcenowy Zheng if (IS_ERR(hdmi)) 140*96f30ee0SIcenowy Zheng return PTR_ERR(hdmi); 141*96f30ee0SIcenowy Zheng 142*96f30ee0SIcenowy Zheng platform_set_drvdata(pdev, hdmi); 143*96f30ee0SIcenowy Zheng 144*96f30ee0SIcenowy Zheng return 0; 145*96f30ee0SIcenowy Zheng } 146*96f30ee0SIcenowy Zheng 147*96f30ee0SIcenowy Zheng static void th1520_dw_hdmi_remove(struct platform_device *pdev) 148*96f30ee0SIcenowy Zheng { 149*96f30ee0SIcenowy Zheng struct dw_hdmi *hdmi = platform_get_drvdata(pdev); 150*96f30ee0SIcenowy Zheng 151*96f30ee0SIcenowy Zheng dw_hdmi_remove(hdmi); 152*96f30ee0SIcenowy Zheng } 153*96f30ee0SIcenowy Zheng 154*96f30ee0SIcenowy Zheng static const struct of_device_id th1520_dw_hdmi_of_table[] = { 155*96f30ee0SIcenowy Zheng { .compatible = "thead,th1520-dw-hdmi" }, 156*96f30ee0SIcenowy Zheng { /* Sentinel */ }, 157*96f30ee0SIcenowy Zheng }; 158*96f30ee0SIcenowy Zheng MODULE_DEVICE_TABLE(of, th1520_dw_hdmi_of_table); 159*96f30ee0SIcenowy Zheng 160*96f30ee0SIcenowy Zheng static struct platform_driver th1520_dw_hdmi_platform_driver = { 161*96f30ee0SIcenowy Zheng .probe = th1520_dw_hdmi_probe, 162*96f30ee0SIcenowy Zheng .remove = th1520_dw_hdmi_remove, 163*96f30ee0SIcenowy Zheng .driver = { 164*96f30ee0SIcenowy Zheng .name = "th1520-dw-hdmi", 165*96f30ee0SIcenowy Zheng .of_match_table = th1520_dw_hdmi_of_table, 166*96f30ee0SIcenowy Zheng }, 167*96f30ee0SIcenowy Zheng }; 168*96f30ee0SIcenowy Zheng 169*96f30ee0SIcenowy Zheng module_platform_driver(th1520_dw_hdmi_platform_driver); 170*96f30ee0SIcenowy Zheng 171*96f30ee0SIcenowy Zheng MODULE_AUTHOR("Icenowy Zheng <uwu@icenowy.me>"); 172*96f30ee0SIcenowy Zheng MODULE_DESCRIPTION("T-Head TH1520 HDMI Encoder Driver"); 173*96f30ee0SIcenowy Zheng MODULE_LICENSE("GPL"); 174