pm.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * pm.c - Common OMAP2+ power management-related code
  3. *
  4. * Copyright (C) 2010 Texas Instruments, Inc.
  5. * Copyright (C) 2010 Nokia Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/err.h>
  15. #include <linux/opp.h>
  16. #include <linux/export.h>
  17. #include <linux/suspend.h>
  18. #include <plat/omap-pm.h>
  19. #include <plat/omap_device.h>
  20. #include "common.h"
  21. #include "prcm-common.h"
  22. #include "voltage.h"
  23. #include "powerdomain.h"
  24. #include "clockdomain.h"
  25. #include "pm.h"
  26. #include "twl-common.h"
  27. static struct omap_device_pm_latency *pm_lats;
  28. /*
  29. * omap_pm_suspend: points to a function that does the SoC-specific
  30. * suspend work
  31. */
  32. int (*omap_pm_suspend)(void);
  33. static int __init _init_omap_device(char *name)
  34. {
  35. struct omap_hwmod *oh;
  36. struct platform_device *pdev;
  37. oh = omap_hwmod_lookup(name);
  38. if (WARN(!oh, "%s: could not find omap_hwmod for %s\n",
  39. __func__, name))
  40. return -ENODEV;
  41. pdev = omap_device_build(oh->name, 0, oh, NULL, 0, pm_lats, 0, false);
  42. if (WARN(IS_ERR(pdev), "%s: could not build omap_device for %s\n",
  43. __func__, name))
  44. return -ENODEV;
  45. return 0;
  46. }
  47. /*
  48. * Build omap_devices for processors and bus.
  49. */
  50. static void __init omap2_init_processor_devices(void)
  51. {
  52. _init_omap_device("mpu");
  53. if (omap3_has_iva())
  54. _init_omap_device("iva");
  55. if (cpu_is_omap44xx()) {
  56. _init_omap_device("l3_main_1");
  57. _init_omap_device("dsp");
  58. _init_omap_device("iva");
  59. } else {
  60. _init_omap_device("l3_main");
  61. }
  62. }
  63. /* Types of sleep_switch used in omap_set_pwrdm_state */
  64. #define FORCEWAKEUP_SWITCH 0
  65. #define LOWPOWERSTATE_SWITCH 1
  66. int __init omap_pm_clkdms_setup(struct clockdomain *clkdm, void *unused)
  67. {
  68. if (clkdm->flags & CLKDM_CAN_ENABLE_AUTO)
  69. clkdm_allow_idle(clkdm);
  70. else if (clkdm->flags & CLKDM_CAN_FORCE_SLEEP &&
  71. atomic_read(&clkdm->usecount) == 0)
  72. clkdm_sleep(clkdm);
  73. return 0;
  74. }
  75. /*
  76. * This sets pwrdm state (other than mpu & core. Currently only ON &
  77. * RET are supported.
  78. */
  79. int omap_set_pwrdm_state(struct powerdomain *pwrdm, u32 pwrst)
  80. {
  81. u8 curr_pwrst, next_pwrst;
  82. int sleep_switch = -1, ret = 0, hwsup = 0;
  83. if (!pwrdm || IS_ERR(pwrdm))
  84. return -EINVAL;
  85. while (!(pwrdm->pwrsts & (1 << pwrst))) {
  86. if (pwrst == PWRDM_POWER_OFF)
  87. return ret;
  88. pwrst--;
  89. }
  90. next_pwrst = pwrdm_read_next_pwrst(pwrdm);
  91. if (next_pwrst == pwrst)
  92. return ret;
  93. curr_pwrst = pwrdm_read_pwrst(pwrdm);
  94. if (curr_pwrst < PWRDM_POWER_ON) {
  95. if ((curr_pwrst > pwrst) &&
  96. (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE)) {
  97. sleep_switch = LOWPOWERSTATE_SWITCH;
  98. } else {
  99. hwsup = clkdm_in_hwsup(pwrdm->pwrdm_clkdms[0]);
  100. clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
  101. sleep_switch = FORCEWAKEUP_SWITCH;
  102. }
  103. }
  104. ret = pwrdm_set_next_pwrst(pwrdm, pwrst);
  105. if (ret)
  106. pr_err("%s: unable to set power state of powerdomain: %s\n",
  107. __func__, pwrdm->name);
  108. switch (sleep_switch) {
  109. case FORCEWAKEUP_SWITCH:
  110. if (hwsup)
  111. clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
  112. else
  113. clkdm_sleep(pwrdm->pwrdm_clkdms[0]);
  114. break;
  115. case LOWPOWERSTATE_SWITCH:
  116. pwrdm_set_lowpwrstchange(pwrdm);
  117. pwrdm_wait_transition(pwrdm);
  118. pwrdm_state_switch(pwrdm);
  119. break;
  120. }
  121. return ret;
  122. }
  123. /*
  124. * This API is to be called during init to set the various voltage
  125. * domains to the voltage as per the opp table. Typically we boot up
  126. * at the nominal voltage. So this function finds out the rate of
  127. * the clock associated with the voltage domain, finds out the correct
  128. * opp entry and sets the voltage domain to the voltage specified
  129. * in the opp entry
  130. */
  131. static int __init omap2_set_init_voltage(char *vdd_name, char *clk_name,
  132. const char *oh_name)
  133. {
  134. struct voltagedomain *voltdm;
  135. struct clk *clk;
  136. struct opp *opp;
  137. unsigned long freq, bootup_volt;
  138. struct device *dev;
  139. if (!vdd_name || !clk_name || !oh_name) {
  140. pr_err("%s: invalid parameters\n", __func__);
  141. goto exit;
  142. }
  143. dev = omap_device_get_by_hwmod_name(oh_name);
  144. if (IS_ERR(dev)) {
  145. pr_err("%s: Unable to get dev pointer for hwmod %s\n",
  146. __func__, oh_name);
  147. goto exit;
  148. }
  149. voltdm = voltdm_lookup(vdd_name);
  150. if (IS_ERR(voltdm)) {
  151. pr_err("%s: unable to get vdd pointer for vdd_%s\n",
  152. __func__, vdd_name);
  153. goto exit;
  154. }
  155. clk = clk_get(NULL, clk_name);
  156. if (IS_ERR(clk)) {
  157. pr_err("%s: unable to get clk %s\n", __func__, clk_name);
  158. goto exit;
  159. }
  160. freq = clk->rate;
  161. clk_put(clk);
  162. rcu_read_lock();
  163. opp = opp_find_freq_ceil(dev, &freq);
  164. if (IS_ERR(opp)) {
  165. rcu_read_unlock();
  166. pr_err("%s: unable to find boot up OPP for vdd_%s\n",
  167. __func__, vdd_name);
  168. goto exit;
  169. }
  170. bootup_volt = opp_get_voltage(opp);
  171. rcu_read_unlock();
  172. if (!bootup_volt) {
  173. pr_err("%s: unable to find voltage corresponding "
  174. "to the bootup OPP for vdd_%s\n", __func__, vdd_name);
  175. goto exit;
  176. }
  177. voltdm_scale(voltdm, bootup_volt);
  178. return 0;
  179. exit:
  180. pr_err("%s: unable to set vdd_%s\n", __func__, vdd_name);
  181. return -EINVAL;
  182. }
  183. #ifdef CONFIG_SUSPEND
  184. static int omap_pm_enter(suspend_state_t suspend_state)
  185. {
  186. int ret = 0;
  187. if (!omap_pm_suspend)
  188. return -ENOENT; /* XXX doublecheck */
  189. switch (suspend_state) {
  190. case PM_SUSPEND_STANDBY:
  191. case PM_SUSPEND_MEM:
  192. ret = omap_pm_suspend();
  193. break;
  194. default:
  195. ret = -EINVAL;
  196. }
  197. return ret;
  198. }
  199. static int omap_pm_begin(suspend_state_t state)
  200. {
  201. disable_hlt();
  202. if (cpu_is_omap34xx())
  203. omap_prcm_irq_prepare();
  204. return 0;
  205. }
  206. static void omap_pm_end(void)
  207. {
  208. enable_hlt();
  209. return;
  210. }
  211. static void omap_pm_finish(void)
  212. {
  213. if (cpu_is_omap34xx())
  214. omap_prcm_irq_complete();
  215. }
  216. static const struct platform_suspend_ops omap_pm_ops = {
  217. .begin = omap_pm_begin,
  218. .end = omap_pm_end,
  219. .enter = omap_pm_enter,
  220. .finish = omap_pm_finish,
  221. .valid = suspend_valid_only_mem,
  222. };
  223. #endif /* CONFIG_SUSPEND */
  224. static void __init omap3_init_voltages(void)
  225. {
  226. if (!cpu_is_omap34xx())
  227. return;
  228. omap2_set_init_voltage("mpu_iva", "dpll1_ck", "mpu");
  229. omap2_set_init_voltage("core", "l3_ick", "l3_main");
  230. }
  231. static void __init omap4_init_voltages(void)
  232. {
  233. if (!cpu_is_omap44xx())
  234. return;
  235. omap2_set_init_voltage("mpu", "dpll_mpu_ck", "mpu");
  236. omap2_set_init_voltage("core", "l3_div_ck", "l3_main_1");
  237. omap2_set_init_voltage("iva", "dpll_iva_m5x2_ck", "iva");
  238. }
  239. static int __init omap2_common_pm_init(void)
  240. {
  241. if (!of_have_populated_dt())
  242. omap2_init_processor_devices();
  243. omap_pm_if_init();
  244. return 0;
  245. }
  246. postcore_initcall(omap2_common_pm_init);
  247. static int __init omap2_common_pm_late_init(void)
  248. {
  249. /* Init the voltage layer */
  250. omap_pmic_late_init();
  251. omap_voltage_late_init();
  252. /* Initialize the voltages */
  253. omap3_init_voltages();
  254. omap4_init_voltages();
  255. /* Smartreflex device init */
  256. omap_devinit_smartreflex();
  257. #ifdef CONFIG_SUSPEND
  258. suspend_set_ops(&omap_pm_ops);
  259. #endif
  260. return 0;
  261. }
  262. late_initcall(omap2_common_pm_late_init);