clk-divider.c 14 KB

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