clk-exynos5-subcmu.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Copyright (c) 2018 Samsung Electronics Co., Ltd.
  4. // Author: Marek Szyprowski <m.szyprowski@samsung.com>
  5. // Common Clock Framework support for Exynos5 power-domain dependent clocks
  6. #include <linux/of_platform.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/pm_domain.h>
  9. #include <linux/pm_runtime.h>
  10. #include "clk.h"
  11. #include "clk-exynos5-subcmu.h"
  12. static struct samsung_clk_provider *ctx;
  13. static const struct exynos5_subcmu_info *cmu;
  14. static int nr_cmus;
  15. static void exynos5_subcmu_clk_save(void __iomem *base,
  16. struct exynos5_subcmu_reg_dump *rd,
  17. unsigned int num_regs)
  18. {
  19. for (; num_regs > 0; --num_regs, ++rd) {
  20. rd->save = readl(base + rd->offset);
  21. writel((rd->save & ~rd->mask) | rd->value, base + rd->offset);
  22. rd->save &= rd->mask;
  23. }
  24. };
  25. static void exynos5_subcmu_clk_restore(void __iomem *base,
  26. struct exynos5_subcmu_reg_dump *rd,
  27. unsigned int num_regs)
  28. {
  29. for (; num_regs > 0; --num_regs, ++rd)
  30. writel((readl(base + rd->offset) & ~rd->mask) | rd->save,
  31. base + rd->offset);
  32. }
  33. static void exynos5_subcmu_defer_gate(struct samsung_clk_provider *ctx,
  34. const struct samsung_gate_clock *list, int nr_clk)
  35. {
  36. while (nr_clk--)
  37. samsung_clk_add_lookup(ctx, ERR_PTR(-EPROBE_DEFER), list++->id);
  38. }
  39. /*
  40. * Pass the needed clock provider context and register sub-CMU clocks
  41. *
  42. * NOTE: This function has to be called from the main, OF_CLK_DECLARE-
  43. * initialized clock provider driver. This happens very early during boot
  44. * process. Then this driver, during core_initcall registers two platform
  45. * drivers: one which binds to the same device-tree node as OF_CLK_DECLARE
  46. * driver and second, for handling its per-domain child-devices. Those
  47. * platform drivers are bound to their devices a bit later in arch_initcall,
  48. * when OF-core populates all device-tree nodes.
  49. */
  50. void exynos5_subcmus_init(struct samsung_clk_provider *_ctx, int _nr_cmus,
  51. const struct exynos5_subcmu_info *_cmu)
  52. {
  53. ctx = _ctx;
  54. cmu = _cmu;
  55. nr_cmus = _nr_cmus;
  56. for (; _nr_cmus--; _cmu++) {
  57. exynos5_subcmu_defer_gate(ctx, _cmu->gate_clks,
  58. _cmu->nr_gate_clks);
  59. exynos5_subcmu_clk_save(ctx->reg_base, _cmu->suspend_regs,
  60. _cmu->nr_suspend_regs);
  61. }
  62. }
  63. static int __maybe_unused exynos5_subcmu_suspend(struct device *dev)
  64. {
  65. struct exynos5_subcmu_info *info = dev_get_drvdata(dev);
  66. unsigned long flags;
  67. spin_lock_irqsave(&ctx->lock, flags);
  68. exynos5_subcmu_clk_save(ctx->reg_base, info->suspend_regs,
  69. info->nr_suspend_regs);
  70. spin_unlock_irqrestore(&ctx->lock, flags);
  71. return 0;
  72. }
  73. static int __maybe_unused exynos5_subcmu_resume(struct device *dev)
  74. {
  75. struct exynos5_subcmu_info *info = dev_get_drvdata(dev);
  76. unsigned long flags;
  77. spin_lock_irqsave(&ctx->lock, flags);
  78. exynos5_subcmu_clk_restore(ctx->reg_base, info->suspend_regs,
  79. info->nr_suspend_regs);
  80. spin_unlock_irqrestore(&ctx->lock, flags);
  81. return 0;
  82. }
  83. static int __init exynos5_subcmu_probe(struct platform_device *pdev)
  84. {
  85. struct device *dev = &pdev->dev;
  86. struct exynos5_subcmu_info *info = dev_get_drvdata(dev);
  87. pm_runtime_set_suspended(dev);
  88. pm_runtime_enable(dev);
  89. pm_runtime_get(dev);
  90. ctx->dev = dev;
  91. samsung_clk_register_div(ctx, info->div_clks, info->nr_div_clks);
  92. samsung_clk_register_gate(ctx, info->gate_clks, info->nr_gate_clks);
  93. ctx->dev = NULL;
  94. pm_runtime_put_sync(dev);
  95. return 0;
  96. }
  97. static const struct dev_pm_ops exynos5_subcmu_pm_ops = {
  98. SET_RUNTIME_PM_OPS(exynos5_subcmu_suspend,
  99. exynos5_subcmu_resume, NULL)
  100. SET_LATE_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  101. pm_runtime_force_resume)
  102. };
  103. static struct platform_driver exynos5_subcmu_driver __refdata = {
  104. .driver = {
  105. .name = "exynos5-subcmu",
  106. .suppress_bind_attrs = true,
  107. .pm = &exynos5_subcmu_pm_ops,
  108. },
  109. .probe = exynos5_subcmu_probe,
  110. };
  111. static int __init exynos5_clk_register_subcmu(struct device *parent,
  112. const struct exynos5_subcmu_info *info,
  113. struct device_node *pd_node)
  114. {
  115. struct of_phandle_args genpdspec = { .np = pd_node };
  116. struct platform_device *pdev;
  117. int ret;
  118. pdev = platform_device_alloc("exynos5-subcmu", PLATFORM_DEVID_AUTO);
  119. if (!pdev)
  120. return -ENOMEM;
  121. pdev->dev.parent = parent;
  122. platform_set_drvdata(pdev, (void *)info);
  123. of_genpd_add_device(&genpdspec, &pdev->dev);
  124. ret = platform_device_add(pdev);
  125. if (ret)
  126. platform_device_put(pdev);
  127. return ret;
  128. }
  129. static int __init exynos5_clk_probe(struct platform_device *pdev)
  130. {
  131. struct device_node *np;
  132. const char *name;
  133. int i;
  134. for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") {
  135. if (of_property_read_string(np, "label", &name) < 0)
  136. continue;
  137. for (i = 0; i < nr_cmus; i++)
  138. if (strcmp(cmu[i].pd_name, name) == 0)
  139. exynos5_clk_register_subcmu(&pdev->dev,
  140. &cmu[i], np);
  141. }
  142. return 0;
  143. }
  144. static const struct of_device_id exynos5_clk_of_match[] = {
  145. { .compatible = "samsung,exynos5250-clock", },
  146. { .compatible = "samsung,exynos5420-clock", },
  147. { .compatible = "samsung,exynos5800-clock", },
  148. { },
  149. };
  150. static struct platform_driver exynos5_clk_driver __refdata = {
  151. .driver = {
  152. .name = "exynos5-clock",
  153. .of_match_table = exynos5_clk_of_match,
  154. .suppress_bind_attrs = true,
  155. },
  156. .probe = exynos5_clk_probe,
  157. };
  158. static int __init exynos5_clk_drv_init(void)
  159. {
  160. platform_driver_register(&exynos5_clk_driver);
  161. platform_driver_register(&exynos5_subcmu_driver);
  162. return 0;
  163. }
  164. core_initcall(exynos5_clk_drv_init);