pm.c 6.5 KB

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