device_pm.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * drivers/acpi/device_pm.c - ACPI device power management routines.
  3. *
  4. * Copyright (C) 2012, Intel Corp.
  5. * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as published
  11. * by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. */
  24. #include <linux/device.h>
  25. #include <linux/export.h>
  26. #include <linux/mutex.h>
  27. #include <linux/pm_qos.h>
  28. #include <linux/pm_runtime.h>
  29. #include <acpi/acpi.h>
  30. #include <acpi/acpi_bus.h>
  31. static DEFINE_MUTEX(acpi_pm_notifier_lock);
  32. /**
  33. * acpi_add_pm_notifier - Register PM notifier for given ACPI device.
  34. * @adev: ACPI device to add the notifier for.
  35. * @context: Context information to pass to the notifier routine.
  36. *
  37. * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
  38. * PM wakeup events. For example, wakeup events may be generated for bridges
  39. * if one of the devices below the bridge is signaling wakeup, even if the
  40. * bridge itself doesn't have a wakeup GPE associated with it.
  41. */
  42. acpi_status acpi_add_pm_notifier(struct acpi_device *adev,
  43. acpi_notify_handler handler, void *context)
  44. {
  45. acpi_status status = AE_ALREADY_EXISTS;
  46. mutex_lock(&acpi_pm_notifier_lock);
  47. if (adev->wakeup.flags.notifier_present)
  48. goto out;
  49. status = acpi_install_notify_handler(adev->handle,
  50. ACPI_SYSTEM_NOTIFY,
  51. handler, context);
  52. if (ACPI_FAILURE(status))
  53. goto out;
  54. adev->wakeup.flags.notifier_present = true;
  55. out:
  56. mutex_unlock(&acpi_pm_notifier_lock);
  57. return status;
  58. }
  59. /**
  60. * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
  61. * @adev: ACPI device to remove the notifier from.
  62. */
  63. acpi_status acpi_remove_pm_notifier(struct acpi_device *adev,
  64. acpi_notify_handler handler)
  65. {
  66. acpi_status status = AE_BAD_PARAMETER;
  67. mutex_lock(&acpi_pm_notifier_lock);
  68. if (!adev->wakeup.flags.notifier_present)
  69. goto out;
  70. status = acpi_remove_notify_handler(adev->handle,
  71. ACPI_SYSTEM_NOTIFY,
  72. handler);
  73. if (ACPI_FAILURE(status))
  74. goto out;
  75. adev->wakeup.flags.notifier_present = false;
  76. out:
  77. mutex_unlock(&acpi_pm_notifier_lock);
  78. return status;
  79. }
  80. /**
  81. * acpi_device_power_state - Get preferred power state of ACPI device.
  82. * @dev: Device whose preferred target power state to return.
  83. * @adev: ACPI device node corresponding to @dev.
  84. * @target_state: System state to match the resultant device state.
  85. * @d_max_in: Deepest low-power state to take into consideration.
  86. * @d_min_p: Location to store the upper limit of the allowed states range.
  87. * Return value: Preferred power state of the device on success, -ENODEV
  88. * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
  89. *
  90. * Find the lowest power (highest number) ACPI device power state that the
  91. * device can be in while the system is in the state represented by
  92. * @target_state. If @d_min_p is set, the highest power (lowest number) device
  93. * power state that @dev can be in for the given system sleep state is stored
  94. * at the location pointed to by it.
  95. *
  96. * Callers must ensure that @dev and @adev are valid pointers and that @adev
  97. * actually corresponds to @dev before using this function.
  98. */
  99. int acpi_device_power_state(struct device *dev, struct acpi_device *adev,
  100. u32 target_state, int d_max_in, int *d_min_p)
  101. {
  102. char acpi_method[] = "_SxD";
  103. unsigned long long d_min, d_max;
  104. bool wakeup = false;
  105. if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3)
  106. return -EINVAL;
  107. if (d_max_in > ACPI_STATE_D3_HOT) {
  108. enum pm_qos_flags_status stat;
  109. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  110. if (stat == PM_QOS_FLAGS_ALL)
  111. d_max_in = ACPI_STATE_D3_HOT;
  112. }
  113. acpi_method[2] = '0' + target_state;
  114. /*
  115. * If the sleep state is S0, the lowest limit from ACPI is D3,
  116. * but if the device has _S0W, we will use the value from _S0W
  117. * as the lowest limit from ACPI. Finally, we will constrain
  118. * the lowest limit with the specified one.
  119. */
  120. d_min = ACPI_STATE_D0;
  121. d_max = ACPI_STATE_D3;
  122. /*
  123. * If present, _SxD methods return the minimum D-state (highest power
  124. * state) we can use for the corresponding S-states. Otherwise, the
  125. * minimum D-state is D0 (ACPI 3.x).
  126. *
  127. * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
  128. * provided -- that's our fault recovery, we ignore retval.
  129. */
  130. if (target_state > ACPI_STATE_S0) {
  131. acpi_evaluate_integer(adev->handle, acpi_method, NULL, &d_min);
  132. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  133. && adev->wakeup.sleep_state >= target_state;
  134. } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
  135. PM_QOS_FLAGS_NONE) {
  136. wakeup = adev->wakeup.flags.valid;
  137. }
  138. /*
  139. * If _PRW says we can wake up the system from the target sleep state,
  140. * the D-state returned by _SxD is sufficient for that (we assume a
  141. * wakeup-aware driver if wake is set). Still, if _SxW exists
  142. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  143. * can wake the system. _S0W may be valid, too.
  144. */
  145. if (wakeup) {
  146. acpi_status status;
  147. acpi_method[3] = 'W';
  148. status = acpi_evaluate_integer(adev->handle, acpi_method, NULL,
  149. &d_max);
  150. if (ACPI_FAILURE(status)) {
  151. if (target_state != ACPI_STATE_S0 ||
  152. status != AE_NOT_FOUND)
  153. d_max = d_min;
  154. } else if (d_max < d_min) {
  155. /* Warn the user of the broken DSDT */
  156. printk(KERN_WARNING "ACPI: Wrong value from %s\n",
  157. acpi_method);
  158. /* Sanitize it */
  159. d_min = d_max;
  160. }
  161. }
  162. if (d_max_in < d_min)
  163. return -EINVAL;
  164. if (d_min_p)
  165. *d_min_p = d_min;
  166. /* constrain d_max with specified lowest limit (max number) */
  167. if (d_max > d_max_in) {
  168. for (d_max = d_max_in; d_max > d_min; d_max--) {
  169. if (adev->power.states[d_max].flags.valid)
  170. break;
  171. }
  172. }
  173. return d_max;
  174. }
  175. EXPORT_SYMBOL_GPL(acpi_device_power_state);
  176. #ifdef CONFIG_PM_RUNTIME
  177. /**
  178. * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device.
  179. * @adev: ACPI device to enable/disable the remote wakeup for.
  180. * @enable: Whether to enable or disable the wakeup functionality.
  181. *
  182. * Enable/disable the GPE associated with @adev so that it can generate
  183. * wakeup signals for the device in response to external (remote) events and
  184. * enable/disable device wakeup power.
  185. *
  186. * Callers must ensure that @adev is a valid ACPI device node before executing
  187. * this function.
  188. */
  189. int __acpi_device_run_wake(struct acpi_device *adev, bool enable)
  190. {
  191. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  192. if (enable) {
  193. acpi_status res;
  194. int error;
  195. error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0);
  196. if (error)
  197. return error;
  198. res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  199. if (ACPI_FAILURE(res)) {
  200. acpi_disable_wakeup_device_power(adev);
  201. return -EIO;
  202. }
  203. } else {
  204. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  205. acpi_disable_wakeup_device_power(adev);
  206. }
  207. return 0;
  208. }
  209. /**
  210. * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
  211. * @dev: Device to enable/disable the platform to wake up.
  212. * @enable: Whether to enable or disable the wakeup functionality.
  213. */
  214. int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
  215. {
  216. struct acpi_device *adev;
  217. acpi_handle handle;
  218. if (!device_run_wake(phys_dev))
  219. return -EINVAL;
  220. handle = DEVICE_ACPI_HANDLE(phys_dev);
  221. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  222. dev_dbg(phys_dev, "ACPI handle without context in %s!\n",
  223. __func__);
  224. return -ENODEV;
  225. }
  226. return __acpi_device_run_wake(adev, enable);
  227. }
  228. EXPORT_SYMBOL(acpi_pm_device_run_wake);
  229. #endif /* CONFIG_PM_RUNTIME */
  230. #ifdef CONFIG_PM_SLEEP
  231. /**
  232. * __acpi_device_sleep_wake - Enable or disable device to wake up the system.
  233. * @dev: Device to enable/desible to wake up the system.
  234. * @target_state: System state the device is supposed to wake up from.
  235. * @enable: Whether to enable or disable @dev to wake up the system.
  236. */
  237. int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state,
  238. bool enable)
  239. {
  240. return enable ?
  241. acpi_enable_wakeup_device_power(adev, target_state) :
  242. acpi_disable_wakeup_device_power(adev);
  243. }
  244. #endif /* CONFIG_PM_SLEEP */