pm_domains.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #define EXYNOS_GPD(PD, BASE, NAME) \
  67. static struct exynos_pm_domain PD = { \
  68. .base = (void __iomem *)BASE, \
  69. .name = NAME, \
  70. .pd = { \
  71. .power_off = exynos_pd_power_off, \
  72. .power_on = exynos_pd_power_on, \
  73. }, \
  74. }
  75. #ifdef CONFIG_OF
  76. static void exynos_add_device_to_domain(struct exynos_pm_domain *pd,
  77. struct device *dev)
  78. {
  79. int ret;
  80. dev_dbg(dev, "adding to power domain %s\n", pd->pd.name);
  81. while (1) {
  82. ret = pm_genpd_add_device(&pd->pd, dev);
  83. if (ret != -EAGAIN)
  84. break;
  85. cond_resched();
  86. }
  87. pm_genpd_dev_need_restore(dev, true);
  88. }
  89. static void exynos_remove_device_from_domain(struct device *dev)
  90. {
  91. struct generic_pm_domain *genpd = dev_to_genpd(dev);
  92. int ret;
  93. dev_dbg(dev, "removing from power domain %s\n", genpd->name);
  94. while (1) {
  95. ret = pm_genpd_remove_device(genpd, dev);
  96. if (ret != -EAGAIN)
  97. break;
  98. cond_resched();
  99. }
  100. }
  101. static void exynos_read_domain_from_dt(struct device *dev)
  102. {
  103. struct platform_device *pd_pdev;
  104. struct exynos_pm_domain *pd;
  105. struct device_node *node;
  106. node = of_parse_phandle(dev->of_node, "samsung,power-domain", 0);
  107. if (!node)
  108. return;
  109. pd_pdev = of_find_device_by_node(node);
  110. if (!pd_pdev)
  111. return;
  112. pd = platform_get_drvdata(pd_pdev);
  113. exynos_add_device_to_domain(pd, dev);
  114. }
  115. static int exynos_pm_notifier_call(struct notifier_block *nb,
  116. unsigned long event, void *data)
  117. {
  118. struct device *dev = data;
  119. switch (event) {
  120. case BUS_NOTIFY_BIND_DRIVER:
  121. if (dev->of_node)
  122. exynos_read_domain_from_dt(dev);
  123. break;
  124. case BUS_NOTIFY_UNBOUND_DRIVER:
  125. exynos_remove_device_from_domain(dev);
  126. break;
  127. }
  128. return NOTIFY_DONE;
  129. }
  130. static struct notifier_block platform_nb = {
  131. .notifier_call = exynos_pm_notifier_call,
  132. };
  133. static __init int exynos_pm_dt_parse_domains(void)
  134. {
  135. struct platform_device *pdev;
  136. struct device_node *np;
  137. for_each_compatible_node(np, NULL, "samsung,exynos4210-pd") {
  138. struct exynos_pm_domain *pd;
  139. int on;
  140. pdev = of_find_device_by_node(np);
  141. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  142. if (!pd) {
  143. pr_err("%s: failed to allocate memory for domain\n",
  144. __func__);
  145. return -ENOMEM;
  146. }
  147. pd->pd.name = kstrdup(np->name, GFP_KERNEL);
  148. pd->name = pd->pd.name;
  149. pd->base = of_iomap(np, 0);
  150. pd->pd.power_off = exynos_pd_power_off;
  151. pd->pd.power_on = exynos_pd_power_on;
  152. pd->pd.of_node = np;
  153. platform_set_drvdata(pdev, pd);
  154. on = __raw_readl(pd->base + 0x4) & S5P_INT_LOCAL_PWR_EN;
  155. pm_genpd_init(&pd->pd, NULL, !on);
  156. }
  157. bus_register_notifier(&platform_bus_type, &platform_nb);
  158. return 0;
  159. }
  160. #else
  161. static __init int exynos_pm_dt_parse_domains(void)
  162. {
  163. return 0;
  164. }
  165. #endif /* CONFIG_OF */
  166. static __init __maybe_unused void exynos_pm_add_dev_to_genpd(struct platform_device *pdev,
  167. struct exynos_pm_domain *pd)
  168. {
  169. if (pdev->dev.bus) {
  170. if (!pm_genpd_add_device(&pd->pd, &pdev->dev))
  171. pm_genpd_dev_need_restore(&pdev->dev, true);
  172. else
  173. pr_info("%s: error in adding %s device to %s power"
  174. "domain\n", __func__, dev_name(&pdev->dev),
  175. pd->name);
  176. }
  177. }
  178. EXYNOS_GPD(exynos4_pd_mfc, S5P_PMU_MFC_CONF, "pd-mfc");
  179. EXYNOS_GPD(exynos4_pd_g3d, S5P_PMU_G3D_CONF, "pd-g3d");
  180. EXYNOS_GPD(exynos4_pd_lcd0, S5P_PMU_LCD0_CONF, "pd-lcd0");
  181. EXYNOS_GPD(exynos4_pd_lcd1, S5P_PMU_LCD1_CONF, "pd-lcd1");
  182. EXYNOS_GPD(exynos4_pd_tv, S5P_PMU_TV_CONF, "pd-tv");
  183. EXYNOS_GPD(exynos4_pd_cam, S5P_PMU_CAM_CONF, "pd-cam");
  184. EXYNOS_GPD(exynos4_pd_gps, S5P_PMU_GPS_CONF, "pd-gps");
  185. static struct exynos_pm_domain *exynos4_pm_domains[] = {
  186. &exynos4_pd_mfc,
  187. &exynos4_pd_g3d,
  188. &exynos4_pd_lcd0,
  189. &exynos4_pd_lcd1,
  190. &exynos4_pd_tv,
  191. &exynos4_pd_cam,
  192. &exynos4_pd_gps,
  193. };
  194. static __init int exynos4_pm_init_power_domain(void)
  195. {
  196. int idx;
  197. if (of_have_populated_dt())
  198. return exynos_pm_dt_parse_domains();
  199. for (idx = 0; idx < ARRAY_SIZE(exynos4_pm_domains); idx++) {
  200. struct exynos_pm_domain *pd = exynos4_pm_domains[idx];
  201. int on = __raw_readl(pd->base + 0x4) & S5P_INT_LOCAL_PWR_EN;
  202. pm_genpd_init(&pd->pd, NULL, !on);
  203. }
  204. #ifdef CONFIG_S5P_DEV_FIMD0
  205. exynos_pm_add_dev_to_genpd(&s5p_device_fimd0, &exynos4_pd_lcd0);
  206. #endif
  207. #ifdef CONFIG_S5P_DEV_TV
  208. exynos_pm_add_dev_to_genpd(&s5p_device_hdmi, &exynos4_pd_tv);
  209. exynos_pm_add_dev_to_genpd(&s5p_device_mixer, &exynos4_pd_tv);
  210. #endif
  211. #ifdef CONFIG_S5P_DEV_MFC
  212. exynos_pm_add_dev_to_genpd(&s5p_device_mfc, &exynos4_pd_mfc);
  213. #endif
  214. #ifdef CONFIG_S5P_DEV_FIMC0
  215. exynos_pm_add_dev_to_genpd(&s5p_device_fimc0, &exynos4_pd_cam);
  216. #endif
  217. #ifdef CONFIG_S5P_DEV_FIMC1
  218. exynos_pm_add_dev_to_genpd(&s5p_device_fimc1, &exynos4_pd_cam);
  219. #endif
  220. #ifdef CONFIG_S5P_DEV_FIMC2
  221. exynos_pm_add_dev_to_genpd(&s5p_device_fimc2, &exynos4_pd_cam);
  222. #endif
  223. #ifdef CONFIG_S5P_DEV_FIMC3
  224. exynos_pm_add_dev_to_genpd(&s5p_device_fimc3, &exynos4_pd_cam);
  225. #endif
  226. #ifdef CONFIG_S5P_DEV_CSIS0
  227. exynos_pm_add_dev_to_genpd(&s5p_device_mipi_csis0, &exynos4_pd_cam);
  228. #endif
  229. #ifdef CONFIG_S5P_DEV_CSIS1
  230. exynos_pm_add_dev_to_genpd(&s5p_device_mipi_csis1, &exynos4_pd_cam);
  231. #endif
  232. #ifdef CONFIG_S5P_DEV_G2D
  233. exynos_pm_add_dev_to_genpd(&s5p_device_g2d, &exynos4_pd_lcd0);
  234. #endif
  235. #ifdef CONFIG_S5P_DEV_JPEG
  236. exynos_pm_add_dev_to_genpd(&s5p_device_jpeg, &exynos4_pd_cam);
  237. #endif
  238. return 0;
  239. }
  240. arch_initcall(exynos4_pm_init_power_domain);
  241. int __init exynos_pm_late_initcall(void)
  242. {
  243. pm_genpd_poweroff_unused();
  244. return 0;
  245. }