pm.c 7.2 KB

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