clk-factors.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 int clk_factors_determine_rate(struct clk_hw *hw,
  68. struct clk_rate_request *req)
  69. {
  70. struct clk_hw *parent, *best_parent = NULL;
  71. int i, num_parents;
  72. unsigned long parent_rate, best = 0, child_rate, best_child_rate = 0;
  73. /* find the parent that can help provide the fastest rate <= rate */
  74. num_parents = clk_hw_get_num_parents(hw);
  75. for (i = 0; i < num_parents; i++) {
  76. parent = clk_hw_get_parent_by_index(hw, i);
  77. if (!parent)
  78. continue;
  79. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
  80. parent_rate = clk_hw_round_rate(parent, req->rate);
  81. else
  82. parent_rate = clk_hw_get_rate(parent);
  83. child_rate = clk_factors_round_rate(hw, req->rate,
  84. &parent_rate);
  85. if (child_rate <= req->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. return -EINVAL;
  93. req->best_parent_hw = best_parent;
  94. req->best_parent_rate = best;
  95. req->rate = best_child_rate;
  96. return 0;
  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. i = of_clk_parent_fill(node, parents, FACTORS_MAX_PARENTS);
  146. /*
  147. * some factor clocks, such as pll5 and pll6, may have multiple
  148. * outputs, and have their name designated in factors_data
  149. */
  150. if (data->name)
  151. clk_name = data->name;
  152. else
  153. of_property_read_string(node, "clock-output-names", &clk_name);
  154. factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
  155. if (!factors)
  156. return NULL;
  157. /* set up factors properties */
  158. factors->reg = reg;
  159. factors->config = data->table;
  160. factors->get_factors = data->getter;
  161. factors->lock = lock;
  162. /* Add a gate if this factor clock can be gated */
  163. if (data->enable) {
  164. gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
  165. if (!gate) {
  166. kfree(factors);
  167. return NULL;
  168. }
  169. /* set up gate properties */
  170. gate->reg = reg;
  171. gate->bit_idx = data->enable;
  172. gate->lock = factors->lock;
  173. gate_hw = &gate->hw;
  174. }
  175. /* Add a mux if this factor clock can be muxed */
  176. if (data->mux) {
  177. mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
  178. if (!mux) {
  179. kfree(factors);
  180. kfree(gate);
  181. return NULL;
  182. }
  183. /* set up gate properties */
  184. mux->reg = reg;
  185. mux->shift = data->mux;
  186. mux->mask = data->muxmask;
  187. mux->lock = factors->lock;
  188. mux_hw = &mux->hw;
  189. }
  190. clk = clk_register_composite(NULL, clk_name,
  191. parents, i,
  192. mux_hw, &clk_mux_ops,
  193. &factors->hw, &clk_factors_ops,
  194. gate_hw, &clk_gate_ops, 0);
  195. if (!IS_ERR(clk)) {
  196. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  197. clk_register_clkdev(clk, clk_name, NULL);
  198. }
  199. return clk;
  200. }