clk-divider.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  3. * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
  4. * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Adjustable divider clock implementation
  11. */
  12. #include <linux/clk-provider.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/io.h>
  16. #include <linux/err.h>
  17. #include <linux/string.h>
  18. /*
  19. * DOC: basic adjustable divider clock that cannot gate
  20. *
  21. * Traits of this clock:
  22. * prepare - clk_prepare only ensures that parents are prepared
  23. * enable - clk_enable only ensures that parents are enabled
  24. * rate - rate is adjustable. clk->rate = parent->rate / divisor
  25. * parent - fixed parent. No clk_set_parent support
  26. */
  27. #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
  28. #define div_mask(d) ((1 << (d->width)) - 1)
  29. static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
  30. unsigned long parent_rate)
  31. {
  32. struct clk_divider *divider = to_clk_divider(hw);
  33. unsigned int div;
  34. div = readl(divider->reg) >> divider->shift;
  35. div &= div_mask(divider);
  36. if (!(divider->flags & CLK_DIVIDER_ONE_BASED))
  37. div++;
  38. return parent_rate / div;
  39. }
  40. EXPORT_SYMBOL_GPL(clk_divider_recalc_rate);
  41. /*
  42. * The reverse of DIV_ROUND_UP: The maximum number which
  43. * divided by m is r
  44. */
  45. #define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1)
  46. static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
  47. unsigned long *best_parent_rate)
  48. {
  49. struct clk_divider *divider = to_clk_divider(hw);
  50. int i, bestdiv = 0;
  51. unsigned long parent_rate, best = 0, now, maxdiv;
  52. if (!rate)
  53. rate = 1;
  54. maxdiv = (1 << divider->width);
  55. if (divider->flags & CLK_DIVIDER_ONE_BASED)
  56. maxdiv--;
  57. if (!best_parent_rate) {
  58. parent_rate = __clk_get_rate(__clk_get_parent(hw->clk));
  59. bestdiv = DIV_ROUND_UP(parent_rate, rate);
  60. bestdiv = bestdiv == 0 ? 1 : bestdiv;
  61. bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
  62. return bestdiv;
  63. }
  64. /*
  65. * The maximum divider we can use without overflowing
  66. * unsigned long in rate * i below
  67. */
  68. maxdiv = min(ULONG_MAX / rate, maxdiv);
  69. for (i = 1; i <= maxdiv; i++) {
  70. parent_rate = __clk_round_rate(__clk_get_parent(hw->clk),
  71. MULT_ROUND_UP(rate, i));
  72. now = parent_rate / i;
  73. if (now <= rate && now > best) {
  74. bestdiv = i;
  75. best = now;
  76. *best_parent_rate = parent_rate;
  77. }
  78. }
  79. if (!bestdiv) {
  80. bestdiv = (1 << divider->width);
  81. if (divider->flags & CLK_DIVIDER_ONE_BASED)
  82. bestdiv--;
  83. *best_parent_rate = __clk_round_rate(__clk_get_parent(hw->clk), 1);
  84. }
  85. return bestdiv;
  86. }
  87. static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
  88. unsigned long *prate)
  89. {
  90. int div;
  91. div = clk_divider_bestdiv(hw, rate, prate);
  92. if (prate)
  93. return *prate / div;
  94. else {
  95. unsigned long r;
  96. r = __clk_get_rate(__clk_get_parent(hw->clk));
  97. return r / div;
  98. }
  99. }
  100. EXPORT_SYMBOL_GPL(clk_divider_round_rate);
  101. static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate)
  102. {
  103. struct clk_divider *divider = to_clk_divider(hw);
  104. unsigned int div;
  105. unsigned long flags = 0;
  106. u32 val;
  107. div = __clk_get_rate(__clk_get_parent(hw->clk)) / rate;
  108. if (!(divider->flags & CLK_DIVIDER_ONE_BASED))
  109. div--;
  110. if (div > div_mask(divider))
  111. div = div_mask(divider);
  112. if (divider->lock)
  113. spin_lock_irqsave(divider->lock, flags);
  114. val = readl(divider->reg);
  115. val &= ~(div_mask(divider) << divider->shift);
  116. val |= div << divider->shift;
  117. writel(val, divider->reg);
  118. if (divider->lock)
  119. spin_unlock_irqrestore(divider->lock, flags);
  120. return 0;
  121. }
  122. EXPORT_SYMBOL_GPL(clk_divider_set_rate);
  123. struct clk_ops clk_divider_ops = {
  124. .recalc_rate = clk_divider_recalc_rate,
  125. .round_rate = clk_divider_round_rate,
  126. .set_rate = clk_divider_set_rate,
  127. };
  128. EXPORT_SYMBOL_GPL(clk_divider_ops);
  129. struct clk *clk_register_divider(struct device *dev, const char *name,
  130. const char *parent_name, unsigned long flags,
  131. void __iomem *reg, u8 shift, u8 width,
  132. u8 clk_divider_flags, spinlock_t *lock)
  133. {
  134. struct clk_divider *div;
  135. struct clk *clk;
  136. div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL);
  137. if (!div) {
  138. pr_err("%s: could not allocate divider clk\n", __func__);
  139. return NULL;
  140. }
  141. /* struct clk_divider assignments */
  142. div->reg = reg;
  143. div->shift = shift;
  144. div->width = width;
  145. div->flags = clk_divider_flags;
  146. div->lock = lock;
  147. if (parent_name) {
  148. div->parent[0] = kstrdup(parent_name, GFP_KERNEL);
  149. if (!div->parent[0])
  150. goto out;
  151. }
  152. clk = clk_register(dev, name,
  153. &clk_divider_ops, &div->hw,
  154. div->parent,
  155. (parent_name ? 1 : 0),
  156. flags);
  157. if (clk)
  158. return clk;
  159. out:
  160. kfree(div->parent[0]);
  161. kfree(div);
  162. return NULL;
  163. }