clk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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/slab.h>
  14. #include <linux/clkdev.h>
  15. #include <linux/clk.h>
  16. #include <linux/clk-provider.h>
  17. #include <linux/of_address.h>
  18. #include <linux/syscore_ops.h>
  19. #include "clk.h"
  20. static LIST_HEAD(clock_reg_cache_list);
  21. void samsung_clk_save(void __iomem *base,
  22. struct samsung_clk_reg_dump *rd,
  23. unsigned int num_regs)
  24. {
  25. for (; num_regs > 0; --num_regs, ++rd)
  26. rd->value = readl(base + rd->offset);
  27. }
  28. void samsung_clk_restore(void __iomem *base,
  29. const struct samsung_clk_reg_dump *rd,
  30. unsigned int num_regs)
  31. {
  32. for (; num_regs > 0; --num_regs, ++rd)
  33. writel(rd->value, base + rd->offset);
  34. }
  35. struct samsung_clk_reg_dump *samsung_clk_alloc_reg_dump(
  36. const unsigned long *rdump,
  37. unsigned long nr_rdump)
  38. {
  39. struct samsung_clk_reg_dump *rd;
  40. unsigned int i;
  41. rd = kcalloc(nr_rdump, sizeof(*rd), GFP_KERNEL);
  42. if (!rd)
  43. return NULL;
  44. for (i = 0; i < nr_rdump; ++i)
  45. rd[i].offset = rdump[i];
  46. return rd;
  47. }
  48. /* setup the essentials required to support clock lookup using ccf */
  49. struct samsung_clk_provider *__init samsung_clk_init(struct device_node *np,
  50. void __iomem *base, unsigned long nr_clks)
  51. {
  52. struct samsung_clk_provider *ctx;
  53. struct clk **clk_table;
  54. int i;
  55. ctx = kzalloc(sizeof(struct samsung_clk_provider), GFP_KERNEL);
  56. if (!ctx)
  57. panic("could not allocate clock provider context.\n");
  58. clk_table = kcalloc(nr_clks, sizeof(struct clk *), GFP_KERNEL);
  59. if (!clk_table)
  60. panic("could not allocate clock lookup table\n");
  61. for (i = 0; i < nr_clks; ++i)
  62. clk_table[i] = ERR_PTR(-ENOENT);
  63. ctx->reg_base = base;
  64. ctx->clk_data.clks = clk_table;
  65. ctx->clk_data.clk_num = nr_clks;
  66. spin_lock_init(&ctx->lock);
  67. return ctx;
  68. }
  69. void __init samsung_clk_of_add_provider(struct device_node *np,
  70. struct samsung_clk_provider *ctx)
  71. {
  72. if (np) {
  73. if (of_clk_add_provider(np, of_clk_src_onecell_get,
  74. &ctx->clk_data))
  75. panic("could not register clk provider\n");
  76. }
  77. }
  78. /* add a clock instance to the clock lookup table used for dt based lookup */
  79. void samsung_clk_add_lookup(struct samsung_clk_provider *ctx, struct clk *clk,
  80. unsigned int id)
  81. {
  82. if (ctx->clk_data.clks && id)
  83. ctx->clk_data.clks[id] = clk;
  84. }
  85. /* register a list of aliases */
  86. void __init samsung_clk_register_alias(struct samsung_clk_provider *ctx,
  87. const struct samsung_clock_alias *list,
  88. unsigned int nr_clk)
  89. {
  90. struct clk *clk;
  91. unsigned int idx, ret;
  92. if (!ctx->clk_data.clks) {
  93. pr_err("%s: clock table missing\n", __func__);
  94. return;
  95. }
  96. for (idx = 0; idx < nr_clk; idx++, list++) {
  97. if (!list->id) {
  98. pr_err("%s: clock id missing for index %d\n", __func__,
  99. idx);
  100. continue;
  101. }
  102. clk = ctx->clk_data.clks[list->id];
  103. if (!clk) {
  104. pr_err("%s: failed to find clock %d\n", __func__,
  105. list->id);
  106. continue;
  107. }
  108. ret = clk_register_clkdev(clk, list->alias, list->dev_name);
  109. if (ret)
  110. pr_err("%s: failed to register lookup %s\n",
  111. __func__, list->alias);
  112. }
  113. }
  114. /* register a list of fixed clocks */
  115. void __init samsung_clk_register_fixed_rate(struct samsung_clk_provider *ctx,
  116. const struct samsung_fixed_rate_clock *list,
  117. unsigned int nr_clk)
  118. {
  119. struct clk *clk;
  120. unsigned int idx, ret;
  121. for (idx = 0; idx < nr_clk; idx++, list++) {
  122. clk = clk_register_fixed_rate(NULL, list->name,
  123. list->parent_name, list->flags, list->fixed_rate);
  124. if (IS_ERR(clk)) {
  125. pr_err("%s: failed to register clock %s\n", __func__,
  126. list->name);
  127. continue;
  128. }
  129. samsung_clk_add_lookup(ctx, clk, list->id);
  130. /*
  131. * Unconditionally add a clock lookup for the fixed rate clocks.
  132. * There are not many of these on any of Samsung platforms.
  133. */
  134. ret = clk_register_clkdev(clk, list->name, NULL);
  135. if (ret)
  136. pr_err("%s: failed to register clock lookup for %s",
  137. __func__, list->name);
  138. }
  139. }
  140. /* register a list of fixed factor clocks */
  141. void __init samsung_clk_register_fixed_factor(struct samsung_clk_provider *ctx,
  142. const struct samsung_fixed_factor_clock *list, unsigned int nr_clk)
  143. {
  144. struct clk *clk;
  145. unsigned int idx;
  146. for (idx = 0; idx < nr_clk; idx++, list++) {
  147. clk = clk_register_fixed_factor(NULL, list->name,
  148. list->parent_name, list->flags, list->mult, list->div);
  149. if (IS_ERR(clk)) {
  150. pr_err("%s: failed to register clock %s\n", __func__,
  151. list->name);
  152. continue;
  153. }
  154. samsung_clk_add_lookup(ctx, clk, list->id);
  155. }
  156. }
  157. /* register a list of mux clocks */
  158. void __init samsung_clk_register_mux(struct samsung_clk_provider *ctx,
  159. const struct samsung_mux_clock *list,
  160. unsigned int nr_clk)
  161. {
  162. struct clk *clk;
  163. unsigned int idx, ret;
  164. for (idx = 0; idx < nr_clk; idx++, list++) {
  165. clk = clk_register_mux(NULL, list->name, list->parent_names,
  166. list->num_parents, list->flags,
  167. ctx->reg_base + list->offset,
  168. list->shift, list->width, list->mux_flags, &ctx->lock);
  169. if (IS_ERR(clk)) {
  170. pr_err("%s: failed to register clock %s\n", __func__,
  171. list->name);
  172. continue;
  173. }
  174. samsung_clk_add_lookup(ctx, clk, list->id);
  175. /* register a clock lookup only if a clock alias is specified */
  176. if (list->alias) {
  177. ret = clk_register_clkdev(clk, list->alias,
  178. list->dev_name);
  179. if (ret)
  180. pr_err("%s: failed to register lookup %s\n",
  181. __func__, list->alias);
  182. }
  183. }
  184. }
  185. /* register a list of div clocks */
  186. void __init samsung_clk_register_div(struct samsung_clk_provider *ctx,
  187. const struct samsung_div_clock *list,
  188. unsigned int nr_clk)
  189. {
  190. struct clk *clk;
  191. unsigned int idx, ret;
  192. for (idx = 0; idx < nr_clk; idx++, list++) {
  193. if (list->table)
  194. clk = clk_register_divider_table(NULL, list->name,
  195. list->parent_name, list->flags,
  196. ctx->reg_base + list->offset,
  197. list->shift, list->width, list->div_flags,
  198. list->table, &ctx->lock);
  199. else
  200. clk = clk_register_divider(NULL, list->name,
  201. list->parent_name, list->flags,
  202. ctx->reg_base + list->offset, list->shift,
  203. list->width, list->div_flags, &ctx->lock);
  204. if (IS_ERR(clk)) {
  205. pr_err("%s: failed to register clock %s\n", __func__,
  206. list->name);
  207. continue;
  208. }
  209. samsung_clk_add_lookup(ctx, clk, list->id);
  210. /* register a clock lookup only if a clock alias is specified */
  211. if (list->alias) {
  212. ret = clk_register_clkdev(clk, list->alias,
  213. list->dev_name);
  214. if (ret)
  215. pr_err("%s: failed to register lookup %s\n",
  216. __func__, list->alias);
  217. }
  218. }
  219. }
  220. /* register a list of gate clocks */
  221. void __init samsung_clk_register_gate(struct samsung_clk_provider *ctx,
  222. const struct samsung_gate_clock *list,
  223. unsigned int nr_clk)
  224. {
  225. struct clk *clk;
  226. unsigned int idx, ret;
  227. for (idx = 0; idx < nr_clk; idx++, list++) {
  228. clk = clk_register_gate(NULL, list->name, list->parent_name,
  229. list->flags, ctx->reg_base + list->offset,
  230. list->bit_idx, list->gate_flags, &ctx->lock);
  231. if (IS_ERR(clk)) {
  232. pr_err("%s: failed to register clock %s\n", __func__,
  233. list->name);
  234. continue;
  235. }
  236. /* register a clock lookup only if a clock alias is specified */
  237. if (list->alias) {
  238. ret = clk_register_clkdev(clk, list->alias,
  239. list->dev_name);
  240. if (ret)
  241. pr_err("%s: failed to register lookup %s\n",
  242. __func__, list->alias);
  243. }
  244. samsung_clk_add_lookup(ctx, clk, list->id);
  245. }
  246. }
  247. /*
  248. * obtain the clock speed of all external fixed clock sources from device
  249. * tree and register it
  250. */
  251. void __init samsung_clk_of_register_fixed_ext(struct samsung_clk_provider *ctx,
  252. struct samsung_fixed_rate_clock *fixed_rate_clk,
  253. unsigned int nr_fixed_rate_clk,
  254. const struct of_device_id *clk_matches)
  255. {
  256. const struct of_device_id *match;
  257. struct device_node *clk_np;
  258. u32 freq;
  259. for_each_matching_node_and_match(clk_np, clk_matches, &match) {
  260. if (of_property_read_u32(clk_np, "clock-frequency", &freq))
  261. continue;
  262. fixed_rate_clk[(unsigned long)match->data].fixed_rate = freq;
  263. }
  264. samsung_clk_register_fixed_rate(ctx, fixed_rate_clk, nr_fixed_rate_clk);
  265. }
  266. /* utility function to get the rate of a specified clock */
  267. unsigned long _get_rate(const char *clk_name)
  268. {
  269. struct clk *clk;
  270. clk = __clk_lookup(clk_name);
  271. if (!clk) {
  272. pr_err("%s: could not find clock %s\n", __func__, clk_name);
  273. return 0;
  274. }
  275. return clk_get_rate(clk);
  276. }
  277. #ifdef CONFIG_PM_SLEEP
  278. static int samsung_clk_suspend(void)
  279. {
  280. struct samsung_clock_reg_cache *reg_cache;
  281. list_for_each_entry(reg_cache, &clock_reg_cache_list, node)
  282. samsung_clk_save(reg_cache->reg_base, reg_cache->rdump,
  283. reg_cache->rd_num);
  284. return 0;
  285. }
  286. static void samsung_clk_resume(void)
  287. {
  288. struct samsung_clock_reg_cache *reg_cache;
  289. list_for_each_entry(reg_cache, &clock_reg_cache_list, node)
  290. samsung_clk_restore(reg_cache->reg_base, reg_cache->rdump,
  291. reg_cache->rd_num);
  292. }
  293. static struct syscore_ops samsung_clk_syscore_ops = {
  294. .suspend = samsung_clk_suspend,
  295. .resume = samsung_clk_resume,
  296. };
  297. void samsung_clk_sleep_init(void __iomem *reg_base,
  298. const unsigned long *rdump,
  299. unsigned long nr_rdump)
  300. {
  301. struct samsung_clock_reg_cache *reg_cache;
  302. reg_cache = kzalloc(sizeof(struct samsung_clock_reg_cache),
  303. GFP_KERNEL);
  304. if (!reg_cache)
  305. panic("could not allocate register reg_cache.\n");
  306. reg_cache->rdump = samsung_clk_alloc_reg_dump(rdump, nr_rdump);
  307. if (!reg_cache->rdump)
  308. panic("could not allocate register dump storage.\n");
  309. if (list_empty(&clock_reg_cache_list))
  310. register_syscore_ops(&samsung_clk_syscore_ops);
  311. reg_cache->reg_base = reg_base;
  312. reg_cache->rd_num = nr_rdump;
  313. list_add_tail(&reg_cache->node, &clock_reg_cache_list);
  314. }
  315. #else
  316. void samsung_clk_sleep_init(void __iomem *reg_base,
  317. const unsigned long *rdump,
  318. unsigned long nr_rdump) {}
  319. #endif
  320. /*
  321. * Common function which registers plls, muxes, dividers and gates
  322. * for each CMU. It also add CMU register list to register cache.
  323. */
  324. struct samsung_clk_provider * __init samsung_cmu_register_one(
  325. struct device_node *np,
  326. const struct samsung_cmu_info *cmu)
  327. {
  328. void __iomem *reg_base;
  329. struct samsung_clk_provider *ctx;
  330. reg_base = of_iomap(np, 0);
  331. if (!reg_base) {
  332. panic("%s: failed to map registers\n", __func__);
  333. return NULL;
  334. }
  335. ctx = samsung_clk_init(np, reg_base, cmu->nr_clk_ids);
  336. if (!ctx) {
  337. panic("%s: unable to allocate ctx\n", __func__);
  338. return ctx;
  339. }
  340. if (cmu->pll_clks)
  341. samsung_clk_register_pll(ctx, cmu->pll_clks, cmu->nr_pll_clks,
  342. reg_base);
  343. if (cmu->mux_clks)
  344. samsung_clk_register_mux(ctx, cmu->mux_clks,
  345. cmu->nr_mux_clks);
  346. if (cmu->div_clks)
  347. samsung_clk_register_div(ctx, cmu->div_clks, cmu->nr_div_clks);
  348. if (cmu->gate_clks)
  349. samsung_clk_register_gate(ctx, cmu->gate_clks,
  350. cmu->nr_gate_clks);
  351. if (cmu->fixed_clks)
  352. samsung_clk_register_fixed_rate(ctx, cmu->fixed_clks,
  353. cmu->nr_fixed_clks);
  354. if (cmu->fixed_factor_clks)
  355. samsung_clk_register_fixed_factor(ctx, cmu->fixed_factor_clks,
  356. cmu->nr_fixed_factor_clks);
  357. if (cmu->clk_regs)
  358. samsung_clk_sleep_init(reg_base, cmu->clk_regs,
  359. cmu->nr_clk_regs);
  360. samsung_clk_of_add_provider(np, ctx);
  361. return ctx;
  362. }