pm.c 5.4 KB

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