Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright (c) 2017 Rockchip Electronics Co. Ltd.
0004  *
0005  * Author: Zheng Yang <zhengyang@rock-chips.com>
0006  *         Heiko Stuebner <heiko@sntech.de>
0007  */
0008 
0009 #include <linux/clk.h>
0010 #include <linux/clk-provider.h>
0011 #include <linux/delay.h>
0012 #include <linux/io.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/nvmem-consumer.h>
0017 #include <linux/of.h>
0018 #include <linux/of_device.h>
0019 #include <linux/platform_device.h>
0020 #include <linux/regmap.h>
0021 #include <linux/phy/phy.h>
0022 #include <linux/slab.h>
0023 
0024 #define UPDATE(x, h, l)     (((x) << (l)) & GENMASK((h), (l)))
0025 
0026 /* REG: 0x00 */
0027 #define RK3228_PRE_PLL_REFCLK_SEL_PCLK          BIT(0)
0028 /* REG: 0x01 */
0029 #define RK3228_BYPASS_RXSENSE_EN            BIT(2)
0030 #define RK3228_BYPASS_PWRON_EN              BIT(1)
0031 #define RK3228_BYPASS_PLLPD_EN              BIT(0)
0032 /* REG: 0x02 */
0033 #define RK3228_BYPASS_PDATA_EN              BIT(4)
0034 #define RK3228_PDATAEN_DISABLE              BIT(0)
0035 /* REG: 0x03 */
0036 #define RK3228_BYPASS_AUTO_TERM_RES_CAL         BIT(7)
0037 #define RK3228_AUTO_TERM_RES_CAL_SPEED_14_8(x)      UPDATE(x, 6, 0)
0038 /* REG: 0x04 */
0039 #define RK3228_AUTO_TERM_RES_CAL_SPEED_7_0(x)       UPDATE(x, 7, 0)
0040 /* REG: 0xaa */
0041 #define RK3228_POST_PLL_CTRL_MANUAL         BIT(0)
0042 /* REG: 0xe0 */
0043 #define RK3228_POST_PLL_POWER_DOWN          BIT(5)
0044 #define RK3228_PRE_PLL_POWER_DOWN           BIT(4)
0045 #define RK3228_RXSENSE_CLK_CH_ENABLE            BIT(3)
0046 #define RK3228_RXSENSE_DATA_CH2_ENABLE          BIT(2)
0047 #define RK3228_RXSENSE_DATA_CH1_ENABLE          BIT(1)
0048 #define RK3228_RXSENSE_DATA_CH0_ENABLE          BIT(0)
0049 /* REG: 0xe1 */
0050 #define RK3228_BANDGAP_ENABLE               BIT(4)
0051 #define RK3228_TMDS_DRIVER_ENABLE           GENMASK(3, 0)
0052 /* REG: 0xe2 */
0053 #define RK3228_PRE_PLL_FB_DIV_8_MASK            BIT(7)
0054 #define RK3228_PRE_PLL_FB_DIV_8(x)          UPDATE((x) >> 8, 7, 7)
0055 #define RK3228_PCLK_VCO_DIV_5_MASK          BIT(5)
0056 #define RK3228_PCLK_VCO_DIV_5(x)            UPDATE(x, 5, 5)
0057 #define RK3228_PRE_PLL_PRE_DIV_MASK         GENMASK(4, 0)
0058 #define RK3228_PRE_PLL_PRE_DIV(x)           UPDATE(x, 4, 0)
0059 /* REG: 0xe3 */
0060 #define RK3228_PRE_PLL_FB_DIV_7_0(x)            UPDATE(x, 7, 0)
0061 /* REG: 0xe4 */
0062 #define RK3228_PRE_PLL_PCLK_DIV_B_MASK          GENMASK(6, 5)
0063 #define RK3228_PRE_PLL_PCLK_DIV_B_SHIFT         5
0064 #define RK3228_PRE_PLL_PCLK_DIV_B(x)            UPDATE(x, 6, 5)
0065 #define RK3228_PRE_PLL_PCLK_DIV_A_MASK          GENMASK(4, 0)
0066 #define RK3228_PRE_PLL_PCLK_DIV_A(x)            UPDATE(x, 4, 0)
0067 /* REG: 0xe5 */
0068 #define RK3228_PRE_PLL_PCLK_DIV_C_MASK          GENMASK(6, 5)
0069 #define RK3228_PRE_PLL_PCLK_DIV_C(x)            UPDATE(x, 6, 5)
0070 #define RK3228_PRE_PLL_PCLK_DIV_D_MASK          GENMASK(4, 0)
0071 #define RK3228_PRE_PLL_PCLK_DIV_D(x)            UPDATE(x, 4, 0)
0072 /* REG: 0xe6 */
0073 #define RK3228_PRE_PLL_TMDSCLK_DIV_C_MASK       GENMASK(5, 4)
0074 #define RK3228_PRE_PLL_TMDSCLK_DIV_C(x)         UPDATE(x, 5, 4)
0075 #define RK3228_PRE_PLL_TMDSCLK_DIV_A_MASK       GENMASK(3, 2)
0076 #define RK3228_PRE_PLL_TMDSCLK_DIV_A(x)         UPDATE(x, 3, 2)
0077 #define RK3228_PRE_PLL_TMDSCLK_DIV_B_MASK       GENMASK(1, 0)
0078 #define RK3228_PRE_PLL_TMDSCLK_DIV_B(x)         UPDATE(x, 1, 0)
0079 /* REG: 0xe8 */
0080 #define RK3228_PRE_PLL_LOCK_STATUS          BIT(0)
0081 /* REG: 0xe9 */
0082 #define RK3228_POST_PLL_POST_DIV_ENABLE         UPDATE(3, 7, 6)
0083 #define RK3228_POST_PLL_PRE_DIV_MASK            GENMASK(4, 0)
0084 #define RK3228_POST_PLL_PRE_DIV(x)          UPDATE(x, 4, 0)
0085 /* REG: 0xea */
0086 #define RK3228_POST_PLL_FB_DIV_7_0(x)           UPDATE(x, 7, 0)
0087 /* REG: 0xeb */
0088 #define RK3228_POST_PLL_FB_DIV_8_MASK           BIT(7)
0089 #define RK3228_POST_PLL_FB_DIV_8(x)         UPDATE((x) >> 8, 7, 7)
0090 #define RK3228_POST_PLL_POST_DIV_MASK           GENMASK(5, 4)
0091 #define RK3228_POST_PLL_POST_DIV(x)         UPDATE(x, 5, 4)
0092 #define RK3228_POST_PLL_LOCK_STATUS         BIT(0)
0093 /* REG: 0xee */
0094 #define RK3228_TMDS_CH_TA_ENABLE            GENMASK(7, 4)
0095 /* REG: 0xef */
0096 #define RK3228_TMDS_CLK_CH_TA(x)            UPDATE(x, 7, 6)
0097 #define RK3228_TMDS_DATA_CH2_TA(x)          UPDATE(x, 5, 4)
0098 #define RK3228_TMDS_DATA_CH1_TA(x)          UPDATE(x, 3, 2)
0099 #define RK3228_TMDS_DATA_CH0_TA(x)          UPDATE(x, 1, 0)
0100 /* REG: 0xf0 */
0101 #define RK3228_TMDS_DATA_CH2_PRE_EMPHASIS_MASK      GENMASK(5, 4)
0102 #define RK3228_TMDS_DATA_CH2_PRE_EMPHASIS(x)        UPDATE(x, 5, 4)
0103 #define RK3228_TMDS_DATA_CH1_PRE_EMPHASIS_MASK      GENMASK(3, 2)
0104 #define RK3228_TMDS_DATA_CH1_PRE_EMPHASIS(x)        UPDATE(x, 3, 2)
0105 #define RK3228_TMDS_DATA_CH0_PRE_EMPHASIS_MASK      GENMASK(1, 0)
0106 #define RK3228_TMDS_DATA_CH0_PRE_EMPHASIS(x)        UPDATE(x, 1, 0)
0107 /* REG: 0xf1 */
0108 #define RK3228_TMDS_CLK_CH_OUTPUT_SWING(x)      UPDATE(x, 7, 4)
0109 #define RK3228_TMDS_DATA_CH2_OUTPUT_SWING(x)        UPDATE(x, 3, 0)
0110 /* REG: 0xf2 */
0111 #define RK3228_TMDS_DATA_CH1_OUTPUT_SWING(x)        UPDATE(x, 7, 4)
0112 #define RK3228_TMDS_DATA_CH0_OUTPUT_SWING(x)        UPDATE(x, 3, 0)
0113 
0114 /* REG: 0x01 */
0115 #define RK3328_BYPASS_RXSENSE_EN            BIT(2)
0116 #define RK3328_BYPASS_POWERON_EN            BIT(1)
0117 #define RK3328_BYPASS_PLLPD_EN              BIT(0)
0118 /* REG: 0x02 */
0119 #define RK3328_INT_POL_HIGH             BIT(7)
0120 #define RK3328_BYPASS_PDATA_EN              BIT(4)
0121 #define RK3328_PDATA_EN                 BIT(0)
0122 /* REG:0x05 */
0123 #define RK3328_INT_TMDS_CLK(x)              UPDATE(x, 7, 4)
0124 #define RK3328_INT_TMDS_D2(x)               UPDATE(x, 3, 0)
0125 /* REG:0x07 */
0126 #define RK3328_INT_TMDS_D1(x)               UPDATE(x, 7, 4)
0127 #define RK3328_INT_TMDS_D0(x)               UPDATE(x, 3, 0)
0128 /* for all RK3328_INT_TMDS_*, ESD_DET as defined in 0xc8-0xcb */
0129 #define RK3328_INT_AGND_LOW_PULSE_LOCKED        BIT(3)
0130 #define RK3328_INT_RXSENSE_LOW_PULSE_LOCKED     BIT(2)
0131 #define RK3328_INT_VSS_AGND_ESD_DET         BIT(1)
0132 #define RK3328_INT_AGND_VSS_ESD_DET         BIT(0)
0133 /* REG: 0xa0 */
0134 #define RK3328_PCLK_VCO_DIV_5_MASK          BIT(1)
0135 #define RK3328_PCLK_VCO_DIV_5(x)            UPDATE(x, 1, 1)
0136 #define RK3328_PRE_PLL_POWER_DOWN           BIT(0)
0137 /* REG: 0xa1 */
0138 #define RK3328_PRE_PLL_PRE_DIV_MASK         GENMASK(5, 0)
0139 #define RK3328_PRE_PLL_PRE_DIV(x)           UPDATE(x, 5, 0)
0140 /* REG: 0xa2 */
0141 /* unset means center spread */
0142 #define RK3328_SPREAD_SPECTRUM_MOD_DOWN         BIT(7)
0143 #define RK3328_SPREAD_SPECTRUM_MOD_DISABLE      BIT(6)
0144 #define RK3328_PRE_PLL_FRAC_DIV_DISABLE         UPDATE(3, 5, 4)
0145 #define RK3328_PRE_PLL_FB_DIV_11_8_MASK         GENMASK(3, 0)
0146 #define RK3328_PRE_PLL_FB_DIV_11_8(x)           UPDATE((x) >> 8, 3, 0)
0147 /* REG: 0xa3 */
0148 #define RK3328_PRE_PLL_FB_DIV_7_0(x)            UPDATE(x, 7, 0)
0149 /* REG: 0xa4*/
0150 #define RK3328_PRE_PLL_TMDSCLK_DIV_C_MASK       GENMASK(1, 0)
0151 #define RK3328_PRE_PLL_TMDSCLK_DIV_C(x)         UPDATE(x, 1, 0)
0152 #define RK3328_PRE_PLL_TMDSCLK_DIV_B_MASK       GENMASK(3, 2)
0153 #define RK3328_PRE_PLL_TMDSCLK_DIV_B(x)         UPDATE(x, 3, 2)
0154 #define RK3328_PRE_PLL_TMDSCLK_DIV_A_MASK       GENMASK(5, 4)
0155 #define RK3328_PRE_PLL_TMDSCLK_DIV_A(x)         UPDATE(x, 5, 4)
0156 /* REG: 0xa5 */
0157 #define RK3328_PRE_PLL_PCLK_DIV_B_SHIFT         5
0158 #define RK3328_PRE_PLL_PCLK_DIV_B_MASK          GENMASK(6, 5)
0159 #define RK3328_PRE_PLL_PCLK_DIV_B(x)            UPDATE(x, 6, 5)
0160 #define RK3328_PRE_PLL_PCLK_DIV_A_MASK          GENMASK(4, 0)
0161 #define RK3328_PRE_PLL_PCLK_DIV_A(x)            UPDATE(x, 4, 0)
0162 /* REG: 0xa6 */
0163 #define RK3328_PRE_PLL_PCLK_DIV_C_SHIFT         5
0164 #define RK3328_PRE_PLL_PCLK_DIV_C_MASK          GENMASK(6, 5)
0165 #define RK3328_PRE_PLL_PCLK_DIV_C(x)            UPDATE(x, 6, 5)
0166 #define RK3328_PRE_PLL_PCLK_DIV_D_MASK          GENMASK(4, 0)
0167 #define RK3328_PRE_PLL_PCLK_DIV_D(x)            UPDATE(x, 4, 0)
0168 /* REG: 0xa9 */
0169 #define RK3328_PRE_PLL_LOCK_STATUS          BIT(0)
0170 /* REG: 0xaa */
0171 #define RK3328_POST_PLL_POST_DIV_ENABLE         GENMASK(3, 2)
0172 #define RK3328_POST_PLL_REFCLK_SEL_TMDS         BIT(1)
0173 #define RK3328_POST_PLL_POWER_DOWN          BIT(0)
0174 /* REG:0xab */
0175 #define RK3328_POST_PLL_FB_DIV_8(x)         UPDATE((x) >> 8, 7, 7)
0176 #define RK3328_POST_PLL_PRE_DIV(x)          UPDATE(x, 4, 0)
0177 /* REG: 0xac */
0178 #define RK3328_POST_PLL_FB_DIV_7_0(x)           UPDATE(x, 7, 0)
0179 /* REG: 0xad */
0180 #define RK3328_POST_PLL_POST_DIV_MASK           GENMASK(1, 0)
0181 #define RK3328_POST_PLL_POST_DIV_2          0x0
0182 #define RK3328_POST_PLL_POST_DIV_4          0x1
0183 #define RK3328_POST_PLL_POST_DIV_8          0x3
0184 /* REG: 0xaf */
0185 #define RK3328_POST_PLL_LOCK_STATUS         BIT(0)
0186 /* REG: 0xb0 */
0187 #define RK3328_BANDGAP_ENABLE               BIT(2)
0188 /* REG: 0xb2 */
0189 #define RK3328_TMDS_CLK_DRIVER_EN           BIT(3)
0190 #define RK3328_TMDS_D2_DRIVER_EN            BIT(2)
0191 #define RK3328_TMDS_D1_DRIVER_EN            BIT(1)
0192 #define RK3328_TMDS_D0_DRIVER_EN            BIT(0)
0193 #define RK3328_TMDS_DRIVER_ENABLE       (RK3328_TMDS_CLK_DRIVER_EN | \
0194                         RK3328_TMDS_D2_DRIVER_EN | \
0195                         RK3328_TMDS_D1_DRIVER_EN | \
0196                         RK3328_TMDS_D0_DRIVER_EN)
0197 /* REG:0xc5 */
0198 #define RK3328_BYPASS_TERM_RESISTOR_CALIB       BIT(7)
0199 #define RK3328_TERM_RESISTOR_CALIB_SPEED_14_8(x)    UPDATE((x) >> 8, 6, 0)
0200 /* REG:0xc6 */
0201 #define RK3328_TERM_RESISTOR_CALIB_SPEED_7_0(x)     UPDATE(x, 7, 0)
0202 /* REG:0xc7 */
0203 #define RK3328_TERM_RESISTOR_50             UPDATE(0, 2, 1)
0204 #define RK3328_TERM_RESISTOR_62_5           UPDATE(1, 2, 1)
0205 #define RK3328_TERM_RESISTOR_75             UPDATE(2, 2, 1)
0206 #define RK3328_TERM_RESISTOR_100            UPDATE(3, 2, 1)
0207 /* REG 0xc8 - 0xcb */
0208 #define RK3328_ESD_DETECT_MASK              GENMASK(7, 6)
0209 #define RK3328_ESD_DETECT_340MV             (0x0 << 6)
0210 #define RK3328_ESD_DETECT_280MV             (0x1 << 6)
0211 #define RK3328_ESD_DETECT_260MV             (0x2 << 6)
0212 #define RK3328_ESD_DETECT_240MV             (0x3 << 6)
0213 /* resistors can be used in parallel */
0214 #define RK3328_TMDS_TERM_RESIST_MASK            GENMASK(5, 0)
0215 #define RK3328_TMDS_TERM_RESIST_75          BIT(5)
0216 #define RK3328_TMDS_TERM_RESIST_150         BIT(4)
0217 #define RK3328_TMDS_TERM_RESIST_300         BIT(3)
0218 #define RK3328_TMDS_TERM_RESIST_600         BIT(2)
0219 #define RK3328_TMDS_TERM_RESIST_1000            BIT(1)
0220 #define RK3328_TMDS_TERM_RESIST_2000            BIT(0)
0221 /* REG: 0xd1 */
0222 #define RK3328_PRE_PLL_FRAC_DIV_23_16(x)        UPDATE((x) >> 16, 7, 0)
0223 /* REG: 0xd2 */
0224 #define RK3328_PRE_PLL_FRAC_DIV_15_8(x)         UPDATE((x) >> 8, 7, 0)
0225 /* REG: 0xd3 */
0226 #define RK3328_PRE_PLL_FRAC_DIV_7_0(x)          UPDATE(x, 7, 0)
0227 
0228 struct inno_hdmi_phy_drv_data;
0229 
0230 struct inno_hdmi_phy {
0231     struct device *dev;
0232     struct regmap *regmap;
0233     int irq;
0234 
0235     struct phy *phy;
0236     struct clk *sysclk;
0237     struct clk *refoclk;
0238     struct clk *refpclk;
0239 
0240     /* platform data */
0241     const struct inno_hdmi_phy_drv_data *plat_data;
0242     int chip_version;
0243 
0244     /* clk provider */
0245     struct clk_hw hw;
0246     struct clk *phyclk;
0247     unsigned long pixclock;
0248 };
0249 
0250 struct pre_pll_config {
0251     unsigned long pixclock;
0252     unsigned long tmdsclock;
0253     u8 prediv;
0254     u16 fbdiv;
0255     u8 tmds_div_a;
0256     u8 tmds_div_b;
0257     u8 tmds_div_c;
0258     u8 pclk_div_a;
0259     u8 pclk_div_b;
0260     u8 pclk_div_c;
0261     u8 pclk_div_d;
0262     u8 vco_div_5_en;
0263     u32 fracdiv;
0264 };
0265 
0266 struct post_pll_config {
0267     unsigned long tmdsclock;
0268     u8 prediv;
0269     u16 fbdiv;
0270     u8 postdiv;
0271     u8 version;
0272 };
0273 
0274 struct phy_config {
0275     unsigned long   tmdsclock;
0276     u8      regs[14];
0277 };
0278 
0279 struct inno_hdmi_phy_ops {
0280     int (*init)(struct inno_hdmi_phy *inno);
0281     int (*power_on)(struct inno_hdmi_phy *inno,
0282             const struct post_pll_config *cfg,
0283             const struct phy_config *phy_cfg);
0284     void (*power_off)(struct inno_hdmi_phy *inno);
0285 };
0286 
0287 struct inno_hdmi_phy_drv_data {
0288     const struct inno_hdmi_phy_ops  *ops;
0289     const struct clk_ops        *clk_ops;
0290     const struct phy_config     *phy_cfg_table;
0291 };
0292 
0293 static const struct pre_pll_config pre_pll_cfg_table[] = {
0294     { 27000000,  27000000, 1,  90, 3, 2, 2, 10, 3, 3, 4, 0, 0},
0295     { 27000000,  33750000, 1,  90, 1, 3, 3, 10, 3, 3, 4, 0, 0},
0296     { 40000000,  40000000, 1,  80, 2, 2, 2, 12, 2, 2, 2, 0, 0},
0297     { 59341000,  59341000, 1,  98, 3, 1, 2,  1, 3, 3, 4, 0, 0xE6AE6B},
0298     { 59400000,  59400000, 1,  99, 3, 1, 1,  1, 3, 3, 4, 0, 0},
0299     { 59341000,  74176250, 1,  98, 0, 3, 3,  1, 3, 3, 4, 0, 0xE6AE6B},
0300     { 59400000,  74250000, 1,  99, 1, 2, 2,  1, 3, 3, 4, 0, 0},
0301     { 74176000,  74176000, 1,  98, 1, 2, 2,  1, 2, 3, 4, 0, 0xE6AE6B},
0302     { 74250000,  74250000, 1,  99, 1, 2, 2,  1, 2, 3, 4, 0, 0},
0303     { 74176000,  92720000, 4, 494, 1, 2, 2,  1, 3, 3, 4, 0, 0x816817},
0304     { 74250000,  92812500, 4, 495, 1, 2, 2,  1, 3, 3, 4, 0, 0},
0305     {148352000, 148352000, 1,  98, 1, 1, 1,  1, 2, 2, 2, 0, 0xE6AE6B},
0306     {148500000, 148500000, 1,  99, 1, 1, 1,  1, 2, 2, 2, 0, 0},
0307     {148352000, 185440000, 4, 494, 0, 2, 2,  1, 3, 2, 2, 0, 0x816817},
0308     {148500000, 185625000, 4, 495, 0, 2, 2,  1, 3, 2, 2, 0, 0},
0309     {296703000, 296703000, 1,  98, 0, 1, 1,  1, 0, 2, 2, 0, 0xE6AE6B},
0310     {297000000, 297000000, 1,  99, 0, 1, 1,  1, 0, 2, 2, 0, 0},
0311     {296703000, 370878750, 4, 494, 1, 2, 0,  1, 3, 1, 1, 0, 0x816817},
0312     {297000000, 371250000, 4, 495, 1, 2, 0,  1, 3, 1, 1, 0, 0},
0313     {593407000, 296703500, 1,  98, 0, 1, 1,  1, 0, 2, 1, 0, 0xE6AE6B},
0314     {594000000, 297000000, 1,  99, 0, 1, 1,  1, 0, 2, 1, 0, 0},
0315     {593407000, 370879375, 4, 494, 1, 2, 0,  1, 3, 1, 1, 1, 0x816817},
0316     {594000000, 371250000, 4, 495, 1, 2, 0,  1, 3, 1, 1, 1, 0},
0317     {593407000, 593407000, 1,  98, 0, 2, 0,  1, 0, 1, 1, 0, 0xE6AE6B},
0318     {594000000, 594000000, 1,  99, 0, 2, 0,  1, 0, 1, 1, 0, 0},
0319     { /* sentinel */ }
0320 };
0321 
0322 static const struct post_pll_config post_pll_cfg_table[] = {
0323     {33750000,  1, 40, 8, 1},
0324     {33750000,  1, 80, 8, 2},
0325     {74250000,  1, 40, 8, 1},
0326     {74250000, 18, 80, 8, 2},
0327     {148500000, 2, 40, 4, 3},
0328     {297000000, 4, 40, 2, 3},
0329     {594000000, 8, 40, 1, 3},
0330     { /* sentinel */ }
0331 };
0332 
0333 /* phy tuning values for an undocumented set of registers */
0334 static const struct phy_config rk3228_phy_cfg[] = {
0335     {   165000000, {
0336             0xaa, 0x00, 0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00,
0337             0x00, 0x00, 0x00, 0x00, 0x00,
0338         },
0339     }, {
0340         340000000, {
0341             0xaa, 0x15, 0x6a, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00,
0342             0x00, 0x00, 0x00, 0x00, 0x00,
0343         },
0344     }, {
0345         594000000, {
0346             0xaa, 0x15, 0x7a, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00,
0347             0x00, 0x00, 0x00, 0x00, 0x00,
0348         },
0349     }, { /* sentinel */ },
0350 };
0351 
0352 /* phy tuning values for an undocumented set of registers */
0353 static const struct phy_config rk3328_phy_cfg[] = {
0354     {   165000000, {
0355             0x07, 0x0a, 0x0a, 0x0a, 0x00, 0x00, 0x08, 0x08, 0x08,
0356             0x00, 0xac, 0xcc, 0xcc, 0xcc,
0357         },
0358     }, {
0359         340000000, {
0360             0x0b, 0x0d, 0x0d, 0x0d, 0x07, 0x15, 0x08, 0x08, 0x08,
0361             0x3f, 0xac, 0xcc, 0xcd, 0xdd,
0362         },
0363     }, {
0364         594000000, {
0365             0x10, 0x1a, 0x1a, 0x1a, 0x07, 0x15, 0x08, 0x08, 0x08,
0366             0x00, 0xac, 0xcc, 0xcc, 0xcc,
0367         },
0368     }, { /* sentinel */ },
0369 };
0370 
0371 static inline struct inno_hdmi_phy *to_inno_hdmi_phy(struct clk_hw *hw)
0372 {
0373     return container_of(hw, struct inno_hdmi_phy, hw);
0374 }
0375 
0376 /*
0377  * The register description of the IP block does not use any distinct names
0378  * but instead the databook simply numbers the registers in one-increments.
0379  * As the registers are obviously 32bit sized, the inno_* functions
0380  * translate the databook register names to the actual registers addresses.
0381  */
0382 static inline void inno_write(struct inno_hdmi_phy *inno, u32 reg, u8 val)
0383 {
0384     regmap_write(inno->regmap, reg * 4, val);
0385 }
0386 
0387 static inline u8 inno_read(struct inno_hdmi_phy *inno, u32 reg)
0388 {
0389     u32 val;
0390 
0391     regmap_read(inno->regmap, reg * 4, &val);
0392 
0393     return val;
0394 }
0395 
0396 static inline void inno_update_bits(struct inno_hdmi_phy *inno, u8 reg,
0397                     u8 mask, u8 val)
0398 {
0399     regmap_update_bits(inno->regmap, reg * 4, mask, val);
0400 }
0401 
0402 #define inno_poll(inno, reg, val, cond, sleep_us, timeout_us) \
0403     regmap_read_poll_timeout((inno)->regmap, (reg) * 4, val, cond, \
0404                  sleep_us, timeout_us)
0405 
0406 static unsigned long inno_hdmi_phy_get_tmdsclk(struct inno_hdmi_phy *inno,
0407                            unsigned long rate)
0408 {
0409     int bus_width = phy_get_bus_width(inno->phy);
0410 
0411     switch (bus_width) {
0412     case 4:
0413     case 5:
0414     case 6:
0415     case 10:
0416     case 12:
0417     case 16:
0418         return (u64)rate * bus_width / 8;
0419     default:
0420         return rate;
0421     }
0422 }
0423 
0424 static irqreturn_t inno_hdmi_phy_rk3328_hardirq(int irq, void *dev_id)
0425 {
0426     struct inno_hdmi_phy *inno = dev_id;
0427     int intr_stat1, intr_stat2, intr_stat3;
0428 
0429     intr_stat1 = inno_read(inno, 0x04);
0430     intr_stat2 = inno_read(inno, 0x06);
0431     intr_stat3 = inno_read(inno, 0x08);
0432 
0433     if (intr_stat1)
0434         inno_write(inno, 0x04, intr_stat1);
0435     if (intr_stat2)
0436         inno_write(inno, 0x06, intr_stat2);
0437     if (intr_stat3)
0438         inno_write(inno, 0x08, intr_stat3);
0439 
0440     if (intr_stat1 || intr_stat2 || intr_stat3)
0441         return IRQ_WAKE_THREAD;
0442 
0443     return IRQ_HANDLED;
0444 }
0445 
0446 static irqreturn_t inno_hdmi_phy_rk3328_irq(int irq, void *dev_id)
0447 {
0448     struct inno_hdmi_phy *inno = dev_id;
0449 
0450     inno_update_bits(inno, 0x02, RK3328_PDATA_EN, 0);
0451     usleep_range(10, 20);
0452     inno_update_bits(inno, 0x02, RK3328_PDATA_EN, RK3328_PDATA_EN);
0453 
0454     return IRQ_HANDLED;
0455 }
0456 
0457 static int inno_hdmi_phy_power_on(struct phy *phy)
0458 {
0459     struct inno_hdmi_phy *inno = phy_get_drvdata(phy);
0460     const struct post_pll_config *cfg = post_pll_cfg_table;
0461     const struct phy_config *phy_cfg = inno->plat_data->phy_cfg_table;
0462     unsigned long tmdsclock = inno_hdmi_phy_get_tmdsclk(inno,
0463                                 inno->pixclock);
0464     int ret;
0465 
0466     if (!tmdsclock) {
0467         dev_err(inno->dev, "TMDS clock is zero!\n");
0468         return -EINVAL;
0469     }
0470 
0471     if (!inno->plat_data->ops->power_on)
0472         return -EINVAL;
0473 
0474     for (; cfg->tmdsclock != 0; cfg++)
0475         if (tmdsclock <= cfg->tmdsclock &&
0476             cfg->version & inno->chip_version)
0477             break;
0478 
0479     for (; phy_cfg->tmdsclock != 0; phy_cfg++)
0480         if (tmdsclock <= phy_cfg->tmdsclock)
0481             break;
0482 
0483     if (cfg->tmdsclock == 0 || phy_cfg->tmdsclock == 0)
0484         return -EINVAL;
0485 
0486     dev_dbg(inno->dev, "Inno HDMI PHY Power On\n");
0487 
0488     ret = clk_prepare_enable(inno->phyclk);
0489     if (ret)
0490         return ret;
0491 
0492     ret = inno->plat_data->ops->power_on(inno, cfg, phy_cfg);
0493     if (ret) {
0494         clk_disable_unprepare(inno->phyclk);
0495         return ret;
0496     }
0497 
0498     return 0;
0499 }
0500 
0501 static int inno_hdmi_phy_power_off(struct phy *phy)
0502 {
0503     struct inno_hdmi_phy *inno = phy_get_drvdata(phy);
0504 
0505     if (!inno->plat_data->ops->power_off)
0506         return -EINVAL;
0507 
0508     inno->plat_data->ops->power_off(inno);
0509 
0510     clk_disable_unprepare(inno->phyclk);
0511 
0512     dev_dbg(inno->dev, "Inno HDMI PHY Power Off\n");
0513 
0514     return 0;
0515 }
0516 
0517 static const struct phy_ops inno_hdmi_phy_ops = {
0518     .owner = THIS_MODULE,
0519     .power_on = inno_hdmi_phy_power_on,
0520     .power_off = inno_hdmi_phy_power_off,
0521 };
0522 
0523 static const
0524 struct pre_pll_config *inno_hdmi_phy_get_pre_pll_cfg(struct inno_hdmi_phy *inno,
0525                              unsigned long rate)
0526 {
0527     const struct pre_pll_config *cfg = pre_pll_cfg_table;
0528     unsigned long tmdsclock = inno_hdmi_phy_get_tmdsclk(inno, rate);
0529 
0530     for (; cfg->pixclock != 0; cfg++)
0531         if (cfg->pixclock == rate && cfg->tmdsclock == tmdsclock)
0532             break;
0533 
0534     if (cfg->pixclock == 0)
0535         return ERR_PTR(-EINVAL);
0536 
0537     return cfg;
0538 }
0539 
0540 static int inno_hdmi_phy_rk3228_clk_is_prepared(struct clk_hw *hw)
0541 {
0542     struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
0543     u8 status;
0544 
0545     status = inno_read(inno, 0xe0) & RK3228_PRE_PLL_POWER_DOWN;
0546     return status ? 0 : 1;
0547 }
0548 
0549 static int inno_hdmi_phy_rk3228_clk_prepare(struct clk_hw *hw)
0550 {
0551     struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
0552 
0553     inno_update_bits(inno, 0xe0, RK3228_PRE_PLL_POWER_DOWN, 0);
0554     return 0;
0555 }
0556 
0557 static void inno_hdmi_phy_rk3228_clk_unprepare(struct clk_hw *hw)
0558 {
0559     struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
0560 
0561     inno_update_bits(inno, 0xe0, RK3228_PRE_PLL_POWER_DOWN,
0562              RK3228_PRE_PLL_POWER_DOWN);
0563 }
0564 
0565 static
0566 unsigned long inno_hdmi_phy_rk3228_clk_recalc_rate(struct clk_hw *hw,
0567                            unsigned long parent_rate)
0568 {
0569     struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
0570     u8 nd, no_a, no_b, no_d;
0571     u64 vco;
0572     u16 nf;
0573 
0574     nd = inno_read(inno, 0xe2) & RK3228_PRE_PLL_PRE_DIV_MASK;
0575     nf = (inno_read(inno, 0xe2) & RK3228_PRE_PLL_FB_DIV_8_MASK) << 1;
0576     nf |= inno_read(inno, 0xe3);
0577     vco = parent_rate * nf;
0578 
0579     if (inno_read(inno, 0xe2) & RK3228_PCLK_VCO_DIV_5_MASK) {
0580         do_div(vco, nd * 5);
0581     } else {
0582         no_a = inno_read(inno, 0xe4) & RK3228_PRE_PLL_PCLK_DIV_A_MASK;
0583         if (!no_a)
0584             no_a = 1;
0585         no_b = inno_read(inno, 0xe4) & RK3228_PRE_PLL_PCLK_DIV_B_MASK;
0586         no_b >>= RK3228_PRE_PLL_PCLK_DIV_B_SHIFT;
0587         no_b += 2;
0588         no_d = inno_read(inno, 0xe5) & RK3228_PRE_PLL_PCLK_DIV_D_MASK;
0589 
0590         do_div(vco, (nd * (no_a == 1 ? no_b : no_a) * no_d * 2));
0591     }
0592 
0593     inno->pixclock = vco;
0594 
0595     dev_dbg(inno->dev, "%s rate %lu\n", __func__, inno->pixclock);
0596 
0597     return vco;
0598 }
0599 
0600 static long inno_hdmi_phy_rk3228_clk_round_rate(struct clk_hw *hw,
0601                         unsigned long rate,
0602                         unsigned long *parent_rate)
0603 {
0604     const struct pre_pll_config *cfg = pre_pll_cfg_table;
0605 
0606     rate = (rate / 1000) * 1000;
0607 
0608     for (; cfg->pixclock != 0; cfg++)
0609         if (cfg->pixclock == rate && !cfg->fracdiv)
0610             break;
0611 
0612     if (cfg->pixclock == 0)
0613         return -EINVAL;
0614 
0615     return cfg->pixclock;
0616 }
0617 
0618 static int inno_hdmi_phy_rk3228_clk_set_rate(struct clk_hw *hw,
0619                          unsigned long rate,
0620                          unsigned long parent_rate)
0621 {
0622     struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
0623     const struct pre_pll_config *cfg;
0624     unsigned long tmdsclock = inno_hdmi_phy_get_tmdsclk(inno, rate);
0625     u32 v;
0626     int ret;
0627 
0628     dev_dbg(inno->dev, "%s rate %lu tmdsclk %lu\n",
0629         __func__, rate, tmdsclock);
0630 
0631     cfg = inno_hdmi_phy_get_pre_pll_cfg(inno, rate);
0632     if (IS_ERR(cfg))
0633         return PTR_ERR(cfg);
0634 
0635     /* Power down PRE-PLL */
0636     inno_update_bits(inno, 0xe0, RK3228_PRE_PLL_POWER_DOWN,
0637              RK3228_PRE_PLL_POWER_DOWN);
0638 
0639     inno_update_bits(inno, 0xe2, RK3228_PRE_PLL_FB_DIV_8_MASK |
0640              RK3228_PCLK_VCO_DIV_5_MASK |
0641              RK3228_PRE_PLL_PRE_DIV_MASK,
0642              RK3228_PRE_PLL_FB_DIV_8(cfg->fbdiv) |
0643              RK3228_PCLK_VCO_DIV_5(cfg->vco_div_5_en) |
0644              RK3228_PRE_PLL_PRE_DIV(cfg->prediv));
0645     inno_write(inno, 0xe3, RK3228_PRE_PLL_FB_DIV_7_0(cfg->fbdiv));
0646     inno_update_bits(inno, 0xe4, RK3228_PRE_PLL_PCLK_DIV_B_MASK |
0647              RK3228_PRE_PLL_PCLK_DIV_A_MASK,
0648              RK3228_PRE_PLL_PCLK_DIV_B(cfg->pclk_div_b) |
0649              RK3228_PRE_PLL_PCLK_DIV_A(cfg->pclk_div_a));
0650     inno_update_bits(inno, 0xe5, RK3228_PRE_PLL_PCLK_DIV_C_MASK |
0651              RK3228_PRE_PLL_PCLK_DIV_D_MASK,
0652              RK3228_PRE_PLL_PCLK_DIV_C(cfg->pclk_div_c) |
0653              RK3228_PRE_PLL_PCLK_DIV_D(cfg->pclk_div_d));
0654     inno_update_bits(inno, 0xe6, RK3228_PRE_PLL_TMDSCLK_DIV_C_MASK |
0655              RK3228_PRE_PLL_TMDSCLK_DIV_A_MASK |
0656              RK3228_PRE_PLL_TMDSCLK_DIV_B_MASK,
0657              RK3228_PRE_PLL_TMDSCLK_DIV_C(cfg->tmds_div_c) |
0658              RK3228_PRE_PLL_TMDSCLK_DIV_A(cfg->tmds_div_a) |
0659              RK3228_PRE_PLL_TMDSCLK_DIV_B(cfg->tmds_div_b));
0660 
0661     /* Power up PRE-PLL */
0662     inno_update_bits(inno, 0xe0, RK3228_PRE_PLL_POWER_DOWN, 0);
0663 
0664     /* Wait for Pre-PLL lock */
0665     ret = inno_poll(inno, 0xe8, v, v & RK3228_PRE_PLL_LOCK_STATUS,
0666             100, 100000);
0667     if (ret) {
0668         dev_err(inno->dev, "Pre-PLL locking failed\n");
0669         return ret;
0670     }
0671 
0672     inno->pixclock = rate;
0673 
0674     return 0;
0675 }
0676 
0677 static const struct clk_ops inno_hdmi_phy_rk3228_clk_ops = {
0678     .prepare = inno_hdmi_phy_rk3228_clk_prepare,
0679     .unprepare = inno_hdmi_phy_rk3228_clk_unprepare,
0680     .is_prepared = inno_hdmi_phy_rk3228_clk_is_prepared,
0681     .recalc_rate = inno_hdmi_phy_rk3228_clk_recalc_rate,
0682     .round_rate = inno_hdmi_phy_rk3228_clk_round_rate,
0683     .set_rate = inno_hdmi_phy_rk3228_clk_set_rate,
0684 };
0685 
0686 static int inno_hdmi_phy_rk3328_clk_is_prepared(struct clk_hw *hw)
0687 {
0688     struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
0689     u8 status;
0690 
0691     status = inno_read(inno, 0xa0) & RK3328_PRE_PLL_POWER_DOWN;
0692     return status ? 0 : 1;
0693 }
0694 
0695 static int inno_hdmi_phy_rk3328_clk_prepare(struct clk_hw *hw)
0696 {
0697     struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
0698 
0699     inno_update_bits(inno, 0xa0, RK3328_PRE_PLL_POWER_DOWN, 0);
0700     return 0;
0701 }
0702 
0703 static void inno_hdmi_phy_rk3328_clk_unprepare(struct clk_hw *hw)
0704 {
0705     struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
0706 
0707     inno_update_bits(inno, 0xa0, RK3328_PRE_PLL_POWER_DOWN,
0708              RK3328_PRE_PLL_POWER_DOWN);
0709 }
0710 
0711 static
0712 unsigned long inno_hdmi_phy_rk3328_clk_recalc_rate(struct clk_hw *hw,
0713                            unsigned long parent_rate)
0714 {
0715     struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
0716     unsigned long frac;
0717     u8 nd, no_a, no_b, no_c, no_d;
0718     u64 vco;
0719     u16 nf;
0720 
0721     nd = inno_read(inno, 0xa1) & RK3328_PRE_PLL_PRE_DIV_MASK;
0722     nf = ((inno_read(inno, 0xa2) & RK3328_PRE_PLL_FB_DIV_11_8_MASK) << 8);
0723     nf |= inno_read(inno, 0xa3);
0724     vco = parent_rate * nf;
0725 
0726     if (!(inno_read(inno, 0xa2) & RK3328_PRE_PLL_FRAC_DIV_DISABLE)) {
0727         frac = inno_read(inno, 0xd3) |
0728                (inno_read(inno, 0xd2) << 8) |
0729                (inno_read(inno, 0xd1) << 16);
0730         vco += DIV_ROUND_CLOSEST(parent_rate * frac, (1 << 24));
0731     }
0732 
0733     if (inno_read(inno, 0xa0) & RK3328_PCLK_VCO_DIV_5_MASK) {
0734         do_div(vco, nd * 5);
0735     } else {
0736         no_a = inno_read(inno, 0xa5) & RK3328_PRE_PLL_PCLK_DIV_A_MASK;
0737         no_b = inno_read(inno, 0xa5) & RK3328_PRE_PLL_PCLK_DIV_B_MASK;
0738         no_b >>= RK3328_PRE_PLL_PCLK_DIV_B_SHIFT;
0739         no_b += 2;
0740         no_c = inno_read(inno, 0xa6) & RK3328_PRE_PLL_PCLK_DIV_C_MASK;
0741         no_c >>= RK3328_PRE_PLL_PCLK_DIV_C_SHIFT;
0742         no_c = 1 << no_c;
0743         no_d = inno_read(inno, 0xa6) & RK3328_PRE_PLL_PCLK_DIV_D_MASK;
0744 
0745         do_div(vco, (nd * (no_a == 1 ? no_b : no_a) * no_d * 2));
0746     }
0747 
0748     inno->pixclock = vco;
0749     dev_dbg(inno->dev, "%s rate %lu\n", __func__, inno->pixclock);
0750 
0751     return vco;
0752 }
0753 
0754 static long inno_hdmi_phy_rk3328_clk_round_rate(struct clk_hw *hw,
0755                         unsigned long rate,
0756                         unsigned long *parent_rate)
0757 {
0758     const struct pre_pll_config *cfg = pre_pll_cfg_table;
0759 
0760     rate = (rate / 1000) * 1000;
0761 
0762     for (; cfg->pixclock != 0; cfg++)
0763         if (cfg->pixclock == rate)
0764             break;
0765 
0766     if (cfg->pixclock == 0)
0767         return -EINVAL;
0768 
0769     return cfg->pixclock;
0770 }
0771 
0772 static int inno_hdmi_phy_rk3328_clk_set_rate(struct clk_hw *hw,
0773                          unsigned long rate,
0774                          unsigned long parent_rate)
0775 {
0776     struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
0777     const struct pre_pll_config *cfg;
0778     unsigned long tmdsclock = inno_hdmi_phy_get_tmdsclk(inno, rate);
0779     u32 val;
0780     int ret;
0781 
0782     dev_dbg(inno->dev, "%s rate %lu tmdsclk %lu\n",
0783         __func__, rate, tmdsclock);
0784 
0785     cfg = inno_hdmi_phy_get_pre_pll_cfg(inno, rate);
0786     if (IS_ERR(cfg))
0787         return PTR_ERR(cfg);
0788 
0789     inno_update_bits(inno, 0xa0, RK3328_PRE_PLL_POWER_DOWN,
0790              RK3328_PRE_PLL_POWER_DOWN);
0791 
0792     /* Configure pre-pll */
0793     inno_update_bits(inno, 0xa0, RK3228_PCLK_VCO_DIV_5_MASK,
0794              RK3228_PCLK_VCO_DIV_5(cfg->vco_div_5_en));
0795     inno_write(inno, 0xa1, RK3328_PRE_PLL_PRE_DIV(cfg->prediv));
0796 
0797     val = RK3328_SPREAD_SPECTRUM_MOD_DISABLE;
0798     if (!cfg->fracdiv)
0799         val |= RK3328_PRE_PLL_FRAC_DIV_DISABLE;
0800     inno_write(inno, 0xa2, RK3328_PRE_PLL_FB_DIV_11_8(cfg->fbdiv) | val);
0801     inno_write(inno, 0xa3, RK3328_PRE_PLL_FB_DIV_7_0(cfg->fbdiv));
0802     inno_write(inno, 0xa5, RK3328_PRE_PLL_PCLK_DIV_A(cfg->pclk_div_a) |
0803            RK3328_PRE_PLL_PCLK_DIV_B(cfg->pclk_div_b));
0804     inno_write(inno, 0xa6, RK3328_PRE_PLL_PCLK_DIV_C(cfg->pclk_div_c) |
0805            RK3328_PRE_PLL_PCLK_DIV_D(cfg->pclk_div_d));
0806     inno_write(inno, 0xa4, RK3328_PRE_PLL_TMDSCLK_DIV_C(cfg->tmds_div_c) |
0807            RK3328_PRE_PLL_TMDSCLK_DIV_A(cfg->tmds_div_a) |
0808            RK3328_PRE_PLL_TMDSCLK_DIV_B(cfg->tmds_div_b));
0809     inno_write(inno, 0xd3, RK3328_PRE_PLL_FRAC_DIV_7_0(cfg->fracdiv));
0810     inno_write(inno, 0xd2, RK3328_PRE_PLL_FRAC_DIV_15_8(cfg->fracdiv));
0811     inno_write(inno, 0xd1, RK3328_PRE_PLL_FRAC_DIV_23_16(cfg->fracdiv));
0812 
0813     inno_update_bits(inno, 0xa0, RK3328_PRE_PLL_POWER_DOWN, 0);
0814 
0815     /* Wait for Pre-PLL lock */
0816     ret = inno_poll(inno, 0xa9, val, val & RK3328_PRE_PLL_LOCK_STATUS,
0817             1000, 10000);
0818     if (ret) {
0819         dev_err(inno->dev, "Pre-PLL locking failed\n");
0820         return ret;
0821     }
0822 
0823     inno->pixclock = rate;
0824 
0825     return 0;
0826 }
0827 
0828 static const struct clk_ops inno_hdmi_phy_rk3328_clk_ops = {
0829     .prepare = inno_hdmi_phy_rk3328_clk_prepare,
0830     .unprepare = inno_hdmi_phy_rk3328_clk_unprepare,
0831     .is_prepared = inno_hdmi_phy_rk3328_clk_is_prepared,
0832     .recalc_rate = inno_hdmi_phy_rk3328_clk_recalc_rate,
0833     .round_rate = inno_hdmi_phy_rk3328_clk_round_rate,
0834     .set_rate = inno_hdmi_phy_rk3328_clk_set_rate,
0835 };
0836 
0837 static int inno_hdmi_phy_clk_register(struct inno_hdmi_phy *inno)
0838 {
0839     struct device *dev = inno->dev;
0840     struct device_node *np = dev->of_node;
0841     struct clk_init_data init;
0842     const char *parent_name;
0843     int ret;
0844 
0845     parent_name = __clk_get_name(inno->refoclk);
0846 
0847     init.parent_names = &parent_name;
0848     init.num_parents = 1;
0849     init.flags = 0;
0850     init.name = "pin_hd20_pclk";
0851     init.ops = inno->plat_data->clk_ops;
0852 
0853     /* optional override of the clock name */
0854     of_property_read_string(np, "clock-output-names", &init.name);
0855 
0856     inno->hw.init = &init;
0857 
0858     inno->phyclk = devm_clk_register(dev, &inno->hw);
0859     if (IS_ERR(inno->phyclk)) {
0860         ret = PTR_ERR(inno->phyclk);
0861         dev_err(dev, "failed to register clock: %d\n", ret);
0862         return ret;
0863     }
0864 
0865     ret = of_clk_add_provider(np, of_clk_src_simple_get, inno->phyclk);
0866     if (ret) {
0867         dev_err(dev, "failed to register clock provider: %d\n", ret);
0868         return ret;
0869     }
0870 
0871     return 0;
0872 }
0873 
0874 static int inno_hdmi_phy_rk3228_init(struct inno_hdmi_phy *inno)
0875 {
0876     /*
0877      * Use phy internal register control
0878      * rxsense/poweron/pllpd/pdataen signal.
0879      */
0880     inno_write(inno, 0x01, RK3228_BYPASS_RXSENSE_EN |
0881            RK3228_BYPASS_PWRON_EN |
0882            RK3228_BYPASS_PLLPD_EN);
0883     inno_update_bits(inno, 0x02, RK3228_BYPASS_PDATA_EN,
0884              RK3228_BYPASS_PDATA_EN);
0885 
0886     /* manual power down post-PLL */
0887     inno_update_bits(inno, 0xaa, RK3228_POST_PLL_CTRL_MANUAL,
0888              RK3228_POST_PLL_CTRL_MANUAL);
0889 
0890     inno->chip_version = 1;
0891 
0892     return 0;
0893 }
0894 
0895 static int
0896 inno_hdmi_phy_rk3228_power_on(struct inno_hdmi_phy *inno,
0897                   const struct post_pll_config *cfg,
0898                   const struct phy_config *phy_cfg)
0899 {
0900     int ret;
0901     u32 v;
0902 
0903     inno_update_bits(inno, 0x02, RK3228_PDATAEN_DISABLE,
0904              RK3228_PDATAEN_DISABLE);
0905     inno_update_bits(inno, 0xe0, RK3228_PRE_PLL_POWER_DOWN |
0906              RK3228_POST_PLL_POWER_DOWN,
0907              RK3228_PRE_PLL_POWER_DOWN |
0908              RK3228_POST_PLL_POWER_DOWN);
0909 
0910     /* Post-PLL update */
0911     inno_update_bits(inno, 0xe9, RK3228_POST_PLL_PRE_DIV_MASK,
0912              RK3228_POST_PLL_PRE_DIV(cfg->prediv));
0913     inno_update_bits(inno, 0xeb, RK3228_POST_PLL_FB_DIV_8_MASK,
0914              RK3228_POST_PLL_FB_DIV_8(cfg->fbdiv));
0915     inno_write(inno, 0xea, RK3228_POST_PLL_FB_DIV_7_0(cfg->fbdiv));
0916 
0917     if (cfg->postdiv == 1) {
0918         inno_update_bits(inno, 0xe9, RK3228_POST_PLL_POST_DIV_ENABLE,
0919                  0);
0920     } else {
0921         int div = cfg->postdiv / 2 - 1;
0922 
0923         inno_update_bits(inno, 0xe9, RK3228_POST_PLL_POST_DIV_ENABLE,
0924                  RK3228_POST_PLL_POST_DIV_ENABLE);
0925         inno_update_bits(inno, 0xeb, RK3228_POST_PLL_POST_DIV_MASK,
0926                  RK3228_POST_PLL_POST_DIV(div));
0927     }
0928 
0929     for (v = 0; v < 4; v++)
0930         inno_write(inno, 0xef + v, phy_cfg->regs[v]);
0931 
0932     inno_update_bits(inno, 0xe0, RK3228_PRE_PLL_POWER_DOWN |
0933              RK3228_POST_PLL_POWER_DOWN, 0);
0934     inno_update_bits(inno, 0xe1, RK3228_BANDGAP_ENABLE,
0935              RK3228_BANDGAP_ENABLE);
0936     inno_update_bits(inno, 0xe1, RK3228_TMDS_DRIVER_ENABLE,
0937              RK3228_TMDS_DRIVER_ENABLE);
0938 
0939     /* Wait for post PLL lock */
0940     ret = inno_poll(inno, 0xeb, v, v & RK3228_POST_PLL_LOCK_STATUS,
0941             100, 100000);
0942     if (ret) {
0943         dev_err(inno->dev, "Post-PLL locking failed\n");
0944         return ret;
0945     }
0946 
0947     if (cfg->tmdsclock > 340000000)
0948         msleep(100);
0949 
0950     inno_update_bits(inno, 0x02, RK3228_PDATAEN_DISABLE, 0);
0951     return 0;
0952 }
0953 
0954 static void inno_hdmi_phy_rk3228_power_off(struct inno_hdmi_phy *inno)
0955 {
0956     inno_update_bits(inno, 0xe1, RK3228_TMDS_DRIVER_ENABLE, 0);
0957     inno_update_bits(inno, 0xe1, RK3228_BANDGAP_ENABLE, 0);
0958     inno_update_bits(inno, 0xe0, RK3228_POST_PLL_POWER_DOWN,
0959              RK3228_POST_PLL_POWER_DOWN);
0960 }
0961 
0962 static const struct inno_hdmi_phy_ops rk3228_hdmi_phy_ops = {
0963     .init = inno_hdmi_phy_rk3228_init,
0964     .power_on = inno_hdmi_phy_rk3228_power_on,
0965     .power_off = inno_hdmi_phy_rk3228_power_off,
0966 };
0967 
0968 static int inno_hdmi_phy_rk3328_init(struct inno_hdmi_phy *inno)
0969 {
0970     struct nvmem_cell *cell;
0971     unsigned char *efuse_buf;
0972     size_t len;
0973 
0974     /*
0975      * Use phy internal register control
0976      * rxsense/poweron/pllpd/pdataen signal.
0977      */
0978     inno_write(inno, 0x01, RK3328_BYPASS_RXSENSE_EN |
0979            RK3328_BYPASS_POWERON_EN |
0980            RK3328_BYPASS_PLLPD_EN);
0981     inno_write(inno, 0x02, RK3328_INT_POL_HIGH | RK3328_BYPASS_PDATA_EN |
0982            RK3328_PDATA_EN);
0983 
0984     /* Disable phy irq */
0985     inno_write(inno, 0x05, 0);
0986     inno_write(inno, 0x07, 0);
0987 
0988     /* try to read the chip-version */
0989     inno->chip_version = 1;
0990     cell = nvmem_cell_get(inno->dev, "cpu-version");
0991     if (IS_ERR(cell)) {
0992         if (PTR_ERR(cell) == -EPROBE_DEFER)
0993             return -EPROBE_DEFER;
0994 
0995         return 0;
0996     }
0997 
0998     efuse_buf = nvmem_cell_read(cell, &len);
0999     nvmem_cell_put(cell);
1000 
1001     if (IS_ERR(efuse_buf))
1002         return 0;
1003     if (len == 1)
1004         inno->chip_version = efuse_buf[0] + 1;
1005     kfree(efuse_buf);
1006 
1007     return 0;
1008 }
1009 
1010 static int
1011 inno_hdmi_phy_rk3328_power_on(struct inno_hdmi_phy *inno,
1012                   const struct post_pll_config *cfg,
1013                   const struct phy_config *phy_cfg)
1014 {
1015     int ret;
1016     u32 v;
1017 
1018     inno_update_bits(inno, 0x02, RK3328_PDATA_EN, 0);
1019     inno_update_bits(inno, 0xaa, RK3328_POST_PLL_POWER_DOWN,
1020              RK3328_POST_PLL_POWER_DOWN);
1021 
1022     inno_write(inno, 0xac, RK3328_POST_PLL_FB_DIV_7_0(cfg->fbdiv));
1023     if (cfg->postdiv == 1) {
1024         inno_write(inno, 0xaa, RK3328_POST_PLL_REFCLK_SEL_TMDS);
1025         inno_write(inno, 0xab, RK3328_POST_PLL_FB_DIV_8(cfg->fbdiv) |
1026                RK3328_POST_PLL_PRE_DIV(cfg->prediv));
1027     } else {
1028         v = (cfg->postdiv / 2) - 1;
1029         v &= RK3328_POST_PLL_POST_DIV_MASK;
1030         inno_write(inno, 0xad, v);
1031         inno_write(inno, 0xab, RK3328_POST_PLL_FB_DIV_8(cfg->fbdiv) |
1032                RK3328_POST_PLL_PRE_DIV(cfg->prediv));
1033         inno_write(inno, 0xaa, RK3328_POST_PLL_POST_DIV_ENABLE |
1034                RK3328_POST_PLL_REFCLK_SEL_TMDS);
1035     }
1036 
1037     for (v = 0; v < 14; v++)
1038         inno_write(inno, 0xb5 + v, phy_cfg->regs[v]);
1039 
1040     /* set ESD detection threshold for TMDS CLK, D2, D1 and D0 */
1041     for (v = 0; v < 4; v++)
1042         inno_update_bits(inno, 0xc8 + v, RK3328_ESD_DETECT_MASK,
1043                  RK3328_ESD_DETECT_340MV);
1044 
1045     if (phy_cfg->tmdsclock > 340000000) {
1046         /* Set termination resistor to 100ohm */
1047         v = clk_get_rate(inno->sysclk) / 100000;
1048         inno_write(inno, 0xc5, RK3328_TERM_RESISTOR_CALIB_SPEED_14_8(v)
1049                | RK3328_BYPASS_TERM_RESISTOR_CALIB);
1050         inno_write(inno, 0xc6, RK3328_TERM_RESISTOR_CALIB_SPEED_7_0(v));
1051         inno_write(inno, 0xc7, RK3328_TERM_RESISTOR_100);
1052         inno_update_bits(inno, 0xc5,
1053                  RK3328_BYPASS_TERM_RESISTOR_CALIB, 0);
1054     } else {
1055         inno_write(inno, 0xc5, RK3328_BYPASS_TERM_RESISTOR_CALIB);
1056 
1057         /* clk termination resistor is 50ohm (parallel resistors) */
1058         if (phy_cfg->tmdsclock > 165000000)
1059             inno_update_bits(inno, 0xc8,
1060                      RK3328_TMDS_TERM_RESIST_MASK,
1061                      RK3328_TMDS_TERM_RESIST_75 |
1062                      RK3328_TMDS_TERM_RESIST_150);
1063 
1064         /* data termination resistor for D2, D1 and D0 is 150ohm */
1065         for (v = 0; v < 3; v++)
1066             inno_update_bits(inno, 0xc9 + v,
1067                      RK3328_TMDS_TERM_RESIST_MASK,
1068                      RK3328_TMDS_TERM_RESIST_150);
1069     }
1070 
1071     inno_update_bits(inno, 0xaa, RK3328_POST_PLL_POWER_DOWN, 0);
1072     inno_update_bits(inno, 0xb0, RK3328_BANDGAP_ENABLE,
1073              RK3328_BANDGAP_ENABLE);
1074     inno_update_bits(inno, 0xb2, RK3328_TMDS_DRIVER_ENABLE,
1075              RK3328_TMDS_DRIVER_ENABLE);
1076 
1077     /* Wait for post PLL lock */
1078     ret = inno_poll(inno, 0xaf, v, v & RK3328_POST_PLL_LOCK_STATUS,
1079             1000, 10000);
1080     if (ret) {
1081         dev_err(inno->dev, "Post-PLL locking failed\n");
1082         return ret;
1083     }
1084 
1085     if (phy_cfg->tmdsclock > 340000000)
1086         msleep(100);
1087 
1088     inno_update_bits(inno, 0x02, RK3328_PDATA_EN, RK3328_PDATA_EN);
1089 
1090     /* Enable PHY IRQ */
1091     inno_write(inno, 0x05, RK3328_INT_TMDS_CLK(RK3328_INT_VSS_AGND_ESD_DET)
1092            | RK3328_INT_TMDS_D2(RK3328_INT_VSS_AGND_ESD_DET));
1093     inno_write(inno, 0x07, RK3328_INT_TMDS_D1(RK3328_INT_VSS_AGND_ESD_DET)
1094            | RK3328_INT_TMDS_D0(RK3328_INT_VSS_AGND_ESD_DET));
1095     return 0;
1096 }
1097 
1098 static void inno_hdmi_phy_rk3328_power_off(struct inno_hdmi_phy *inno)
1099 {
1100     inno_update_bits(inno, 0xb2, RK3328_TMDS_DRIVER_ENABLE, 0);
1101     inno_update_bits(inno, 0xb0, RK3328_BANDGAP_ENABLE, 0);
1102     inno_update_bits(inno, 0xaa, RK3328_POST_PLL_POWER_DOWN,
1103              RK3328_POST_PLL_POWER_DOWN);
1104 
1105     /* Disable PHY IRQ */
1106     inno_write(inno, 0x05, 0);
1107     inno_write(inno, 0x07, 0);
1108 }
1109 
1110 static const struct inno_hdmi_phy_ops rk3328_hdmi_phy_ops = {
1111     .init = inno_hdmi_phy_rk3328_init,
1112     .power_on = inno_hdmi_phy_rk3328_power_on,
1113     .power_off = inno_hdmi_phy_rk3328_power_off,
1114 };
1115 
1116 static const struct inno_hdmi_phy_drv_data rk3228_hdmi_phy_drv_data = {
1117     .ops = &rk3228_hdmi_phy_ops,
1118     .clk_ops = &inno_hdmi_phy_rk3228_clk_ops,
1119     .phy_cfg_table = rk3228_phy_cfg,
1120 };
1121 
1122 static const struct inno_hdmi_phy_drv_data rk3328_hdmi_phy_drv_data = {
1123     .ops = &rk3328_hdmi_phy_ops,
1124     .clk_ops = &inno_hdmi_phy_rk3328_clk_ops,
1125     .phy_cfg_table = rk3328_phy_cfg,
1126 };
1127 
1128 static const struct regmap_config inno_hdmi_phy_regmap_config = {
1129     .reg_bits = 32,
1130     .val_bits = 32,
1131     .reg_stride = 4,
1132     .max_register = 0x400,
1133 };
1134 
1135 static void inno_hdmi_phy_action(void *data)
1136 {
1137     struct inno_hdmi_phy *inno = data;
1138 
1139     clk_disable_unprepare(inno->refpclk);
1140     clk_disable_unprepare(inno->sysclk);
1141 }
1142 
1143 static int inno_hdmi_phy_probe(struct platform_device *pdev)
1144 {
1145     struct inno_hdmi_phy *inno;
1146     struct phy_provider *phy_provider;
1147     void __iomem *regs;
1148     int ret;
1149 
1150     inno = devm_kzalloc(&pdev->dev, sizeof(*inno), GFP_KERNEL);
1151     if (!inno)
1152         return -ENOMEM;
1153 
1154     inno->dev = &pdev->dev;
1155 
1156     inno->plat_data = of_device_get_match_data(inno->dev);
1157     if (!inno->plat_data || !inno->plat_data->ops)
1158         return -EINVAL;
1159 
1160     regs = devm_platform_ioremap_resource(pdev, 0);
1161     if (IS_ERR(regs))
1162         return PTR_ERR(regs);
1163 
1164     inno->sysclk = devm_clk_get(inno->dev, "sysclk");
1165     if (IS_ERR(inno->sysclk)) {
1166         ret = PTR_ERR(inno->sysclk);
1167         dev_err(inno->dev, "failed to get sysclk: %d\n", ret);
1168         return ret;
1169     }
1170 
1171     inno->refpclk = devm_clk_get(inno->dev, "refpclk");
1172     if (IS_ERR(inno->refpclk)) {
1173         ret = PTR_ERR(inno->refpclk);
1174         dev_err(inno->dev, "failed to get ref clock: %d\n", ret);
1175         return ret;
1176     }
1177 
1178     inno->refoclk = devm_clk_get(inno->dev, "refoclk");
1179     if (IS_ERR(inno->refoclk)) {
1180         ret = PTR_ERR(inno->refoclk);
1181         dev_err(inno->dev, "failed to get oscillator-ref clock: %d\n",
1182             ret);
1183         return ret;
1184     }
1185 
1186     ret = clk_prepare_enable(inno->sysclk);
1187     if (ret) {
1188         dev_err(inno->dev, "Cannot enable inno phy sysclk: %d\n", ret);
1189         return ret;
1190     }
1191 
1192     /*
1193      * Refpclk needs to be on, on at least the rk3328 for still
1194      * unknown reasons.
1195      */
1196     ret = clk_prepare_enable(inno->refpclk);
1197     if (ret) {
1198         dev_err(inno->dev, "failed to enable refpclk\n");
1199         clk_disable_unprepare(inno->sysclk);
1200         return ret;
1201     }
1202 
1203     ret = devm_add_action_or_reset(inno->dev, inno_hdmi_phy_action,
1204                        inno);
1205     if (ret)
1206         return ret;
1207 
1208     inno->regmap = devm_regmap_init_mmio(inno->dev, regs,
1209                          &inno_hdmi_phy_regmap_config);
1210     if (IS_ERR(inno->regmap))
1211         return PTR_ERR(inno->regmap);
1212 
1213     /* only the newer rk3328 hdmiphy has an interrupt */
1214     inno->irq = platform_get_irq(pdev, 0);
1215     if (inno->irq > 0) {
1216         ret = devm_request_threaded_irq(inno->dev, inno->irq,
1217                         inno_hdmi_phy_rk3328_hardirq,
1218                         inno_hdmi_phy_rk3328_irq,
1219                         IRQF_SHARED,
1220                         dev_name(inno->dev), inno);
1221         if (ret)
1222             return ret;
1223     }
1224 
1225     inno->phy = devm_phy_create(inno->dev, NULL, &inno_hdmi_phy_ops);
1226     if (IS_ERR(inno->phy)) {
1227         dev_err(inno->dev, "failed to create HDMI PHY\n");
1228         return PTR_ERR(inno->phy);
1229     }
1230 
1231     phy_set_drvdata(inno->phy, inno);
1232     phy_set_bus_width(inno->phy, 8);
1233 
1234     if (inno->plat_data->ops->init) {
1235         ret = inno->plat_data->ops->init(inno);
1236         if (ret)
1237             return ret;
1238     }
1239 
1240     ret = inno_hdmi_phy_clk_register(inno);
1241     if (ret)
1242         return ret;
1243 
1244     phy_provider = devm_of_phy_provider_register(inno->dev,
1245                              of_phy_simple_xlate);
1246     return PTR_ERR_OR_ZERO(phy_provider);
1247 }
1248 
1249 static int inno_hdmi_phy_remove(struct platform_device *pdev)
1250 {
1251     of_clk_del_provider(pdev->dev.of_node);
1252 
1253     return 0;
1254 }
1255 
1256 static const struct of_device_id inno_hdmi_phy_of_match[] = {
1257     {
1258         .compatible = "rockchip,rk3228-hdmi-phy",
1259         .data = &rk3228_hdmi_phy_drv_data
1260     }, {
1261         .compatible = "rockchip,rk3328-hdmi-phy",
1262         .data = &rk3328_hdmi_phy_drv_data
1263     }, { /* sentinel */ }
1264 };
1265 MODULE_DEVICE_TABLE(of, inno_hdmi_phy_of_match);
1266 
1267 static struct platform_driver inno_hdmi_phy_driver = {
1268     .probe  = inno_hdmi_phy_probe,
1269     .remove = inno_hdmi_phy_remove,
1270     .driver = {
1271         .name = "inno-hdmi-phy",
1272         .of_match_table = inno_hdmi_phy_of_match,
1273     },
1274 };
1275 module_platform_driver(inno_hdmi_phy_driver);
1276 
1277 MODULE_AUTHOR("Zheng Yang <zhengyang@rock-chips.com>");
1278 MODULE_DESCRIPTION("Innosilion HDMI 2.0 Transmitter PHY Driver");
1279 MODULE_LICENSE("GPL v2");