pm_domains.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 MAX_CLK_PER_DOMAIN 4
  25. struct exynos_pm_domain_config {
  26. /* Value for LOCAL_PWR_CFG and STATUS fields for each domain */
  27. u32 local_pwr_cfg;
  28. };
  29. /*
  30. * Exynos specific wrapper around the generic power domain
  31. */
  32. struct exynos_pm_domain {
  33. void __iomem *base;
  34. char const *name;
  35. bool is_off;
  36. struct generic_pm_domain pd;
  37. struct clk *oscclk;
  38. struct clk *clk[MAX_CLK_PER_DOMAIN];
  39. struct clk *pclk[MAX_CLK_PER_DOMAIN];
  40. struct clk *asb_clk[MAX_CLK_PER_DOMAIN];
  41. u32 local_pwr_cfg;
  42. };
  43. static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on)
  44. {
  45. struct exynos_pm_domain *pd;
  46. void __iomem *base;
  47. u32 timeout, pwr;
  48. char *op;
  49. int i;
  50. pd = container_of(domain, struct exynos_pm_domain, pd);
  51. base = pd->base;
  52. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  53. if (IS_ERR(pd->asb_clk[i]))
  54. break;
  55. clk_prepare_enable(pd->asb_clk[i]);
  56. }
  57. /* Set oscclk before powering off a domain*/
  58. if (!power_on) {
  59. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  60. if (IS_ERR(pd->clk[i]))
  61. break;
  62. pd->pclk[i] = clk_get_parent(pd->clk[i]);
  63. if (clk_set_parent(pd->clk[i], pd->oscclk))
  64. pr_err("%s: error setting oscclk as parent to clock %d\n",
  65. pd->name, i);
  66. }
  67. }
  68. pwr = power_on ? pd->local_pwr_cfg : 0;
  69. writel_relaxed(pwr, base);
  70. /* Wait max 1ms */
  71. timeout = 10;
  72. while ((readl_relaxed(base + 0x4) & pd->local_pwr_cfg) != pwr) {
  73. if (!timeout) {
  74. op = (power_on) ? "enable" : "disable";
  75. pr_err("Power domain %s %s failed\n", domain->name, op);
  76. return -ETIMEDOUT;
  77. }
  78. timeout--;
  79. cpu_relax();
  80. usleep_range(80, 100);
  81. }
  82. /* Restore clocks after powering on a domain*/
  83. if (power_on) {
  84. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  85. if (IS_ERR(pd->clk[i]))
  86. break;
  87. if (IS_ERR(pd->pclk[i]))
  88. continue; /* Skip on first power up */
  89. if (clk_set_parent(pd->clk[i], pd->pclk[i]))
  90. pr_err("%s: error setting parent to clock%d\n",
  91. pd->name, i);
  92. }
  93. }
  94. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  95. if (IS_ERR(pd->asb_clk[i]))
  96. break;
  97. clk_disable_unprepare(pd->asb_clk[i]);
  98. }
  99. return 0;
  100. }
  101. static int exynos_pd_power_on(struct generic_pm_domain *domain)
  102. {
  103. return exynos_pd_power(domain, true);
  104. }
  105. static int exynos_pd_power_off(struct generic_pm_domain *domain)
  106. {
  107. return exynos_pd_power(domain, false);
  108. }
  109. static const struct exynos_pm_domain_config exynos4210_cfg __initconst = {
  110. .local_pwr_cfg = 0x7,
  111. };
  112. static const struct of_device_id exynos_pm_domain_of_match[] __initconst = {
  113. {
  114. .compatible = "samsung,exynos4210-pd",
  115. .data = &exynos4210_cfg,
  116. },
  117. { },
  118. };
  119. static __init int exynos4_pm_init_power_domain(void)
  120. {
  121. struct device_node *np;
  122. const struct of_device_id *match;
  123. for_each_matching_node_and_match(np, exynos_pm_domain_of_match, &match) {
  124. const struct exynos_pm_domain_config *pm_domain_cfg;
  125. struct exynos_pm_domain *pd;
  126. int on, i;
  127. pm_domain_cfg = match->data;
  128. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  129. if (!pd) {
  130. pr_err("%s: failed to allocate memory for domain\n",
  131. __func__);
  132. of_node_put(np);
  133. return -ENOMEM;
  134. }
  135. pd->pd.name = kstrdup_const(strrchr(np->full_name, '/') + 1,
  136. GFP_KERNEL);
  137. if (!pd->pd.name) {
  138. kfree(pd);
  139. of_node_put(np);
  140. return -ENOMEM;
  141. }
  142. pd->name = pd->pd.name;
  143. pd->base = of_iomap(np, 0);
  144. if (!pd->base) {
  145. pr_warn("%s: failed to map memory\n", __func__);
  146. kfree_const(pd->pd.name);
  147. kfree(pd);
  148. continue;
  149. }
  150. pd->pd.power_off = exynos_pd_power_off;
  151. pd->pd.power_on = exynos_pd_power_on;
  152. pd->local_pwr_cfg = pm_domain_cfg->local_pwr_cfg;
  153. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  154. char clk_name[8];
  155. snprintf(clk_name, sizeof(clk_name), "asb%d", i);
  156. pd->asb_clk[i] = of_clk_get_by_name(np, clk_name);
  157. if (IS_ERR(pd->asb_clk[i]))
  158. break;
  159. }
  160. pd->oscclk = of_clk_get_by_name(np, "oscclk");
  161. if (IS_ERR(pd->oscclk))
  162. goto no_clk;
  163. for (i = 0; i < MAX_CLK_PER_DOMAIN; i++) {
  164. char clk_name[8];
  165. snprintf(clk_name, sizeof(clk_name), "clk%d", i);
  166. pd->clk[i] = of_clk_get_by_name(np, clk_name);
  167. if (IS_ERR(pd->clk[i]))
  168. break;
  169. /*
  170. * Skip setting parent on first power up.
  171. * The parent at this time may not be useful at all.
  172. */
  173. pd->pclk[i] = ERR_PTR(-EINVAL);
  174. }
  175. if (IS_ERR(pd->clk[0]))
  176. clk_put(pd->oscclk);
  177. no_clk:
  178. on = readl_relaxed(pd->base + 0x4) & pd->local_pwr_cfg;
  179. pm_genpd_init(&pd->pd, NULL, !on);
  180. of_genpd_add_provider_simple(np, &pd->pd);
  181. }
  182. /* Assign the child power domains to their parents */
  183. for_each_matching_node(np, exynos_pm_domain_of_match) {
  184. struct of_phandle_args child, parent;
  185. child.np = np;
  186. child.args_count = 0;
  187. if (of_parse_phandle_with_args(np, "power-domains",
  188. "#power-domain-cells", 0,
  189. &parent) != 0)
  190. continue;
  191. if (of_genpd_add_subdomain(&parent, &child))
  192. pr_warn("%s failed to add subdomain: %s\n",
  193. parent.np->name, child.np->name);
  194. else
  195. pr_info("%s has as child subdomain: %s.\n",
  196. parent.np->name, child.np->name);
  197. }
  198. return 0;
  199. }
  200. core_initcall(exynos4_pm_init_power_domain);