hdmi_phy.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/of.h>
  6. #include <linux/platform_device.h>
  7. #include "hdmi.h"
  8. static int msm_hdmi_phy_resource_init(struct hdmi_phy *phy)
  9. {
  10. struct hdmi_phy_cfg *cfg = phy->cfg;
  11. struct device *dev = &phy->pdev->dev;
  12. int i, ret;
  13. phy->regs = devm_kcalloc(dev, cfg->num_regs, sizeof(phy->regs[0]),
  14. GFP_KERNEL);
  15. if (!phy->regs)
  16. return -ENOMEM;
  17. phy->clks = devm_kcalloc(dev, cfg->num_clks, sizeof(phy->clks[0]),
  18. GFP_KERNEL);
  19. if (!phy->clks)
  20. return -ENOMEM;
  21. for (i = 0; i < cfg->num_regs; i++)
  22. phy->regs[i].supply = cfg->reg_names[i];
  23. ret = devm_regulator_bulk_get(dev, cfg->num_regs, phy->regs);
  24. if (ret) {
  25. if (ret != -EPROBE_DEFER)
  26. DRM_DEV_ERROR(dev, "failed to get phy regulators: %d\n", ret);
  27. return ret;
  28. }
  29. for (i = 0; i < cfg->num_clks; i++) {
  30. struct clk *clk;
  31. clk = msm_clk_get(phy->pdev, cfg->clk_names[i]);
  32. if (IS_ERR(clk)) {
  33. ret = PTR_ERR(clk);
  34. DRM_DEV_ERROR(dev, "failed to get phy clock: %s (%d)\n",
  35. cfg->clk_names[i], ret);
  36. return ret;
  37. }
  38. phy->clks[i] = clk;
  39. }
  40. return 0;
  41. }
  42. int msm_hdmi_phy_resource_enable(struct hdmi_phy *phy)
  43. {
  44. struct hdmi_phy_cfg *cfg = phy->cfg;
  45. struct device *dev = &phy->pdev->dev;
  46. int i, ret = 0;
  47. pm_runtime_get_sync(dev);
  48. ret = regulator_bulk_enable(cfg->num_regs, phy->regs);
  49. if (ret) {
  50. DRM_DEV_ERROR(dev, "failed to enable regulators: (%d)\n", ret);
  51. return ret;
  52. }
  53. for (i = 0; i < cfg->num_clks; i++) {
  54. ret = clk_prepare_enable(phy->clks[i]);
  55. if (ret)
  56. DRM_DEV_ERROR(dev, "failed to enable clock: %s (%d)\n",
  57. cfg->clk_names[i], ret);
  58. }
  59. return ret;
  60. }
  61. void msm_hdmi_phy_resource_disable(struct hdmi_phy *phy)
  62. {
  63. struct hdmi_phy_cfg *cfg = phy->cfg;
  64. struct device *dev = &phy->pdev->dev;
  65. int i;
  66. for (i = cfg->num_clks - 1; i >= 0; i--)
  67. clk_disable_unprepare(phy->clks[i]);
  68. regulator_bulk_disable(cfg->num_regs, phy->regs);
  69. pm_runtime_put_sync(dev);
  70. }
  71. void msm_hdmi_phy_powerup(struct hdmi_phy *phy, unsigned long int pixclock)
  72. {
  73. if (!phy || !phy->cfg->powerup)
  74. return;
  75. phy->cfg->powerup(phy, pixclock);
  76. }
  77. void msm_hdmi_phy_powerdown(struct hdmi_phy *phy)
  78. {
  79. if (!phy || !phy->cfg->powerdown)
  80. return;
  81. phy->cfg->powerdown(phy);
  82. }
  83. static int msm_hdmi_phy_pll_init(struct platform_device *pdev,
  84. enum hdmi_phy_type type)
  85. {
  86. int ret;
  87. switch (type) {
  88. case MSM_HDMI_PHY_8960:
  89. ret = msm_hdmi_pll_8960_init(pdev);
  90. break;
  91. case MSM_HDMI_PHY_8996:
  92. ret = msm_hdmi_pll_8996_init(pdev);
  93. break;
  94. case MSM_HDMI_PHY_8998:
  95. ret = msm_hdmi_pll_8998_init(pdev);
  96. break;
  97. /*
  98. * we don't have PLL support for these, don't report an error for now
  99. */
  100. case MSM_HDMI_PHY_8x60:
  101. case MSM_HDMI_PHY_8x74:
  102. default:
  103. ret = 0;
  104. break;
  105. }
  106. return ret;
  107. }
  108. static int msm_hdmi_phy_probe(struct platform_device *pdev)
  109. {
  110. struct device *dev = &pdev->dev;
  111. struct hdmi_phy *phy;
  112. int ret;
  113. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  114. if (!phy)
  115. return -ENODEV;
  116. phy->cfg = (struct hdmi_phy_cfg *)of_device_get_match_data(dev);
  117. if (!phy->cfg)
  118. return -ENODEV;
  119. phy->mmio = msm_ioremap(pdev, "hdmi_phy");
  120. if (IS_ERR(phy->mmio)) {
  121. DRM_DEV_ERROR(dev, "%s: failed to map phy base\n", __func__);
  122. return -ENOMEM;
  123. }
  124. phy->pdev = pdev;
  125. ret = msm_hdmi_phy_resource_init(phy);
  126. if (ret)
  127. return ret;
  128. pm_runtime_enable(&pdev->dev);
  129. ret = msm_hdmi_phy_resource_enable(phy);
  130. if (ret)
  131. return ret;
  132. ret = msm_hdmi_phy_pll_init(pdev, phy->cfg->type);
  133. if (ret) {
  134. DRM_DEV_ERROR(dev, "couldn't init PLL\n");
  135. msm_hdmi_phy_resource_disable(phy);
  136. return ret;
  137. }
  138. msm_hdmi_phy_resource_disable(phy);
  139. platform_set_drvdata(pdev, phy);
  140. return 0;
  141. }
  142. static void msm_hdmi_phy_remove(struct platform_device *pdev)
  143. {
  144. pm_runtime_disable(&pdev->dev);
  145. }
  146. static const struct of_device_id msm_hdmi_phy_dt_match[] = {
  147. { .compatible = "qcom,hdmi-phy-8660",
  148. .data = &msm_hdmi_phy_8x60_cfg },
  149. { .compatible = "qcom,hdmi-phy-8960",
  150. .data = &msm_hdmi_phy_8960_cfg },
  151. { .compatible = "qcom,hdmi-phy-8974",
  152. .data = &msm_hdmi_phy_8x74_cfg },
  153. { .compatible = "qcom,hdmi-phy-8084",
  154. .data = &msm_hdmi_phy_8x74_cfg },
  155. { .compatible = "qcom,hdmi-phy-8996",
  156. .data = &msm_hdmi_phy_8996_cfg },
  157. { .compatible = "qcom,hdmi-phy-8998",
  158. .data = &msm_hdmi_phy_8998_cfg },
  159. {}
  160. };
  161. static struct platform_driver msm_hdmi_phy_platform_driver = {
  162. .probe = msm_hdmi_phy_probe,
  163. .remove_new = msm_hdmi_phy_remove,
  164. .driver = {
  165. .name = "msm_hdmi_phy",
  166. .of_match_table = msm_hdmi_phy_dt_match,
  167. },
  168. };
  169. void __init msm_hdmi_phy_driver_register(void)
  170. {
  171. platform_driver_register(&msm_hdmi_phy_platform_driver);
  172. }
  173. void __exit msm_hdmi_phy_driver_unregister(void)
  174. {
  175. platform_driver_unregister(&msm_hdmi_phy_platform_driver);
  176. }