clk-fractional-divider.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Intel Corporation
  4. *
  5. * Adjustable fractional divider clock implementation.
  6. * Uses rational best approximation algorithm.
  7. *
  8. * Output is calculated as
  9. *
  10. * rate = (m / n) * parent_rate (1)
  11. *
  12. * This is useful when we have a prescaler block which asks for
  13. * m (numerator) and n (denominator) values to be provided to satisfy
  14. * the (1) as much as possible.
  15. *
  16. * Since m and n have the limitation by a range, e.g.
  17. *
  18. * n >= 1, n < N_width, where N_width = 2^nwidth (2)
  19. *
  20. * for some cases the output may be saturated. Hence, from (1) and (2),
  21. * assuming the worst case when m = 1, the inequality
  22. *
  23. * floor(log2(parent_rate / rate)) <= nwidth (3)
  24. *
  25. * may be derived. Thus, in cases when
  26. *
  27. * (parent_rate / rate) >> N_width (4)
  28. *
  29. * we might scale up the rate by 2^scale (see the description of
  30. * CLK_FRAC_DIVIDER_POWER_OF_TWO_PS for additional information), where
  31. *
  32. * scale = floor(log2(parent_rate / rate)) - nwidth (5)
  33. *
  34. * and assume that the IP, that needs m and n, has also its own
  35. * prescaler, which is capable to divide by 2^scale. In this way
  36. * we get the denominator to satisfy the desired range (2) and
  37. * at the same time a much better result of m and n than simple
  38. * saturated values.
  39. */
  40. #include <linux/clk-provider.h>
  41. #include <linux/io.h>
  42. #include <linux/module.h>
  43. #include <linux/device.h>
  44. #include <linux/slab.h>
  45. #include <linux/rational.h>
  46. #include "clk-fractional-divider.h"
  47. static inline u32 clk_fd_readl(struct clk_fractional_divider *fd)
  48. {
  49. if (fd->flags & CLK_FRAC_DIVIDER_BIG_ENDIAN)
  50. return ioread32be(fd->reg);
  51. return readl(fd->reg);
  52. }
  53. static inline void clk_fd_writel(struct clk_fractional_divider *fd, u32 val)
  54. {
  55. if (fd->flags & CLK_FRAC_DIVIDER_BIG_ENDIAN)
  56. iowrite32be(val, fd->reg);
  57. else
  58. writel(val, fd->reg);
  59. }
  60. static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
  61. unsigned long parent_rate)
  62. {
  63. struct clk_fractional_divider *fd = to_clk_fd(hw);
  64. unsigned long flags = 0;
  65. unsigned long m, n;
  66. u32 val;
  67. u64 ret;
  68. if (fd->lock)
  69. spin_lock_irqsave(fd->lock, flags);
  70. else
  71. __acquire(fd->lock);
  72. val = clk_fd_readl(fd);
  73. if (fd->lock)
  74. spin_unlock_irqrestore(fd->lock, flags);
  75. else
  76. __release(fd->lock);
  77. m = (val & fd->mmask) >> fd->mshift;
  78. n = (val & fd->nmask) >> fd->nshift;
  79. if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) {
  80. m++;
  81. n++;
  82. }
  83. if (!n || !m)
  84. return parent_rate;
  85. ret = (u64)parent_rate * m;
  86. do_div(ret, n);
  87. return ret;
  88. }
  89. void clk_fractional_divider_general_approximation(struct clk_hw *hw,
  90. unsigned long rate,
  91. unsigned long *parent_rate,
  92. unsigned long *m, unsigned long *n)
  93. {
  94. struct clk_fractional_divider *fd = to_clk_fd(hw);
  95. /*
  96. * Get rate closer to *parent_rate to guarantee there is no overflow
  97. * for m and n. In the result it will be the nearest rate left shifted
  98. * by (scale - fd->nwidth) bits.
  99. *
  100. * For the detailed explanation see the top comment in this file.
  101. */
  102. if (fd->flags & CLK_FRAC_DIVIDER_POWER_OF_TWO_PS) {
  103. unsigned long scale = fls_long(*parent_rate / rate - 1);
  104. if (scale > fd->nwidth)
  105. rate <<= scale - fd->nwidth;
  106. }
  107. rational_best_approximation(rate, *parent_rate,
  108. GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
  109. m, n);
  110. }
  111. EXPORT_SYMBOL_GPL(clk_fractional_divider_general_approximation);
  112. static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
  113. unsigned long *parent_rate)
  114. {
  115. struct clk_fractional_divider *fd = to_clk_fd(hw);
  116. unsigned long m, n;
  117. u64 ret;
  118. if (!rate || (!clk_hw_can_set_rate_parent(hw) && rate >= *parent_rate))
  119. return *parent_rate;
  120. if (fd->approximation)
  121. fd->approximation(hw, rate, parent_rate, &m, &n);
  122. else
  123. clk_fractional_divider_general_approximation(hw, rate, parent_rate, &m, &n);
  124. ret = (u64)*parent_rate * m;
  125. do_div(ret, n);
  126. return ret;
  127. }
  128. static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
  129. unsigned long parent_rate)
  130. {
  131. struct clk_fractional_divider *fd = to_clk_fd(hw);
  132. unsigned long flags = 0;
  133. unsigned long m, n;
  134. u32 val;
  135. rational_best_approximation(rate, parent_rate,
  136. GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
  137. &m, &n);
  138. if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) {
  139. m--;
  140. n--;
  141. }
  142. if (fd->lock)
  143. spin_lock_irqsave(fd->lock, flags);
  144. else
  145. __acquire(fd->lock);
  146. val = clk_fd_readl(fd);
  147. val &= ~(fd->mmask | fd->nmask);
  148. val |= (m << fd->mshift) | (n << fd->nshift);
  149. clk_fd_writel(fd, val);
  150. if (fd->lock)
  151. spin_unlock_irqrestore(fd->lock, flags);
  152. else
  153. __release(fd->lock);
  154. return 0;
  155. }
  156. const struct clk_ops clk_fractional_divider_ops = {
  157. .recalc_rate = clk_fd_recalc_rate,
  158. .round_rate = clk_fd_round_rate,
  159. .set_rate = clk_fd_set_rate,
  160. };
  161. EXPORT_SYMBOL_GPL(clk_fractional_divider_ops);
  162. struct clk_hw *clk_hw_register_fractional_divider(struct device *dev,
  163. const char *name, const char *parent_name, unsigned long flags,
  164. void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
  165. u8 clk_divider_flags, spinlock_t *lock)
  166. {
  167. struct clk_fractional_divider *fd;
  168. struct clk_init_data init;
  169. struct clk_hw *hw;
  170. int ret;
  171. fd = kzalloc(sizeof(*fd), GFP_KERNEL);
  172. if (!fd)
  173. return ERR_PTR(-ENOMEM);
  174. init.name = name;
  175. init.ops = &clk_fractional_divider_ops;
  176. init.flags = flags;
  177. init.parent_names = parent_name ? &parent_name : NULL;
  178. init.num_parents = parent_name ? 1 : 0;
  179. fd->reg = reg;
  180. fd->mshift = mshift;
  181. fd->mwidth = mwidth;
  182. fd->mmask = GENMASK(mwidth - 1, 0) << mshift;
  183. fd->nshift = nshift;
  184. fd->nwidth = nwidth;
  185. fd->nmask = GENMASK(nwidth - 1, 0) << nshift;
  186. fd->flags = clk_divider_flags;
  187. fd->lock = lock;
  188. fd->hw.init = &init;
  189. hw = &fd->hw;
  190. ret = clk_hw_register(dev, hw);
  191. if (ret) {
  192. kfree(fd);
  193. hw = ERR_PTR(ret);
  194. }
  195. return hw;
  196. }
  197. EXPORT_SYMBOL_GPL(clk_hw_register_fractional_divider);
  198. struct clk *clk_register_fractional_divider(struct device *dev,
  199. const char *name, const char *parent_name, unsigned long flags,
  200. void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
  201. u8 clk_divider_flags, spinlock_t *lock)
  202. {
  203. struct clk_hw *hw;
  204. hw = clk_hw_register_fractional_divider(dev, name, parent_name, flags,
  205. reg, mshift, mwidth, nshift, nwidth, clk_divider_flags,
  206. lock);
  207. if (IS_ERR(hw))
  208. return ERR_CAST(hw);
  209. return hw->clk;
  210. }
  211. EXPORT_SYMBOL_GPL(clk_register_fractional_divider);
  212. void clk_hw_unregister_fractional_divider(struct clk_hw *hw)
  213. {
  214. struct clk_fractional_divider *fd;
  215. fd = to_clk_fd(hw);
  216. clk_hw_unregister(hw);
  217. kfree(fd);
  218. }