clk-divider.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 = DIV_ROUND_UP(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(width) ((1 << (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_table_mindiv(const struct clk_div_table *table)
  40. {
  41. unsigned int mindiv = UINT_MAX;
  42. const struct clk_div_table *clkt;
  43. for (clkt = table; clkt->div; clkt++)
  44. if (clkt->div < mindiv)
  45. mindiv = clkt->div;
  46. return mindiv;
  47. }
  48. static unsigned int _get_maxdiv(const struct clk_div_table *table, u8 width,
  49. unsigned long flags)
  50. {
  51. if (flags & CLK_DIVIDER_ONE_BASED)
  52. return div_mask(width);
  53. if (flags & CLK_DIVIDER_POWER_OF_TWO)
  54. return 1 << div_mask(width);
  55. if (table)
  56. return _get_table_maxdiv(table);
  57. return div_mask(width) + 1;
  58. }
  59. static unsigned int _get_table_div(const struct clk_div_table *table,
  60. unsigned int val)
  61. {
  62. const struct clk_div_table *clkt;
  63. for (clkt = table; clkt->div; clkt++)
  64. if (clkt->val == val)
  65. return clkt->div;
  66. return 0;
  67. }
  68. static unsigned int _get_div(const struct clk_div_table *table,
  69. unsigned int val, unsigned long flags)
  70. {
  71. if (flags & CLK_DIVIDER_ONE_BASED)
  72. return val;
  73. if (flags & CLK_DIVIDER_POWER_OF_TWO)
  74. return 1 << val;
  75. if (table)
  76. return _get_table_div(table, val);
  77. return val + 1;
  78. }
  79. static unsigned int _get_table_val(const struct clk_div_table *table,
  80. unsigned int div)
  81. {
  82. const struct clk_div_table *clkt;
  83. for (clkt = table; clkt->div; clkt++)
  84. if (clkt->div == div)
  85. return clkt->val;
  86. return 0;
  87. }
  88. static unsigned int _get_val(const struct clk_div_table *table,
  89. unsigned int div, unsigned long flags)
  90. {
  91. if (flags & CLK_DIVIDER_ONE_BASED)
  92. return div;
  93. if (flags & CLK_DIVIDER_POWER_OF_TWO)
  94. return __ffs(div);
  95. if (table)
  96. return _get_table_val(table, div);
  97. return div - 1;
  98. }
  99. unsigned long divider_recalc_rate(struct clk_hw *hw, unsigned long parent_rate,
  100. unsigned int val,
  101. const struct clk_div_table *table,
  102. unsigned long flags)
  103. {
  104. unsigned int div;
  105. div = _get_div(table, val, flags);
  106. if (!div) {
  107. WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO),
  108. "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
  109. __clk_get_name(hw->clk));
  110. return parent_rate;
  111. }
  112. return DIV_ROUND_UP(parent_rate, div);
  113. }
  114. EXPORT_SYMBOL_GPL(divider_recalc_rate);
  115. static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
  116. unsigned long parent_rate)
  117. {
  118. struct clk_divider *divider = to_clk_divider(hw);
  119. unsigned int val;
  120. val = clk_readl(divider->reg) >> divider->shift;
  121. val &= div_mask(divider->width);
  122. return divider_recalc_rate(hw, parent_rate, val, divider->table,
  123. divider->flags);
  124. }
  125. static bool _is_valid_table_div(const struct clk_div_table *table,
  126. unsigned int div)
  127. {
  128. const struct clk_div_table *clkt;
  129. for (clkt = table; clkt->div; clkt++)
  130. if (clkt->div == div)
  131. return true;
  132. return false;
  133. }
  134. static bool _is_valid_div(const struct clk_div_table *table, unsigned int div,
  135. unsigned long flags)
  136. {
  137. if (flags & CLK_DIVIDER_POWER_OF_TWO)
  138. return is_power_of_2(div);
  139. if (table)
  140. return _is_valid_table_div(table, div);
  141. return true;
  142. }
  143. static int _round_up_table(const struct clk_div_table *table, int div)
  144. {
  145. const struct clk_div_table *clkt;
  146. int up = INT_MAX;
  147. for (clkt = table; clkt->div; clkt++) {
  148. if (clkt->div == div)
  149. return clkt->div;
  150. else if (clkt->div < div)
  151. continue;
  152. if ((clkt->div - div) < (up - div))
  153. up = clkt->div;
  154. }
  155. return up;
  156. }
  157. static int _round_down_table(const struct clk_div_table *table, int div)
  158. {
  159. const struct clk_div_table *clkt;
  160. int down = _get_table_mindiv(table);
  161. for (clkt = table; clkt->div; clkt++) {
  162. if (clkt->div == div)
  163. return clkt->div;
  164. else if (clkt->div > div)
  165. continue;
  166. if ((div - clkt->div) < (div - down))
  167. down = clkt->div;
  168. }
  169. return down;
  170. }
  171. static int _div_round_up(const struct clk_div_table *table,
  172. unsigned long parent_rate, unsigned long rate,
  173. unsigned long flags)
  174. {
  175. int div = DIV_ROUND_UP(parent_rate, rate);
  176. if (flags & CLK_DIVIDER_POWER_OF_TWO)
  177. div = __roundup_pow_of_two(div);
  178. if (table)
  179. div = _round_up_table(table, div);
  180. return div;
  181. }
  182. static int _div_round_closest(const struct clk_div_table *table,
  183. unsigned long parent_rate, unsigned long rate,
  184. unsigned long flags)
  185. {
  186. int up, down;
  187. unsigned long up_rate, down_rate;
  188. up = DIV_ROUND_UP(parent_rate, rate);
  189. down = parent_rate / rate;
  190. if (flags & CLK_DIVIDER_POWER_OF_TWO) {
  191. up = __roundup_pow_of_two(up);
  192. down = __rounddown_pow_of_two(down);
  193. } else if (table) {
  194. up = _round_up_table(table, up);
  195. down = _round_down_table(table, down);
  196. }
  197. up_rate = DIV_ROUND_UP(parent_rate, up);
  198. down_rate = DIV_ROUND_UP(parent_rate, down);
  199. return (rate - up_rate) <= (down_rate - rate) ? up : down;
  200. }
  201. static int _div_round(const struct clk_div_table *table,
  202. unsigned long parent_rate, unsigned long rate,
  203. unsigned long flags)
  204. {
  205. if (flags & CLK_DIVIDER_ROUND_CLOSEST)
  206. return _div_round_closest(table, parent_rate, rate, flags);
  207. return _div_round_up(table, parent_rate, rate, flags);
  208. }
  209. static bool _is_best_div(unsigned long rate, unsigned long now,
  210. unsigned long best, unsigned long flags)
  211. {
  212. if (flags & CLK_DIVIDER_ROUND_CLOSEST)
  213. return abs(rate - now) < abs(rate - best);
  214. return now <= rate && now > best;
  215. }
  216. static int _next_div(const struct clk_div_table *table, int div,
  217. unsigned long flags)
  218. {
  219. div++;
  220. if (flags & CLK_DIVIDER_POWER_OF_TWO)
  221. return __roundup_pow_of_two(div);
  222. if (table)
  223. return _round_up_table(table, div);
  224. return div;
  225. }
  226. static int clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
  227. unsigned long *best_parent_rate,
  228. const struct clk_div_table *table, u8 width,
  229. unsigned long flags)
  230. {
  231. int i, bestdiv = 0;
  232. unsigned long parent_rate, best = 0, now, maxdiv;
  233. unsigned long parent_rate_saved = *best_parent_rate;
  234. if (!rate)
  235. rate = 1;
  236. maxdiv = _get_maxdiv(table, width, flags);
  237. if (!(__clk_get_flags(hw->clk) & CLK_SET_RATE_PARENT)) {
  238. parent_rate = *best_parent_rate;
  239. bestdiv = _div_round(table, parent_rate, rate, flags);
  240. bestdiv = bestdiv == 0 ? 1 : bestdiv;
  241. bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
  242. return bestdiv;
  243. }
  244. /*
  245. * The maximum divider we can use without overflowing
  246. * unsigned long in rate * i below
  247. */
  248. maxdiv = min(ULONG_MAX / rate, maxdiv);
  249. for (i = 1; i <= maxdiv; i = _next_div(table, i, flags)) {
  250. if (!_is_valid_div(table, i, flags))
  251. continue;
  252. if (rate * i == parent_rate_saved) {
  253. /*
  254. * It's the most ideal case if the requested rate can be
  255. * divided from parent clock without needing to change
  256. * parent rate, so return the divider immediately.
  257. */
  258. *best_parent_rate = parent_rate_saved;
  259. return i;
  260. }
  261. parent_rate = __clk_round_rate(__clk_get_parent(hw->clk),
  262. rate * i);
  263. now = DIV_ROUND_UP(parent_rate, i);
  264. if (_is_best_div(rate, now, best, flags)) {
  265. bestdiv = i;
  266. best = now;
  267. *best_parent_rate = parent_rate;
  268. }
  269. }
  270. if (!bestdiv) {
  271. bestdiv = _get_maxdiv(table, width, flags);
  272. *best_parent_rate = __clk_round_rate(__clk_get_parent(hw->clk), 1);
  273. }
  274. return bestdiv;
  275. }
  276. long divider_round_rate(struct clk_hw *hw, unsigned long rate,
  277. unsigned long *prate, const struct clk_div_table *table,
  278. u8 width, unsigned long flags)
  279. {
  280. int div;
  281. div = clk_divider_bestdiv(hw, rate, prate, table, width, flags);
  282. return DIV_ROUND_UP(*prate, div);
  283. }
  284. EXPORT_SYMBOL_GPL(divider_round_rate);
  285. static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
  286. unsigned long *prate)
  287. {
  288. struct clk_divider *divider = to_clk_divider(hw);
  289. int bestdiv;
  290. /* if read only, just return current value */
  291. if (divider->flags & CLK_DIVIDER_READ_ONLY) {
  292. bestdiv = readl(divider->reg) >> divider->shift;
  293. bestdiv &= div_mask(divider->width);
  294. bestdiv = _get_div(divider->table, bestdiv, divider->flags);
  295. return DIV_ROUND_UP(*prate, bestdiv);
  296. }
  297. return divider_round_rate(hw, rate, prate, divider->table,
  298. divider->width, divider->flags);
  299. }
  300. int divider_get_val(unsigned long rate, unsigned long parent_rate,
  301. const struct clk_div_table *table, u8 width,
  302. unsigned long flags)
  303. {
  304. unsigned int div, value;
  305. div = DIV_ROUND_UP(parent_rate, rate);
  306. if (!_is_valid_div(table, div, flags))
  307. return -EINVAL;
  308. value = _get_val(table, div, flags);
  309. return min_t(unsigned int, value, div_mask(width));
  310. }
  311. EXPORT_SYMBOL_GPL(divider_get_val);
  312. static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
  313. unsigned long parent_rate)
  314. {
  315. struct clk_divider *divider = to_clk_divider(hw);
  316. unsigned int value;
  317. unsigned long flags = 0;
  318. u32 val;
  319. value = divider_get_val(rate, parent_rate, divider->table,
  320. divider->width, divider->flags);
  321. if (divider->lock)
  322. spin_lock_irqsave(divider->lock, flags);
  323. if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
  324. val = div_mask(divider->width) << (divider->shift + 16);
  325. } else {
  326. val = clk_readl(divider->reg);
  327. val &= ~(div_mask(divider->width) << divider->shift);
  328. }
  329. val |= value << divider->shift;
  330. clk_writel(val, divider->reg);
  331. if (divider->lock)
  332. spin_unlock_irqrestore(divider->lock, flags);
  333. return 0;
  334. }
  335. const struct clk_ops clk_divider_ops = {
  336. .recalc_rate = clk_divider_recalc_rate,
  337. .round_rate = clk_divider_round_rate,
  338. .set_rate = clk_divider_set_rate,
  339. };
  340. EXPORT_SYMBOL_GPL(clk_divider_ops);
  341. static struct clk *_register_divider(struct device *dev, const char *name,
  342. const char *parent_name, unsigned long flags,
  343. void __iomem *reg, u8 shift, u8 width,
  344. u8 clk_divider_flags, const struct clk_div_table *table,
  345. spinlock_t *lock)
  346. {
  347. struct clk_divider *div;
  348. struct clk *clk;
  349. struct clk_init_data init;
  350. if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
  351. if (width + shift > 16) {
  352. pr_warn("divider value exceeds LOWORD field\n");
  353. return ERR_PTR(-EINVAL);
  354. }
  355. }
  356. /* allocate the divider */
  357. div = kzalloc(sizeof(struct clk_divider), GFP_KERNEL);
  358. if (!div) {
  359. pr_err("%s: could not allocate divider clk\n", __func__);
  360. return ERR_PTR(-ENOMEM);
  361. }
  362. init.name = name;
  363. init.ops = &clk_divider_ops;
  364. init.flags = flags | CLK_IS_BASIC;
  365. init.parent_names = (parent_name ? &parent_name: NULL);
  366. init.num_parents = (parent_name ? 1 : 0);
  367. /* struct clk_divider assignments */
  368. div->reg = reg;
  369. div->shift = shift;
  370. div->width = width;
  371. div->flags = clk_divider_flags;
  372. div->lock = lock;
  373. div->hw.init = &init;
  374. div->table = table;
  375. /* register the clock */
  376. clk = clk_register(dev, &div->hw);
  377. if (IS_ERR(clk))
  378. kfree(div);
  379. return clk;
  380. }
  381. /**
  382. * clk_register_divider - register a divider clock with the clock framework
  383. * @dev: device registering this clock
  384. * @name: name of this clock
  385. * @parent_name: name of clock's parent
  386. * @flags: framework-specific flags
  387. * @reg: register address to adjust divider
  388. * @shift: number of bits to shift the bitfield
  389. * @width: width of the bitfield
  390. * @clk_divider_flags: divider-specific flags for this clock
  391. * @lock: shared register lock for this clock
  392. */
  393. struct clk *clk_register_divider(struct device *dev, const char *name,
  394. const char *parent_name, unsigned long flags,
  395. void __iomem *reg, u8 shift, u8 width,
  396. u8 clk_divider_flags, spinlock_t *lock)
  397. {
  398. return _register_divider(dev, name, parent_name, flags, reg, shift,
  399. width, clk_divider_flags, NULL, lock);
  400. }
  401. EXPORT_SYMBOL_GPL(clk_register_divider);
  402. /**
  403. * clk_register_divider_table - register a table based divider clock with
  404. * the clock framework
  405. * @dev: device registering this clock
  406. * @name: name of this clock
  407. * @parent_name: name of clock's parent
  408. * @flags: framework-specific flags
  409. * @reg: register address to adjust divider
  410. * @shift: number of bits to shift the bitfield
  411. * @width: width of the bitfield
  412. * @clk_divider_flags: divider-specific flags for this clock
  413. * @table: array of divider/value pairs ending with a div set to 0
  414. * @lock: shared register lock for this clock
  415. */
  416. struct clk *clk_register_divider_table(struct device *dev, const char *name,
  417. const char *parent_name, unsigned long flags,
  418. void __iomem *reg, u8 shift, u8 width,
  419. u8 clk_divider_flags, const struct clk_div_table *table,
  420. spinlock_t *lock)
  421. {
  422. return _register_divider(dev, name, parent_name, flags, reg, shift,
  423. width, clk_divider_flags, table, lock);
  424. }
  425. EXPORT_SYMBOL_GPL(clk_register_divider_table);
  426. void clk_unregister_divider(struct clk *clk)
  427. {
  428. struct clk_divider *div;
  429. struct clk_hw *hw;
  430. hw = __clk_get_hw(clk);
  431. if (!hw)
  432. return;
  433. div = to_clk_divider(hw);
  434. clk_unregister(clk);
  435. kfree(div);
  436. }
  437. EXPORT_SYMBOL_GPL(clk_unregister_divider);