clk-fractional-divider.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
  43. unsigned long *parent_rate)
  44. {
  45. struct clk_fractional_divider *fd = to_clk_fd(hw);
  46. unsigned long scale;
  47. unsigned long m, n;
  48. u64 ret;
  49. if (!rate || rate >= *parent_rate)
  50. return *parent_rate;
  51. /*
  52. * Get rate closer to *parent_rate to guarantee there is no overflow
  53. * for m and n. In the result it will be the nearest rate left shifted
  54. * by (scale - fd->nwidth) bits.
  55. */
  56. scale = fls_long(*parent_rate / rate - 1);
  57. if (scale > fd->nwidth)
  58. rate <<= scale - fd->nwidth;
  59. rational_best_approximation(rate, *parent_rate,
  60. GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
  61. &m, &n);
  62. ret = (u64)*parent_rate * m;
  63. do_div(ret, n);
  64. return ret;
  65. }
  66. static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
  67. unsigned long parent_rate)
  68. {
  69. struct clk_fractional_divider *fd = to_clk_fd(hw);
  70. unsigned long flags = 0;
  71. unsigned long m, n;
  72. u32 val;
  73. rational_best_approximation(rate, parent_rate,
  74. GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
  75. &m, &n);
  76. if (fd->lock)
  77. spin_lock_irqsave(fd->lock, flags);
  78. else
  79. __acquire(fd->lock);
  80. val = clk_readl(fd->reg);
  81. val &= ~(fd->mmask | fd->nmask);
  82. val |= (m << fd->mshift) | (n << fd->nshift);
  83. clk_writel(val, fd->reg);
  84. if (fd->lock)
  85. spin_unlock_irqrestore(fd->lock, flags);
  86. else
  87. __release(fd->lock);
  88. return 0;
  89. }
  90. const struct clk_ops clk_fractional_divider_ops = {
  91. .recalc_rate = clk_fd_recalc_rate,
  92. .round_rate = clk_fd_round_rate,
  93. .set_rate = clk_fd_set_rate,
  94. };
  95. EXPORT_SYMBOL_GPL(clk_fractional_divider_ops);
  96. struct clk_hw *clk_hw_register_fractional_divider(struct device *dev,
  97. const char *name, const char *parent_name, unsigned long flags,
  98. void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
  99. u8 clk_divider_flags, spinlock_t *lock)
  100. {
  101. struct clk_fractional_divider *fd;
  102. struct clk_init_data init = {};
  103. struct clk_hw *hw;
  104. int ret;
  105. fd = kzalloc(sizeof(*fd), GFP_KERNEL);
  106. if (!fd)
  107. return ERR_PTR(-ENOMEM);
  108. init.name = name;
  109. init.ops = &clk_fractional_divider_ops;
  110. init.flags = flags | CLK_IS_BASIC;
  111. init.parent_names = parent_name ? &parent_name : NULL;
  112. init.num_parents = parent_name ? 1 : 0;
  113. fd->reg = reg;
  114. fd->mshift = mshift;
  115. fd->mwidth = mwidth;
  116. fd->mmask = GENMASK(mwidth - 1, 0) << mshift;
  117. fd->nshift = nshift;
  118. fd->nwidth = nwidth;
  119. fd->nmask = GENMASK(nwidth - 1, 0) << nshift;
  120. fd->flags = clk_divider_flags;
  121. fd->lock = lock;
  122. fd->hw.init = &init;
  123. hw = &fd->hw;
  124. ret = clk_hw_register(dev, hw);
  125. if (ret) {
  126. kfree(fd);
  127. hw = ERR_PTR(ret);
  128. }
  129. return hw;
  130. }
  131. EXPORT_SYMBOL_GPL(clk_hw_register_fractional_divider);
  132. struct clk *clk_register_fractional_divider(struct device *dev,
  133. const char *name, const char *parent_name, unsigned long flags,
  134. void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
  135. u8 clk_divider_flags, spinlock_t *lock)
  136. {
  137. struct clk_hw *hw;
  138. hw = clk_hw_register_fractional_divider(dev, name, parent_name, flags,
  139. reg, mshift, mwidth, nshift, nwidth, clk_divider_flags,
  140. lock);
  141. if (IS_ERR(hw))
  142. return ERR_CAST(hw);
  143. return hw->clk;
  144. }
  145. EXPORT_SYMBOL_GPL(clk_register_fractional_divider);
  146. void clk_hw_unregister_fractional_divider(struct clk_hw *hw)
  147. {
  148. struct clk_fractional_divider *fd;
  149. fd = to_clk_fd(hw);
  150. clk_hw_unregister(hw);
  151. kfree(fd);
  152. }