pm.c 6.9 KB

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