pm_domains.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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/pm_runtime.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. struct generic_pm_domain pd;
  29. u32 local_pwr_cfg;
  30. };
  31. static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on)
  32. {
  33. struct exynos_pm_domain *pd;
  34. void __iomem *base;
  35. u32 timeout, pwr;
  36. char *op;
  37. pd = container_of(domain, struct exynos_pm_domain, pd);
  38. base = pd->base;
  39. pwr = power_on ? pd->local_pwr_cfg : 0;
  40. writel_relaxed(pwr, base);
  41. /* Wait max 1ms */
  42. timeout = 10;
  43. while ((readl_relaxed(base + 0x4) & pd->local_pwr_cfg) != pwr) {
  44. if (!timeout) {
  45. op = (power_on) ? "enable" : "disable";
  46. pr_err("Power domain %s %s failed\n", domain->name, op);
  47. return -ETIMEDOUT;
  48. }
  49. timeout--;
  50. cpu_relax();
  51. usleep_range(80, 100);
  52. }
  53. return 0;
  54. }
  55. static int exynos_pd_power_on(struct generic_pm_domain *domain)
  56. {
  57. return exynos_pd_power(domain, true);
  58. }
  59. static int exynos_pd_power_off(struct generic_pm_domain *domain)
  60. {
  61. return exynos_pd_power(domain, false);
  62. }
  63. static const struct exynos_pm_domain_config exynos4210_cfg = {
  64. .local_pwr_cfg = 0x7,
  65. };
  66. static const struct exynos_pm_domain_config exynos5433_cfg = {
  67. .local_pwr_cfg = 0xf,
  68. };
  69. static const struct of_device_id exynos_pm_domain_of_match[] = {
  70. {
  71. .compatible = "samsung,exynos4210-pd",
  72. .data = &exynos4210_cfg,
  73. }, {
  74. .compatible = "samsung,exynos5433-pd",
  75. .data = &exynos5433_cfg,
  76. },
  77. { },
  78. };
  79. static const char *exynos_get_domain_name(struct device_node *node)
  80. {
  81. const char *name;
  82. if (of_property_read_string(node, "label", &name) < 0)
  83. name = kbasename(node->full_name);
  84. return kstrdup_const(name, GFP_KERNEL);
  85. }
  86. static int exynos_pd_probe(struct platform_device *pdev)
  87. {
  88. const struct exynos_pm_domain_config *pm_domain_cfg;
  89. struct device *dev = &pdev->dev;
  90. struct device_node *np = dev->of_node;
  91. struct of_phandle_args child, parent;
  92. struct exynos_pm_domain *pd;
  93. int on, ret;
  94. pm_domain_cfg = of_device_get_match_data(dev);
  95. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  96. if (!pd)
  97. return -ENOMEM;
  98. pd->pd.name = exynos_get_domain_name(np);
  99. if (!pd->pd.name)
  100. return -ENOMEM;
  101. pd->base = of_iomap(np, 0);
  102. if (!pd->base) {
  103. kfree_const(pd->pd.name);
  104. return -ENODEV;
  105. }
  106. pd->pd.power_off = exynos_pd_power_off;
  107. pd->pd.power_on = exynos_pd_power_on;
  108. pd->local_pwr_cfg = pm_domain_cfg->local_pwr_cfg;
  109. on = readl_relaxed(pd->base + 0x4) & pd->local_pwr_cfg;
  110. pm_genpd_init(&pd->pd, NULL, !on);
  111. ret = of_genpd_add_provider_simple(np, &pd->pd);
  112. if (ret == 0 && of_parse_phandle_with_args(np, "power-domains",
  113. "#power-domain-cells", 0, &parent) == 0) {
  114. child.np = np;
  115. child.args_count = 0;
  116. if (of_genpd_add_subdomain(&parent, &child))
  117. pr_warn("%pOF failed to add subdomain: %pOF\n",
  118. parent.np, child.np);
  119. else
  120. pr_info("%pOF has as child subdomain: %pOF.\n",
  121. parent.np, child.np);
  122. }
  123. pm_runtime_enable(dev);
  124. return ret;
  125. }
  126. static struct platform_driver exynos_pd_driver = {
  127. .probe = exynos_pd_probe,
  128. .driver = {
  129. .name = "exynos-pd",
  130. .of_match_table = exynos_pm_domain_of_match,
  131. .suppress_bind_attrs = true,
  132. }
  133. };
  134. static __init int exynos4_pm_init_power_domain(void)
  135. {
  136. return platform_driver_register(&exynos_pd_driver);
  137. }
  138. core_initcall(exynos4_pm_init_power_domain);