pm_domains.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Exynos Generic power domain support.
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com
  6. *
  7. * Implementation of Exynos specific power domain control which is used in
  8. * conjunction with runtime-pm. Support for both device-tree and non-device-tree
  9. * based power domain support is included.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/io.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/pm_domain.h>
  19. #include <linux/clk.h>
  20. #include <linux/delay.h>
  21. #include <linux/of_address.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/sched.h>
  24. #define INT_LOCAL_PWR_EN 0x7
  25. #define MAX_CLK_PER_DOMAIN 4
  26. /*
  27. * Exynos specific wrapper around the generic power domain
  28. */
  29. struct exynos_pm_domain {
  30. void __iomem *base;
  31. char const *name;
  32. bool is_off;
  33. struct generic_pm_domain pd;
  34. struct clk *oscclk;
  35. struct clk *clk[MAX_CLK_PER_DOMAIN];
  36. struct clk *pclk[MAX_CLK_PER_DOMAIN];
  37. };
  38. static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on)
  39. {
  40. struct exynos_pm_domain *pd;
  41. void __iomem *base;
  42. u32 timeout, pwr;
  43. char *op;
  44. pd = container_of(domain, struct exynos_pm_domain, pd);
  45. base = pd->base;
  46. /* Set oscclk before powering off a domain*/
  47. if (!power_on) {
  48. int i;
  49. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  50. if (IS_ERR(pd->clk[i]))
  51. break;
  52. if (clk_set_parent(pd->clk[i], pd->oscclk))
  53. pr_err("%s: error setting oscclk as parent to clock %d\n",
  54. pd->name, i);
  55. }
  56. }
  57. pwr = power_on ? INT_LOCAL_PWR_EN : 0;
  58. __raw_writel(pwr, base);
  59. /* Wait max 1ms */
  60. timeout = 10;
  61. while ((__raw_readl(base + 0x4) & INT_LOCAL_PWR_EN) != pwr) {
  62. if (!timeout) {
  63. op = (power_on) ? "enable" : "disable";
  64. pr_err("Power domain %s %s failed\n", domain->name, op);
  65. return -ETIMEDOUT;
  66. }
  67. timeout--;
  68. cpu_relax();
  69. usleep_range(80, 100);
  70. }
  71. /* Restore clocks after powering on a domain*/
  72. if (power_on) {
  73. int i;
  74. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  75. if (IS_ERR(pd->clk[i]))
  76. break;
  77. if (clk_set_parent(pd->clk[i], pd->pclk[i]))
  78. pr_err("%s: error setting parent to clock%d\n",
  79. pd->name, i);
  80. }
  81. }
  82. return 0;
  83. }
  84. static int exynos_pd_power_on(struct generic_pm_domain *domain)
  85. {
  86. return exynos_pd_power(domain, true);
  87. }
  88. static int exynos_pd_power_off(struct generic_pm_domain *domain)
  89. {
  90. return exynos_pd_power(domain, false);
  91. }
  92. static __init int exynos4_pm_init_power_domain(void)
  93. {
  94. struct platform_device *pdev;
  95. struct device_node *np;
  96. for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") {
  97. struct exynos_pm_domain *pd;
  98. int on, i;
  99. struct device *dev;
  100. pdev = of_find_device_by_node(np);
  101. dev = &pdev->dev;
  102. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  103. if (!pd) {
  104. pr_err("%s: failed to allocate memory for domain\n",
  105. __func__);
  106. return -ENOMEM;
  107. }
  108. pd->pd.name = kstrdup(np->name, GFP_KERNEL);
  109. pd->name = pd->pd.name;
  110. pd->base = of_iomap(np, 0);
  111. pd->pd.power_off = exynos_pd_power_off;
  112. pd->pd.power_on = exynos_pd_power_on;
  113. pd->oscclk = clk_get(dev, "oscclk");
  114. if (IS_ERR(pd->oscclk))
  115. goto no_clk;
  116. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  117. char clk_name[8];
  118. snprintf(clk_name, sizeof(clk_name), "clk%d", i);
  119. pd->clk[i] = clk_get(dev, clk_name);
  120. if (IS_ERR(pd->clk[i]))
  121. break;
  122. snprintf(clk_name, sizeof(clk_name), "pclk%d", i);
  123. pd->pclk[i] = clk_get(dev, clk_name);
  124. if (IS_ERR(pd->pclk[i])) {
  125. clk_put(pd->clk[i]);
  126. pd->clk[i] = ERR_PTR(-EINVAL);
  127. break;
  128. }
  129. }
  130. if (IS_ERR(pd->clk[0]))
  131. clk_put(pd->oscclk);
  132. no_clk:
  133. on = __raw_readl(pd->base + 0x4) & INT_LOCAL_PWR_EN;
  134. pm_genpd_init(&pd->pd, NULL, !on);
  135. of_genpd_add_provider_simple(np, &pd->pd);
  136. }
  137. return 0;
  138. }
  139. arch_initcall(exynos4_pm_init_power_domain);