clk-fractional-divider.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (C) 2014 Intel Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Adjustable fractional divider clock implementation.
  9. * Output rate = (m / n) * parent_rate.
  10. * Uses rational best approximation algorithm.
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/module.h>
  14. #include <linux/device.h>
  15. #include <linux/slab.h>
  16. #include <linux/rational.h>
  17. static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
  18. unsigned long parent_rate)
  19. {
  20. struct clk_fractional_divider *fd = to_clk_fd(hw);
  21. unsigned long flags = 0;
  22. unsigned long m, n;
  23. u32 val;
  24. u64 ret;
  25. if (fd->lock)
  26. spin_lock_irqsave(fd->lock, flags);
  27. else
  28. __acquire(fd->lock);
  29. val = clk_readl(fd->reg);
  30. if (fd->lock)
  31. spin_unlock_irqrestore(fd->lock, flags);
  32. else
  33. __release(fd->lock);
  34. m = (val & fd->mmask) >> fd->mshift;
  35. n = (val & fd->nmask) >> fd->nshift;
  36. if (!n || !m)
  37. return parent_rate;
  38. ret = (u64)parent_rate * m;
  39. do_div(ret, n);
  40. return ret;
  41. }
  42. static void clk_fd_general_approximation(struct clk_hw *hw, unsigned long rate,
  43. unsigned long *parent_rate,
  44. unsigned long *m, unsigned long *n)
  45. {
  46. struct clk_fractional_divider *fd = to_clk_fd(hw);
  47. unsigned long scale;
  48. /*
  49. * Get rate closer to *parent_rate to guarantee there is no overflow
  50. * for m and n. In the result it will be the nearest rate left shifted
  51. * by (scale - fd->nwidth) bits.
  52. */
  53. scale = fls_long(*parent_rate / rate - 1);
  54. if (scale > fd->nwidth)
  55. rate <<= scale - fd->nwidth;
  56. rational_best_approximation(rate, *parent_rate,
  57. GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
  58. m, n);
  59. }
  60. static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
  61. unsigned long *parent_rate)
  62. {
  63. struct clk_fractional_divider *fd = to_clk_fd(hw);
  64. unsigned long m, n;
  65. u64 ret;
  66. if (!rate || (!clk_hw_can_set_rate_parent(hw) && rate >= *parent_rate))
  67. return *parent_rate;
  68. if (fd->approximation)
  69. fd->approximation(hw, rate, parent_rate, &m, &n);
  70. else
  71. clk_fd_general_approximation(hw, rate, parent_rate, &m, &n);
  72. ret = (u64)*parent_rate * m;
  73. do_div(ret, n);
  74. return ret;
  75. }
  76. static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
  77. unsigned long parent_rate)
  78. {
  79. struct clk_fractional_divider *fd = to_clk_fd(hw);
  80. unsigned long flags = 0;
  81. unsigned long m, n;
  82. u32 val;
  83. rational_best_approximation(rate, parent_rate,
  84. GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
  85. &m, &n);
  86. if (fd->lock)
  87. spin_lock_irqsave(fd->lock, flags);
  88. else
  89. __acquire(fd->lock);
  90. val = clk_readl(fd->reg);
  91. val &= ~(fd->mmask | fd->nmask);
  92. val |= (m << fd->mshift) | (n << fd->nshift);
  93. clk_writel(val, fd->reg);
  94. if (fd->lock)
  95. spin_unlock_irqrestore(fd->lock, flags);
  96. else
  97. __release(fd->lock);
  98. return 0;
  99. }
  100. const struct clk_ops clk_fractional_divider_ops = {
  101. .recalc_rate = clk_fd_recalc_rate,
  102. .round_rate = clk_fd_round_rate,
  103. .set_rate = clk_fd_set_rate,
  104. };
  105. EXPORT_SYMBOL_GPL(clk_fractional_divider_ops);
  106. struct clk_hw *clk_hw_register_fractional_divider(struct device *dev,
  107. const char *name, const char *parent_name, unsigned long flags,
  108. void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
  109. u8 clk_divider_flags, spinlock_t *lock)
  110. {
  111. struct clk_fractional_divider *fd;
  112. struct clk_init_data init;
  113. struct clk_hw *hw;
  114. int ret;
  115. fd = kzalloc(sizeof(*fd), GFP_KERNEL);
  116. if (!fd)
  117. return ERR_PTR(-ENOMEM);
  118. init.name = name;
  119. init.ops = &clk_fractional_divider_ops;
  120. init.flags = flags | CLK_IS_BASIC;
  121. init.parent_names = parent_name ? &parent_name : NULL;
  122. init.num_parents = parent_name ? 1 : 0;
  123. fd->reg = reg;
  124. fd->mshift = mshift;
  125. fd->mwidth = mwidth;
  126. fd->mmask = GENMASK(mwidth - 1, 0) << mshift;
  127. fd->nshift = nshift;
  128. fd->nwidth = nwidth;
  129. fd->nmask = GENMASK(nwidth - 1, 0) << nshift;
  130. fd->flags = clk_divider_flags;
  131. fd->lock = lock;
  132. fd->hw.init = &init;
  133. hw = &fd->hw;
  134. ret = clk_hw_register(dev, hw);
  135. if (ret) {
  136. kfree(fd);
  137. hw = ERR_PTR(ret);
  138. }
  139. return hw;
  140. }
  141. EXPORT_SYMBOL_GPL(clk_hw_register_fractional_divider);
  142. struct clk *clk_register_fractional_divider(struct device *dev,
  143. const char *name, const char *parent_name, unsigned long flags,
  144. void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
  145. u8 clk_divider_flags, spinlock_t *lock)
  146. {
  147. struct clk_hw *hw;
  148. hw = clk_hw_register_fractional_divider(dev, name, parent_name, flags,
  149. reg, mshift, mwidth, nshift, nwidth, clk_divider_flags,
  150. lock);
  151. if (IS_ERR(hw))
  152. return ERR_CAST(hw);
  153. return hw->clk;
  154. }
  155. EXPORT_SYMBOL_GPL(clk_register_fractional_divider);
  156. void clk_hw_unregister_fractional_divider(struct clk_hw *hw)
  157. {
  158. struct clk_fractional_divider *fd;
  159. fd = to_clk_fd(hw);
  160. clk_hw_unregister(hw);
  161. kfree(fd);
  162. }