sun4i_hdmi_tmds_clk.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (C) 2016 Free Electrons
  3. * Copyright (C) 2016 NextThing Co
  4. *
  5. * Maxime Ripard <maxime.ripard@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. */
  12. #include <linux/clk-provider.h>
  13. #include "sun4i_tcon.h"
  14. #include "sun4i_hdmi.h"
  15. struct sun4i_tmds {
  16. struct clk_hw hw;
  17. struct sun4i_hdmi *hdmi;
  18. };
  19. static inline struct sun4i_tmds *hw_to_tmds(struct clk_hw *hw)
  20. {
  21. return container_of(hw, struct sun4i_tmds, hw);
  22. }
  23. static unsigned long sun4i_tmds_calc_divider(unsigned long rate,
  24. unsigned long parent_rate,
  25. u8 *div,
  26. bool *half)
  27. {
  28. unsigned long best_rate = 0;
  29. u8 best_m = 0, m;
  30. bool is_double;
  31. for (m = 1; m < 16; m++) {
  32. u8 d;
  33. for (d = 1; d < 3; d++) {
  34. unsigned long tmp_rate;
  35. tmp_rate = parent_rate / m / d;
  36. if (tmp_rate > rate)
  37. continue;
  38. if (!best_rate ||
  39. (rate - tmp_rate) < (rate - best_rate)) {
  40. best_rate = tmp_rate;
  41. best_m = m;
  42. is_double = d;
  43. }
  44. }
  45. }
  46. if (div && half) {
  47. *div = best_m;
  48. *half = is_double;
  49. }
  50. return best_rate;
  51. }
  52. static int sun4i_tmds_determine_rate(struct clk_hw *hw,
  53. struct clk_rate_request *req)
  54. {
  55. struct clk_hw *parent;
  56. unsigned long best_parent = 0;
  57. unsigned long rate = req->rate;
  58. int best_div = 1, best_half = 1;
  59. int i, j;
  60. /*
  61. * We only consider PLL3, since the TCON is very likely to be
  62. * clocked from it, and to have the same rate than our HDMI
  63. * clock, so we should not need to do anything.
  64. */
  65. parent = clk_hw_get_parent_by_index(hw, 0);
  66. if (!parent)
  67. return -EINVAL;
  68. for (i = 1; i < 3; i++) {
  69. for (j = 1; j < 16; j++) {
  70. unsigned long ideal = rate * i * j;
  71. unsigned long rounded;
  72. rounded = clk_hw_round_rate(parent, ideal);
  73. if (rounded == ideal) {
  74. best_parent = rounded;
  75. best_half = i;
  76. best_div = j;
  77. goto out;
  78. }
  79. if (abs(rate - rounded / i) <
  80. abs(rate - best_parent / best_div)) {
  81. best_parent = rounded;
  82. best_div = i;
  83. }
  84. }
  85. }
  86. out:
  87. req->rate = best_parent / best_half / best_div;
  88. req->best_parent_rate = best_parent;
  89. req->best_parent_hw = parent;
  90. return 0;
  91. }
  92. static unsigned long sun4i_tmds_recalc_rate(struct clk_hw *hw,
  93. unsigned long parent_rate)
  94. {
  95. struct sun4i_tmds *tmds = hw_to_tmds(hw);
  96. u32 reg;
  97. reg = readl(tmds->hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
  98. if (reg & SUN4I_HDMI_PAD_CTRL1_HALVE_CLK)
  99. parent_rate /= 2;
  100. reg = readl(tmds->hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
  101. reg = (reg >> 4) & 0xf;
  102. if (!reg)
  103. reg = 1;
  104. return parent_rate / reg;
  105. }
  106. static int sun4i_tmds_set_rate(struct clk_hw *hw, unsigned long rate,
  107. unsigned long parent_rate)
  108. {
  109. struct sun4i_tmds *tmds = hw_to_tmds(hw);
  110. bool half;
  111. u32 reg;
  112. u8 div;
  113. sun4i_tmds_calc_divider(rate, parent_rate, &div, &half);
  114. reg = readl(tmds->hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
  115. reg &= ~SUN4I_HDMI_PAD_CTRL1_HALVE_CLK;
  116. if (half)
  117. reg |= SUN4I_HDMI_PAD_CTRL1_HALVE_CLK;
  118. writel(reg, tmds->hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
  119. reg = readl(tmds->hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
  120. reg &= ~SUN4I_HDMI_PLL_CTRL_DIV_MASK;
  121. writel(reg | SUN4I_HDMI_PLL_CTRL_DIV(div),
  122. tmds->hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
  123. return 0;
  124. }
  125. static u8 sun4i_tmds_get_parent(struct clk_hw *hw)
  126. {
  127. struct sun4i_tmds *tmds = hw_to_tmds(hw);
  128. u32 reg;
  129. reg = readl(tmds->hdmi->base + SUN4I_HDMI_PLL_DBG0_REG);
  130. return ((reg & SUN4I_HDMI_PLL_DBG0_TMDS_PARENT_MASK) >>
  131. SUN4I_HDMI_PLL_DBG0_TMDS_PARENT_SHIFT);
  132. }
  133. static int sun4i_tmds_set_parent(struct clk_hw *hw, u8 index)
  134. {
  135. struct sun4i_tmds *tmds = hw_to_tmds(hw);
  136. u32 reg;
  137. if (index > 1)
  138. return -EINVAL;
  139. reg = readl(tmds->hdmi->base + SUN4I_HDMI_PLL_DBG0_REG);
  140. reg &= ~SUN4I_HDMI_PLL_DBG0_TMDS_PARENT_MASK;
  141. writel(reg | SUN4I_HDMI_PLL_DBG0_TMDS_PARENT(index),
  142. tmds->hdmi->base + SUN4I_HDMI_PLL_DBG0_REG);
  143. return 0;
  144. }
  145. static const struct clk_ops sun4i_tmds_ops = {
  146. .determine_rate = sun4i_tmds_determine_rate,
  147. .recalc_rate = sun4i_tmds_recalc_rate,
  148. .set_rate = sun4i_tmds_set_rate,
  149. .get_parent = sun4i_tmds_get_parent,
  150. .set_parent = sun4i_tmds_set_parent,
  151. };
  152. int sun4i_tmds_create(struct sun4i_hdmi *hdmi)
  153. {
  154. struct clk_init_data init;
  155. struct sun4i_tmds *tmds;
  156. const char *parents[2];
  157. parents[0] = __clk_get_name(hdmi->pll0_clk);
  158. if (!parents[0])
  159. return -ENODEV;
  160. parents[1] = __clk_get_name(hdmi->pll1_clk);
  161. if (!parents[1])
  162. return -ENODEV;
  163. tmds = devm_kzalloc(hdmi->dev, sizeof(*tmds), GFP_KERNEL);
  164. if (!tmds)
  165. return -ENOMEM;
  166. init.name = "hdmi-tmds";
  167. init.ops = &sun4i_tmds_ops;
  168. init.parent_names = parents;
  169. init.num_parents = 2;
  170. init.flags = CLK_SET_RATE_PARENT;
  171. tmds->hdmi = hdmi;
  172. tmds->hw.init = &init;
  173. hdmi->tmds_clk = devm_clk_register(hdmi->dev, &tmds->hw);
  174. if (IS_ERR(hdmi->tmds_clk))
  175. return PTR_ERR(hdmi->tmds_clk);
  176. return 0;
  177. }