clk-factors.c 7.4 KB

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