pm_domains.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. pd->pclk[i] = clk_get_parent(pd->clk[i]);
  59. if (clk_set_parent(pd->clk[i], pd->oscclk))
  60. pr_err("%s: error setting oscclk as parent to clock %d\n",
  61. pd->name, i);
  62. }
  63. }
  64. pwr = power_on ? INT_LOCAL_PWR_EN : 0;
  65. __raw_writel(pwr, base);
  66. /* Wait max 1ms */
  67. timeout = 10;
  68. while ((__raw_readl(base + 0x4) & INT_LOCAL_PWR_EN) != pwr) {
  69. if (!timeout) {
  70. op = (power_on) ? "enable" : "disable";
  71. pr_err("Power domain %s %s failed\n", domain->name, op);
  72. return -ETIMEDOUT;
  73. }
  74. timeout--;
  75. cpu_relax();
  76. usleep_range(80, 100);
  77. }
  78. /* Restore clocks after powering on a domain*/
  79. if (power_on) {
  80. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  81. if (IS_ERR(pd->clk[i]))
  82. break;
  83. if (IS_ERR(pd->clk[i]))
  84. continue; /* Skip on first power up */
  85. if (clk_set_parent(pd->clk[i], pd->pclk[i]))
  86. pr_err("%s: error setting parent to clock%d\n",
  87. pd->name, i);
  88. }
  89. }
  90. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  91. if (IS_ERR(pd->asb_clk[i]))
  92. break;
  93. clk_disable_unprepare(pd->asb_clk[i]);
  94. }
  95. return 0;
  96. }
  97. static int exynos_pd_power_on(struct generic_pm_domain *domain)
  98. {
  99. return exynos_pd_power(domain, true);
  100. }
  101. static int exynos_pd_power_off(struct generic_pm_domain *domain)
  102. {
  103. return exynos_pd_power(domain, false);
  104. }
  105. static __init int exynos4_pm_init_power_domain(void)
  106. {
  107. struct device_node *np;
  108. for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") {
  109. struct exynos_pm_domain *pd;
  110. int on, i;
  111. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  112. if (!pd) {
  113. pr_err("%s: failed to allocate memory for domain\n",
  114. __func__);
  115. of_node_put(np);
  116. return -ENOMEM;
  117. }
  118. pd->pd.name = kstrdup_const(strrchr(np->full_name, '/') + 1,
  119. GFP_KERNEL);
  120. if (!pd->pd.name) {
  121. kfree(pd);
  122. of_node_put(np);
  123. return -ENOMEM;
  124. }
  125. pd->name = pd->pd.name;
  126. pd->base = of_iomap(np, 0);
  127. if (!pd->base) {
  128. pr_warn("%s: failed to map memory\n", __func__);
  129. kfree_const(pd->pd.name);
  130. kfree(pd);
  131. continue;
  132. }
  133. pd->pd.power_off = exynos_pd_power_off;
  134. pd->pd.power_on = exynos_pd_power_on;
  135. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  136. char clk_name[8];
  137. snprintf(clk_name, sizeof(clk_name), "asb%d", i);
  138. pd->asb_clk[i] = of_clk_get_by_name(np, clk_name);
  139. if (IS_ERR(pd->asb_clk[i]))
  140. break;
  141. }
  142. pd->oscclk = of_clk_get_by_name(np, "oscclk");
  143. if (IS_ERR(pd->oscclk))
  144. goto no_clk;
  145. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  146. char clk_name[8];
  147. snprintf(clk_name, sizeof(clk_name), "clk%d", i);
  148. pd->clk[i] = of_clk_get_by_name(np, clk_name);
  149. if (IS_ERR(pd->clk[i]))
  150. break;
  151. /*
  152. * Skip setting parent on first power up.
  153. * The parent at this time may not be useful at all.
  154. */
  155. pd->pclk[i] = ERR_PTR(-EINVAL);
  156. }
  157. if (IS_ERR(pd->clk[0]))
  158. clk_put(pd->oscclk);
  159. no_clk:
  160. on = __raw_readl(pd->base + 0x4) & INT_LOCAL_PWR_EN;
  161. pm_genpd_init(&pd->pd, NULL, !on);
  162. of_genpd_add_provider_simple(np, &pd->pd);
  163. }
  164. /* Assign the child power domains to their parents */
  165. for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") {
  166. struct generic_pm_domain *child_domain, *parent_domain;
  167. struct of_phandle_args args;
  168. args.np = np;
  169. args.args_count = 0;
  170. child_domain = of_genpd_get_from_provider(&args);
  171. if (IS_ERR(child_domain))
  172. continue;
  173. if (of_parse_phandle_with_args(np, "power-domains",
  174. "#power-domain-cells", 0, &args) != 0)
  175. continue;
  176. parent_domain = of_genpd_get_from_provider(&args);
  177. if (IS_ERR(parent_domain))
  178. continue;
  179. if (pm_genpd_add_subdomain(parent_domain, child_domain))
  180. pr_warn("%s failed to add subdomain: %s\n",
  181. parent_domain->name, child_domain->name);
  182. else
  183. pr_info("%s has as child subdomain: %s.\n",
  184. parent_domain->name, child_domain->name);
  185. }
  186. return 0;
  187. }
  188. core_initcall(exynos4_pm_init_power_domain);