sun4i_dotclock.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 <linux/regmap.h>
  14. #include "sun4i_tcon.h"
  15. #include "sun4i_dotclock.h"
  16. struct sun4i_dclk {
  17. struct clk_hw hw;
  18. struct regmap *regmap;
  19. struct sun4i_tcon *tcon;
  20. };
  21. static inline struct sun4i_dclk *hw_to_dclk(struct clk_hw *hw)
  22. {
  23. return container_of(hw, struct sun4i_dclk, hw);
  24. }
  25. static void sun4i_dclk_disable(struct clk_hw *hw)
  26. {
  27. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  28. regmap_update_bits(dclk->regmap, SUN4I_TCON0_DCLK_REG,
  29. BIT(SUN4I_TCON0_DCLK_GATE_BIT), 0);
  30. }
  31. static int sun4i_dclk_enable(struct clk_hw *hw)
  32. {
  33. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  34. return regmap_update_bits(dclk->regmap, SUN4I_TCON0_DCLK_REG,
  35. BIT(SUN4I_TCON0_DCLK_GATE_BIT),
  36. BIT(SUN4I_TCON0_DCLK_GATE_BIT));
  37. }
  38. static int sun4i_dclk_is_enabled(struct clk_hw *hw)
  39. {
  40. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  41. u32 val;
  42. regmap_read(dclk->regmap, SUN4I_TCON0_DCLK_REG, &val);
  43. return val & BIT(SUN4I_TCON0_DCLK_GATE_BIT);
  44. }
  45. static unsigned long sun4i_dclk_recalc_rate(struct clk_hw *hw,
  46. unsigned long parent_rate)
  47. {
  48. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  49. u32 val;
  50. regmap_read(dclk->regmap, SUN4I_TCON0_DCLK_REG, &val);
  51. val >>= SUN4I_TCON0_DCLK_DIV_SHIFT;
  52. val &= (1 << SUN4I_TCON0_DCLK_DIV_WIDTH) - 1;
  53. if (!val)
  54. val = 1;
  55. return parent_rate / val;
  56. }
  57. static long sun4i_dclk_round_rate(struct clk_hw *hw, unsigned long rate,
  58. unsigned long *parent_rate)
  59. {
  60. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  61. struct sun4i_tcon *tcon = dclk->tcon;
  62. unsigned long best_parent = 0;
  63. u8 best_div = 1;
  64. int i;
  65. for (i = tcon->dclk_min_div; i <= tcon->dclk_max_div; i++) {
  66. u64 ideal = (u64)rate * i;
  67. unsigned long rounded;
  68. /*
  69. * ideal has overflowed the max value that can be stored in an
  70. * unsigned long, and every clk operation we might do on a
  71. * truncated u64 value will give us incorrect results.
  72. * Let's just stop there since bigger dividers will result in
  73. * the same overflow issue.
  74. */
  75. if (ideal > ULONG_MAX)
  76. goto out;
  77. rounded = clk_hw_round_rate(clk_hw_get_parent(hw),
  78. ideal);
  79. if (rounded == ideal) {
  80. best_parent = rounded;
  81. best_div = i;
  82. goto out;
  83. }
  84. if (abs(rate - rounded / i) <
  85. abs(rate - best_parent / best_div)) {
  86. best_parent = rounded;
  87. best_div = i;
  88. }
  89. }
  90. out:
  91. *parent_rate = best_parent;
  92. return best_parent / best_div;
  93. }
  94. static int sun4i_dclk_set_rate(struct clk_hw *hw, unsigned long rate,
  95. unsigned long parent_rate)
  96. {
  97. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  98. u8 div = parent_rate / rate;
  99. return regmap_update_bits(dclk->regmap, SUN4I_TCON0_DCLK_REG,
  100. GENMASK(6, 0), div);
  101. }
  102. static int sun4i_dclk_get_phase(struct clk_hw *hw)
  103. {
  104. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  105. u32 val;
  106. regmap_read(dclk->regmap, SUN4I_TCON0_IO_POL_REG, &val);
  107. val >>= 28;
  108. val &= 3;
  109. return val * 120;
  110. }
  111. static int sun4i_dclk_set_phase(struct clk_hw *hw, int degrees)
  112. {
  113. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  114. u32 val = degrees / 120;
  115. val <<= 28;
  116. regmap_update_bits(dclk->regmap, SUN4I_TCON0_IO_POL_REG,
  117. GENMASK(29, 28),
  118. val);
  119. return 0;
  120. }
  121. static const struct clk_ops sun4i_dclk_ops = {
  122. .disable = sun4i_dclk_disable,
  123. .enable = sun4i_dclk_enable,
  124. .is_enabled = sun4i_dclk_is_enabled,
  125. .recalc_rate = sun4i_dclk_recalc_rate,
  126. .round_rate = sun4i_dclk_round_rate,
  127. .set_rate = sun4i_dclk_set_rate,
  128. .get_phase = sun4i_dclk_get_phase,
  129. .set_phase = sun4i_dclk_set_phase,
  130. };
  131. int sun4i_dclk_create(struct device *dev, struct sun4i_tcon *tcon)
  132. {
  133. const char *clk_name, *parent_name;
  134. struct clk_init_data init;
  135. struct sun4i_dclk *dclk;
  136. int ret;
  137. parent_name = __clk_get_name(tcon->sclk0);
  138. ret = of_property_read_string_index(dev->of_node,
  139. "clock-output-names", 0,
  140. &clk_name);
  141. if (ret)
  142. return ret;
  143. dclk = devm_kzalloc(dev, sizeof(*dclk), GFP_KERNEL);
  144. if (!dclk)
  145. return -ENOMEM;
  146. dclk->tcon = tcon;
  147. init.name = clk_name;
  148. init.ops = &sun4i_dclk_ops;
  149. init.parent_names = &parent_name;
  150. init.num_parents = 1;
  151. init.flags = CLK_SET_RATE_PARENT;
  152. dclk->regmap = tcon->regs;
  153. dclk->hw.init = &init;
  154. tcon->dclk = clk_register(dev, &dclk->hw);
  155. if (IS_ERR(tcon->dclk))
  156. return PTR_ERR(tcon->dclk);
  157. return 0;
  158. }
  159. EXPORT_SYMBOL(sun4i_dclk_create);
  160. int sun4i_dclk_free(struct sun4i_tcon *tcon)
  161. {
  162. clk_unregister(tcon->dclk);
  163. return 0;
  164. }
  165. EXPORT_SYMBOL(sun4i_dclk_free);