clk-factors.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/delay.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/of_address.h>
  16. #include <linux/slab.h>
  17. #include <linux/string.h>
  18. #include "clk-factors.h"
  19. /*
  20. * DOC: basic adjustable factor-based clock
  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.
  26. * clk->rate = (parent->rate * N * (K + 1) >> P) / (M + 1)
  27. * parent - fixed parent. No clk_set_parent support
  28. */
  29. #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
  30. #define FACTORS_MAX_PARENTS 5
  31. #define SETMASK(len, pos) (((1U << (len)) - 1) << (pos))
  32. #define CLRMASK(len, pos) (~(SETMASK(len, pos)))
  33. #define FACTOR_GET(bit, len, reg) (((reg) & SETMASK(len, bit)) >> (bit))
  34. #define FACTOR_SET(bit, len, reg, val) \
  35. (((reg) & CLRMASK(len, bit)) | (val << (bit)))
  36. static unsigned long clk_factors_recalc_rate(struct clk_hw *hw,
  37. unsigned long parent_rate)
  38. {
  39. u8 n = 1, k = 0, p = 0, m = 0;
  40. u32 reg;
  41. unsigned long rate;
  42. struct clk_factors *factors = to_clk_factors(hw);
  43. struct clk_factors_config *config = factors->config;
  44. /* Fetch the register value */
  45. reg = readl(factors->reg);
  46. /* Get each individual factor if applicable */
  47. if (config->nwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  48. n = FACTOR_GET(config->nshift, config->nwidth, reg);
  49. if (config->kwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  50. k = FACTOR_GET(config->kshift, config->kwidth, reg);
  51. if (config->mwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  52. m = FACTOR_GET(config->mshift, config->mwidth, reg);
  53. if (config->pwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  54. p = FACTOR_GET(config->pshift, config->pwidth, reg);
  55. /* Calculate the rate */
  56. rate = (parent_rate * (n + config->n_start) * (k + 1) >> p) / (m + 1);
  57. return rate;
  58. }
  59. static long clk_factors_round_rate(struct clk_hw *hw, unsigned long rate,
  60. unsigned long *parent_rate)
  61. {
  62. struct clk_factors *factors = to_clk_factors(hw);
  63. factors->get_factors((u32 *)&rate, (u32)*parent_rate,
  64. NULL, NULL, NULL, NULL);
  65. return rate;
  66. }
  67. static long clk_factors_determine_rate(struct clk_hw *hw, unsigned long rate,
  68. unsigned long min_rate,
  69. unsigned long max_rate,
  70. unsigned long *best_parent_rate,
  71. struct clk_hw **best_parent_p)
  72. {
  73. struct clk *clk = hw->clk, *parent, *best_parent = NULL;
  74. int i, num_parents;
  75. unsigned long parent_rate, best = 0, child_rate, best_child_rate = 0;
  76. /* find the parent that can help provide the fastest rate <= rate */
  77. num_parents = __clk_get_num_parents(clk);
  78. for (i = 0; i < num_parents; i++) {
  79. parent = clk_get_parent_by_index(clk, i);
  80. if (!parent)
  81. continue;
  82. if (__clk_get_flags(clk) & CLK_SET_RATE_PARENT)
  83. parent_rate = __clk_round_rate(parent, rate);
  84. else
  85. parent_rate = __clk_get_rate(parent);
  86. child_rate = clk_factors_round_rate(hw, rate, &parent_rate);
  87. if (child_rate <= rate && child_rate > best_child_rate) {
  88. best_parent = parent;
  89. best = parent_rate;
  90. best_child_rate = child_rate;
  91. }
  92. }
  93. if (best_parent)
  94. *best_parent_p = __clk_get_hw(best_parent);
  95. *best_parent_rate = best;
  96. return best_child_rate;
  97. }
  98. static int clk_factors_set_rate(struct clk_hw *hw, unsigned long rate,
  99. unsigned long parent_rate)
  100. {
  101. u8 n = 0, k = 0, m = 0, p = 0;
  102. u32 reg;
  103. struct clk_factors *factors = to_clk_factors(hw);
  104. struct clk_factors_config *config = factors->config;
  105. unsigned long flags = 0;
  106. factors->get_factors((u32 *)&rate, (u32)parent_rate, &n, &k, &m, &p);
  107. if (factors->lock)
  108. spin_lock_irqsave(factors->lock, flags);
  109. /* Fetch the register value */
  110. reg = readl(factors->reg);
  111. /* Set up the new factors - macros do not do anything if width is 0 */
  112. reg = FACTOR_SET(config->nshift, config->nwidth, reg, n);
  113. reg = FACTOR_SET(config->kshift, config->kwidth, reg, k);
  114. reg = FACTOR_SET(config->mshift, config->mwidth, reg, m);
  115. reg = FACTOR_SET(config->pshift, config->pwidth, reg, p);
  116. /* Apply them now */
  117. writel(reg, factors->reg);
  118. /* delay 500us so pll stabilizes */
  119. __delay((rate >> 20) * 500 / 2);
  120. if (factors->lock)
  121. spin_unlock_irqrestore(factors->lock, flags);
  122. return 0;
  123. }
  124. static const struct clk_ops clk_factors_ops = {
  125. .determine_rate = clk_factors_determine_rate,
  126. .recalc_rate = clk_factors_recalc_rate,
  127. .round_rate = clk_factors_round_rate,
  128. .set_rate = clk_factors_set_rate,
  129. };
  130. struct clk *sunxi_factors_register(struct device_node *node,
  131. const struct factors_data *data,
  132. spinlock_t *lock,
  133. void __iomem *reg)
  134. {
  135. struct clk *clk;
  136. struct clk_factors *factors;
  137. struct clk_gate *gate = NULL;
  138. struct clk_mux *mux = NULL;
  139. struct clk_hw *gate_hw = NULL;
  140. struct clk_hw *mux_hw = NULL;
  141. const char *clk_name = node->name;
  142. const char *parents[FACTORS_MAX_PARENTS];
  143. int i = 0;
  144. /* if we have a mux, we will have >1 parents */
  145. while (i < FACTORS_MAX_PARENTS &&
  146. (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
  147. i++;
  148. /*
  149. * some factor clocks, such as pll5 and pll6, may have multiple
  150. * outputs, and have their name designated in factors_data
  151. */
  152. if (data->name)
  153. clk_name = data->name;
  154. else
  155. of_property_read_string(node, "clock-output-names", &clk_name);
  156. factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
  157. if (!factors)
  158. return NULL;
  159. /* set up factors properties */
  160. factors->reg = reg;
  161. factors->config = data->table;
  162. factors->get_factors = data->getter;
  163. factors->lock = lock;
  164. /* Add a gate if this factor clock can be gated */
  165. if (data->enable) {
  166. gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
  167. if (!gate) {
  168. kfree(factors);
  169. return NULL;
  170. }
  171. /* set up gate properties */
  172. gate->reg = reg;
  173. gate->bit_idx = data->enable;
  174. gate->lock = factors->lock;
  175. gate_hw = &gate->hw;
  176. }
  177. /* Add a mux if this factor clock can be muxed */
  178. if (data->mux) {
  179. mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
  180. if (!mux) {
  181. kfree(factors);
  182. kfree(gate);
  183. return NULL;
  184. }
  185. /* set up gate properties */
  186. mux->reg = reg;
  187. mux->shift = data->mux;
  188. mux->mask = data->muxmask;
  189. mux->lock = factors->lock;
  190. mux_hw = &mux->hw;
  191. }
  192. clk = clk_register_composite(NULL, clk_name,
  193. parents, i,
  194. mux_hw, &clk_mux_ops,
  195. &factors->hw, &clk_factors_ops,
  196. gate_hw, &clk_gate_ops, 0);
  197. if (!IS_ERR(clk)) {
  198. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  199. clk_register_clkdev(clk, clk_name, NULL);
  200. }
  201. return clk;
  202. }