Add Allwinner A733 DW-HDMI 2.0 controller and TOP PHY support.
Configure RESCAL calibration, the TOP PHY PLL, and the C422-0 PHY.

Signed-off-by: Alastair D'Silva <[email protected]>
---
 drivers/gpu/drm/sun4i/sun8i_dw_hdmi.c  | 114 +++++++--
 drivers/gpu/drm/sun4i/sun8i_dw_hdmi.h  |   7 +
 drivers/gpu/drm/sun4i/sun8i_hdmi_phy.c | 333 +++++++++++++++++++++++++
 3 files changed, 437 insertions(+), 17 deletions(-)

diff --git a/drivers/gpu/drm/sun4i/sun8i_dw_hdmi.c 
b/drivers/gpu/drm/sun4i/sun8i_dw_hdmi.c
index e554cd577af9..424659791198 100644
--- a/drivers/gpu/drm/sun4i/sun8i_dw_hdmi.c
+++ b/drivers/gpu/drm/sun4i/sun8i_dw_hdmi.c
@@ -7,6 +7,7 @@
 #include <linux/module.h>
 #include <linux/of.h>
 #include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
 
 #include <drm/drm_modeset_helper_vtables.h>
 #include <drm/drm_encoder.h>
@@ -133,56 +134,108 @@ static int sun8i_dw_hdmi_bind(struct device *dev, struct 
device *master,
        if (encoder->possible_crtcs == 0)
                return -EPROBE_DEFER;
 
-       hdmi->rst_ctrl = devm_reset_control_get(dev, "ctrl");
+       hdmi->rst_ctrl = devm_reset_control_array_get_exclusive(dev);
        if (IS_ERR(hdmi->rst_ctrl))
                return dev_err_probe(dev, PTR_ERR(hdmi->rst_ctrl),
-                                    "Could not get ctrl reset control\n");
+                                    "Could not get reset controls\n");
 
        hdmi->clk_tmds = devm_clk_get(dev, "tmds");
        if (IS_ERR(hdmi->clk_tmds))
                return dev_err_probe(dev, PTR_ERR(hdmi->clk_tmds),
                                     "Couldn't get the tmds clock\n");
 
-       hdmi->regulator = devm_regulator_get(dev, "hvcc");
-       if (IS_ERR(hdmi->regulator))
-               return dev_err_probe(dev, PTR_ERR(hdmi->regulator),
-                                    "Couldn't get regulator\n");
+       hdmi->clk_ahb = devm_clk_get_optional(dev, "ahb");
+       if (IS_ERR(hdmi->clk_ahb))
+               return dev_err_probe(dev, PTR_ERR(hdmi->clk_ahb),
+                                    "Couldn't get the ahb clock\n");
 
