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