clk-divider.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. #include <linux/log2.h>
  19. /*
  20. * DOC: basic adjustable divider clock that cannot gate
  21. *
  22. * Traits of this clock:
  23. * prepare - clk_prepare only ensures that parents are prepared
  24. * enable - clk_enable only ensures that parents are enabled
  25. * rate - rate is adjustable. clk->rate = parent->rate / divisor
  26. * parent - fixed parent. No clk_set_parent support
  27. */
  28. #define to_clk_divider(_hw) container_of(_hw, struct clk_divider, hw)
  29. #define div_mask(d) ((1 << ((d)->width)) - 1)
  30. static unsigned int _get_table_maxdiv(const struct clk_div_table *table)
  31. {
  32. unsigned int maxdiv = 0;
  33. const struct clk_div_table *clkt;
  34. for (clkt = table; clkt->div; clkt++)
  35. if (clkt->div > maxdiv)
  36. maxdiv = clkt->div;
  37. return maxdiv;
  38. }
  39. static unsigned int _get_maxdiv(struct clk_divider *divider)
  40. {
  41. if (divider->flags & CLK_DIVIDER_ONE_BASED)
  42. return div_mask(divider);
  43. if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
  44. return 1 << div_mask(divider);
  45. if (divider->table)
  46. return _get_table_maxdiv(divider->table);
  47. return div_mask(divider) + 1;
  48. }
  49. static unsigned int _get_table_div(const struct clk_div_table *table,
  50. unsigned int val)
  51. {
  52. const struct clk_div_table *clkt;
  53. for (clkt = table; clkt->div; clkt++)
  54. if (clkt->val == val)
  55. return clkt->div;
  56. return 0;
  57. }
  58. static unsigned int _get_div(struct clk_divider *divider, unsigned int val)
  59. {
  60. if (divider->flags & CLK_DIVIDER_ONE_BASED)
  61. return val;
  62. if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
  63. return 1 << val;
  64. if (divider->table)
  65. return _get_table_div(divider->table, val);
  66. return val + 1;
  67. }
  68. static unsigned int _get_table_val(const struct clk_div_table *table,
  69. unsigned int div)
  70. {
  71. const struct clk_div_table *clkt;
  72. for (clkt = table; clkt->div; clkt++)
  73. if (clkt->div == div)
  74. return clkt->val;
  75. return 0;
  76. }
  77. static unsigned int _get_val(struct clk_divider *divider, u8 div)
  78. {
  79. if (divider->flags & CLK_DIVIDER_ONE_BASED)
  80. return div;
  81. if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
  82. return __ffs(div);
  83. if (divider->table)
  84. return _get_table_val(divider->table, div);
  85. return div - 1;
  86. }
  87. static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
  88. unsigned long parent_rate)
  89. {
  90. struct clk_divider *divider = to_clk_divider(hw);
  91. unsigned int div, val;
  92. val = readl(divider->reg) >> divider->shift;
  93. val &= div_mask(divider);
  94. div = _get_div(divider, val);
  95. if (!div) {
  96. WARN(!(divider->flags & CLK_DIVIDER_ALLOW_ZERO),
  97. "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
  98. __clk_get_name(hw->clk));
  99. return parent_rate;
  100. }
  101. return parent_rate / div;
  102. }
  103. /*
  104. * The reverse of DIV_ROUND_UP: The maximum number which
  105. * divided by m is r
  106. */
  107. #define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1)
  108. static bool _is_valid_table_div(const struct clk_div_table *table,
  109. unsigned int div)
  110. {
  111. const struct clk_div_table *clkt;
  112. for (clkt = table; clkt->div; clkt++)
  113. if (clkt->div == div)
  114. return true;
  115. return false;
  116. }
  117. static bool _is_valid_div(struct clk_divider *divider, unsigned int div)
  118. {
  119. if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
  120. return is_power_of_2(div);
  121. if (divider->table)
  122. return _is_valid_table_div(divider->table, div);
  123. return true;
  124. }
  125. static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
  126. unsigned long *best_parent_rate)
  127. {
  128. struct clk_divider *divider = to_clk_divider(hw);
  129. int i, bestdiv = 0;
  130. unsigned long parent_rate, best = 0, now, maxdiv;
  131. unsigned long parent_rate_saved = *best_parent_rate;
  132. if (!rate)
  133. rate = 1;
  134. maxdiv = _get_maxdiv(divider);
  135. if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) {
  136. parent_rate = *best_parent_rate;
  137. bestdiv = DIV_ROUND_UP(parent_rate, rate);
  138. bestdiv = bestdiv == 0 ? 1 : bestdiv;
  139. bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
  140. return bestdiv;
  141. }
  142. /*
  143. * The maximum divider we can use without overflowing
  144. * unsigned long in rate * i below
  145. */
  146. maxdiv = min(ULONG_MAX / rate, maxdiv);
  147. for (i = 1; i <= maxdiv; i++) {
  148. if (!_is_valid_div(divider, i))
  149. continue;
  150. if (rate * i == parent_rate_saved) {
  151. /*
  152. * It's the most ideal case if the requested rate can be
  153. * divided from parent clock without needing to change
  154. * parent rate, so return the divider immediately.
  155. */
  156. *best_parent_rate = parent_rate_saved;
  157. return i;
  158. }
  159. parent_rate = __clk_round_rate(__clk_get_parent(hw->clk),
  160. MULT_ROUND_UP(rate, i));
  161. now = parent_rate / i;
  162. if (now <= rate && now > best) {
  163. bestdiv = i;
  164. best = now;
  165. *best_parent_rate = parent_rate;
  166. }
  167. }
  168. if (!bestdiv) {
  169. bestdiv = _get_maxdiv(divider);
  170. *best_parent_rate = __clk_round_rate(__clk_get_parent(hw->clk), 1);
  171. }
  172. return bestdiv;
  173. }
  174. static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
  175. unsigned long *prate)
  176. {
  177. int div;
  178. div = clk_divider_bestdiv(hw, rate, prate);
  179. return *prate / div;
  180. }
  181. static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
  182. unsigned long parent_rate)
  183. {
  184. struct clk_divider *divider = to_clk_divider(hw);
  185. unsigned int div, value;
  186. unsigned long flags = 0;
  187. u32 val;
  188. div = parent_rate / rate;
  189. value = _get_val(divider, div);
  190. if (value > div_mask(divider))
  191. value = div_mask(divider);
  192. if (divider->lock)
  193. spin_lock_irqsave(divider->lock, flags);
  194. val = readl(divider->reg);
  195. val &= ~(div_mask(divider) << divider->shift);
  196. val |= value << divider->shift;
  197. writel(val, divider->reg);
  198. if (divider->lock)
  199. spin_unlock_irqrestore(divider->lock, flags);
  200. return 0;
  201. }
  202. const struct clk_ops clk_divider_ops = {
  203. .recalc_rate = clk_divider_recalc_rate,
  204. .round_rate = clk_divider_round_rate,
  205. .set_rate = clk_divider_set_rate,
  206. };
  207. EXPORT_SYMBOL_GPL(clk_divider_ops);
  208. static struct clk *_register_divider(struct device *dev, const char *name,
  209. const char *parent_name, unsigned long flags,
  210. void __iomem *reg, u8 shift, u8 width,
  211. u8 clk_divider_flags, const struct clk_div_table *table,
  212. spinlock_t *lock)
  213. {
  214. struct clk_divider *div;
  215. struct clk *clk;
  216. struct clk_init_data init;
  217. /* allocate the divider */
  218. div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL);
  219. if (!div) {
  220. pr_err("%s: could not allocate divider clk\n", __func__);
  221. return ERR_PTR(-ENOMEM);
  222. }
  223. init.name = name;
  224. init.ops = &clk_divider_ops;
  225. init.flags = flags | CLK_IS_BASIC;
  226. init.parent_names = (parent_name ? &parent_name: NULL);
  227. init.num_parents = (parent_name ? 1 : 0);
  228. /* struct clk_divider assignments */
  229. div->reg = reg;
  230. div->shift = shift;
  231. div->width = width;
  232. div->flags = clk_divider_flags;
  233. div->lock = lock;
  234. div->hw.init = &init;
  235. div->table = table;
  236. /* register the clock */
  237. clk = clk_register(dev, &div->hw);
  238. if (IS_ERR(clk))
  239. kfree(div);
  240. return clk;
  241. }
  242. /**
  243. * clk_register_divider - register a divider clock with the clock framework
  244. * @dev: device registering this clock
  245. * @name: name of this clock
  246. * @parent_name: name of clock's parent
  247. * @flags: framework-specific flags
  248. * @reg: register address to adjust divider
  249. * @shift: number of bits to shift the bitfield
  250. * @width: width of the bitfield
  251. * @clk_divider_flags: divider-specific flags for this clock
  252. * @lock: shared register lock for this clock
  253. */
  254. struct clk *clk_register_divider(struct device *dev, const char *name,
  255. const char *parent_name, unsigned long flags,
  256. void __iomem *reg, u8 shift, u8 width,
  257. u8 clk_divider_flags, spinlock_t *lock)
  258. {
  259. return _register_divider(dev, name, parent_name, flags, reg, shift,
  260. width, clk_divider_flags, NULL, lock);
  261. }
  262. /**
  263. * clk_register_divider_table - register a table based divider clock with
  264. * the clock framework
  265. * @dev: device registering this clock
  266. * @name: name of this clock
  267. * @parent_name: name of clock's parent
  268. * @flags: framework-specific flags
  269. * @reg: register address to adjust divider
  270. * @shift: number of bits to shift the bitfield
  271. * @width: width of the bitfield
  272. * @clk_divider_flags: divider-specific flags for this clock
  273. * @table: array of divider/value pairs ending with a div set to 0
  274. * @lock: shared register lock for this clock
  275. */
  276. struct clk *clk_register_divider_table(struct device *dev, const char *name,
  277. const char *parent_name, unsigned long flags,
  278. void __iomem *reg, u8 shift, u8 width,
  279. u8 clk_divider_flags, const struct clk_div_table *table,
  280. spinlock_t *lock)
  281. {
  282. return _register_divider(dev, name, parent_name, flags, reg, shift,
  283. width, clk_divider_flags, table, lock);
  284. }