pm.c 7.8 KB

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