pm_domains.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/delay.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/sched.h>
  23. #include <mach/regs-pmu.h>
  24. #include <plat/devs.h>
  25. /*
  26. * Exynos specific wrapper around the generic power domain
  27. */
  28. struct exynos_pm_domain {
  29. void __iomem *base;
  30. char const *name;
  31. bool is_off;
  32. struct generic_pm_domain pd;
  33. };
  34. static int exynos_pd_power(struct generic_pm_domain *domain, bool power_on)
  35. {
  36. struct exynos_pm_domain *pd;
  37. void __iomem *base;
  38. u32 timeout, pwr;
  39. char *op;
  40. pd = container_of(domain, struct exynos_pm_domain, pd);
  41. base = pd->base;
  42. pwr = power_on ? S5P_INT_LOCAL_PWR_EN : 0;
  43. __raw_writel(pwr, base);
  44. /* Wait max 1ms */
  45. timeout = 10;
  46. while ((__raw_readl(base + 0x4) & S5P_INT_LOCAL_PWR_EN) != pwr) {
  47. if (!timeout) {
  48. op = (power_on) ? "enable" : "disable";
  49. pr_err("Power domain %s %s failed\n", domain->name, op);
  50. return -ETIMEDOUT;
  51. }
  52. timeout--;
  53. cpu_relax();
  54. usleep_range(80, 100);
  55. }
  56. return 0;
  57. }
  58. static int exynos_pd_power_on(struct generic_pm_domain *domain)
  59. {
  60. return exynos_pd_power(domain, true);
  61. }
  62. static int exynos_pd_power_off(struct generic_pm_domain *domain)
  63. {
  64. return exynos_pd_power(domain, false);
  65. }
  66. static void exynos_add_device_to_domain(struct exynos_pm_domain *pd,
  67. struct device *dev)
  68. {
  69. int ret;
  70. dev_dbg(dev, "adding to power domain %s\n", pd->pd.name);
  71. while (1) {
  72. ret = pm_genpd_add_device(&pd->pd, dev);
  73. if (ret != -EAGAIN)
  74. break;
  75. cond_resched();
  76. }
  77. pm_genpd_dev_need_restore(dev, true);
  78. }
  79. static void exynos_remove_device_from_domain(struct device *dev)
  80. {
  81. struct generic_pm_domain *genpd = dev_to_genpd(dev);
  82. int ret;
  83. dev_dbg(dev, "removing from power domain %s\n", genpd->name);
  84. while (1) {
  85. ret = pm_genpd_remove_device(genpd, dev);
  86. if (ret != -EAGAIN)
  87. break;
  88. cond_resched();
  89. }
  90. }
  91. static void exynos_read_domain_from_dt(struct device *dev)
  92. {
  93. struct platform_device *pd_pdev;
  94. struct exynos_pm_domain *pd;
  95. struct device_node *node;
  96. node = of_parse_phandle(dev->of_node, "samsung,power-domain", 0);
  97. if (!node)
  98. return;
  99. pd_pdev = of_find_device_by_node(node);
  100. if (!pd_pdev)
  101. return;
  102. pd = platform_get_drvdata(pd_pdev);
  103. exynos_add_device_to_domain(pd, dev);
  104. }
  105. static int exynos_pm_notifier_call(struct notifier_block *nb,
  106. unsigned long event, void *data)
  107. {
  108. struct device *dev = data;
  109. switch (event) {
  110. case BUS_NOTIFY_BIND_DRIVER:
  111. if (dev->of_node)
  112. exynos_read_domain_from_dt(dev);
  113. break;
  114. case BUS_NOTIFY_UNBOUND_DRIVER:
  115. exynos_remove_device_from_domain(dev);
  116. break;
  117. }
  118. return NOTIFY_DONE;
  119. }
  120. static struct notifier_block platform_nb = {
  121. .notifier_call = exynos_pm_notifier_call,
  122. };
  123. static __init int exynos4_pm_init_power_domain(void)
  124. {
  125. struct platform_device *pdev;
  126. struct device_node *np;
  127. for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") {
  128. struct exynos_pm_domain *pd;
  129. int on;
  130. pdev = of_find_device_by_node(np);
  131. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  132. if (!pd) {
  133. pr_err("%s: failed to allocate memory for domain\n",
  134. __func__);
  135. return -ENOMEM;
  136. }
  137. pd->pd.name = kstrdup(np->name, GFP_KERNEL);
  138. pd->name = pd->pd.name;
  139. pd->base = of_iomap(np, 0);
  140. pd->pd.power_off = exynos_pd_power_off;
  141. pd->pd.power_on = exynos_pd_power_on;
  142. pd->pd.of_node = np;
  143. platform_set_drvdata(pdev, pd);
  144. on = __raw_readl(pd->base + 0x4) & S5P_INT_LOCAL_PWR_EN;
  145. pm_genpd_init(&pd->pd, NULL, !on);
  146. }
  147. bus_register_notifier(&platform_bus_type, &platform_nb);
  148. return 0;
  149. }
  150. arch_initcall(exynos4_pm_init_power_domain);
  151. int __init exynos_pm_late_initcall(void)
  152. {
  153. pm_genpd_poweroff_unused();
  154. return 0;
  155. }