phy-meson8-hdmi-tx.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Meson8, Meson8b and Meson8m2 HDMI TX PHY.
  4. *
  5. * Copyright (C) 2021 Martin Blumenstingl <[email protected]>
  6. */
  7. #include <linux/bitfield.h>
  8. #include <linux/bits.h>
  9. #include <linux/clk.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/of_device.h>
  13. #include <linux/phy/phy.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/property.h>
  16. #include <linux/regmap.h>
  17. /*
  18. * Unfortunately there is no detailed documentation available for the
  19. * HHI_HDMI_PHY_CNTL0 register. CTL0 and CTL1 is all we know about.
  20. * Magic register values in the driver below are taken from the vendor
  21. * BSP / kernel.
  22. */
  23. #define HHI_HDMI_PHY_CNTL0 0x3a0
  24. #define HHI_HDMI_PHY_CNTL0_HDMI_CTL1 GENMASK(31, 16)
  25. #define HHI_HDMI_PHY_CNTL0_HDMI_CTL0 GENMASK(15, 0)
  26. #define HHI_HDMI_PHY_CNTL1 0x3a4
  27. #define HHI_HDMI_PHY_CNTL1_CLOCK_ENABLE BIT(1)
  28. #define HHI_HDMI_PHY_CNTL1_SOFT_RESET BIT(0)
  29. #define HHI_HDMI_PHY_CNTL2 0x3a8
  30. struct phy_meson8_hdmi_tx_priv {
  31. struct regmap *hhi;
  32. struct clk *tmds_clk;
  33. };
  34. static int phy_meson8_hdmi_tx_init(struct phy *phy)
  35. {
  36. struct phy_meson8_hdmi_tx_priv *priv = phy_get_drvdata(phy);
  37. return clk_prepare_enable(priv->tmds_clk);
  38. }
  39. static int phy_meson8_hdmi_tx_exit(struct phy *phy)
  40. {
  41. struct phy_meson8_hdmi_tx_priv *priv = phy_get_drvdata(phy);
  42. clk_disable_unprepare(priv->tmds_clk);
  43. return 0;
  44. }
  45. static int phy_meson8_hdmi_tx_power_on(struct phy *phy)
  46. {
  47. struct phy_meson8_hdmi_tx_priv *priv = phy_get_drvdata(phy);
  48. unsigned int i;
  49. u16 hdmi_ctl0;
  50. if (clk_get_rate(priv->tmds_clk) >= 2970UL * 1000 * 1000)
  51. hdmi_ctl0 = 0x1e8b;
  52. else
  53. hdmi_ctl0 = 0x4d0b;
  54. regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0,
  55. FIELD_PREP(HHI_HDMI_PHY_CNTL0_HDMI_CTL1, 0x08c3) |
  56. FIELD_PREP(HHI_HDMI_PHY_CNTL0_HDMI_CTL0, hdmi_ctl0));
  57. regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL1, 0x0);
  58. /* Reset three times, just like the vendor driver does */
  59. for (i = 0; i < 3; i++) {
  60. regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL1,
  61. HHI_HDMI_PHY_CNTL1_CLOCK_ENABLE |
  62. HHI_HDMI_PHY_CNTL1_SOFT_RESET);
  63. usleep_range(1000, 2000);
  64. regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL1,
  65. HHI_HDMI_PHY_CNTL1_CLOCK_ENABLE);
  66. usleep_range(1000, 2000);
  67. }
  68. return 0;
  69. }
  70. static int phy_meson8_hdmi_tx_power_off(struct phy *phy)
  71. {
  72. struct phy_meson8_hdmi_tx_priv *priv = phy_get_drvdata(phy);
  73. regmap_write(priv->hhi, HHI_HDMI_PHY_CNTL0,
  74. FIELD_PREP(HHI_HDMI_PHY_CNTL0_HDMI_CTL1, 0x0841) |
  75. FIELD_PREP(HHI_HDMI_PHY_CNTL0_HDMI_CTL0, 0x8d00));
  76. return 0;
  77. }
  78. static const struct phy_ops phy_meson8_hdmi_tx_ops = {
  79. .init = phy_meson8_hdmi_tx_init,
  80. .exit = phy_meson8_hdmi_tx_exit,
  81. .power_on = phy_meson8_hdmi_tx_power_on,
  82. .power_off = phy_meson8_hdmi_tx_power_off,
  83. .owner = THIS_MODULE,
  84. };
  85. static int phy_meson8_hdmi_tx_probe(struct platform_device *pdev)
  86. {
  87. struct device_node *np = pdev->dev.of_node;
  88. struct phy_meson8_hdmi_tx_priv *priv;
  89. struct phy_provider *phy_provider;
  90. struct resource *res;
  91. struct phy *phy;
  92. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  93. if (!res)
  94. return -EINVAL;
  95. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  96. if (!priv)
  97. return -ENOMEM;
  98. priv->hhi = syscon_node_to_regmap(np->parent);
  99. if (IS_ERR(priv->hhi))
  100. return PTR_ERR(priv->hhi);
  101. priv->tmds_clk = devm_clk_get(&pdev->dev, NULL);
  102. if (IS_ERR(priv->tmds_clk))
  103. return PTR_ERR(priv->tmds_clk);
  104. phy = devm_phy_create(&pdev->dev, np, &phy_meson8_hdmi_tx_ops);
  105. if (IS_ERR(phy))
  106. return PTR_ERR(phy);
  107. phy_set_drvdata(phy, priv);
  108. phy_provider = devm_of_phy_provider_register(&pdev->dev,
  109. of_phy_simple_xlate);
  110. return PTR_ERR_OR_ZERO(phy_provider);
  111. }
  112. static const struct of_device_id phy_meson8_hdmi_tx_of_match[] = {
  113. { .compatible = "amlogic,meson8-hdmi-tx-phy" },
  114. { /* sentinel */ }
  115. };
  116. MODULE_DEVICE_TABLE(of, phy_meson8_hdmi_tx_of_match);
  117. static struct platform_driver phy_meson8_hdmi_tx_driver = {
  118. .probe = phy_meson8_hdmi_tx_probe,
  119. .driver = {
  120. .name = "phy-meson8-hdmi-tx",
  121. .of_match_table = phy_meson8_hdmi_tx_of_match,
  122. },
  123. };
  124. module_platform_driver(phy_meson8_hdmi_tx_driver);
  125. MODULE_AUTHOR("Martin Blumenstingl <[email protected]>");
  126. MODULE_DESCRIPTION("Meson8, Meson8b and Meson8m2 HDMI TX PHY driver");
  127. MODULE_LICENSE("GPL v2");