clk-divider.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /*
  41. * The reverse of DIV_ROUND_UP: The maximum number which
  42. * divided by m is r
  43. */
  44. #define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1)
  45. static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
  46. unsigned long *best_parent_rate)
  47. {
  48. struct clk_divider *divider = to_clk_divider(hw);
  49. int i, bestdiv = 0;
  50. unsigned long parent_rate, best = 0, now, maxdiv;
  51. if (!rate)
  52. rate = 1;
  53. maxdiv = (1 << divider->width);
  54. if (divider->flags & CLK_DIVIDER_ONE_BASED)
  55. maxdiv--;
  56. if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) {
  57. parent_rate = *best_parent_rate;
  58. bestdiv = DIV_ROUND_UP(parent_rate, rate);
  59. bestdiv = bestdiv == 0 ? 1 : bestdiv;
  60. bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
  61. return bestdiv;
  62. }
  63. /*
  64. * The maximum divider we can use without overflowing
  65. * unsigned long in rate * i below
  66. */
  67. maxdiv = min(ULONG_MAX / rate, maxdiv);
  68. for (i = 1; i <= maxdiv; i++) {
  69. parent_rate = __clk_round_rate(__clk_get_parent(hw->clk),
  70. MULT_ROUND_UP(rate, i));
  71. now = parent_rate / i;
  72. if (now <= rate && now > best) {
  73. bestdiv = i;
  74. best = now;
  75. *best_parent_rate = parent_rate;
  76. }
  77. }
  78. if (!bestdiv) {
  79. bestdiv = (1 << divider->width);
  80. if (divider->flags & CLK_DIVIDER_ONE_BASED)
  81. bestdiv--;
  82. *best_parent_rate = __clk_round_rate(__clk_get_parent(hw->clk), 1);
  83. }
  84. return bestdiv;
  85. }
  86. static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
  87. unsigned long *prate)
  88. {
  89. int div;
  90. div = clk_divider_bestdiv(hw, rate, prate);
  91. return *prate / div;
  92. }
  93. static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
  94. unsigned long parent_rate)
  95. {
  96. struct clk_divider *divider = to_clk_divider(hw);
  97. unsigned int div;
  98. unsigned long flags = 0;
  99. u32 val;
  100. div = parent_rate / rate;
  101. if (!(divider->flags & CLK_DIVIDER_ONE_BASED))
  102. div--;
  103. if (div > div_mask(divider))
  104. div = div_mask(divider);
  105. if (divider->lock)
  106. spin_lock_irqsave(divider->lock, flags);
  107. val = readl(divider->reg);
  108. val &= ~(div_mask(divider) << divider->shift);
  109. val |= div << divider->shift;
  110. writel(val, divider->reg);
  111. if (divider->lock)
  112. spin_unlock_irqrestore(divider->lock, flags);
  113. return 0;
  114. }
  115. const struct clk_ops clk_divider_ops = {
  116. .recalc_rate = clk_divider_recalc_rate,
  117. .round_rate = clk_divider_round_rate,
  118. .set_rate = clk_divider_set_rate,
  119. };
  120. EXPORT_SYMBOL_GPL(clk_divider_ops);
  121. /**
  122. * clk_register_divider - register a divider clock with the clock framework
  123. * @dev: device registering this clock
  124. * @name: name of this clock
  125. * @parent_name: name of clock's parent
  126. * @flags: framework-specific flags
  127. * @reg: register address to adjust divider
  128. * @shift: number of bits to shift the bitfield
  129. * @width: width of the bitfield
  130. * @clk_divider_flags: divider-specific flags for this clock
  131. * @lock: shared register lock for this clock
  132. */
  133. struct clk *clk_register_divider(struct device *dev, const char *name,
  134. const char *parent_name, unsigned long flags,
  135. void __iomem *reg, u8 shift, u8 width,
  136. u8 clk_divider_flags, spinlock_t *lock)
  137. {
  138. struct clk_divider *div;
  139. struct clk *clk;
  140. struct clk_init_data init;
  141. /* allocate the divider */
  142. div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL);
  143. if (!div) {
  144. pr_err("%s: could not allocate divider clk\n", __func__);
  145. return ERR_PTR(-ENOMEM);
  146. }
  147. init.name = name;
  148. init.ops = &clk_divider_ops;
  149. init.flags = flags;
  150. init.parent_names = (parent_name ? &parent_name: NULL);
  151. init.num_parents = (parent_name ? 1 : 0);
  152. /* struct clk_divider assignments */
  153. div->reg = reg;
  154. div->shift = shift;
  155. div->width = width;
  156. div->flags = clk_divider_flags;
  157. div->lock = lock;
  158. div->hw.init = &init;
  159. /* register the clock */
  160. clk = clk_register(dev, &div->hw);
  161. if (IS_ERR(clk))
  162. kfree(div);
  163. return clk;
  164. }