pm.c 7.8 KB

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