hdmi_phy.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2016, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/of_device.h>
  14. #include "hdmi.h"
  15. static int msm_hdmi_phy_resource_init(struct hdmi_phy *phy)
  16. {
  17. struct hdmi_phy_cfg *cfg = phy->cfg;
  18. struct device *dev = &phy->pdev->dev;
  19. int i, ret;
  20. phy->regs = devm_kzalloc(dev, sizeof(phy->regs[0]) * cfg->num_regs,
  21. GFP_KERNEL);
  22. if (!phy->regs)
  23. return -ENOMEM;
  24. phy->clks = devm_kzalloc(dev, sizeof(phy->clks[0]) * cfg->num_clks,
  25. GFP_KERNEL);
  26. if (!phy->clks)
  27. return -ENOMEM;
  28. for (i = 0; i < cfg->num_regs; i++) {
  29. struct regulator *reg;
  30. reg = devm_regulator_get(dev, cfg->reg_names[i]);
  31. if (IS_ERR(reg)) {
  32. ret = PTR_ERR(reg);
  33. dev_err(dev, "failed to get phy regulator: %s (%d)\n",
  34. cfg->reg_names[i], ret);
  35. return ret;
  36. }
  37. phy->regs[i] = reg;
  38. }
  39. for (i = 0; i < cfg->num_clks; i++) {
  40. struct clk *clk;
  41. clk = devm_clk_get(dev, cfg->clk_names[i]);
  42. if (IS_ERR(clk)) {
  43. ret = PTR_ERR(clk);
  44. dev_err(dev, "failed to get phy clock: %s (%d)\n",
  45. cfg->clk_names[i], ret);
  46. return ret;
  47. }
  48. phy->clks[i] = clk;
  49. }
  50. return 0;
  51. }
  52. int msm_hdmi_phy_resource_enable(struct hdmi_phy *phy)
  53. {
  54. struct hdmi_phy_cfg *cfg = phy->cfg;
  55. struct device *dev = &phy->pdev->dev;
  56. int i, ret = 0;
  57. pm_runtime_get_sync(dev);
  58. for (i = 0; i < cfg->num_regs; i++) {
  59. ret = regulator_enable(phy->regs[i]);
  60. if (ret)
  61. dev_err(dev, "failed to enable regulator: %s (%d)\n",
  62. cfg->reg_names[i], ret);
  63. }
  64. for (i = 0; i < cfg->num_clks; i++) {
  65. ret = clk_prepare_enable(phy->clks[i]);
  66. if (ret)
  67. dev_err(dev, "failed to enable clock: %s (%d)\n",
  68. cfg->clk_names[i], ret);
  69. }
  70. return ret;
  71. }
  72. void msm_hdmi_phy_resource_disable(struct hdmi_phy *phy)
  73. {
  74. struct hdmi_phy_cfg *cfg = phy->cfg;
  75. struct device *dev = &phy->pdev->dev;
  76. int i;
  77. for (i = cfg->num_clks - 1; i >= 0; i--)
  78. clk_disable_unprepare(phy->clks[i]);
  79. for (i = cfg->num_regs - 1; i >= 0; i--)
  80. regulator_disable(phy->regs[i]);
  81. pm_runtime_put_sync(dev);
  82. }
  83. void msm_hdmi_phy_powerup(struct hdmi_phy *phy, unsigned long int pixclock)
  84. {
  85. if (!phy || !phy->cfg->powerup)
  86. return;
  87. phy->cfg->powerup(phy, pixclock);
  88. }
  89. void msm_hdmi_phy_powerdown(struct hdmi_phy *phy)
  90. {
  91. if (!phy || !phy->cfg->powerdown)
  92. return;
  93. phy->cfg->powerdown(phy);
  94. }
  95. static int msm_hdmi_phy_pll_init(struct platform_device *pdev,
  96. enum hdmi_phy_type type)
  97. {
  98. int ret;
  99. switch (type) {
  100. case MSM_HDMI_PHY_8960:
  101. ret = msm_hdmi_pll_8960_init(pdev);
  102. break;
  103. case MSM_HDMI_PHY_8996:
  104. ret = msm_hdmi_pll_8996_init(pdev);
  105. break;
  106. /*
  107. * we don't have PLL support for these, don't report an error for now
  108. */
  109. case MSM_HDMI_PHY_8x60:
  110. case MSM_HDMI_PHY_8x74:
  111. default:
  112. ret = 0;
  113. break;
  114. }
  115. return ret;
  116. }
  117. static int msm_hdmi_phy_probe(struct platform_device *pdev)
  118. {
  119. struct device *dev = &pdev->dev;
  120. struct hdmi_phy *phy;
  121. int ret;
  122. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  123. if (!phy)
  124. return -ENODEV;
  125. phy->cfg = (struct hdmi_phy_cfg *)of_device_get_match_data(dev);
  126. if (!phy->cfg)
  127. return -ENODEV;
  128. phy->mmio = msm_ioremap(pdev, "hdmi_phy", "HDMI_PHY");
  129. if (IS_ERR(phy->mmio)) {
  130. dev_err(dev, "%s: failed to map phy base\n", __func__);
  131. return -ENOMEM;
  132. }
  133. phy->pdev = pdev;
  134. ret = msm_hdmi_phy_resource_init(phy);
  135. if (ret)
  136. return ret;
  137. pm_runtime_enable(&pdev->dev);
  138. ret = msm_hdmi_phy_resource_enable(phy);
  139. if (ret)
  140. return ret;
  141. ret = msm_hdmi_phy_pll_init(pdev, phy->cfg->type);
  142. if (ret) {
  143. dev_err(dev, "couldn't init PLL\n");
  144. msm_hdmi_phy_resource_disable(phy);
  145. return ret;
  146. }
  147. msm_hdmi_phy_resource_disable(phy);
  148. platform_set_drvdata(pdev, phy);
  149. return 0;
  150. }
  151. static int msm_hdmi_phy_remove(struct platform_device *pdev)
  152. {
  153. pm_runtime_disable(&pdev->dev);
  154. return 0;
  155. }
  156. static const struct of_device_id msm_hdmi_phy_dt_match[] = {
  157. { .compatible = "qcom,hdmi-phy-8660",
  158. .data = &msm_hdmi_phy_8x60_cfg },
  159. { .compatible = "qcom,hdmi-phy-8960",
  160. .data = &msm_hdmi_phy_8960_cfg },
  161. { .compatible = "qcom,hdmi-phy-8974",
  162. .data = &msm_hdmi_phy_8x74_cfg },
  163. { .compatible = "qcom,hdmi-phy-8084",
  164. .data = &msm_hdmi_phy_8x74_cfg },
  165. { .compatible = "qcom,hdmi-phy-8996",
  166. .data = &msm_hdmi_phy_8996_cfg },
  167. {}
  168. };
  169. static struct platform_driver msm_hdmi_phy_platform_driver = {
  170. .probe = msm_hdmi_phy_probe,
  171. .remove = msm_hdmi_phy_remove,
  172. .driver = {
  173. .name = "msm_hdmi_phy",
  174. .of_match_table = msm_hdmi_phy_dt_match,
  175. },
  176. };
  177. void __init msm_hdmi_phy_driver_register(void)
  178. {
  179. platform_driver_register(&msm_hdmi_phy_platform_driver);
  180. }
  181. void __exit msm_hdmi_phy_driver_unregister(void)
  182. {
  183. platform_driver_unregister(&msm_hdmi_phy_platform_driver);
  184. }