clk-factors.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Emilio López <[email protected]>
  4. *
  5. * Adjustable factor-based clock implementation
  6. */
  7. #include <linux/clk-provider.h>
  8. #include <linux/delay.h>
  9. #include <linux/err.h>
  10. #include <linux/io.h>
  11. #include <linux/of_address.h>
  12. #include <linux/slab.h>
  13. #include <linux/string.h>
  14. #include "clk-factors.h"
  15. /*
  16. * DOC: basic adjustable factor-based clock
  17. *
  18. * Traits of this clock:
  19. * prepare - clk_prepare only ensures that parents are prepared
  20. * enable - clk_enable only ensures that parents are enabled
  21. * rate - rate is adjustable.
  22. * clk->rate = (parent->rate * N * (K + 1) >> P) / (M + 1)
  23. * parent - fixed parent. No clk_set_parent support
  24. */
  25. #define to_clk_factors(_hw) container_of(_hw, struct clk_factors, hw)
  26. #define FACTORS_MAX_PARENTS 5
  27. #define SETMASK(len, pos) (((1U << (len)) - 1) << (pos))
  28. #define CLRMASK(len, pos) (~(SETMASK(len, pos)))
  29. #define FACTOR_GET(bit, len, reg) (((reg) & SETMASK(len, bit)) >> (bit))
  30. #define FACTOR_SET(bit, len, reg, val) \
  31. (((reg) & CLRMASK(len, bit)) | (val << (bit)))
  32. static unsigned long clk_factors_recalc_rate(struct clk_hw *hw,
  33. unsigned long parent_rate)
  34. {
  35. u8 n = 1, k = 0, p = 0, m = 0;
  36. u32 reg;
  37. unsigned long rate;
  38. struct clk_factors *factors = to_clk_factors(hw);
  39. const struct clk_factors_config *config = factors->config;
  40. /* Fetch the register value */
  41. reg = readl(factors->reg);
  42. /* Get each individual factor if applicable */
  43. if (config->nwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  44. n = FACTOR_GET(config->nshift, config->nwidth, reg);
  45. if (config->kwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  46. k = FACTOR_GET(config->kshift, config->kwidth, reg);
  47. if (config->mwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  48. m = FACTOR_GET(config->mshift, config->mwidth, reg);
  49. if (config->pwidth != SUNXI_FACTORS_NOT_APPLICABLE)
  50. p = FACTOR_GET(config->pshift, config->pwidth, reg);
  51. if (factors->recalc) {
  52. struct factors_request factors_req = {
  53. .parent_rate = parent_rate,
  54. .n = n,
  55. .k = k,
  56. .m = m,
  57. .p = p,
  58. };
  59. /* get mux details from mux clk structure */
  60. if (factors->mux)
  61. factors_req.parent_index =
  62. (reg >> factors->mux->shift) &
  63. factors->mux->mask;
  64. factors->recalc(&factors_req);
  65. return factors_req.rate;
  66. }
  67. /* Calculate the rate */
  68. rate = (parent_rate * (n + config->n_start) * (k + 1) >> p) / (m + 1);
  69. return rate;
  70. }
  71. static int clk_factors_determine_rate(struct clk_hw *hw,
  72. struct clk_rate_request *req)
  73. {
  74. struct clk_factors *factors = to_clk_factors(hw);
  75. struct clk_hw *parent, *best_parent = NULL;
  76. int i, num_parents;
  77. unsigned long parent_rate, best = 0, child_rate, best_child_rate = 0;
  78. /* find the parent that can help provide the fastest rate <= rate */
  79. num_parents = clk_hw_get_num_parents(hw);
  80. for (i = 0; i < num_parents; i++) {
  81. struct factors_request factors_req = {
  82. .rate = req->rate,
  83. .parent_index = i,
  84. };
  85. parent = clk_hw_get_parent_by_index(hw, i);
  86. if (!parent)
  87. continue;
  88. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)
  89. parent_rate = clk_hw_round_rate(parent, req->rate);
  90. else
  91. parent_rate = clk_hw_get_rate(parent);
  92. factors_req.parent_rate = parent_rate;
  93. factors->get_factors(&factors_req);
  94. child_rate = factors_req.rate;
  95. if (child_rate <= req->rate && child_rate > best_child_rate) {
  96. best_parent = parent;
  97. best = parent_rate;
  98. best_child_rate = child_rate;
  99. }
  100. }
  101. if (!best_parent)
  102. return -EINVAL;
  103. req->best_parent_hw = best_parent;
  104. req->best_parent_rate = best;
  105. req->rate = best_child_rate;
  106. return 0;
  107. }
  108. static int clk_factors_set_rate(struct clk_hw *hw, unsigned long rate,
  109. unsigned long parent_rate)
  110. {
  111. struct factors_request req = {
  112. .rate = rate,
  113. .parent_rate = parent_rate,
  114. };
  115. u32 reg;
  116. struct clk_factors *factors = to_clk_factors(hw);
  117. const struct clk_factors_config *config = factors->config;
  118. unsigned long flags = 0;
  119. factors->get_factors(&req);
  120. if (factors->lock)
  121. spin_lock_irqsave(factors->lock, flags);
  122. /* Fetch the register value */
  123. reg = readl(factors->reg);
  124. /* Set up the new factors - macros do not do anything if width is 0 */
  125. reg = FACTOR_SET(config->nshift, config->nwidth, reg, req.n);
  126. reg = FACTOR_SET(config->kshift, config->kwidth, reg, req.k);
  127. reg = FACTOR_SET(config->mshift, config->mwidth, reg, req.m);
  128. reg = FACTOR_SET(config->pshift, config->pwidth, reg, req.p);
  129. /* Apply them now */
  130. writel(reg, factors->reg);
  131. /* delay 500us so pll stabilizes */
  132. __delay((rate >> 20) * 500 / 2);
  133. if (factors->lock)
  134. spin_unlock_irqrestore(factors->lock, flags);
  135. return 0;
  136. }
  137. static const struct clk_ops clk_factors_ops = {
  138. .determine_rate = clk_factors_determine_rate,
  139. .recalc_rate = clk_factors_recalc_rate,
  140. .set_rate = clk_factors_set_rate,
  141. };
  142. static struct clk *__sunxi_factors_register(struct device_node *node,
  143. const struct factors_data *data,
  144. spinlock_t *lock, void __iomem *reg,
  145. unsigned long flags)
  146. {
  147. struct clk *clk;
  148. struct clk_factors *factors;
  149. struct clk_gate *gate = NULL;
  150. struct clk_mux *mux = NULL;
  151. struct clk_hw *gate_hw = NULL;
  152. struct clk_hw *mux_hw = NULL;
  153. const char *clk_name = node->name;
  154. const char *parents[FACTORS_MAX_PARENTS];
  155. int ret, i = 0;
  156. /* if we have a mux, we will have >1 parents */
  157. i = of_clk_parent_fill(node, parents, FACTORS_MAX_PARENTS);
  158. /*
  159. * some factor clocks, such as pll5 and pll6, may have multiple
  160. * outputs, and have their name designated in factors_data
  161. */
  162. if (data->name)
  163. clk_name = data->name;
  164. else
  165. of_property_read_string(node, "clock-output-names", &clk_name);
  166. factors = kzalloc(sizeof(struct clk_factors), GFP_KERNEL);
  167. if (!factors)
  168. goto err_factors;
  169. /* set up factors properties */
  170. factors->reg = reg;
  171. factors->config = data->table;
  172. factors->get_factors = data->getter;
  173. factors->recalc = data->recalc;
  174. factors->lock = lock;
  175. /* Add a gate if this factor clock can be gated */
  176. if (data->enable) {
  177. gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
  178. if (!gate)
  179. goto err_gate;
  180. factors->gate = gate;
  181. /* set up gate properties */
  182. gate->reg = reg;
  183. gate->bit_idx = data->enable;
  184. gate->lock = factors->lock;
  185. gate_hw = &gate->hw;
  186. }
  187. /* Add a mux if this factor clock can be muxed */
  188. if (data->mux) {
  189. mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
  190. if (!mux)
  191. goto err_mux;
  192. factors->mux = mux;
  193. /* set up gate properties */
  194. mux->reg = reg;
  195. mux->shift = data->mux;
  196. mux->mask = data->muxmask;
  197. mux->lock = factors->lock;
  198. mux_hw = &mux->hw;
  199. }
  200. clk = clk_register_composite(NULL, clk_name,
  201. parents, i,
  202. mux_hw, &clk_mux_ops,
  203. &factors->hw, &clk_factors_ops,
  204. gate_hw, &clk_gate_ops, CLK_IS_CRITICAL);
  205. if (IS_ERR(clk))
  206. goto err_register;
  207. ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  208. if (ret)
  209. goto err_provider;
  210. return clk;
  211. err_provider:
  212. /* TODO: The composite clock stuff will leak a bit here. */
  213. clk_unregister(clk);
  214. err_register:
  215. kfree(mux);
  216. err_mux:
  217. kfree(gate);
  218. err_gate:
  219. kfree(factors);
  220. err_factors:
  221. return NULL;
  222. }
  223. struct clk *sunxi_factors_register(struct device_node *node,
  224. const struct factors_data *data,
  225. spinlock_t *lock,
  226. void __iomem *reg)
  227. {
  228. return __sunxi_factors_register(node, data, lock, reg, 0);
  229. }
  230. struct clk *sunxi_factors_register_critical(struct device_node *node,
  231. const struct factors_data *data,
  232. spinlock_t *lock,
  233. void __iomem *reg)
  234. {
  235. return __sunxi_factors_register(node, data, lock, reg, CLK_IS_CRITICAL);
  236. }
  237. void sunxi_factors_unregister(struct device_node *node, struct clk *clk)
  238. {
  239. struct clk_hw *hw = __clk_get_hw(clk);
  240. struct clk_factors *factors;
  241. if (!hw)
  242. return;
  243. factors = to_clk_factors(hw);
  244. of_clk_del_provider(node);
  245. /* TODO: The composite clock stuff will leak a bit here. */
  246. clk_unregister(clk);
  247. kfree(factors->mux);
  248. kfree(factors->gate);
  249. kfree(factors);
  250. }