clk.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. int i;
  54. ctx = kzalloc(sizeof(struct samsung_clk_provider) +
  55. sizeof(*ctx->clk_data.hws) * nr_clks, GFP_KERNEL);
  56. if (!ctx)
  57. panic("could not allocate clock provider context.\n");
  58. for (i = 0; i < nr_clks; ++i)
  59. ctx->clk_data.hws[i] = ERR_PTR(-ENOENT);
  60. ctx->reg_base = base;
  61. ctx->clk_data.num = nr_clks;
  62. spin_lock_init(&ctx->lock);
  63. return ctx;
  64. }
  65. void __init samsung_clk_of_add_provider(struct device_node *np,
  66. struct samsung_clk_provider *ctx)
  67. {
  68. if (np) {
  69. if (of_clk_add_hw_provider(np, of_clk_hw_onecell_get,
  70. &ctx->clk_data))
  71. panic("could not register clk provider\n");
  72. }
  73. }
  74. /* add a clock instance to the clock lookup table used for dt based lookup */
  75. void samsung_clk_add_lookup(struct samsung_clk_provider *ctx,
  76. struct clk_hw *clk_hw, unsigned int id)
  77. {
  78. if (id)
  79. ctx->clk_data.hws[id] = clk_hw;
  80. }
  81. /* register a list of aliases */
  82. void __init samsung_clk_register_alias(struct samsung_clk_provider *ctx,
  83. const struct samsung_clock_alias *list,
  84. unsigned int nr_clk)
  85. {
  86. struct clk_hw *clk_hw;
  87. unsigned int idx, ret;
  88. for (idx = 0; idx < nr_clk; idx++, list++) {
  89. if (!list->id) {
  90. pr_err("%s: clock id missing for index %d\n", __func__,
  91. idx);
  92. continue;
  93. }
  94. clk_hw = ctx->clk_data.hws[list->id];
  95. if (!clk_hw) {
  96. pr_err("%s: failed to find clock %d\n", __func__,
  97. list->id);
  98. continue;
  99. }
  100. ret = clk_hw_register_clkdev(clk_hw, list->alias,
  101. list->dev_name);
  102. if (ret)
  103. pr_err("%s: failed to register lookup %s\n",
  104. __func__, list->alias);
  105. }
  106. }
  107. /* register a list of fixed clocks */
  108. void __init samsung_clk_register_fixed_rate(struct samsung_clk_provider *ctx,
  109. const struct samsung_fixed_rate_clock *list,
  110. unsigned int nr_clk)
  111. {
  112. struct clk_hw *clk_hw;
  113. unsigned int idx, ret;
  114. for (idx = 0; idx < nr_clk; idx++, list++) {
  115. clk_hw = clk_hw_register_fixed_rate(NULL, list->name,
  116. list->parent_name, list->flags, list->fixed_rate);
  117. if (IS_ERR(clk_hw)) {
  118. pr_err("%s: failed to register clock %s\n", __func__,
  119. list->name);
  120. continue;
  121. }
  122. samsung_clk_add_lookup(ctx, clk_hw, 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_hw_register_clkdev(clk_hw, 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. const struct samsung_fixed_factor_clock *list, unsigned int nr_clk)
  136. {
  137. struct clk_hw *clk_hw;
  138. unsigned int idx;
  139. for (idx = 0; idx < nr_clk; idx++, list++) {
  140. clk_hw = clk_hw_register_fixed_factor(NULL, list->name,
  141. list->parent_name, list->flags, list->mult, list->div);
  142. if (IS_ERR(clk_hw)) {
  143. pr_err("%s: failed to register clock %s\n", __func__,
  144. list->name);
  145. continue;
  146. }
  147. samsung_clk_add_lookup(ctx, clk_hw, list->id);
  148. }
  149. }
  150. /* register a list of mux clocks */
  151. void __init samsung_clk_register_mux(struct samsung_clk_provider *ctx,
  152. const struct samsung_mux_clock *list,
  153. unsigned int nr_clk)
  154. {
  155. struct clk_hw *clk_hw;
  156. unsigned int idx, ret;
  157. for (idx = 0; idx < nr_clk; idx++, list++) {
  158. clk_hw = clk_hw_register_mux(NULL, list->name,
  159. list->parent_names, 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_hw)) {
  163. pr_err("%s: failed to register clock %s\n", __func__,
  164. list->name);
  165. continue;
  166. }
  167. samsung_clk_add_lookup(ctx, clk_hw, list->id);
  168. /* register a clock lookup only if a clock alias is specified */
  169. if (list->alias) {
  170. ret = clk_hw_register_clkdev(clk_hw, 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. const struct samsung_div_clock *list,
  181. unsigned int nr_clk)
  182. {
  183. struct clk_hw *clk_hw;
  184. unsigned int idx, ret;
  185. for (idx = 0; idx < nr_clk; idx++, list++) {
  186. if (list->table)
  187. clk_hw = clk_hw_register_divider_table(NULL,
  188. list->name, 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_hw = clk_hw_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_hw)) {
  198. pr_err("%s: failed to register clock %s\n", __func__,
  199. list->name);
  200. continue;
  201. }
  202. samsung_clk_add_lookup(ctx, clk_hw, list->id);
  203. /* register a clock lookup only if a clock alias is specified */
  204. if (list->alias) {
  205. ret = clk_hw_register_clkdev(clk_hw, 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. const struct samsung_gate_clock *list,
  216. unsigned int nr_clk)
  217. {
  218. struct clk_hw *clk_hw;
  219. unsigned int idx, ret;
  220. for (idx = 0; idx < nr_clk; idx++, list++) {
  221. clk_hw = clk_hw_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_hw)) {
  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_hw_register_clkdev(clk_hw, 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_hw, 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. void __init samsung_clk_of_register_fixed_ext(struct samsung_clk_provider *ctx,
  245. struct samsung_fixed_rate_clock *fixed_rate_clk,
  246. unsigned int nr_fixed_rate_clk,
  247. const struct of_device_id *clk_matches)
  248. {
  249. const struct of_device_id *match;
  250. struct device_node *clk_np;
  251. u32 freq;
  252. for_each_matching_node_and_match(clk_np, clk_matches, &match) {
  253. if (of_property_read_u32(clk_np, "clock-frequency", &freq))
  254. continue;
  255. fixed_rate_clk[(unsigned long)match->data].fixed_rate = freq;
  256. }
  257. samsung_clk_register_fixed_rate(ctx, fixed_rate_clk, nr_fixed_rate_clk);
  258. }
  259. /* utility function to get the rate of a specified clock */
  260. unsigned long _get_rate(const char *clk_name)
  261. {
  262. struct clk *clk;
  263. clk = __clk_lookup(clk_name);
  264. if (!clk) {
  265. pr_err("%s: could not find clock %s\n", __func__, clk_name);
  266. return 0;
  267. }
  268. return clk_get_rate(clk);
  269. }
  270. #ifdef CONFIG_PM_SLEEP
  271. static int samsung_clk_suspend(void)
  272. {
  273. struct samsung_clock_reg_cache *reg_cache;
  274. list_for_each_entry(reg_cache, &clock_reg_cache_list, node)
  275. samsung_clk_save(reg_cache->reg_base, reg_cache->rdump,
  276. reg_cache->rd_num);
  277. return 0;
  278. }
  279. static void samsung_clk_resume(void)
  280. {
  281. struct samsung_clock_reg_cache *reg_cache;
  282. list_for_each_entry(reg_cache, &clock_reg_cache_list, node)
  283. samsung_clk_restore(reg_cache->reg_base, reg_cache->rdump,
  284. reg_cache->rd_num);
  285. }
  286. static struct syscore_ops samsung_clk_syscore_ops = {
  287. .suspend = samsung_clk_suspend,
  288. .resume = samsung_clk_resume,
  289. };
  290. void samsung_clk_sleep_init(void __iomem *reg_base,
  291. const unsigned long *rdump,
  292. unsigned long nr_rdump)
  293. {
  294. struct samsung_clock_reg_cache *reg_cache;
  295. reg_cache = kzalloc(sizeof(struct samsung_clock_reg_cache),
  296. GFP_KERNEL);
  297. if (!reg_cache)
  298. panic("could not allocate register reg_cache.\n");
  299. reg_cache->rdump = samsung_clk_alloc_reg_dump(rdump, nr_rdump);
  300. if (!reg_cache->rdump)
  301. panic("could not allocate register dump storage.\n");
  302. if (list_empty(&clock_reg_cache_list))
  303. register_syscore_ops(&samsung_clk_syscore_ops);
  304. reg_cache->reg_base = reg_base;
  305. reg_cache->rd_num = nr_rdump;
  306. list_add_tail(&reg_cache->node, &clock_reg_cache_list);
  307. }
  308. #else
  309. void samsung_clk_sleep_init(void __iomem *reg_base,
  310. const unsigned long *rdump,
  311. unsigned long nr_rdump) {}
  312. #endif
  313. /*
  314. * Common function which registers plls, muxes, dividers and gates
  315. * for each CMU. It also add CMU register list to register cache.
  316. */
  317. struct samsung_clk_provider * __init samsung_cmu_register_one(
  318. struct device_node *np,
  319. const struct samsung_cmu_info *cmu)
  320. {
  321. void __iomem *reg_base;
  322. struct samsung_clk_provider *ctx;
  323. reg_base = of_iomap(np, 0);
  324. if (!reg_base) {
  325. panic("%s: failed to map registers\n", __func__);
  326. return NULL;
  327. }
  328. ctx = samsung_clk_init(np, reg_base, cmu->nr_clk_ids);
  329. if (!ctx) {
  330. panic("%s: unable to allocate ctx\n", __func__);
  331. return ctx;
  332. }
  333. if (cmu->pll_clks)
  334. samsung_clk_register_pll(ctx, cmu->pll_clks, cmu->nr_pll_clks,
  335. reg_base);
  336. if (cmu->mux_clks)
  337. samsung_clk_register_mux(ctx, cmu->mux_clks,
  338. cmu->nr_mux_clks);
  339. if (cmu->div_clks)
  340. samsung_clk_register_div(ctx, cmu->div_clks, cmu->nr_div_clks);
  341. if (cmu->gate_clks)
  342. samsung_clk_register_gate(ctx, cmu->gate_clks,
  343. cmu->nr_gate_clks);
  344. if (cmu->fixed_clks)
  345. samsung_clk_register_fixed_rate(ctx, cmu->fixed_clks,
  346. cmu->nr_fixed_clks);
  347. if (cmu->fixed_factor_clks)
  348. samsung_clk_register_fixed_factor(ctx, cmu->fixed_factor_clks,
  349. cmu->nr_fixed_factor_clks);
  350. if (cmu->clk_regs)
  351. samsung_clk_sleep_init(reg_base, cmu->clk_regs,
  352. cmu->nr_clk_regs);
  353. samsung_clk_of_add_provider(np, ctx);
  354. return ctx;
  355. }