sun4i_dotclock.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. unsigned long ideal = rate * i;
  67. unsigned long rounded;
  68. rounded = clk_hw_round_rate(clk_hw_get_parent(hw),
  69. ideal);
  70. if (rounded == ideal) {
  71. best_parent = rounded;
  72. best_div = i;
  73. goto out;
  74. }
  75. if (abs(rate - rounded / i) <
  76. abs(rate - best_parent / best_div)) {
  77. best_parent = rounded;
  78. best_div = i;
  79. }
  80. }
  81. out:
  82. *parent_rate = best_parent;
  83. return best_parent / best_div;
  84. }
  85. static int sun4i_dclk_set_rate(struct clk_hw *hw, unsigned long rate,
  86. unsigned long parent_rate)
  87. {
  88. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  89. u8 div = parent_rate / rate;
  90. return regmap_update_bits(dclk->regmap, SUN4I_TCON0_DCLK_REG,
  91. GENMASK(6, 0), div);
  92. }
  93. static int sun4i_dclk_get_phase(struct clk_hw *hw)
  94. {
  95. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  96. u32 val;
  97. regmap_read(dclk->regmap, SUN4I_TCON0_IO_POL_REG, &val);
  98. val >>= 28;
  99. val &= 3;
  100. return val * 120;
  101. }
  102. static int sun4i_dclk_set_phase(struct clk_hw *hw, int degrees)
  103. {
  104. struct sun4i_dclk *dclk = hw_to_dclk(hw);
  105. u32 val = degrees / 120;
  106. val <<= 28;
  107. regmap_update_bits(dclk->regmap, SUN4I_TCON0_IO_POL_REG,
  108. GENMASK(29, 28),
  109. val);
  110. return 0;
  111. }
  112. static const struct clk_ops sun4i_dclk_ops = {
  113. .disable = sun4i_dclk_disable,
  114. .enable = sun4i_dclk_enable,
  115. .is_enabled = sun4i_dclk_is_enabled,
  116. .recalc_rate = sun4i_dclk_recalc_rate,
  117. .round_rate = sun4i_dclk_round_rate,
  118. .set_rate = sun4i_dclk_set_rate,
  119. .get_phase = sun4i_dclk_get_phase,
  120. .set_phase = sun4i_dclk_set_phase,
  121. };
  122. int sun4i_dclk_create(struct device *dev, struct sun4i_tcon *tcon)
  123. {
  124. const char *clk_name, *parent_name;
  125. struct clk_init_data init;
  126. struct sun4i_dclk *dclk;
  127. int ret;
  128. parent_name = __clk_get_name(tcon->sclk0);
  129. ret = of_property_read_string_index(dev->of_node,
  130. "clock-output-names", 0,
  131. &clk_name);
  132. if (ret)
  133. return ret;
  134. dclk = devm_kzalloc(dev, sizeof(*dclk), GFP_KERNEL);
  135. if (!dclk)
  136. return -ENOMEM;
  137. dclk->tcon = tcon;
  138. init.name = clk_name;
  139. init.ops = &sun4i_dclk_ops;
  140. init.parent_names = &parent_name;
  141. init.num_parents = 1;
  142. init.flags = CLK_SET_RATE_PARENT;
  143. dclk->regmap = tcon->regs;
  144. dclk->hw.init = &init;
  145. tcon->dclk = clk_register(dev, &dclk->hw);
  146. if (IS_ERR(tcon->dclk))
  147. return PTR_ERR(tcon->dclk);
  148. return 0;
  149. }
  150. EXPORT_SYMBOL(sun4i_dclk_create);
  151. int sun4i_dclk_free(struct sun4i_tcon *tcon)
  152. {
  153. clk_unregister(tcon->dclk);
  154. return 0;
  155. }
  156. EXPORT_SYMBOL(sun4i_dclk_free);