-       ret = regulator_enable(hdmi->regulator);
-       if (ret) {
-               dev_err(dev, "Failed to enable regulator\n");
-               return ret;
+       /*
+        * Enable iahb/isfr before sun8i_hdmi_phy_init(), as A733 TOP PHY
+        * registers are accessed before dw_hdmi_bind() enables them.
+        */
+       hdmi->clk_iahb = devm_clk_get(dev, "iahb");
+       if (IS_ERR(hdmi->clk_iahb))
+               return dev_err_probe(dev, PTR_ERR(hdmi->clk_iahb),
+                                    "Couldn't get the iahb clock\n");
+
+       hdmi->clk_isfr = devm_clk_get(dev, "isfr");
+       if (IS_ERR(hdmi->clk_isfr))
+               return dev_err_probe(dev, PTR_ERR(hdmi->clk_isfr),
+                                    "Couldn't get the isfr clock\n");
+
+       if (hdmi->quirks->optional_hvcc)
+               hdmi->regulator = devm_regulator_get_optional(dev, "hvcc");
+       else
+               hdmi->regulator = devm_regulator_get(dev, "hvcc");
+       if (IS_ERR(hdmi->regulator)) {
+               if (hdmi->quirks->optional_hvcc &&
+                   PTR_ERR(hdmi->regulator) == -ENODEV)
+                       hdmi->regulator = NULL;
+               else
+                       return dev_err_probe(dev, PTR_ERR(hdmi->regulator),
+                                            "Couldn't get regulator\n");
+       }
+
+       pm_runtime_enable(dev);
+       ret = pm_runtime_resume_and_get(dev);
+       if (ret < 0)
+               goto err_pm_disable;
+
+       if (hdmi->regulator) {
+               ret = regulator_enable(hdmi->regulator);
+               if (ret) {
+                       dev_err(dev, "Failed to enable regulator\n");
+                       goto err_pm_put;
+               }
        }
 
        ret = reset_control_deassert(hdmi->rst_ctrl);
        if (ret) {
-               dev_err(dev, "Could not deassert ctrl reset control\n");
+               dev_err(dev, "Could not deassert reset controls\n");
                goto err_disable_regulator;
        }
 
+       ret = clk_prepare_enable(hdmi->clk_ahb);
+       if (ret) {
+               dev_err(dev, "Could not enable ahb clock\n");
+               goto err_assert_ctrl_reset;
+       }
+
        ret = clk_prepare_enable(hdmi->clk_tmds);
        if (ret) {
                dev_err(dev, "Could not enable tmds clock\n");
-               goto err_assert_ctrl_reset;
+               goto err_disable_clk_ahb;
+       }
+
+       ret = clk_prepare_enable(hdmi->clk_iahb);
+       if (ret) {
+               dev_err(dev, "Could not enable iahb clock\n");
+               goto err_disable_clk_tmds;
+       }
+
+       ret = clk_prepare_enable(hdmi->clk_isfr);
+       if (ret) {
+               dev_err(dev, "Could not enable isfr clock\n");
+               goto err_disable_clk_iahb;
        }
 
        phy_node = of_parse_phandle(dev->of_node, "phys", 0);
        if (!phy_node) {
                dev_err(dev, "Can't found PHY phandle\n");
                ret = -EINVAL;
-               goto err_disable_clk_tmds;
+               goto err_disable_clk_isfr;
        }
 
        ret = sun8i_hdmi_phy_get(hdmi, phy_node);
        of_node_put(phy_node);
        if (ret) {
                dev_err(dev, "Couldn't get the HDMI PHY\n");
-               goto err_disable_clk_tmds;
+               goto err_disable_clk_isfr;
        }
 
        ret = sun8i_hdmi_phy_init(hdmi->phy);
        if (ret)
-               goto err_disable_clk_tmds;
+               goto err_disable_clk_isfr;
 
        drm_encoder_helper_add(encoder, &sun8i_dw_hdmi_encoder_helper_funcs);
        drm_encoder_init(drm, encoder, &sun8i_dw_hdmi_encoder_funcs,
@@ -210,12 +263,23 @@ static int sun8i_dw_hdmi_bind(struct device *dev, struct 
device *master,
 cleanup_encoder:
        drm_encoder_cleanup(encoder);
        sun8i_hdmi_phy_deinit(hdmi->phy);
+err_disable_clk_isfr:
+       clk_disable_unprepare(hdmi->clk_isfr);
+err_disable_clk_iahb:
+       clk_disable_unprepare(hdmi->clk_iahb);
 err_disable_clk_tmds:
        clk_disable_unprepare(hdmi->clk_tmds);
+err_disable_clk_ahb:
+       clk_disable_unprepare(hdmi->clk_ahb);
 err_assert_ctrl_reset:
        reset_control_assert(hdmi->rst_ctrl);
 err_disable_regulator:
-       regulator_disable(hdmi->regulator);
+       if (hdmi->regulator)
+               regulator_disable(hdmi->regulator);
+err_pm_put:
+       pm_runtime_put_sync(dev);
+err_pm_disable:
+       pm_runtime_disable(dev);
 
        return ret;
 }
@@ -227,9 +291,15 @@ static void sun8i_dw_hdmi_unbind(struct device *dev, 
struct device *master,
 
        dw_hdmi_unbind(hdmi->hdmi);
        sun8i_hdmi_phy_deinit(hdmi->phy);
+       clk_disable_unprepare(hdmi->clk_isfr);
+       clk_disable_unprepare(hdmi->clk_iahb);
        clk_disable_unprepare(hdmi->clk_tmds);
+       clk_disable_unprepare(hdmi->clk_ahb);
        reset_control_assert(hdmi->rst_ctrl);
-       regulator_disable(hdmi->regulator);
+       if (hdmi->regulator)
+               regulator_disable(hdmi->regulator);
+       pm_runtime_put_sync(dev);
+       pm_runtime_disable(dev);
 }
 
 static const struct component_ops sun8i_dw_hdmi_ops = {
@@ -256,6 +326,12 @@ static const struct sun8i_dw_hdmi_quirks sun50i_h6_quirks 
= {
        .use_drm_infoframe = true,
 };
 
+static const struct sun8i_dw_hdmi_quirks sun60i_a733_quirks = {
+       .mode_valid = sun8i_dw_hdmi_mode_valid_h6,
+       .use_drm_infoframe = true,
+       .optional_hvcc = true,
+};
+
 static const struct of_device_id sun8i_dw_hdmi_dt_ids[] = {
        {
                .compatible = "allwinner,sun8i-a83t-dw-hdmi",
@@ -265,6 +341,10 @@ static const struct of_device_id sun8i_dw_hdmi_dt_ids[] = {
                .compatible = "allwinner,sun50i-h6-dw-hdmi",
                .data = &sun50i_h6_quirks,
        },
+       {
+               .compatible = "allwinner,sun60i-a733-dw-hdmi",
+               .data = &sun60i_a733_quirks,
+       },
        { /* sentinel */ },
 };
 MODULE_DEVICE_TABLE(of, sun8i_dw_hdmi_dt_ids);
diff --git a/drivers/gpu/drm/sun4i/sun8i_dw_hdmi.h 
b/drivers/gpu/drm/sun4i/sun8i_dw_hdmi.h
index ab80d52a70bb..e15e8a59a63e 100644
--- a/drivers/gpu/drm/sun4i/sun8i_dw_hdmi.h
+++ b/drivers/gpu/drm/sun4i/sun8i_dw_hdmi.h
@@ -150,6 +150,7 @@ struct sun8i_hdmi_phy;
 struct sun8i_hdmi_phy_variant {
        bool has_phy_clk;
        bool has_second_pll;
+       bool is_top_phy;
        const struct dw_hdmi_curr_ctrl *cur_ctr;
        const struct dw_hdmi_mpll_config *mpll_cfg;
        const struct dw_hdmi_phy_config *phy_cfg;
@@ -163,9 +164,11 @@ struct sun8i_hdmi_phy {
        struct clk                      *clk_phy;
        struct clk                      *clk_pll0;
        struct clk                      *clk_pll1;
+       struct clk                      *clk_ref;
        struct device                   *dev;
        unsigned int                    rcal;
        struct regmap                   *regs;
+       struct regmap                   *rescal;
        struct reset_control            *rst_phy;
        const struct sun8i_hdmi_phy_variant *variant;
 };
@@ -175,10 +178,14 @@ struct sun8i_dw_hdmi_quirks {
                                           const struct drm_display_info *info,
                                           const struct drm_display_mode *mode);
        unsigned int use_drm_infoframe : 1;
+       unsigned int optional_hvcc : 1;
 };
 
 struct sun8i_dw_hdmi {
        struct clk                      *clk_tmds;
+       struct clk                      *clk_ahb;
+       struct clk                      *clk_iahb;
+       struct clk                      *clk_isfr;
        struct device                   *dev;
        struct dw_hdmi                  *hdmi;
        struct drm_encoder              encoder;
diff --git a/drivers/gpu/drm/sun4i/sun8i_hdmi_phy.c 
b/drivers/gpu/drm/sun4i/sun8i_hdmi_phy.c
index 0d284804e11f..0cae47704fdc 100644
--- a/drivers/gpu/drm/sun4i/sun8i_hdmi_phy.c
+++ b/drivers/gpu/drm/sun4i/sun8i_hdmi_phy.c
@@ -4,12 +4,49 @@
  */
 
 #include <linux/delay.h>
+#include <linux/math.h>
+#include <linux/mfd/syscon.h>
 #include <linux/of.h>
 #include <linux/of_platform.h>
 #include <linux/platform_device.h>
 
 #include "sun8i_dw_hdmi.h"
 
+#define SUN60I_RESCAL_CTRL_OFF         0x0160
+#define SUN60I_RESCAL_EN               BIT(0)
+#define SUN60I_RESCAL_ANA_EN           BIT(1)
+#define SUN60I_RESCAL_HDMI_SEL         BIT(12)
+
+#define SUN60I_TOP_PHY_CTRL            0x0000
+#define SUN60I_TOP_PHY_CTRL_RESET      BIT(0)
+#define SUN60I_TOP_PHY_CTRL_PDDQ       BIT(1)
+#define SUN60I_TOP_PHY_CTRL_TXPWRON    BIT(2)
+#define SUN60I_TOP_PHY_CTRL_ENHPDRXSENSE BIT(4)
+#define SUN60I_TOP_PHY_PLL_CTRL                0x0020
+#define SUN60I_TOP_PHY_PLL_MASK                0x007fffe2
+#define SUN60I_TOP_PHY_PLL_OUTPUT_GATE BIT(27)
+#define SUN60I_TOP_PHY_PLL_LOCK_EN     BIT(29)
+#define SUN60I_TOP_PHY_PLL_LDO_EN      BIT(30)
+#define SUN60I_TOP_PHY_PLL_EN          BIT(31)
+#define SUN60I_TOP_PHY_PLL_STATUS      0x0024
+#define SUN60I_TOP_PHY_PLL_LVL_SHIFT_GATE BIT(7)
+#define SUN60I_TOP_PHY_PLL_CLK_SEL_CCU BIT(8)
+#define SUN60I_TOP_PHY_PLL_LDO         0x0028
+#define SUN60I_TOP_PHY_PLL_LDO_VAL     0x00035000
+#define SUN60I_TOP_PHY_PLL_PAT0                0x002c
+#define SUN60I_TOP_PHY_PLL_PAT1                0x0030
+#define SUN60I_TOP_PHY_PLL_PAT1_VAL    0x30000000
+#define SUN60I_TOP_PHY_PLL_LOCK_STATUS 0x0040
+#define SUN60I_TOP_PHY_PLL_LOCK                BIT(0)
+
+#define SUN60I_C422_PHY_OPMODE_PLLCFG  0x06
+#define SUN60I_C422_PHY_CKSYMTXCTRL    0x09
+#define SUN60I_C422_PHY_VLEVCTRL       0x0e
+#define SUN60I_C422_PHY_PLLCURRCTRL    0x10
+#define SUN60I_C422_PHY_PLLDIVCTRL     0x11
+#define SUN60I_C422_PHY_TXTERM         0x19
+#define SUN60I_C422_PHY_I2C_SLAVE_ADDR 0x69
+
 /*
  * Address can be actually any value. Here is set to same value as
  * it is set in BSP driver.
@@ -519,6 +556,11 @@ int sun8i_hdmi_phy_init(struct sun8i_hdmi_phy *phy)
 {
        int ret;
 
+       if (phy->variant->is_top_phy) {
+               phy->variant->phy_init(phy);
+               return 0;
+       }
+
        ret = reset_control_deassert(phy->rst_phy);
        if (ret) {
                dev_err(phy->dev, "Cannot deassert phy reset control: %d\n", 
ret);
@@ -564,6 +606,9 @@ int sun8i_hdmi_phy_init(struct sun8i_hdmi_phy *phy)
 
 void sun8i_hdmi_phy_deinit(struct sun8i_hdmi_phy *phy)
 {
+       if (phy->variant->is_top_phy)
+               return;
+
        clk_disable_unprepare(phy->clk_mod);
        clk_disable_unprepare(phy->clk_bus);
        clk_disable_unprepare(phy->clk_phy);
@@ -619,6 +664,272 @@ static const struct sun8i_hdmi_phy_variant 
sun50i_a64_hdmi_phy = {
        .phy_init = &sun8i_hdmi_phy_init_h3,
 };
 
+static const struct regmap_config sun60i_hdmi_phy_regmap_config = {
+       .reg_bits       = 32,
+       .val_bits       = 32,
+       .reg_stride     = 4,
+       .max_register   = SUN60I_TOP_PHY_PLL_LOCK_STATUS,
+       .name           = "phy"
+};
+
+struct sun60i_top_phy_pll_cfg {
+       u32 ref_khz;
+       u32 pll_val;
+       u32 pat0;
+};
+
+static const struct sun60i_top_phy_pll_cfg sun60i_top_pll_24m[] = {
+       {  13500, 0x005f3500, 0x00000000 },
+       {  27000, 0x00576200, 0x80000000 },
+       {  54000, 0x002b6200, 0x80000000 },
+       {  65000, 0x00246200, 0x80000000 },
+       {  74250, 0x001f6200, 0x80000000 },
+       { 148500, 0x000f6200, 0x80000000 },
+       { 185625, 0x000f6200, 0x80000000 },
+       { 297000, 0x00076200, 0x00000000 },
+       { 371250, 0x00076200, 0x00000000 },
+       { 594000, 0x00036200, 0x00000000 },
+};
+
+static const struct sun60i_top_phy_pll_cfg sun60i_top_pll_26m[] = {
+       {  13500, 0x00673500, 0x00000000 },
+       {  27000, 0x00595c00, 0x80000000 },
+       {  54000, 0x000c1a00, 0x80000000 },
+       {  65000, 0x00235a00, 0x80000000 },
+       {  74250, 0x001f5a00, 0x80000000 },
+       { 108000, 0x000c3500, 0x00000000 },
+       { 148500, 0x000f5a00, 0x80000000 },
+       { 185625, 0x000f5a00, 0x80000000 },
+       { 297000, 0x0007b602, 0x00000000 },
+       { 371250, 0x0007b602, 0x00000000 },
+       { 594000, 0x0003b602, 0x00000000 },
+};
+
+struct sun60i_c422_mpll_cfg {
+       u32 khz;
+       u16 opmode;
+       u16 curr;
+       u16 div;
+};
+
+static const struct sun60i_c422_mpll_cfg sun60i_mpll_8bit[] = {
+       {  25175, 0x0003, 0x0283, 0x0628 },
+       {  27000, 0x0003, 0x0283, 0x0628 },
+       {  33750, 0x0003, 0x0283, 0x0628 },
+       {  35500, 0x0003, 0x0283, 0x0628 },
+       {  36000, 0x0003, 0x0285, 0x0228 },
+       {  40000, 0x0003, 0x0285, 0x0228 },
+       {  44900, 0x0003, 0x0285, 0x0228 },
+       {  49500, 0x0002, 0x1183, 0x0614 },
+       {  50000, 0x0002, 0x1183, 0x0614 },
+       {  50350, 0x0002, 0x1183, 0x0614 },
+       {  54000, 0x0002, 0x1183, 0x0694 },
+       {  56250, 0x0002, 0x1183, 0x0614 },
+       {  59400, 0x0002, 0x1183, 0x0614 },
+       {  65000, 0x0002, 0x1183, 0x0614 },
+       {  68250, 0x0002, 0x1183, 0x0614 },
+       {  71000, 0x0002, 0x1183, 0x0614 },
+       {  72000, 0x0002, 0x1142, 0x0214 },
+       {  73250, 0x0002, 0x1142, 0x0214 },
+       {  74250, 0x0002, 0x1142, 0x0214 },
+       {  82500, 0x0002, 0x1142, 0x0214 },
+       {  90000, 0x0002, 0x1142, 0x0214 },
+       {  99000, 0x0001, 0x20c0, 0x060a },
+       { 100700, 0x0001, 0x20c0, 0x060a },
+       { 108000, 0x0001, 0x20c0, 0x060a },
+       { 118000, 0x0001, 0x20c0, 0x060a },
+       { 144000, 0x0001, 0x2080, 0x020a },
+       { 148500, 0x0001, 0x2080, 0x020a },
+       { 165000, 0x0001, 0x2080, 0x020a },
+       { 180000, 0x0001, 0x2080, 0x020a },
+       { 185625, 0x0000, 0x3040, 0x0605 },
+       { 198000, 0x0000, 0x3040, 0x0605 },
+       { 216000, 0x0000, 0x3040, 0x0605 },
+       { 237600, 0x0000, 0x3040, 0x0605 },
+       { 288000, 0x0000, 0x3041, 0x0205 },
+       { 297000, 0x0000, 0x3041, 0x0205 },
+       { 330000, 0x0000, 0x3041, 0x0205 },
+       { 360000, 0x0640, 0x3041, 0x0205 },
+       { 371250, 0x0640, 0x3041, 0x0205 },
+       { 396000, 0x0640, 0x3041, 0x0205 },
+       { 432000, 0x0640, 0x3041, 0x0205 },
+       { 495000, 0x0640, 0x3080, 0x0005 },
+       { 505250, 0x0640, 0x3080, 0x0005 },
+       { 552750, 0x0640, 0x3080, 0x0005 },
+       { 594000, 0x0640, 0x3080, 0x0005 },
+};
+
+struct sun60i_c422_drive_cfg {
+       u32 min_khz;
+       u32 max_khz;
+       u16 txterm;
+       u16 vlev;
+       u16 cksym;
+};
+
+static const struct sun60i_c422_drive_cfg sun60i_drive[] = {
+       {  25000, 165000, 0x0007, 0x8160, 0x8188 },
+       { 165000, 340000, 0x0004, 0x8040, 0x8e85 },
+       { 340000, 600000, 0x0000, 0x80c0, 0x82f6 },
+};
+
+static void sun60i_hdmi_phy_init_a733(struct sun8i_hdmi_phy *phy)
+{
+       regmap_clear_bits(phy->rescal, SUN60I_RESCAL_CTRL_OFF,
+                         SUN60I_RESCAL_EN | SUN60I_RESCAL_HDMI_SEL);
+       regmap_set_bits(phy->rescal, SUN60I_RESCAL_CTRL_OFF,
+                       SUN60I_RESCAL_ANA_EN | SUN60I_RESCAL_EN);
+       usleep_range(200, 250);
+       regmap_clear_bits(phy->rescal, SUN60I_RESCAL_CTRL_OFF,
+                         SUN60I_RESCAL_ANA_EN | SUN60I_RESCAL_EN);
+
+       regmap_set_bits(phy->regs, SUN60I_TOP_PHY_CTRL,
+                       SUN60I_TOP_PHY_CTRL_RESET |
+                       SUN60I_TOP_PHY_CTRL_PDDQ |
+                       SUN60I_TOP_PHY_CTRL_TXPWRON |
+                       SUN60I_TOP_PHY_CTRL_ENHPDRXSENSE);
+       regmap_clear_bits(phy->regs, SUN60I_TOP_PHY_PLL_CTRL,
+                         SUN60I_TOP_PHY_PLL_OUTPUT_GATE);
+       regmap_clear_bits(phy->regs, SUN60I_TOP_PHY_PLL_STATUS,
+                         SUN60I_TOP_PHY_PLL_CLK_SEL_CCU);
+}
+
+static int sun60i_top_phy_pll_set(struct sun8i_hdmi_phy *phy, u32 clk_khz)
+{
+       const struct sun60i_top_phy_pll_cfg *table = sun60i_top_pll_24m;
+       unsigned int table_size = ARRAY_SIZE(sun60i_top_pll_24m);
+       const struct sun60i_top_phy_pll_cfg *cfg;
+       u32 val, best_diff = ~0U;
+       int i, ret;
+
+       if (phy->clk_ref && clk_get_rate(phy->clk_ref) == 26000000) {
+               table = sun60i_top_pll_26m;
+               table_size = ARRAY_SIZE(sun60i_top_pll_26m);
+       }
+
+       cfg = &table[0];
+       for (i = 0; i < table_size; i++) {
+               u32 diff = abs_diff(clk_khz, table[i].ref_khz);
+
+               if (diff < best_diff) {
+                       best_diff = diff;
+                       cfg = &table[i];
+               }
+       }
+
+       regmap_clear_bits(phy->regs, SUN60I_TOP_PHY_PLL_CTRL,
+                         SUN60I_TOP_PHY_PLL_OUTPUT_GATE);
+       regmap_update_bits(phy->regs, SUN60I_TOP_PHY_PLL_CTRL,
+                          SUN60I_TOP_PHY_PLL_MASK, cfg->pll_val);
+       regmap_write(phy->regs, SUN60I_TOP_PHY_PLL_LDO,
+                    SUN60I_TOP_PHY_PLL_LDO_VAL);
+       regmap_write(phy->regs, SUN60I_TOP_PHY_PLL_PAT0, cfg->pat0);
+       regmap_write(phy->regs, SUN60I_TOP_PHY_PLL_PAT1,
+                    SUN60I_TOP_PHY_PLL_PAT1_VAL);
+       regmap_set_bits(phy->regs, SUN60I_TOP_PHY_PLL_CTRL,
+                       SUN60I_TOP_PHY_PLL_EN | SUN60I_TOP_PHY_PLL_LDO_EN |
+                       SUN60I_TOP_PHY_PLL_LOCK_EN);
+       regmap_set_bits(phy->regs, SUN60I_TOP_PHY_PLL_STATUS,
+                       SUN60I_TOP_PHY_PLL_LVL_SHIFT_GATE);
+
+       ret = regmap_read_poll_timeout(phy->regs,
+                                      SUN60I_TOP_PHY_PLL_LOCK_STATUS, val,
+                                      val & SUN60I_TOP_PHY_PLL_LOCK,
+                                      10, 20000);
+       if (ret) {
+               dev_err(phy->dev, "TOP PHY PLL lock timeout\n");
+               return ret;
+       }
+
+       usleep_range(20, 40);
+       regmap_set_bits(phy->regs, SUN60I_TOP_PHY_PLL_CTRL,
+                       SUN60I_TOP_PHY_PLL_OUTPUT_GATE);
+
+       return 0;
+}
+
+static int sun60i_hdmi_phy_config(struct dw_hdmi *hdmi, void *data,
+                                 const struct drm_display_info *display,
+                                 const struct drm_display_mode *mode)
+{
+       struct sun8i_hdmi_phy *phy = data;
+       u32 clk_khz = mode->crtc_clock;
+       const struct sun60i_c422_mpll_cfg *mpll = &sun60i_mpll_8bit[0];
+       const struct sun60i_c422_drive_cfg *drv =
+               &sun60i_drive[ARRAY_SIZE(sun60i_drive) - 1];
+       u32 best_diff = ~0U;
+       int i, ret;
+
+       ret = sun60i_top_phy_pll_set(phy, clk_khz);
+       if (ret)
+               return ret;
+
+       for (i = 0; i < ARRAY_SIZE(sun60i_mpll_8bit); i++) {
+               u32 diff = abs_diff(clk_khz, sun60i_mpll_8bit[i].khz);
+
+               if (diff < best_diff) {
+                       best_diff = diff;
+                       mpll = &sun60i_mpll_8bit[i];
+               }
+       }
+
+       for (i = 0; i < ARRAY_SIZE(sun60i_drive); i++) {
+               if (clk_khz >= sun60i_drive[i].min_khz &&
+                   clk_khz < sun60i_drive[i].max_khz) {
+                       drv = &sun60i_drive[i];
+                       break;
+               }
+       }
+
+       dw_hdmi_phy_gen2_txpwron(hdmi, 0);
+       dw_hdmi_phy_gen2_pddq(hdmi, 1);
+       dw_hdmi_set_high_tmds_clock_ratio(hdmi, display);
+       dw_hdmi_phy_enable_svsret(hdmi, 0);
+       usleep_range(5, 10);
+       dw_hdmi_phy_enable_svsret(hdmi, 1);
+       dw_hdmi_phy_gen2_reset(hdmi);
+
+       dw_hdmi_phy_i2c_set_addr(hdmi, SUN60I_C422_PHY_I2C_SLAVE_ADDR);
+       dw_hdmi_phy_i2c_write(hdmi, mpll->opmode, 
SUN60I_C422_PHY_OPMODE_PLLCFG);
+       dw_hdmi_phy_i2c_write(hdmi, mpll->curr, SUN60I_C422_PHY_PLLCURRCTRL);
+       dw_hdmi_phy_i2c_write(hdmi, mpll->div, SUN60I_C422_PHY_PLLDIVCTRL);
+       dw_hdmi_phy_i2c_write(hdmi, drv->txterm, SUN60I_C422_PHY_TXTERM);
+       dw_hdmi_phy_i2c_write(hdmi, drv->vlev, SUN60I_C422_PHY_VLEVCTRL);
+       dw_hdmi_phy_i2c_write(hdmi, drv->cksym, SUN60I_C422_PHY_CKSYMTXCTRL);
+
+       dw_hdmi_phy_gen2_txpwron(hdmi, 1);
+       dw_hdmi_phy_gen2_pddq(hdmi, 0);
+
+       return 0;
+}
+
+static void sun60i_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
+{
+       struct sun8i_hdmi_phy *phy = data;
+
+       dw_hdmi_phy_gen2_txpwron(hdmi, 0);
+       dw_hdmi_phy_gen2_pddq(hdmi, 1);
+
+       regmap_clear_bits(phy->regs, SUN60I_TOP_PHY_PLL_CTRL,
+                         SUN60I_TOP_PHY_PLL_OUTPUT_GATE |
+                         SUN60I_TOP_PHY_PLL_EN | SUN60I_TOP_PHY_PLL_LDO_EN |
+                         SUN60I_TOP_PHY_PLL_LOCK_EN);
+}
+
+static const struct dw_hdmi_phy_ops sun60i_a733_hdmi_phy_ops = {
+       .init           = sun60i_hdmi_phy_config,
+       .disable        = sun60i_hdmi_phy_disable,
+       .read_hpd       = dw_hdmi_phy_read_hpd,
+       .update_hpd     = dw_hdmi_phy_update_hpd,
+       .setup_hpd      = dw_hdmi_phy_setup_hpd,
+};
+
+static const struct sun8i_hdmi_phy_variant sun60i_a733_hdmi_phy = {
+       .is_top_phy = true,
+       .phy_ops    = &sun60i_a733_hdmi_phy_ops,
+       .phy_init   = &sun60i_hdmi_phy_init_a733,
+};
+
 static const struct sun8i_hdmi_phy_variant sun50i_h6_hdmi_phy = {
        .cur_ctr  = sun50i_h6_cur_ctr,
        .mpll_cfg = sun50i_h6_mpll_cfg,
@@ -647,6 +958,10 @@ static const struct of_device_id sun8i_hdmi_phy_of_table[] 
= {
                .compatible = "allwinner,sun50i-h6-hdmi-phy",
                .data = &sun50i_h6_hdmi_phy,
        },
+       {
+               .compatible = "allwinner,sun60i-a733-hdmi-phy",
+               .data = &sun60i_a733_hdmi_phy,
+       },
        { /* sentinel */ }
 };
 
@@ -690,11 +1005,29 @@ static int sun8i_hdmi_phy_probe(struct platform_device 
*pdev)
                                     "Couldn't map the HDMI PHY registers\n");
 
        phy->regs = devm_regmap_init_mmio(dev, regs,
+                                         phy->variant->is_top_phy ?
+                                         &sun60i_hdmi_phy_regmap_config :
                                          &sun8i_hdmi_phy_regmap_config);
        if (IS_ERR(phy->regs))
                return dev_err_probe(dev, PTR_ERR(phy->regs),
                                     "Couldn't create the HDMI PHY regmap\n");
 
+       if (phy->variant->is_top_phy) {
+               phy->rescal = syscon_regmap_lookup_by_phandle(dev->of_node,
+                                                             
"allwinner,rescal");
+               if (IS_ERR(phy->rescal))
+                       return dev_err_probe(dev, PTR_ERR(phy->rescal),
+                                            "Could not get rescal syscon\n");
+
+               phy->clk_ref = devm_clk_get_optional_enabled(dev, "ref");
+               if (IS_ERR(phy->clk_ref))
+                       return dev_err_probe(dev, PTR_ERR(phy->clk_ref),
+                                            "Could not get ref clock\n");
+
+               platform_set_drvdata(pdev, phy);
+               return 0;
+       }
+
        phy->clk_bus = devm_clk_get(dev, "bus");
        if (IS_ERR(phy->clk_bus))
                return dev_err_probe(dev, PTR_ERR(phy->clk_bus),
-- 
2.53.0

Reply via email to