pm.c 7.8 KB

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