clk-flexgen.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * clk-flexgen.c
  3. *
  4. * Copyright (C) ST-Microelectronics SA 2013
  5. * Author: Maxime Coquelin <maxime.coquelin@st.com> for ST-Microelectronics.
  6. * License terms: GNU General Public License (GPL), version 2 */
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/module.h>
  10. #include <linux/slab.h>
  11. #include <linux/io.h>
  12. #include <linux/err.h>
  13. #include <linux/string.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. struct flexgen {
  17. struct clk_hw hw;
  18. /* Crossbar */
  19. struct clk_mux mux;
  20. /* Pre-divisor's gate */
  21. struct clk_gate pgate;
  22. /* Pre-divisor */
  23. struct clk_divider pdiv;
  24. /* Final divisor's gate */
  25. struct clk_gate fgate;
  26. /* Final divisor */
  27. struct clk_divider fdiv;
  28. };
  29. #define to_flexgen(_hw) container_of(_hw, struct flexgen, hw)
  30. static int flexgen_enable(struct clk_hw *hw)
  31. {
  32. struct flexgen *flexgen = to_flexgen(hw);
  33. struct clk_hw *pgate_hw = &flexgen->pgate.hw;
  34. struct clk_hw *fgate_hw = &flexgen->fgate.hw;
  35. __clk_hw_set_clk(pgate_hw, hw);
  36. __clk_hw_set_clk(fgate_hw, hw);
  37. clk_gate_ops.enable(pgate_hw);
  38. clk_gate_ops.enable(fgate_hw);
  39. pr_debug("%s: flexgen output enabled\n", clk_hw_get_name(hw));
  40. return 0;
  41. }
  42. static void flexgen_disable(struct clk_hw *hw)
  43. {
  44. struct flexgen *flexgen = to_flexgen(hw);
  45. struct clk_hw *fgate_hw = &flexgen->fgate.hw;
  46. /* disable only the final gate */
  47. __clk_hw_set_clk(fgate_hw, hw);
  48. clk_gate_ops.disable(fgate_hw);
  49. pr_debug("%s: flexgen output disabled\n", clk_hw_get_name(hw));
  50. }
  51. static int flexgen_is_enabled(struct clk_hw *hw)
  52. {
  53. struct flexgen *flexgen = to_flexgen(hw);
  54. struct clk_hw *fgate_hw = &flexgen->fgate.hw;
  55. __clk_hw_set_clk(fgate_hw, hw);
  56. if (!clk_gate_ops.is_enabled(fgate_hw))
  57. return 0;
  58. return 1;
  59. }
  60. static u8 flexgen_get_parent(struct clk_hw *hw)
  61. {
  62. struct flexgen *flexgen = to_flexgen(hw);
  63. struct clk_hw *mux_hw = &flexgen->mux.hw;
  64. __clk_hw_set_clk(mux_hw, hw);
  65. return clk_mux_ops.get_parent(mux_hw);
  66. }
  67. static int flexgen_set_parent(struct clk_hw *hw, u8 index)
  68. {
  69. struct flexgen *flexgen = to_flexgen(hw);
  70. struct clk_hw *mux_hw = &flexgen->mux.hw;
  71. __clk_hw_set_clk(mux_hw, hw);
  72. return clk_mux_ops.set_parent(mux_hw, index);
  73. }
  74. static inline unsigned long
  75. clk_best_div(unsigned long parent_rate, unsigned long rate)
  76. {
  77. return parent_rate / rate + ((rate > (2*(parent_rate % rate))) ? 0 : 1);
  78. }
  79. static long flexgen_round_rate(struct clk_hw *hw, unsigned long rate,
  80. unsigned long *prate)
  81. {
  82. unsigned long div;
  83. /* Round div according to exact prate and wished rate */
  84. div = clk_best_div(*prate, rate);
  85. if (clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT) {
  86. *prate = rate * div;
  87. return rate;
  88. }
  89. return *prate / div;
  90. }
  91. static unsigned long flexgen_recalc_rate(struct clk_hw *hw,
  92. unsigned long parent_rate)
  93. {
  94. struct flexgen *flexgen = to_flexgen(hw);
  95. struct clk_hw *pdiv_hw = &flexgen->pdiv.hw;
  96. struct clk_hw *fdiv_hw = &flexgen->fdiv.hw;
  97. unsigned long mid_rate;
  98. __clk_hw_set_clk(pdiv_hw, hw);
  99. __clk_hw_set_clk(fdiv_hw, hw);
  100. mid_rate = clk_divider_ops.recalc_rate(pdiv_hw, parent_rate);
  101. return clk_divider_ops.recalc_rate(fdiv_hw, mid_rate);
  102. }
  103. static int flexgen_set_rate(struct clk_hw *hw, unsigned long rate,
  104. unsigned long parent_rate)
  105. {
  106. struct flexgen *flexgen = to_flexgen(hw);
  107. struct clk_hw *pdiv_hw = &flexgen->pdiv.hw;
  108. struct clk_hw *fdiv_hw = &flexgen->fdiv.hw;
  109. unsigned long div = 0;
  110. int ret = 0;
  111. __clk_hw_set_clk(pdiv_hw, hw);
  112. __clk_hw_set_clk(fdiv_hw, hw);
  113. div = clk_best_div(parent_rate, rate);
  114. /*
  115. * pdiv is mainly targeted for low freq results, while fdiv
  116. * should be used for div <= 64. The other way round can
  117. * lead to 'duty cycle' issues.
  118. */
  119. if (div <= 64) {
  120. clk_divider_ops.set_rate(pdiv_hw, parent_rate, parent_rate);
  121. ret = clk_divider_ops.set_rate(fdiv_hw, rate, rate * div);
  122. } else {
  123. clk_divider_ops.set_rate(fdiv_hw, parent_rate, parent_rate);
  124. ret = clk_divider_ops.set_rate(pdiv_hw, rate, rate * div);
  125. }
  126. return ret;
  127. }
  128. static const struct clk_ops flexgen_ops = {
  129. .enable = flexgen_enable,
  130. .disable = flexgen_disable,
  131. .is_enabled = flexgen_is_enabled,
  132. .get_parent = flexgen_get_parent,
  133. .set_parent = flexgen_set_parent,
  134. .round_rate = flexgen_round_rate,
  135. .recalc_rate = flexgen_recalc_rate,
  136. .set_rate = flexgen_set_rate,
  137. };
  138. static struct clk *clk_register_flexgen(const char *name,
  139. const char **parent_names, u8 num_parents,
  140. void __iomem *reg, spinlock_t *lock, u32 idx,
  141. unsigned long flexgen_flags) {
  142. struct flexgen *fgxbar;
  143. struct clk *clk;
  144. struct clk_init_data init;
  145. u32 xbar_shift;
  146. void __iomem *xbar_reg, *fdiv_reg;
  147. fgxbar = kzalloc(sizeof(struct flexgen), GFP_KERNEL);
  148. if (!fgxbar)
  149. return ERR_PTR(-ENOMEM);
  150. init.name = name;
  151. init.ops = &flexgen_ops;
  152. init.flags = CLK_IS_BASIC | CLK_GET_RATE_NOCACHE | flexgen_flags;
  153. init.parent_names = parent_names;
  154. init.num_parents = num_parents;
  155. xbar_reg = reg + 0x18 + (idx & ~0x3);
  156. xbar_shift = (idx % 4) * 0x8;
  157. fdiv_reg = reg + 0x164 + idx * 4;
  158. /* Crossbar element config */
  159. fgxbar->mux.lock = lock;
  160. fgxbar->mux.mask = BIT(6) - 1;
  161. fgxbar->mux.reg = xbar_reg;
  162. fgxbar->mux.shift = xbar_shift;
  163. fgxbar->mux.table = NULL;
  164. /* Pre-divider's gate config (in xbar register)*/
  165. fgxbar->pgate.lock = lock;
  166. fgxbar->pgate.reg = xbar_reg;
  167. fgxbar->pgate.bit_idx = xbar_shift + 6;
  168. /* Pre-divider config */
  169. fgxbar->pdiv.lock = lock;
  170. fgxbar->pdiv.reg = reg + 0x58 + idx * 4;
  171. fgxbar->pdiv.width = 10;
  172. /* Final divider's gate config */
  173. fgxbar->fgate.lock = lock;
  174. fgxbar->fgate.reg = fdiv_reg;
  175. fgxbar->fgate.bit_idx = 6;
  176. /* Final divider config */
  177. fgxbar->fdiv.lock = lock;
  178. fgxbar->fdiv.reg = fdiv_reg;
  179. fgxbar->fdiv.width = 6;
  180. fgxbar->hw.init = &init;
  181. clk = clk_register(NULL, &fgxbar->hw);
  182. if (IS_ERR(clk))
  183. kfree(fgxbar);
  184. else
  185. pr_debug("%s: parent %s rate %u\n",
  186. __clk_get_name(clk),
  187. __clk_get_name(clk_get_parent(clk)),
  188. (unsigned int)clk_get_rate(clk));
  189. return clk;
  190. }
  191. static const char ** __init flexgen_get_parents(struct device_node *np,
  192. int *num_parents)
  193. {
  194. const char **parents;
  195. int nparents;
  196. nparents = of_clk_get_parent_count(np);
  197. if (WARN_ON(nparents <= 0))
  198. return NULL;
  199. parents = kcalloc(nparents, sizeof(const char *), GFP_KERNEL);
  200. if (!parents)
  201. return NULL;
  202. *num_parents = of_clk_parent_fill(np, parents, nparents);
  203. return parents;
  204. }
  205. static void __init st_of_flexgen_setup(struct device_node *np)
  206. {
  207. struct device_node *pnode;
  208. void __iomem *reg;
  209. struct clk_onecell_data *clk_data;
  210. const char **parents;
  211. int num_parents, i;
  212. spinlock_t *rlock = NULL;
  213. unsigned long flex_flags = 0;
  214. int ret;
  215. pnode = of_get_parent(np);
  216. if (!pnode)
  217. return;
  218. reg = of_iomap(pnode, 0);
  219. if (!reg)
  220. return;
  221. parents = flexgen_get_parents(np, &num_parents);
  222. if (!parents)
  223. return;
  224. clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
  225. if (!clk_data)
  226. goto err;
  227. ret = of_property_count_strings(np, "clock-output-names");
  228. if (ret <= 0) {
  229. pr_err("%s: Failed to get number of output clocks (%d)",
  230. __func__, clk_data->clk_num);
  231. goto err;
  232. }
  233. clk_data->clk_num = ret;
  234. clk_data->clks = kcalloc(clk_data->clk_num, sizeof(struct clk *),
  235. GFP_KERNEL);
  236. if (!clk_data->clks)
  237. goto err;
  238. rlock = kzalloc(sizeof(spinlock_t), GFP_KERNEL);
  239. if (!rlock)
  240. goto err;
  241. spin_lock_init(rlock);
  242. for (i = 0; i < clk_data->clk_num; i++) {
  243. struct clk *clk;
  244. const char *clk_name;
  245. if (of_property_read_string_index(np, "clock-output-names",
  246. i, &clk_name)) {
  247. break;
  248. }
  249. /*
  250. * If we read an empty clock name then the output is unused
  251. */
  252. if (*clk_name == '\0')
  253. continue;
  254. clk = clk_register_flexgen(clk_name, parents, num_parents,
  255. reg, rlock, i, flex_flags);
  256. if (IS_ERR(clk))
  257. goto err;
  258. clk_data->clks[i] = clk;
  259. }
  260. kfree(parents);
  261. of_clk_add_provider(np, of_clk_src_onecell_get, clk_data);
  262. return;
  263. err:
  264. if (clk_data)
  265. kfree(clk_data->clks);
  266. kfree(clk_data);
  267. kfree(parents);
  268. kfree(rlock);
  269. }
  270. CLK_OF_DECLARE(flexgen, "st,flexgen", st_of_flexgen_setup);