pm.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <plat/omap-pm.h>
  18. #include <plat/omap_device.h>
  19. #include <plat/common.h>
  20. #include "voltage.h"
  21. #include "powerdomain.h"
  22. #include "clockdomain.h"
  23. #include "pm.h"
  24. static struct omap_device_pm_latency *pm_lats;
  25. static int _init_omap_device(char *name)
  26. {
  27. struct omap_hwmod *oh;
  28. struct platform_device *pdev;
  29. oh = omap_hwmod_lookup(name);
  30. if (WARN(!oh, "%s: could not find omap_hwmod for %s\n",
  31. __func__, name))
  32. return -ENODEV;
  33. pdev = omap_device_build(oh->name, 0, oh, NULL, 0, pm_lats, 0, false);
  34. if (WARN(IS_ERR(pdev), "%s: could not build omap_device for %s\n",
  35. __func__, name))
  36. return -ENODEV;
  37. return 0;
  38. }
  39. /*
  40. * Build omap_devices for processors and bus.
  41. */
  42. static void omap2_init_processor_devices(void)
  43. {
  44. _init_omap_device("mpu");
  45. if (omap3_has_iva())
  46. _init_omap_device("iva");
  47. if (cpu_is_omap44xx()) {
  48. _init_omap_device("l3_main_1");
  49. _init_omap_device("dsp");
  50. _init_omap_device("iva");
  51. } else {
  52. _init_omap_device("l3_main");
  53. }
  54. }
  55. /* Types of sleep_switch used in omap_set_pwrdm_state */
  56. #define FORCEWAKEUP_SWITCH 0
  57. #define LOWPOWERSTATE_SWITCH 1
  58. /*
  59. * This sets pwrdm state (other than mpu & core. Currently only ON &
  60. * RET are supported.
  61. */
  62. int omap_set_pwrdm_state(struct powerdomain *pwrdm, u32 state)
  63. {
  64. u32 cur_state;
  65. int sleep_switch = -1;
  66. int ret = 0;
  67. int hwsup = 0;
  68. if (pwrdm == NULL || IS_ERR(pwrdm))
  69. return -EINVAL;
  70. while (!(pwrdm->pwrsts & (1 << state))) {
  71. if (state == PWRDM_POWER_OFF)
  72. return ret;
  73. state--;
  74. }
  75. cur_state = pwrdm_read_next_pwrst(pwrdm);
  76. if (cur_state == state)
  77. return ret;
  78. if (pwrdm_read_pwrst(pwrdm) < PWRDM_POWER_ON) {
  79. if ((pwrdm_read_pwrst(pwrdm) > state) &&
  80. (pwrdm->flags & PWRDM_HAS_LOWPOWERSTATECHANGE)) {
  81. sleep_switch = LOWPOWERSTATE_SWITCH;
  82. } else {
  83. hwsup = clkdm_in_hwsup(pwrdm->pwrdm_clkdms[0]);
  84. clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
  85. sleep_switch = FORCEWAKEUP_SWITCH;
  86. }
  87. }
  88. ret = pwrdm_set_next_pwrst(pwrdm, state);
  89. if (ret) {
  90. pr_err("%s: unable to set state of powerdomain: %s\n",
  91. __func__, pwrdm->name);
  92. goto err;
  93. }
  94. switch (sleep_switch) {
  95. case FORCEWAKEUP_SWITCH:
  96. if (hwsup)
  97. clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
  98. else
  99. clkdm_sleep(pwrdm->pwrdm_clkdms[0]);
  100. break;
  101. case LOWPOWERSTATE_SWITCH:
  102. pwrdm_set_lowpwrstchange(pwrdm);
  103. break;
  104. default:
  105. return ret;
  106. }
  107. pwrdm_state_switch(pwrdm);
  108. err:
  109. return ret;
  110. }
  111. /*
  112. * This API is to be called during init to set the various voltage
  113. * domains to the voltage as per the opp table. Typically we boot up
  114. * at the nominal voltage. So this function finds out the rate of
  115. * the clock associated with the voltage domain, finds out the correct
  116. * opp entry and sets the voltage domain to the voltage specified
  117. * in the opp entry
  118. */
  119. static int __init omap2_set_init_voltage(char *vdd_name, char *clk_name,
  120. const char *oh_name)
  121. {
  122. struct voltagedomain *voltdm;
  123. struct clk *clk;
  124. struct opp *opp;
  125. unsigned long freq, bootup_volt;
  126. struct device *dev;
  127. if (!vdd_name || !clk_name || !oh_name) {
  128. pr_err("%s: invalid parameters\n", __func__);
  129. goto exit;
  130. }
  131. dev = omap_device_get_by_hwmod_name(oh_name);
  132. if (IS_ERR(dev)) {
  133. pr_err("%s: Unable to get dev pointer for hwmod %s\n",
  134. __func__, oh_name);
  135. goto exit;
  136. }
  137. voltdm = voltdm_lookup(vdd_name);
  138. if (IS_ERR(voltdm)) {
  139. pr_err("%s: unable to get vdd pointer for vdd_%s\n",
  140. __func__, vdd_name);
  141. goto exit;
  142. }
  143. clk = clk_get(NULL, clk_name);
  144. if (IS_ERR(clk)) {
  145. pr_err("%s: unable to get clk %s\n", __func__, clk_name);
  146. goto exit;
  147. }
  148. freq = clk->rate;
  149. clk_put(clk);
  150. opp = opp_find_freq_ceil(dev, &freq);
  151. if (IS_ERR(opp)) {
  152. pr_err("%s: unable to find boot up OPP for vdd_%s\n",
  153. __func__, vdd_name);
  154. goto exit;
  155. }
  156. bootup_volt = opp_get_voltage(opp);
  157. if (!bootup_volt) {
  158. pr_err("%s: unable to find voltage corresponding "
  159. "to the bootup OPP for vdd_%s\n", __func__, vdd_name);
  160. goto exit;
  161. }
  162. voltdm_scale(voltdm, bootup_volt);
  163. return 0;
  164. exit:
  165. pr_err("%s: unable to set vdd_%s\n", __func__, vdd_name);
  166. return -EINVAL;
  167. }
  168. static void __init omap3_init_voltages(void)
  169. {
  170. if (!cpu_is_omap34xx())
  171. return;
  172. omap2_set_init_voltage("mpu_iva", "dpll1_ck", "mpu");
  173. omap2_set_init_voltage("core", "l3_ick", "l3_main");
  174. }
  175. static void __init omap4_init_voltages(void)
  176. {
  177. if (!cpu_is_omap44xx())
  178. return;
  179. omap2_set_init_voltage("mpu", "dpll_mpu_ck", "mpu");
  180. omap2_set_init_voltage("core", "l3_div_ck", "l3_main_1");
  181. omap2_set_init_voltage("iva", "dpll_iva_m5x2_ck", "iva");
  182. }
  183. static int __init omap2_common_pm_init(void)
  184. {
  185. if (!of_have_populated_dt())
  186. omap2_init_processor_devices();
  187. omap_pm_if_init();
  188. return 0;
  189. }
  190. postcore_initcall(omap2_common_pm_init);
  191. static int __init omap2_common_pm_late_init(void)
  192. {
  193. /* Init the OMAP TWL parameters */
  194. omap3_twl_init();
  195. omap4_twl_init();
  196. /* Init the voltage layer */
  197. omap_voltage_late_init();
  198. /* Initialize the voltages */
  199. omap3_init_voltages();
  200. omap4_init_voltages();
  201. /* Smartreflex device init */
  202. omap_devinit_smartreflex();
  203. return 0;
  204. }
  205. late_initcall(omap2_common_pm_late_init);