sun8i_hdmi_phy_clk.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018 Jernej Skrabec <jernej.skrabec@siol.net>
  4. */
  5. #include <linux/clk-provider.h>
  6. #include "sun8i_dw_hdmi.h"
  7. struct sun8i_phy_clk {
  8. struct clk_hw hw;
  9. struct sun8i_hdmi_phy *phy;
  10. };
  11. static inline struct sun8i_phy_clk *hw_to_phy_clk(struct clk_hw *hw)
  12. {
  13. return container_of(hw, struct sun8i_phy_clk, hw);
  14. }
  15. static int sun8i_phy_clk_determine_rate(struct clk_hw *hw,
  16. struct clk_rate_request *req)
  17. {
  18. unsigned long rate = req->rate;
  19. unsigned long best_rate = 0;
  20. struct clk_hw *parent;
  21. int best_div = 1;
  22. int i;
  23. parent = clk_hw_get_parent(hw);
  24. for (i = 1; i <= 16; i++) {
  25. unsigned long ideal = rate * i;
  26. unsigned long rounded;
  27. rounded = clk_hw_round_rate(parent, ideal);
  28. if (rounded == ideal) {
  29. best_rate = rounded;
  30. best_div = i;
  31. break;
  32. }
  33. if (!best_rate ||
  34. abs(rate - rounded / i) <
  35. abs(rate - best_rate / best_div)) {
  36. best_rate = rounded;
  37. best_div = i;
  38. }
  39. }
  40. req->rate = best_rate / best_div;
  41. req->best_parent_rate = best_rate;
  42. req->best_parent_hw = parent;
  43. return 0;
  44. }
  45. static unsigned long sun8i_phy_clk_recalc_rate(struct clk_hw *hw,
  46. unsigned long parent_rate)
  47. {
  48. struct sun8i_phy_clk *priv = hw_to_phy_clk(hw);
  49. u32 reg;
  50. regmap_read(priv->phy->regs, SUN8I_HDMI_PHY_PLL_CFG2_REG, &reg);
  51. reg = ((reg >> SUN8I_HDMI_PHY_PLL_CFG2_PREDIV_SHIFT) &
  52. SUN8I_HDMI_PHY_PLL_CFG2_PREDIV_MSK) + 1;
  53. return parent_rate / reg;
  54. }
  55. static int sun8i_phy_clk_set_rate(struct clk_hw *hw, unsigned long rate,
  56. unsigned long parent_rate)
  57. {
  58. struct sun8i_phy_clk *priv = hw_to_phy_clk(hw);
  59. unsigned long best_rate = 0;
  60. u8 best_m = 0, m;
  61. for (m = 1; m <= 16; m++) {
  62. unsigned long tmp_rate = parent_rate / m;
  63. if (tmp_rate > rate)
  64. continue;
  65. if (!best_rate ||
  66. (rate - tmp_rate) < (rate - best_rate)) {
  67. best_rate = tmp_rate;
  68. best_m = m;
  69. }
  70. }
  71. regmap_update_bits(priv->phy->regs, SUN8I_HDMI_PHY_PLL_CFG2_REG,
  72. SUN8I_HDMI_PHY_PLL_CFG2_PREDIV_MSK,
  73. SUN8I_HDMI_PHY_PLL_CFG2_PREDIV(best_m));
  74. return 0;
  75. }
  76. static const struct clk_ops sun8i_phy_clk_ops = {
  77. .determine_rate = sun8i_phy_clk_determine_rate,
  78. .recalc_rate = sun8i_phy_clk_recalc_rate,
  79. .set_rate = sun8i_phy_clk_set_rate,
  80. };
  81. int sun8i_phy_clk_create(struct sun8i_hdmi_phy *phy, struct device *dev)
  82. {
  83. struct clk_init_data init;
  84. struct sun8i_phy_clk *priv;
  85. const char *parents[1];
  86. parents[0] = __clk_get_name(phy->clk_pll0);
  87. if (!parents[0])
  88. return -ENODEV;
  89. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  90. if (!priv)
  91. return -ENOMEM;
  92. init.name = "hdmi-phy-clk";
  93. init.ops = &sun8i_phy_clk_ops;
  94. init.parent_names = parents;
  95. init.num_parents = 1;
  96. init.flags = CLK_SET_RATE_PARENT;
  97. priv->phy = phy;
  98. priv->hw.init = &init;
  99. phy->clk_phy = devm_clk_register(dev, &priv->hw);
  100. if (IS_ERR(phy->clk_phy))
  101. return PTR_ERR(phy->clk_phy);
  102. return 0;
  103. }