clk-divider.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 div_mask(width) ((1 << (width)) - 1)
  29. static unsigned int _get_table_maxdiv(const struct clk_div_table *table,
  30. u8 width)
  31. {
  32. unsigned int maxdiv = 0, mask = div_mask(width);
  33. const struct clk_div_table *clkt;
  34. for (clkt = table; clkt->div; clkt++)
  35. if (clkt->div > maxdiv && clkt->val <= mask)
  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, width);
  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, unsigned long width)
  107. {
  108. unsigned int div;
  109. div = _get_div(table, val, flags, width);
  110. if (!div) {
  111. WARN(!(flags & CLK_DIVIDER_ALLOW_ZERO),
  112. "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
  113. clk_hw_get_name(hw));
  114. return parent_rate;
  115. }
  116. return DIV_ROUND_UP_ULL((u64)parent_rate, div);
  117. }
  118. EXPORT_SYMBOL_GPL(divider_recalc_rate);
  119. static unsigned long clk_divider_recalc_rate(struct clk_hw *hw,
  120. unsigned long parent_rate)
  121. {
  122. struct clk_divider *divider = to_clk_divider(hw);
  123. unsigned int val;
  124. val = clk_readl(divider->reg) >> divider->shift;
  125. val &= div_mask(divider->width);
  126. return divider_recalc_rate(hw, parent_rate, val, divider->table,
  127. divider->flags, divider->width);
  128. }
  129. static bool _is_valid_table_div(const struct clk_div_table *table,
  130. unsigned int div)
  131. {
  132. const struct clk_div_table *clkt;
  133. for (clkt = table; clkt->div; clkt++)
  134. if (clkt->div == div)
  135. return true;
  136. return false;
  137. }
  138. static bool _is_valid_div(const struct clk_div_table *table, unsigned int div,
  139. unsigned long flags)
  140. {
  141. if (flags & CLK_DIVIDER_POWER_OF_TWO)
  142. return is_power_of_2(div);
  143. if (table)
  144. return _is_valid_table_div(table, div);
  145. return true;
  146. }
  147. static int _round_up_table(const struct clk_div_table *table, int div)
  148. {
  149. const struct clk_div_table *clkt;
  150. int up = INT_MAX;
  151. for (clkt = table; clkt->div; clkt++) {
  152. if (clkt->div == div)
  153. return clkt->div;
  154. else if (clkt->div < div)
  155. continue;
  156. if ((clkt->div - div) < (up - div))
  157. up = clkt->div;
  158. }
  159. return up;
  160. }
  161. static int _round_down_table(const struct clk_div_table *table, int div)
  162. {
  163. const struct clk_div_table *clkt;
  164. int down = _get_table_mindiv(table);
  165. for (clkt = table; clkt->div; clkt++) {
  166. if (clkt->div == div)
  167. return clkt->div;
  168. else if (clkt->div > div)
  169. continue;
  170. if ((div - clkt->div) < (div - down))
  171. down = clkt->div;
  172. }
  173. return down;
  174. }
  175. static int _div_round_up(const struct clk_div_table *table,
  176. unsigned long parent_rate, unsigned long rate,
  177. unsigned long flags)
  178. {
  179. int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
  180. if (flags & CLK_DIVIDER_POWER_OF_TWO)
  181. div = __roundup_pow_of_two(div);
  182. if (table)
  183. div = _round_up_table(table, div);
  184. return div;
  185. }
  186. static int _div_round_closest(const struct clk_div_table *table,
  187. unsigned long parent_rate, unsigned long rate,
  188. unsigned long flags)
  189. {
  190. int up, down;
  191. unsigned long up_rate, down_rate;
  192. up = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
  193. down = parent_rate / rate;
  194. if (flags & CLK_DIVIDER_POWER_OF_TWO) {
  195. up = __roundup_pow_of_two(up);
  196. down = __rounddown_pow_of_two(down);
  197. } else if (table) {
  198. up = _round_up_table(table, up);
  199. down = _round_down_table(table, down);
  200. }
  201. up_rate = DIV_ROUND_UP_ULL((u64)parent_rate, up);
  202. down_rate = DIV_ROUND_UP_ULL((u64)parent_rate, down);
  203. return (rate - up_rate) <= (down_rate - rate) ? up : down;
  204. }
  205. static int _div_round(const struct clk_div_table *table,
  206. unsigned long parent_rate, unsigned long rate,
  207. unsigned long flags)
  208. {
  209. if (flags & CLK_DIVIDER_ROUND_CLOSEST)
  210. return _div_round_closest(table, parent_rate, rate, flags);
  211. return _div_round_up(table, parent_rate, rate, flags);
  212. }
  213. static bool _is_best_div(unsigned long rate, unsigned long now,
  214. unsigned long best, unsigned long flags)
  215. {
  216. if (flags & CLK_DIVIDER_ROUND_CLOSEST)
  217. return abs(rate - now) < abs(rate - best);
  218. return now <= rate && now > best;
  219. }
  220. static int _next_div(const struct clk_div_table *table, int div,
  221. unsigned long flags)
  222. {
  223. div++;
  224. if (flags & CLK_DIVIDER_POWER_OF_TWO)
  225. return __roundup_pow_of_two(div);
  226. if (table)
  227. return _round_up_table(table, div);
  228. return div;
  229. }
  230. static int clk_divider_bestdiv(struct clk_hw *hw, struct clk_hw *parent,
  231. 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 = _next_div(table, 0, flags); i <= maxdiv;
  255. i = _next_div(table, i, flags)) {
  256. if (rate * i == parent_rate_saved) {
  257. /*
  258. * It's the most ideal case if the requested rate can be
  259. * divided from parent clock without needing to change
  260. * parent rate, so return the divider immediately.
  261. */
  262. *best_parent_rate = parent_rate_saved;
  263. return i;
  264. }
  265. parent_rate = clk_hw_round_rate(parent, rate * i);
  266. now = DIV_ROUND_UP_ULL((u64)parent_rate, i);
  267. if (_is_best_div(rate, now, best, flags)) {
  268. bestdiv = i;
  269. best = now;
  270. *best_parent_rate = parent_rate;
  271. }
  272. }
  273. if (!bestdiv) {
  274. bestdiv = _get_maxdiv(table, width, flags);
  275. *best_parent_rate = clk_hw_round_rate(parent, 1);
  276. }
  277. return bestdiv;
  278. }
  279. long divider_round_rate_parent(struct clk_hw *hw, struct clk_hw *parent,
  280. unsigned long rate, unsigned long *prate,
  281. const struct clk_div_table *table,
  282. u8 width, unsigned long flags)
  283. {
  284. int div;
  285. div = clk_divider_bestdiv(hw, parent, rate, prate, table, width, flags);
  286. return DIV_ROUND_UP_ULL((u64)*prate, div);
  287. }
  288. EXPORT_SYMBOL_GPL(divider_round_rate_parent);
  289. static long clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
  290. unsigned long *prate)
  291. {
  292. struct clk_divider *divider = to_clk_divider(hw);
  293. int bestdiv;
  294. /* if read only, just return current value */
  295. if (divider->flags & CLK_DIVIDER_READ_ONLY) {
  296. bestdiv = clk_readl(divider->reg) >> divider->shift;
  297. bestdiv &= div_mask(divider->width);
  298. bestdiv = _get_div(divider->table, bestdiv, divider->flags,
  299. divider->width);
  300. return DIV_ROUND_UP_ULL((u64)*prate, bestdiv);
  301. }
  302. return divider_round_rate(hw, rate, prate, divider->table,
  303. divider->width, divider->flags);
  304. }
  305. int divider_get_val(unsigned long rate, unsigned long parent_rate,
  306. const struct clk_div_table *table, u8 width,
  307. unsigned long flags)
  308. {
  309. unsigned int div, value;
  310. div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
  311. if (!_is_valid_div(table, div, flags))
  312. return -EINVAL;
  313. value = _get_val(table, div, flags, width);
  314. return min_t(unsigned int, value, div_mask(width));
  315. }
  316. EXPORT_SYMBOL_GPL(divider_get_val);
  317. static int clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
  318. unsigned long parent_rate)
  319. {
  320. struct clk_divider *divider = to_clk_divider(hw);
  321. int value;
  322. unsigned long flags = 0;
  323. u32 val;
  324. value = divider_get_val(rate, parent_rate, divider->table,
  325. divider->width, divider->flags);
  326. if (value < 0)
  327. return value;
  328. if (divider->lock)
  329. spin_lock_irqsave(divider->lock, flags);
  330. else
  331. __acquire(divider->lock);
  332. if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
  333. val = div_mask(divider->width) << (divider->shift + 16);
  334. } else {
  335. val = clk_readl(divider->reg);
  336. val &= ~(div_mask(divider->width) << divider->shift);
  337. }
  338. val |= (u32)value << divider->shift;
  339. clk_writel(val, divider->reg);
  340. if (divider->lock)
  341. spin_unlock_irqrestore(divider->lock, flags);
  342. else
  343. __release(divider->lock);
  344. return 0;
  345. }
  346. const struct clk_ops clk_divider_ops = {
  347. .recalc_rate = clk_divider_recalc_rate,
  348. .round_rate = clk_divider_round_rate,
  349. .set_rate = clk_divider_set_rate,
  350. };
  351. EXPORT_SYMBOL_GPL(clk_divider_ops);
  352. const struct clk_ops clk_divider_ro_ops = {
  353. .recalc_rate = clk_divider_recalc_rate,
  354. .round_rate = clk_divider_round_rate,
  355. };
  356. EXPORT_SYMBOL_GPL(clk_divider_ro_ops);
  357. static struct clk_hw *_register_divider(struct device *dev, const char *name,
  358. const char *parent_name, unsigned long flags,
  359. void __iomem *reg, u8 shift, u8 width,
  360. u8 clk_divider_flags, const struct clk_div_table *table,
  361. spinlock_t *lock)
  362. {
  363. struct clk_divider *div;
  364. struct clk_hw *hw;
  365. struct clk_init_data init;
  366. int ret;
  367. if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
  368. if (width + shift > 16) {
  369. pr_warn("divider value exceeds LOWORD field\n");
  370. return ERR_PTR(-EINVAL);
  371. }
  372. }
  373. /* allocate the divider */
  374. div = kzalloc(sizeof(*div), GFP_KERNEL);
  375. if (!div)
  376. return ERR_PTR(-ENOMEM);
  377. init.name = name;
  378. if (clk_divider_flags & CLK_DIVIDER_READ_ONLY)
  379. init.ops = &clk_divider_ro_ops;
  380. else
  381. init.ops = &clk_divider_ops;
  382. init.flags = flags | CLK_IS_BASIC;
  383. init.parent_names = (parent_name ? &parent_name: NULL);
  384. init.num_parents = (parent_name ? 1 : 0);
  385. /* struct clk_divider assignments */
  386. div->reg = reg;
  387. div->shift = shift;
  388. div->width = width;
  389. div->flags = clk_divider_flags;
  390. div->lock = lock;
  391. div->hw.init = &init;
  392. div->table = table;
  393. /* register the clock */
  394. hw = &div->hw;
  395. ret = clk_hw_register(dev, hw);
  396. if (ret) {
  397. kfree(div);
  398. hw = ERR_PTR(ret);
  399. }
  400. return hw;
  401. }
  402. /**
  403. * clk_register_divider - register a divider clock with the clock framework
  404. * @dev: device registering this clock
  405. * @name: name of this clock
  406. * @parent_name: name of clock's parent
  407. * @flags: framework-specific flags
  408. * @reg: register address to adjust divider
  409. * @shift: number of bits to shift the bitfield
  410. * @width: width of the bitfield
  411. * @clk_divider_flags: divider-specific flags for this clock
  412. * @lock: shared register lock for this clock
  413. */
  414. struct clk *clk_register_divider(struct device *dev, const char *name,
  415. const char *parent_name, unsigned long flags,
  416. void __iomem *reg, u8 shift, u8 width,
  417. u8 clk_divider_flags, spinlock_t *lock)
  418. {
  419. struct clk_hw *hw;
  420. hw = _register_divider(dev, name, parent_name, flags, reg, shift,
  421. width, clk_divider_flags, NULL, lock);
  422. if (IS_ERR(hw))
  423. return ERR_CAST(hw);
  424. return hw->clk;
  425. }
  426. EXPORT_SYMBOL_GPL(clk_register_divider);
  427. /**
  428. * clk_hw_register_divider - register a divider clock with the clock framework
  429. * @dev: device registering this clock
  430. * @name: name of this clock
  431. * @parent_name: name of clock's parent
  432. * @flags: framework-specific flags
  433. * @reg: register address to adjust divider
  434. * @shift: number of bits to shift the bitfield
  435. * @width: width of the bitfield
  436. * @clk_divider_flags: divider-specific flags for this clock
  437. * @lock: shared register lock for this clock
  438. */
  439. struct clk_hw *clk_hw_register_divider(struct device *dev, const char *name,
  440. const char *parent_name, unsigned long flags,
  441. void __iomem *reg, u8 shift, u8 width,
  442. u8 clk_divider_flags, spinlock_t *lock)
  443. {
  444. return _register_divider(dev, name, parent_name, flags, reg, shift,
  445. width, clk_divider_flags, NULL, lock);
  446. }
  447. EXPORT_SYMBOL_GPL(clk_hw_register_divider);
  448. /**
  449. * clk_register_divider_table - register a table based divider clock with
  450. * the clock framework
  451. * @dev: device registering this clock
  452. * @name: name of this clock
  453. * @parent_name: name of clock's parent
  454. * @flags: framework-specific flags
  455. * @reg: register address to adjust divider
  456. * @shift: number of bits to shift the bitfield
  457. * @width: width of the bitfield
  458. * @clk_divider_flags: divider-specific flags for this clock
  459. * @table: array of divider/value pairs ending with a div set to 0
  460. * @lock: shared register lock for this clock
  461. */
  462. struct clk *clk_register_divider_table(struct device *dev, const char *name,
  463. const char *parent_name, unsigned long flags,
  464. void __iomem *reg, u8 shift, u8 width,
  465. u8 clk_divider_flags, const struct clk_div_table *table,
  466. spinlock_t *lock)
  467. {
  468. struct clk_hw *hw;
  469. hw = _register_divider(dev, name, parent_name, flags, reg, shift,
  470. width, clk_divider_flags, table, lock);
  471. if (IS_ERR(hw))
  472. return ERR_CAST(hw);
  473. return hw->clk;
  474. }
  475. EXPORT_SYMBOL_GPL(clk_register_divider_table);
  476. /**
  477. * clk_hw_register_divider_table - register a table based divider clock with
  478. * the clock framework
  479. * @dev: device registering this clock
  480. * @name: name of this clock
  481. * @parent_name: name of clock's parent
  482. * @flags: framework-specific flags
  483. * @reg: register address to adjust divider
  484. * @shift: number of bits to shift the bitfield
  485. * @width: width of the bitfield
  486. * @clk_divider_flags: divider-specific flags for this clock
  487. * @table: array of divider/value pairs ending with a div set to 0
  488. * @lock: shared register lock for this clock
  489. */
  490. struct clk_hw *clk_hw_register_divider_table(struct device *dev,
  491. const char *name, const char *parent_name, unsigned long flags,
  492. void __iomem *reg, u8 shift, u8 width,
  493. u8 clk_divider_flags, const struct clk_div_table *table,
  494. spinlock_t *lock)
  495. {
  496. return _register_divider(dev, name, parent_name, flags, reg, shift,
  497. width, clk_divider_flags, table, lock);
  498. }
  499. EXPORT_SYMBOL_GPL(clk_hw_register_divider_table);
  500. void clk_unregister_divider(struct clk *clk)
  501. {
  502. struct clk_divider *div;
  503. struct clk_hw *hw;
  504. hw = __clk_get_hw(clk);
  505. if (!hw)
  506. return;
  507. div = to_clk_divider(hw);
  508. clk_unregister(clk);
  509. kfree(div);
  510. }
  511. EXPORT_SYMBOL_GPL(clk_unregister_divider);
  512. /**
  513. * clk_hw_unregister_divider - unregister a clk divider
  514. * @hw: hardware-specific clock data to unregister
  515. */
  516. void clk_hw_unregister_divider(struct clk_hw *hw)
  517. {
  518. struct clk_divider *div;
  519. div = to_clk_divider(hw);
  520. clk_hw_unregister(hw);
  521. kfree(div);
  522. }
  523. EXPORT_SYMBOL_GPL(clk_hw_unregister_divider);