pm_domains.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Exynos Generic power domain support.
  4. //
  5. // Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. // http://www.samsung.com
  7. //
  8. // Implementation of Exynos specific power domain control which is used in
  9. // conjunction with runtime-pm. Support for both device-tree and non-device-tree
  10. // based power domain support is included.
  11. #include <linux/io.h>
  12. #include <linux/err.h>
  13. #include <linux/slab.h>
  14. #include <linux/pm_domain.h>
  15. #include <linux/delay.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/sched.h>
  19. struct exynos_pm_domain_config {
  20. /* Value for LOCAL_PWR_CFG and STATUS fields for each domain */
  21. u32 local_pwr_cfg;
  22. };
  23. /*
  24. * Exynos specific wrapper around the generic power domain
  25. */
  26. struct exynos_pm_domain {
  27. void __iomem *base;
  28. bool is_off;
  29. struct generic_pm_domain pd;
  30. u32 local_pwr_cfg;
  31. };
  32. static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on)
  33. {
  34. struct exynos_pm_domain *pd;
  35. void __iomem *base;
  36. u32 timeout, pwr;
  37. char *op;
  38. pd = container_of(domain, struct exynos_pm_domain, pd);
  39. base = pd->base;
  40. pwr = power_on ? pd->local_pwr_cfg : 0;
  41. writel_relaxed(pwr, base);
  42. /* Wait max 1ms */
  43. timeout = 10;
  44. while ((readl_relaxed(base + 0x4) & pd->local_pwr_cfg) != pwr) {
  45. if (!timeout) {
  46. op = (power_on) ? "enable" : "disable";
  47. pr_err("Power domain %s %s failed\n", domain->name, op);
  48. return -ETIMEDOUT;
  49. }
  50. timeout--;
  51. cpu_relax();
  52. usleep_range(80, 100);
  53. }
  54. return 0;
  55. }
  56. static int exynos_pd_power_on(struct generic_pm_domain *domain)
  57. {
  58. return exynos_pd_power(domain, true);
  59. }
  60. static int exynos_pd_power_off(struct generic_pm_domain *domain)
  61. {
  62. return exynos_pd_power(domain, false);
  63. }
  64. static const struct exynos_pm_domain_config exynos4210_cfg __initconst = {
  65. .local_pwr_cfg = 0x7,
  66. };
  67. static const struct exynos_pm_domain_config exynos5433_cfg __initconst = {
  68. .local_pwr_cfg = 0xf,
  69. };
  70. static const struct of_device_id exynos_pm_domain_of_match[] __initconst = {
  71. {
  72. .compatible = "samsung,exynos4210-pd",
  73. .data = &exynos4210_cfg,
  74. }, {
  75. .compatible = "samsung,exynos5433-pd",
  76. .data = &exynos5433_cfg,
  77. },
  78. { },
  79. };
  80. static __init const char *exynos_get_domain_name(struct device_node *node)
  81. {
  82. const char *name;
  83. if (of_property_read_string(node, "label", &name) < 0)
  84. name = kbasename(node->full_name);
  85. return kstrdup_const(name, GFP_KERNEL);
  86. }
  87. static __init int exynos4_pm_init_power_domain(void)
  88. {
  89. struct device_node *np;
  90. const struct of_device_id *match;
  91. for_each_matching_node_and_match(np, exynos_pm_domain_of_match, &match) {
  92. const struct exynos_pm_domain_config *pm_domain_cfg;
  93. struct exynos_pm_domain *pd;
  94. int on;
  95. pm_domain_cfg = match->data;
  96. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  97. if (!pd) {
  98. of_node_put(np);
  99. return -ENOMEM;
  100. }
  101. pd->pd.name = exynos_get_domain_name(np);
  102. if (!pd->pd.name) {
  103. kfree(pd);
  104. of_node_put(np);
  105. return -ENOMEM;
  106. }
  107. pd->base = of_iomap(np, 0);
  108. if (!pd->base) {
  109. pr_warn("%s: failed to map memory\n", __func__);
  110. kfree_const(pd->pd.name);
  111. kfree(pd);
  112. continue;
  113. }
  114. pd->pd.power_off = exynos_pd_power_off;
  115. pd->pd.power_on = exynos_pd_power_on;
  116. pd->local_pwr_cfg = pm_domain_cfg->local_pwr_cfg;
  117. on = readl_relaxed(pd->base + 0x4) & pd->local_pwr_cfg;
  118. pm_genpd_init(&pd->pd, NULL, !on);
  119. of_genpd_add_provider_simple(np, &pd->pd);
  120. }
  121. /* Assign the child power domains to their parents */
  122. for_each_matching_node(np, exynos_pm_domain_of_match) {
  123. struct of_phandle_args child, parent;
  124. child.np = np;
  125. child.args_count = 0;
  126. if (of_parse_phandle_with_args(np, "power-domains",
  127. "#power-domain-cells", 0,
  128. &parent) != 0)
  129. continue;
  130. if (of_genpd_add_subdomain(&parent, &child))
  131. pr_warn("%pOF failed to add subdomain: %pOF\n",
  132. parent.np, child.np);
  133. else
  134. pr_info("%pOF has as child subdomain: %pOF.\n",
  135. parent.np, child.np);
  136. }
  137. return 0;
  138. }
  139. core_initcall(exynos4_pm_init_power_domain);