pm_domains.c 6.8 KB

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