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 *best_parent_rate,
  69. struct clk_hw **best_parent_p)
  70. {
  71. struct clk *clk = hw->clk, *parent, *best_parent = NULL;
  72. int i, num_parents;
  73. unsigned long parent_rate, best = 0, child_rate, best_child_rate = 0;
  74. /* find the parent that can help provide the fastest rate <= rate */
  75. num_parents = __clk_get_num_parents(clk);
  76. for (i = 0; i < num_parents; i++) {
  77. parent = clk_get_parent_by_index(clk, i);
  78. if (!parent)
  79. continue;
  80. if (__clk_get_flags(clk) & CLK_SET_RATE_PARENT)
  81. parent_rate = __clk_round_rate(parent, rate);
  82. else
  83. parent_rate = __clk_get_rate(parent);
  84. child_rate = clk_factors_round_rate(hw, rate, &parent_rate);
  85. if (child_rate <= rate && child_rate > best_child_rate) {
  86. best_parent = parent;
  87. best = parent_rate;
  88. best_child_rate = child_rate;
  89. }
  90. }
  91. if (best_parent)
  92. *best_parent_p = __clk_get_hw(best_parent);
  93. *best_parent_rate = best;
  94. return best_child_rate;
  95. }
  96. static int clk_factors_set_rate(struct clk_hw *hw, unsigned long rate,
  97. unsigned long parent_rate)
  98. {
  99. u8 n = 0, k = 0, m = 0, p = 0;
  100. u32 reg;
  101. struct clk_factors *factors = to_clk_factors(hw);
  102. struct clk_factors_config *config = factors->config;
  103. unsigned long flags = 0;
  104. factors->get_factors((u32 *)&rate, (u32)parent_rate, &n, &k, &m, &p);
  105. if (factors->lock)
  106. spin_lock_irqsave(factors->lock, flags);
  107. /* Fetch the register value */
  108. reg = readl(factors->reg);
  109. /* Set up the new factors - macros do not do anything if width is 0 */
  110. reg = FACTOR_SET(config->nshift, config->nwidth, reg, n);
  111. reg = FACTOR_SET(config->kshift, config->kwidth, reg, k);
  112. reg = FACTOR_SET(config->mshift, config->mwidth, reg, m);
  113. reg = FACTOR_SET(config->pshift, config->pwidth, reg, p);
  114. /* Apply them now */
  115. writel(reg, factors->reg);
  116. /* delay 500us so pll stabilizes */
  117. __delay((rate >> 20) * 500 / 2);
  118. if (factors->lock)
  119. spin_unlock_irqrestore(factors->lock, flags);
  120. return 0;
  121. }
  122. static const struct clk_ops clk_factors_ops = {
  123. .determine_rate = clk_factors_determine_rate,
  124. .recalc_rate = clk_factors_recalc_rate,
  125. .round_rate = clk_factors_round_rate,
  126. .set_rate = clk_factors_set_rate,
  127. };
  128. struct clk * __init sunxi_factors_register(struct device_node *node,
  129. const struct factors_data *data,
  130. spinlock_t *lock)
  131. {
  132. struct clk *clk;
  133. struct clk_factors *factors;
  134. struct clk_gate *gate = NULL;
  135. struct clk_mux *mux = NULL;
  136. struct clk_hw *gate_hw = NULL;
  137. struct clk_hw *mux_hw = NULL;
  138. const char *clk_name = node->name;
  139. const char *parents[FACTORS_MAX_PARENTS];
  140. void __iomem *reg;
  141. int i = 0;
  142. reg = of_iomap(node, 0);
  143. /* if we have a mux, we will have >1 parents */
  144. while (i < FACTORS_MAX_PARENTS &&
  145. (parents[i] = of_clk_get_parent_name(node, i)) != NULL)
  146. i++;
  147. /*
  148. * some factor clocks, such as pll5 and pll6, may have multiple
  149. * outputs, and have their name designated in factors_data
  150. */
  151. if (data->name)
  152. clk_name = data->name;
  153. else
  154. of_property_read_string(node, "clock-output-names", &clk_name);
  155. factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
  156. if (!factors)
  157. return NULL;
  158. /* set up factors properties */
  159. factors->reg = reg;
  160. factors->config = data->table;
  161. factors->get_factors = data->getter;
  162. factors->lock = lock;
  163. /* Add a gate if this factor clock can be gated */
  164. if (data->enable) {
  165. gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
  166. if (!gate) {
  167. kfree(factors);
  168. return NULL;
  169. }
  170. /* set up gate properties */
  171. gate->reg = reg;
  172. gate->bit_idx = data->enable;
  173. gate->lock = factors->lock;
  174. gate_hw = &gate->hw;
  175. }
  176. /* Add a mux if this factor clock can be muxed */
  177. if (data->mux) {
  178. mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
  179. if (!mux) {
  180. kfree(factors);
  181. kfree(gate);
  182. return NULL;
  183. }
  184. /* set up gate properties */
  185. mux->reg = reg;
  186. mux->shift = data->mux;
  187. mux->mask = data->muxmask;
  188. mux->lock = factors->lock;
  189. mux_hw = &mux->hw;
  190. }
  191. clk = clk_register_composite(NULL, clk_name,
  192. parents, i,
  193. mux_hw, &clk_mux_ops,
  194. &factors->hw, &clk_factors_ops,
  195. gate_hw, &clk_gate_ops, 0);
  196. if (!IS_ERR(clk)) {
  197. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  198. clk_register_clkdev(clk, clk_name, NULL);
  199. }
  200. return clk;
  201. }