clk-factors.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (C) 2013 Emilio López <emilio@elopez.com.ar>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Adjustable factor-based clock implementation
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include <linux/io.h>
  14. #include <linux/err.h>
  15. #include <linux/string.h>
  16. #include <linux/delay.h>
  17. #include "clk-factors.h"
  18. /*
  19. * DOC: basic adjustable factor-based clock that cannot gate
  20. *
  21. * Traits of this clock:
  22. * prepare - clk_prepare only ensures that parents are prepared
  23. * enable - clk_enable only ensures that parents are enabled
  24. * rate - rate is adjustable.
  25. * clk->rate = (parent->rate * N * (K + 1) >> P) / (M + 1)
  26. * parent - fixed parent. No clk_set_parent support
  27. */
  28. #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
  29. #define SETMASK(len, pos) (((1U << (len)) - 1) << (pos))
  30. #define CLRMASK(len, pos) (~(SETMASK(len, pos)))
  31. #define FACTOR_GET(bit, len, reg) (((reg) & SETMASK(len, bit)) >> (bit))
  32. #define FACTOR_SET(bit, len, reg, val) \
  33. (((reg) & CLRMASK(len, bit)) | (val << (bit)))
  34. static unsigned long clk_factors_recalc_rate(struct clk_hw *hw,
  35. unsigned long parent_rate)
  36. {
  37. u8 n = 1, k = 0, p = 0, m = 0;
  38. u32 reg;
  39. unsigned long rate;
  40. struct clk_factors *factors = to_clk_factors(hw);
  41. struct clk_factors_config *config = factors->config;
  42. /* Fetch the register value */
  43. reg = readl(factors->reg);
  44. /* Get each individual factor if applicable */
  45. if (config->nwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  46. n = FACTOR_GET(config->nshift, config->nwidth, reg);
  47. if (config->kwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  48. k = FACTOR_GET(config->kshift, config->kwidth, reg);
  49. if (config->mwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  50. m = FACTOR_GET(config->mshift, config->mwidth, reg);
  51. if (config->pwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  52. p = FACTOR_GET(config->pshift, config->pwidth, reg);
  53. /* Calculate the rate */
  54. rate = (parent_rate * n * (k + 1) >> p) / (m + 1);
  55. return rate;
  56. }
  57. static long clk_factors_round_rate(struct clk_hw *hw, unsigned long rate,
  58. unsigned long *parent_rate)
  59. {
  60. struct clk_factors *factors = to_clk_factors(hw);
  61. factors->get_factors((u32 *)&rate, (u32)*parent_rate,
  62. NULL, NULL, NULL, NULL);
  63. return rate;
  64. }
  65. static long clk_factors_determine_rate(struct clk_hw *hw, unsigned long rate,
  66. unsigned long *best_parent_rate,
  67. struct clk **best_parent_p)
  68. {
  69. struct clk *clk = hw->clk, *parent, *best_parent = NULL;
  70. int i, num_parents;
  71. unsigned long parent_rate, best = 0, child_rate, best_child_rate = 0;
  72. /* find the parent that can help provide the fastest rate <= rate */
  73. num_parents = __clk_get_num_parents(clk);
  74. for (i = 0; i < num_parents; i++) {
  75. parent = clk_get_parent_by_index(clk, i);
  76. if (!parent)
  77. continue;
  78. if (__clk_get_flags(clk) & CLK_SET_RATE_PARENT)
  79. parent_rate = __clk_round_rate(parent, rate);
  80. else
  81. parent_rate = __clk_get_rate(parent);
  82. child_rate = clk_factors_round_rate(hw, rate, &parent_rate);
  83. if (child_rate <= rate && child_rate > best_child_rate) {
  84. best_parent = parent;
  85. best = parent_rate;
  86. best_child_rate = child_rate;
  87. }
  88. }
  89. if (best_parent)
  90. *best_parent_p = best_parent;
  91. *best_parent_rate = best;
  92. return best_child_rate;
  93. }
  94. static int clk_factors_set_rate(struct clk_hw *hw, unsigned long rate,
  95. unsigned long parent_rate)
  96. {
  97. u8 n = 0, k = 0, m = 0, p = 0;
  98. u32 reg;
  99. struct clk_factors *factors = to_clk_factors(hw);
  100. struct clk_factors_config *config = factors->config;
  101. unsigned long flags = 0;
  102. factors->get_factors((u32 *)&rate, (u32)parent_rate, &n, &k, &m, &p);
  103. if (factors->lock)
  104. spin_lock_irqsave(factors->lock, flags);
  105. /* Fetch the register value */
  106. reg = readl(factors->reg);
  107. /* Set up the new factors - macros do not do anything if width is 0 */
  108. reg = FACTOR_SET(config->nshift, config->nwidth, reg, n);
  109. reg = FACTOR_SET(config->kshift, config->kwidth, reg, k);
  110. reg = FACTOR_SET(config->mshift, config->mwidth, reg, m);
  111. reg = FACTOR_SET(config->pshift, config->pwidth, reg, p);
  112. /* Apply them now */
  113. writel(reg, factors->reg);
  114. /* delay 500us so pll stabilizes */
  115. __delay((rate >> 20) * 500 / 2);
  116. if (factors->lock)
  117. spin_unlock_irqrestore(factors->lock, flags);
  118. return 0;
  119. }
  120. const struct clk_ops clk_factors_ops = {
  121. .determine_rate = clk_factors_determine_rate,
  122. .recalc_rate = clk_factors_recalc_rate,
  123. .round_rate = clk_factors_round_rate,
  124. .set_rate = clk_factors_set_rate,
  125. };