clk-divider.c 17 KB

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