sun4i_hdmi_tmds_clk.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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_hdmi.h"
  14. struct sun4i_tmds {
  15. struct clk_hw hw;
  16. struct sun4i_hdmi *hdmi;
  17. u8 div_offset;
  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_offset,
  26. u8 *div,
  27. bool *half)
  28. {
  29. unsigned long best_rate = 0;
  30. u8 best_m = 0, m;
  31. bool is_double;
  32. for (m = div_offset ?: 1; m < (16 + div_offset); m++) {
  33. u8 d;
  34. for (d = 1; d < 3; d++) {
  35. unsigned long tmp_rate;
  36. tmp_rate = parent_rate / m / d;
  37. if (tmp_rate > rate)
  38. continue;
  39. if (!best_rate ||
  40. (rate - tmp_rate) < (rate - best_rate)) {
  41. best_rate = tmp_rate;
  42. best_m = m;
  43. is_double = d;
  44. }
  45. }
  46. }
  47. if (div && half) {
  48. *div = best_m;
  49. *half = is_double;
  50. }
  51. return best_rate;
  52. }
  53. static int sun4i_tmds_determine_rate(struct clk_hw *hw,
  54. struct clk_rate_request *req)
  55. {
  56. struct sun4i_tmds *tmds = hw_to_tmds(hw);
  57. struct clk_hw *parent = NULL;
  58. unsigned long best_parent = 0;
  59. unsigned long rate = req->rate;
  60. int best_div = 1, best_half = 1;
  61. int i, j, p;
  62. /*
  63. * We only consider PLL3, since the TCON is very likely to be
  64. * clocked from it, and to have the same rate than our HDMI
  65. * clock, so we should not need to do anything.
  66. */
  67. for (p = 0; p < clk_hw_get_num_parents(hw); p++) {
  68. parent = clk_hw_get_parent_by_index(hw, p);
  69. if (!parent)
  70. continue;
  71. for (i = 1; i < 3; i++) {
  72. for (j = tmds->div_offset ?: 1;
  73. j < (16 + tmds->div_offset); j++) {
  74. unsigned long ideal = rate * i * j;
  75. unsigned long rounded;
  76. rounded = clk_hw_round_rate(parent, ideal);
  77. if (rounded == ideal) {
  78. best_parent = rounded;
  79. best_half = i;
  80. best_div = j;
  81. goto out;
  82. }
  83. if (!best_parent ||
  84. abs(rate - rounded / i / j) <
  85. abs(rate - best_parent / best_half /
  86. best_div)) {
  87. best_parent = rounded;
  88. best_half = i;
  89. best_div = j;
  90. }
  91. }
  92. }
  93. }
  94. if (!parent)
  95. return -EINVAL;
  96. out:
  97. req->rate = best_parent / best_half / best_div;
  98. req->best_parent_rate = best_parent;
  99. req->best_parent_hw = parent;
  100. return 0;
  101. }
  102. static unsigned long sun4i_tmds_recalc_rate(struct clk_hw *hw,
  103. unsigned long parent_rate)
  104. {
  105. struct sun4i_tmds *tmds = hw_to_tmds(hw);
  106. u32 reg;
  107. reg = readl(tmds->hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
  108. if (reg & SUN4I_HDMI_PAD_CTRL1_HALVE_CLK)
  109. parent_rate /= 2;
  110. reg = readl(tmds->hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
  111. reg = ((reg >> 4) & 0xf) + tmds->div_offset;
  112. if (!reg)
  113. reg = 1;
  114. return parent_rate / reg;
  115. }
  116. static int sun4i_tmds_set_rate(struct clk_hw *hw, unsigned long rate,
  117. unsigned long parent_rate)
  118. {
  119. struct sun4i_tmds *tmds = hw_to_tmds(hw);
  120. bool half;
  121. u32 reg;
  122. u8 div;
  123. sun4i_tmds_calc_divider(rate, parent_rate, tmds->div_offset,
  124. &div, &half);
  125. reg = readl(tmds->hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
  126. reg &= ~SUN4I_HDMI_PAD_CTRL1_HALVE_CLK;
  127. if (half)
  128. reg |= SUN4I_HDMI_PAD_CTRL1_HALVE_CLK;
  129. writel(reg, tmds->hdmi->base + SUN4I_HDMI_PAD_CTRL1_REG);
  130. reg = readl(tmds->hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
  131. reg &= ~SUN4I_HDMI_PLL_CTRL_DIV_MASK;
  132. writel(reg | SUN4I_HDMI_PLL_CTRL_DIV(div - tmds->div_offset),
  133. tmds->hdmi->base + SUN4I_HDMI_PLL_CTRL_REG);
  134. return 0;
  135. }
  136. static u8 sun4i_tmds_get_parent(struct clk_hw *hw)
  137. {
  138. struct sun4i_tmds *tmds = hw_to_tmds(hw);
  139. u32 reg;
  140. reg = readl(tmds->hdmi->base + SUN4I_HDMI_PLL_DBG0_REG);
  141. return ((reg & SUN4I_HDMI_PLL_DBG0_TMDS_PARENT_MASK) >>
  142. SUN4I_HDMI_PLL_DBG0_TMDS_PARENT_SHIFT);
  143. }
  144. static int sun4i_tmds_set_parent(struct clk_hw *hw, u8 index)
  145. {
  146. struct sun4i_tmds *tmds = hw_to_tmds(hw);
  147. u32 reg;
  148. if (index > 1)
  149. return -EINVAL;
  150. reg = readl(tmds->hdmi->base + SUN4I_HDMI_PLL_DBG0_REG);
  151. reg &= ~SUN4I_HDMI_PLL_DBG0_TMDS_PARENT_MASK;
  152. writel(reg | SUN4I_HDMI_PLL_DBG0_TMDS_PARENT(index),
  153. tmds->hdmi->base + SUN4I_HDMI_PLL_DBG0_REG);
  154. return 0;
  155. }
  156. static const struct clk_ops sun4i_tmds_ops = {
  157. .determine_rate = sun4i_tmds_determine_rate,
  158. .recalc_rate = sun4i_tmds_recalc_rate,
  159. .set_rate = sun4i_tmds_set_rate,
  160. .get_parent = sun4i_tmds_get_parent,
  161. .set_parent = sun4i_tmds_set_parent,
  162. };
  163. int sun4i_tmds_create(struct sun4i_hdmi *hdmi)
  164. {
  165. struct clk_init_data init;
  166. struct sun4i_tmds *tmds;
  167. const char *parents[2];
  168. parents[0] = __clk_get_name(hdmi->pll0_clk);
  169. if (!parents[0])
  170. return -ENODEV;
  171. parents[1] = __clk_get_name(hdmi->pll1_clk);
  172. if (!parents[1])
  173. return -ENODEV;
  174. tmds = devm_kzalloc(hdmi->dev, sizeof(*tmds), GFP_KERNEL);
  175. if (!tmds)
  176. return -ENOMEM;
  177. init.name = "hdmi-tmds";
  178. init.ops = &sun4i_tmds_ops;
  179. init.parent_names = parents;
  180. init.num_parents = 2;
  181. init.flags = CLK_SET_RATE_PARENT;
  182. tmds->hdmi = hdmi;
  183. tmds->hw.init = &init;
  184. tmds->div_offset = hdmi->variant->tmds_clk_div_offset;
  185. hdmi->tmds_clk = devm_clk_register(hdmi->dev, &tmds->hw);
  186. if (IS_ERR(hdmi->tmds_clk))
  187. return PTR_ERR(hdmi->tmds_clk);
  188. return 0;
  189. }