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