pm_domains.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. struct clk *asb_clk[MAX_CLK_PER_DOMAIN];
  38. };
  39. static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on)
  40. {
  41. struct exynos_pm_domain *pd;
  42. void __iomem *base;
  43. u32 timeout, pwr;
  44. char *op;
  45. int i;
  46. pd = container_of(domain, struct exynos_pm_domain, pd);
  47. base = pd->base;
  48. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  49. if (IS_ERR(pd->asb_clk[i]))
  50. break;
  51. clk_prepare_enable(pd->asb_clk[i]);
  52. }
  53. /* Set oscclk before powering off a domain*/
  54. if (!power_on) {
  55. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  56. if (IS_ERR(pd->clk[i]))
  57. break;
  58. if (clk_set_parent(pd->clk[i], pd->oscclk))
  59. pr_err("%s: error setting oscclk as parent to clock %d\n",
  60. pd->name, i);
  61. }
  62. }
  63. pwr = power_on ? INT_LOCAL_PWR_EN : 0;
  64. __raw_writel(pwr, base);
  65. /* Wait max 1ms */
  66. timeout = 10;
  67. while ((__raw_readl(base + 0x4) & INT_LOCAL_PWR_EN) != pwr) {
  68. if (!timeout) {
  69. op = (power_on) ? "enable" : "disable";
  70. pr_err("Power domain %s %s failed\n", domain->name, op);
  71. return -ETIMEDOUT;
  72. }
  73. timeout--;
  74. cpu_relax();
  75. usleep_range(80, 100);
  76. }
  77. /* Restore clocks after powering on a domain*/
  78. if (power_on) {
  79. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  80. if (IS_ERR(pd->clk[i]))
  81. break;
  82. if (clk_set_parent(pd->clk[i], pd->pclk[i]))
  83. pr_err("%s: error setting parent to clock%d\n",
  84. pd->name, i);
  85. }
  86. }
  87. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  88. if (IS_ERR(pd->asb_clk[i]))
  89. break;
  90. clk_disable_unprepare(pd->asb_clk[i]);
  91. }
  92. return 0;
  93. }
  94. static int exynos_pd_power_on(struct generic_pm_domain *domain)
  95. {
  96. return exynos_pd_power(domain, true);
  97. }
  98. static int exynos_pd_power_off(struct generic_pm_domain *domain)
  99. {
  100. return exynos_pd_power(domain, false);
  101. }
  102. static __init int exynos4_pm_init_power_domain(void)
  103. {
  104. struct platform_device *pdev;
  105. struct device_node *np;
  106. for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") {
  107. struct exynos_pm_domain *pd;
  108. int on, i;
  109. struct device *dev;
  110. pdev = of_find_device_by_node(np);
  111. dev = &pdev->dev;
  112. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  113. if (!pd) {
  114. pr_err("%s: failed to allocate memory for domain\n",
  115. __func__);
  116. return -ENOMEM;
  117. }
  118. pd->pd.name = kstrdup(dev_name(dev), GFP_KERNEL);
  119. pd->name = pd->pd.name;
  120. pd->base = of_iomap(np, 0);
  121. pd->pd.power_off = exynos_pd_power_off;
  122. pd->pd.power_on = exynos_pd_power_on;
  123. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  124. char clk_name[8];
  125. snprintf(clk_name, sizeof(clk_name), "asb%d", i);
  126. pd->asb_clk[i] = clk_get(dev, clk_name);
  127. if (IS_ERR(pd->asb_clk[i]))
  128. break;
  129. }
  130. pd->oscclk = clk_get(dev, "oscclk");
  131. if (IS_ERR(pd->oscclk))
  132. goto no_clk;
  133. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  134. char clk_name[8];
  135. snprintf(clk_name, sizeof(clk_name), "clk%d", i);
  136. pd->clk[i] = clk_get(dev, clk_name);
  137. if (IS_ERR(pd->clk[i]))
  138. break;
  139. snprintf(clk_name, sizeof(clk_name), "pclk%d", i);
  140. pd->pclk[i] = clk_get(dev, clk_name);
  141. if (IS_ERR(pd->pclk[i])) {
  142. clk_put(pd->clk[i]);
  143. pd->clk[i] = ERR_PTR(-EINVAL);
  144. break;
  145. }
  146. }
  147. if (IS_ERR(pd->clk[0]))
  148. clk_put(pd->oscclk);
  149. no_clk:
  150. on = __raw_readl(pd->base + 0x4) & INT_LOCAL_PWR_EN;
  151. pm_genpd_init(&pd->pd, NULL, !on);
  152. of_genpd_add_provider_simple(np, &pd->pd);
  153. }
  154. /* Assign the child power domains to their parents */
  155. for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") {
  156. struct generic_pm_domain *child_domain, *parent_domain;
  157. struct of_phandle_args args;
  158. args.np = np;
  159. args.args_count = 0;
  160. child_domain = of_genpd_get_from_provider(&args);
  161. if (IS_ERR(child_domain))
  162. continue;
  163. if (of_parse_phandle_with_args(np, "power-domains",
  164. "#power-domain-cells", 0, &args) != 0)
  165. continue;
  166. parent_domain = of_genpd_get_from_provider(&args);
  167. if (IS_ERR(parent_domain))
  168. continue;
  169. if (pm_genpd_add_subdomain(parent_domain, child_domain))
  170. pr_warn("%s failed to add subdomain: %s\n",
  171. parent_domain->name, child_domain->name);
  172. else
  173. pr_info("%s has as child subdomain: %s.\n",
  174. parent_domain->name, child_domain->name);
  175. of_node_put(np);
  176. }
  177. return 0;
  178. }
  179. arch_initcall(exynos4_pm_init_power_domain);