clk-divider.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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)
  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 = _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(clk_hw_get_parent(hw),
  266. rate * i);
  267. now = DIV_ROUND_UP_ULL((u64)parent_rate, i);
  268. if (_is_best_div(rate, now, best, flags)) {
  269. bestdiv = i;
  270. best = now;
  271. *best_parent_rate = parent_rate;
  272. }
  273. }
  274. if (!bestdiv) {
  275. bestdiv = _get_maxdiv(table, width, flags);
  276. *best_parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw), 1);
  277. }
  278. return bestdiv;
  279. }
  280. long divider_round_rate(struct clk_hw *hw, unsigned long rate,
  281. unsigned long *prate, const struct clk_div_table *table,
  282. u8 width, unsigned long flags)
  283. {
  284. int div;
  285. div = clk_divider_bestdiv(hw, rate, prate, table, width, flags);
  286. return DIV_ROUND_UP_ULL((u64)*prate, div);
  287. }
  288. EXPORT_SYMBOL_GPL(divider_round_rate);
  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. unsigned 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 (divider->lock)
  327. spin_lock_irqsave(divider->lock, flags);
  328. else
  329. __acquire(divider->lock);
  330. if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
  331. val = div_mask(divider->width) << (divider->shift + 16);
  332. } else {
  333. val = clk_readl(divider->reg);
  334. val &= ~(div_mask(divider->width) << divider->shift);
  335. }
  336. val |= value << divider->shift;
  337. clk_writel(val, divider->reg);
  338. if (divider->lock)
  339. spin_unlock_irqrestore(divider->lock, flags);
  340. else
  341. __release(divider->lock);
  342. return 0;
  343. }
  344. const struct clk_ops clk_divider_ops = {
  345. .recalc_rate = clk_divider_recalc_rate,
  346. .round_rate = clk_divider_round_rate,
  347. .set_rate = clk_divider_set_rate,
  348. };
  349. EXPORT_SYMBOL_GPL(clk_divider_ops);
  350. const struct clk_ops clk_divider_ro_ops = {
  351. .recalc_rate = clk_divider_recalc_rate,
  352. .round_rate = clk_divider_round_rate,
  353. };
  354. EXPORT_SYMBOL_GPL(clk_divider_ro_ops);
  355. static struct clk_hw *_register_divider(struct device *dev, const char *name,
  356. const char *parent_name, unsigned long flags,
  357. void __iomem *reg, u8 shift, u8 width,
  358. u8 clk_divider_flags, const struct clk_div_table *table,
  359. spinlock_t *lock)
  360. {
  361. struct clk_divider *div;
  362. struct clk_hw *hw;
  363. struct clk_init_data init;
  364. int ret;
  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. hw = &div->hw;
  393. ret = clk_hw_register(dev, hw);
  394. if (ret) {
  395. kfree(div);
  396. hw = ERR_PTR(ret);
  397. }
  398. return hw;
  399. }
  400. /**
  401. * clk_register_divider - register a divider clock with the clock framework
  402. * @dev: device registering this clock
  403. * @name: name of this clock
  404. * @parent_name: name of clock's parent
  405. * @flags: framework-specific flags
  406. * @reg: register address to adjust divider
  407. * @shift: number of bits to shift the bitfield
  408. * @width: width of the bitfield
  409. * @clk_divider_flags: divider-specific flags for this clock
  410. * @lock: shared register lock for this clock
  411. */
  412. struct clk *clk_register_divider(struct device *dev, const char *name,
  413. const char *parent_name, unsigned long flags,
  414. void __iomem *reg, u8 shift, u8 width,
  415. u8 clk_divider_flags, spinlock_t *lock)
  416. {
  417. struct clk_hw *hw;
  418. hw = _register_divider(dev, name, parent_name, flags, reg, shift,
  419. width, clk_divider_flags, NULL, lock);
  420. if (IS_ERR(hw))
  421. return ERR_CAST(hw);
  422. return hw->clk;
  423. }
  424. EXPORT_SYMBOL_GPL(clk_register_divider);
  425. /**
  426. * clk_hw_register_divider - register a divider clock with the clock framework
  427. * @dev: device registering this clock
  428. * @name: name of this clock
  429. * @parent_name: name of clock's parent
  430. * @flags: framework-specific flags
  431. * @reg: register address to adjust divider
  432. * @shift: number of bits to shift the bitfield
  433. * @width: width of the bitfield
  434. * @clk_divider_flags: divider-specific flags for this clock
  435. * @lock: shared register lock for this clock
  436. */
  437. struct clk_hw *clk_hw_register_divider(struct device *dev, const char *name,
  438. const char *parent_name, unsigned long flags,
  439. void __iomem *reg, u8 shift, u8 width,
  440. u8 clk_divider_flags, spinlock_t *lock)
  441. {
  442. return _register_divider(dev, name, parent_name, flags, reg, shift,
  443. width, clk_divider_flags, NULL, lock);
  444. }
  445. EXPORT_SYMBOL_GPL(clk_hw_register_divider);
  446. /**
  447. * clk_register_divider_table - register a table based divider clock with
  448. * the clock framework
  449. * @dev: device registering this clock
  450. * @name: name of this clock
  451. * @parent_name: name of clock's parent
  452. * @flags: framework-specific flags
  453. * @reg: register address to adjust divider
  454. * @shift: number of bits to shift the bitfield
  455. * @width: width of the bitfield
  456. * @clk_divider_flags: divider-specific flags for this clock
  457. * @table: array of divider/value pairs ending with a div set to 0
  458. * @lock: shared register lock for this clock
  459. */
  460. struct clk *clk_register_divider_table(struct device *dev, const char *name,
  461. const char *parent_name, unsigned long flags,
  462. void __iomem *reg, u8 shift, u8 width,
  463. u8 clk_divider_flags, const struct clk_div_table *table,
  464. spinlock_t *lock)
  465. {
  466. struct clk_hw *hw;
  467. hw = _register_divider(dev, name, parent_name, flags, reg, shift,
  468. width, clk_divider_flags, table, lock);
  469. if (IS_ERR(hw))
  470. return ERR_CAST(hw);
  471. return hw->clk;
  472. }
  473. EXPORT_SYMBOL_GPL(clk_register_divider_table);
  474. /**
  475. * clk_hw_register_divider_table - register a table based divider clock with
  476. * the clock framework
  477. * @dev: device registering this clock
  478. * @name: name of this clock
  479. * @parent_name: name of clock's parent
  480. * @flags: framework-specific flags
  481. * @reg: register address to adjust divider
  482. * @shift: number of bits to shift the bitfield
  483. * @width: width of the bitfield
  484. * @clk_divider_flags: divider-specific flags for this clock
  485. * @table: array of divider/value pairs ending with a div set to 0
  486. * @lock: shared register lock for this clock
  487. */
  488. struct clk_hw *clk_hw_register_divider_table(struct device *dev,
  489. const char *name, const char *parent_name, unsigned long flags,
  490. void __iomem *reg, u8 shift, u8 width,
  491. u8 clk_divider_flags, const struct clk_div_table *table,
  492. spinlock_t *lock)
  493. {
  494. return _register_divider(dev, name, parent_name, flags, reg, shift,
  495. width, clk_divider_flags, table, lock);
  496. }
  497. EXPORT_SYMBOL_GPL(clk_hw_register_divider_table);
  498. void clk_unregister_divider(struct clk *clk)
  499. {
  500. struct clk_divider *div;
  501. struct clk_hw *hw;
  502. hw = __clk_get_hw(clk);
  503. if (!hw)
  504. return;
  505. div = to_clk_divider(hw);
  506. clk_unregister(clk);
  507. kfree(div);
  508. }
  509. EXPORT_SYMBOL_GPL(clk_unregister_divider);
  510. /**
  511. * clk_hw_unregister_divider - unregister a clk divider
  512. * @hw: hardware-specific clock data to unregister
  513. */
  514. void clk_hw_unregister_divider(struct clk_hw *hw)
  515. {
  516. struct clk_divider *div;
  517. div = to_clk_divider(hw);
  518. clk_hw_unregister(hw);
  519. kfree(div);
  520. }
  521. EXPORT_SYMBOL_GPL(clk_hw_unregister_divider);