pm.c 7.2 KB

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