pm.c 7.0 KB

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