pm.c 5.4 KB

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