clk.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2013 Samsung Electronics Co., Ltd.
  3. * Copyright (c) 2013 Linaro Ltd.
  4. * Author: Thomas Abraham <thomas.ab@samsung.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * This file includes utility functions to register clocks to common
  11. * clock framework for Samsung platforms.
  12. */
  13. #include <linux/syscore_ops.h>
  14. #include "clk.h"
  15. void samsung_clk_save(void __iomem *base,
  16. struct samsung_clk_reg_dump *rd,
  17. unsigned int num_regs)
  18. {
  19. for (; num_regs > 0; --num_regs, ++rd)
  20. rd->value = readl(base + rd->offset);
  21. }
  22. void samsung_clk_restore(void __iomem *base,
  23. const struct samsung_clk_reg_dump *rd,
  24. unsigned int num_regs)
  25. {
  26. for (; num_regs > 0; --num_regs, ++rd)
  27. writel(rd->value, base + rd->offset);
  28. }
  29. struct samsung_clk_reg_dump *samsung_clk_alloc_reg_dump(
  30. const unsigned long *rdump,
  31. unsigned long nr_rdump)
  32. {
  33. struct samsung_clk_reg_dump *rd;
  34. unsigned int i;
  35. rd = kcalloc(nr_rdump, sizeof(*rd), GFP_KERNEL);
  36. if (!rd)
  37. return NULL;
  38. for (i = 0; i < nr_rdump; ++i)
  39. rd[i].offset = rdump[i];
  40. return rd;
  41. }
  42. /* setup the essentials required to support clock lookup using ccf */
  43. struct samsung_clk_provider *__init samsung_clk_init(struct device_node *np,
  44. void __iomem *base, unsigned long nr_clks)
  45. {
  46. struct samsung_clk_provider *ctx;
  47. struct clk **clk_table;
  48. int i;
  49. ctx = kzalloc(sizeof(struct samsung_clk_provider), GFP_KERNEL);
  50. if (!ctx)
  51. panic("could not allocate clock provider context.\n");
  52. clk_table = kcalloc(nr_clks, sizeof(struct clk *), GFP_KERNEL);
  53. if (!clk_table)
  54. panic("could not allocate clock lookup table\n");
  55. for (i = 0; i < nr_clks; ++i)
  56. clk_table[i] = ERR_PTR(-ENOENT);
  57. ctx->reg_base = base;
  58. ctx->clk_data.clks = clk_table;
  59. ctx->clk_data.clk_num = nr_clks;
  60. spin_lock_init(&ctx->lock);
  61. return ctx;
  62. }
  63. void __init samsung_clk_of_add_provider(struct device_node *np,
  64. struct samsung_clk_provider *ctx)
  65. {
  66. if (np) {
  67. if (of_clk_add_provider(np, of_clk_src_onecell_get,
  68. &ctx->clk_data))
  69. panic("could not register clk provider\n");
  70. }
  71. }
  72. /* add a clock instance to the clock lookup table used for dt based lookup */
  73. void samsung_clk_add_lookup(struct samsung_clk_provider *ctx, struct clk *clk,
  74. unsigned int id)
  75. {
  76. if (ctx->clk_data.clks && id)
  77. ctx->clk_data.clks[id] = clk;
  78. }
  79. /* register a list of aliases */
  80. void __init samsung_clk_register_alias(struct samsung_clk_provider *ctx,
  81. struct samsung_clock_alias *list,
  82. unsigned int nr_clk)
  83. {
  84. struct clk *clk;
  85. unsigned int idx, ret;
  86. if (!ctx->clk_data.clks) {
  87. pr_err("%s: clock table missing\n", __func__);
  88. return;
  89. }
  90. for (idx = 0; idx < nr_clk; idx++, list++) {
  91. if (!list->id) {
  92. pr_err("%s: clock id missing for index %d\n", __func__,
  93. idx);
  94. continue;
  95. }
  96. clk = ctx->clk_data.clks[list->id];
  97. if (!clk) {
  98. pr_err("%s: failed to find clock %d\n", __func__,
  99. list->id);
  100. continue;
  101. }
  102. ret = clk_register_clkdev(clk, list->alias, list->dev_name);
  103. if (ret)
  104. pr_err("%s: failed to register lookup %s\n",
  105. __func__, list->alias);
  106. }
  107. }
  108. /* register a list of fixed clocks */
  109. void __init samsung_clk_register_fixed_rate(struct samsung_clk_provider *ctx,
  110. struct samsung_fixed_rate_clock *list, unsigned int nr_clk)
  111. {
  112. struct clk *clk;
  113. unsigned int idx, ret;
  114. for (idx = 0; idx < nr_clk; idx++, list++) {
  115. clk = clk_register_fixed_rate(NULL, list->name,
  116. list->parent_name, list->flags, list->fixed_rate);
  117. if (IS_ERR(clk)) {
  118. pr_err("%s: failed to register clock %s\n", __func__,
  119. list->name);
  120. continue;
  121. }
  122. samsung_clk_add_lookup(ctx, clk, list->id);
  123. /*
  124. * Unconditionally add a clock lookup for the fixed rate clocks.
  125. * There are not many of these on any of Samsung platforms.
  126. */
  127. ret = clk_register_clkdev(clk, list->name, NULL);
  128. if (ret)
  129. pr_err("%s: failed to register clock lookup for %s",
  130. __func__, list->name);
  131. }
  132. }
  133. /* register a list of fixed factor clocks */
  134. void __init samsung_clk_register_fixed_factor(struct samsung_clk_provider *ctx,
  135. struct samsung_fixed_factor_clock *list, unsigned int nr_clk)
  136. {
  137. struct clk *clk;
  138. unsigned int idx;
  139. for (idx = 0; idx < nr_clk; idx++, list++) {
  140. clk = clk_register_fixed_factor(NULL, list->name,
  141. list->parent_name, list->flags, list->mult, list->div);
  142. if (IS_ERR(clk)) {
  143. pr_err("%s: failed to register clock %s\n", __func__,
  144. list->name);
  145. continue;
  146. }
  147. samsung_clk_add_lookup(ctx, clk, list->id);
  148. }
  149. }
  150. /* register a list of mux clocks */
  151. void __init samsung_clk_register_mux(struct samsung_clk_provider *ctx,
  152. struct samsung_mux_clock *list,
  153. unsigned int nr_clk)
  154. {
  155. struct clk *clk;
  156. unsigned int idx, ret;
  157. for (idx = 0; idx < nr_clk; idx++, list++) {
  158. clk = clk_register_mux(NULL, list->name, list->parent_names,
  159. list->num_parents, list->flags,
  160. ctx->reg_base + list->offset,
  161. list->shift, list->width, list->mux_flags, &ctx->lock);
  162. if (IS_ERR(clk)) {
  163. pr_err("%s: failed to register clock %s\n", __func__,
  164. list->name);
  165. continue;
  166. }
  167. samsung_clk_add_lookup(ctx, clk, list->id);
  168. /* register a clock lookup only if a clock alias is specified */
  169. if (list->alias) {
  170. ret = clk_register_clkdev(clk, list->alias,
  171. list->dev_name);
  172. if (ret)
  173. pr_err("%s: failed to register lookup %s\n",
  174. __func__, list->alias);
  175. }
  176. }
  177. }
  178. /* register a list of div clocks */
  179. void __init samsung_clk_register_div(struct samsung_clk_provider *ctx,
  180. struct samsung_div_clock *list,
  181. unsigned int nr_clk)
  182. {
  183. struct clk *clk;
  184. unsigned int idx, ret;
  185. for (idx = 0; idx < nr_clk; idx++, list++) {
  186. if (list->table)
  187. clk = clk_register_divider_table(NULL, list->name,
  188. list->parent_name, list->flags,
  189. ctx->reg_base + list->offset,
  190. list->shift, list->width, list->div_flags,
  191. list->table, &ctx->lock);
  192. else
  193. clk = clk_register_divider(NULL, list->name,
  194. list->parent_name, list->flags,
  195. ctx->reg_base + list->offset, list->shift,
  196. list->width, list->div_flags, &ctx->lock);
  197. if (IS_ERR(clk)) {
  198. pr_err("%s: failed to register clock %s\n", __func__,
  199. list->name);
  200. continue;
  201. }
  202. samsung_clk_add_lookup(ctx, clk, list->id);
  203. /* register a clock lookup only if a clock alias is specified */
  204. if (list->alias) {
  205. ret = clk_register_clkdev(clk, list->alias,
  206. list->dev_name);
  207. if (ret)
  208. pr_err("%s: failed to register lookup %s\n",
  209. __func__, list->alias);
  210. }
  211. }
  212. }
  213. /* register a list of gate clocks */
  214. void __init samsung_clk_register_gate(struct samsung_clk_provider *ctx,
  215. struct samsung_gate_clock *list,
  216. unsigned int nr_clk)
  217. {
  218. struct clk *clk;
  219. unsigned int idx, ret;
  220. for (idx = 0; idx < nr_clk; idx++, list++) {
  221. clk = clk_register_gate(NULL, list->name, list->parent_name,
  222. list->flags, ctx->reg_base + list->offset,
  223. list->bit_idx, list->gate_flags, &ctx->lock);
  224. if (IS_ERR(clk)) {
  225. pr_err("%s: failed to register clock %s\n", __func__,
  226. list->name);
  227. continue;
  228. }
  229. /* register a clock lookup only if a clock alias is specified */
  230. if (list->alias) {
  231. ret = clk_register_clkdev(clk, list->alias,
  232. list->dev_name);
  233. if (ret)
  234. pr_err("%s: failed to register lookup %s\n",
  235. __func__, list->alias);
  236. }
  237. samsung_clk_add_lookup(ctx, clk, list->id);
  238. }
  239. }
  240. /*
  241. * obtain the clock speed of all external fixed clock sources from device
  242. * tree and register it
  243. */
  244. #ifdef CONFIG_OF
  245. void __init samsung_clk_of_register_fixed_ext(struct samsung_clk_provider *ctx,
  246. struct samsung_fixed_rate_clock *fixed_rate_clk,
  247. unsigned int nr_fixed_rate_clk,
  248. const struct of_device_id *clk_matches)
  249. {
  250. const struct of_device_id *match;
  251. struct device_node *clk_np;
  252. u32 freq;
  253. for_each_matching_node_and_match(clk_np, clk_matches, &match) {
  254. if (of_property_read_u32(clk_np, "clock-frequency", &freq))
  255. continue;
  256. fixed_rate_clk[(unsigned long)match->data].fixed_rate = freq;
  257. }
  258. samsung_clk_register_fixed_rate(ctx, fixed_rate_clk, nr_fixed_rate_clk);
  259. }
  260. #endif
  261. /* utility function to get the rate of a specified clock */
  262. unsigned long _get_rate(const char *clk_name)
  263. {
  264. struct clk *clk;
  265. clk = __clk_lookup(clk_name);
  266. if (!clk) {
  267. pr_err("%s: could not find clock %s\n", __func__, clk_name);
  268. return 0;
  269. }
  270. return clk_get_rate(clk);
  271. }