device_pm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. /**
  177. * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
  178. * @dev: Device whose preferred target power state to return.
  179. * @d_min_p: Location to store the upper limit of the allowed states range.
  180. * @d_max_in: Deepest low-power state to take into consideration.
  181. * Return value: Preferred power state of the device on success, -ENODEV
  182. * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
  183. *
  184. * The caller must ensure that @dev is valid before using this function.
  185. */
  186. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
  187. {
  188. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  189. struct acpi_device *adev;
  190. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  191. dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
  192. return -ENODEV;
  193. }
  194. return acpi_device_power_state(dev, adev, acpi_target_system_state(),
  195. d_max_in, d_min_p);
  196. }
  197. EXPORT_SYMBOL(acpi_pm_device_sleep_state);
  198. #ifdef CONFIG_PM_RUNTIME
  199. /**
  200. * acpi_wakeup_device - Wakeup notification handler for ACPI devices.
  201. * @handle: ACPI handle of the device the notification is for.
  202. * @event: Type of the signaled event.
  203. * @context: Device corresponding to @handle.
  204. */
  205. static void acpi_wakeup_device(acpi_handle handle, u32 event, void *context)
  206. {
  207. struct device *dev = context;
  208. if (event == ACPI_NOTIFY_DEVICE_WAKE && dev) {
  209. pm_wakeup_event(dev, 0);
  210. pm_runtime_resume(dev);
  211. }
  212. }
  213. /**
  214. * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device.
  215. * @adev: ACPI device to enable/disable the remote wakeup for.
  216. * @enable: Whether to enable or disable the wakeup functionality.
  217. *
  218. * Enable/disable the GPE associated with @adev so that it can generate
  219. * wakeup signals for the device in response to external (remote) events and
  220. * enable/disable device wakeup power.
  221. *
  222. * Callers must ensure that @adev is a valid ACPI device node before executing
  223. * this function.
  224. */
  225. int __acpi_device_run_wake(struct acpi_device *adev, bool enable)
  226. {
  227. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  228. if (enable) {
  229. acpi_status res;
  230. int error;
  231. error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0);
  232. if (error)
  233. return error;
  234. res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  235. if (ACPI_FAILURE(res)) {
  236. acpi_disable_wakeup_device_power(adev);
  237. return -EIO;
  238. }
  239. } else {
  240. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  241. acpi_disable_wakeup_device_power(adev);
  242. }
  243. return 0;
  244. }
  245. /**
  246. * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
  247. * @dev: Device to enable/disable the platform to wake up.
  248. * @enable: Whether to enable or disable the wakeup functionality.
  249. */
  250. int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
  251. {
  252. struct acpi_device *adev;
  253. acpi_handle handle;
  254. if (!device_run_wake(phys_dev))
  255. return -EINVAL;
  256. handle = DEVICE_ACPI_HANDLE(phys_dev);
  257. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  258. dev_dbg(phys_dev, "ACPI handle without context in %s!\n",
  259. __func__);
  260. return -ENODEV;
  261. }
  262. return __acpi_device_run_wake(adev, enable);
  263. }
  264. EXPORT_SYMBOL(acpi_pm_device_run_wake);
  265. #else
  266. static inline void acpi_wakeup_device(acpi_handle handle, u32 event,
  267. void *context) {}
  268. #endif /* CONFIG_PM_RUNTIME */
  269. #ifdef CONFIG_PM_SLEEP
  270. /**
  271. * __acpi_device_sleep_wake - Enable or disable device to wake up the system.
  272. * @dev: Device to enable/desible to wake up the system.
  273. * @target_state: System state the device is supposed to wake up from.
  274. * @enable: Whether to enable or disable @dev to wake up the system.
  275. */
  276. int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state,
  277. bool enable)
  278. {
  279. return enable ?
  280. acpi_enable_wakeup_device_power(adev, target_state) :
  281. acpi_disable_wakeup_device_power(adev);
  282. }
  283. /**
  284. * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
  285. * @dev: Device to enable/desible to wake up the system from sleep states.
  286. * @enable: Whether to enable or disable @dev to wake up the system.
  287. */
  288. int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
  289. {
  290. acpi_handle handle;
  291. struct acpi_device *adev;
  292. int error;
  293. if (!device_can_wakeup(dev))
  294. return -EINVAL;
  295. handle = DEVICE_ACPI_HANDLE(dev);
  296. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  297. dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
  298. return -ENODEV;
  299. }
  300. error = __acpi_device_sleep_wake(adev, acpi_target_system_state(),
  301. enable);
  302. if (!error)
  303. dev_info(dev, "System wakeup %s by ACPI\n",
  304. enable ? "enabled" : "disabled");
  305. return error;
  306. }
  307. #endif /* CONFIG_PM_SLEEP */
  308. /**
  309. * acpi_dev_pm_get_node - Get ACPI device node for the given physical device.
  310. * @dev: Device to get the ACPI node for.
  311. */
  312. static struct acpi_device *acpi_dev_pm_get_node(struct device *dev)
  313. {
  314. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  315. struct acpi_device *adev;
  316. return handle && ACPI_SUCCESS(acpi_bus_get_device(handle, &adev)) ?
  317. adev : NULL;
  318. }
  319. /**
  320. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  321. * @dev: Device to put into a low-power state.
  322. * @adev: ACPI device node corresponding to @dev.
  323. * @system_state: System state to choose the device state for.
  324. */
  325. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  326. u32 system_state)
  327. {
  328. int power_state;
  329. if (!acpi_device_power_manageable(adev))
  330. return 0;
  331. power_state = acpi_device_power_state(dev, adev, system_state,
  332. ACPI_STATE_D3, NULL);
  333. if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3)
  334. return -EIO;
  335. return acpi_device_set_power(adev, power_state);
  336. }
  337. /**
  338. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  339. * @adev: ACPI device node to put into the full-power state.
  340. */
  341. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  342. {
  343. return acpi_device_power_manageable(adev) ?
  344. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  345. }
  346. #ifdef CONFIG_PM_RUNTIME
  347. /**
  348. * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
  349. * @dev: Device to put into a low-power state.
  350. *
  351. * Put the given device into a runtime low-power state using the standard ACPI
  352. * mechanism. Set up remote wakeup if desired, choose the state to put the
  353. * device into (this checks if remote wakeup is expected to work too), and set
  354. * the power state of the device.
  355. */
  356. int acpi_dev_runtime_suspend(struct device *dev)
  357. {
  358. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  359. bool remote_wakeup;
  360. int error;
  361. if (!adev)
  362. return 0;
  363. remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
  364. PM_QOS_FLAGS_NONE;
  365. error = __acpi_device_run_wake(adev, remote_wakeup);
  366. if (remote_wakeup && error)
  367. return -EAGAIN;
  368. error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  369. if (error)
  370. __acpi_device_run_wake(adev, false);
  371. return error;
  372. }
  373. EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
  374. /**
  375. * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
  376. * @dev: Device to put into the full-power state.
  377. *
  378. * Put the given device into the full-power state using the standard ACPI
  379. * mechanism at run time. Set the power state of the device to ACPI D0 and
  380. * disable remote wakeup.
  381. */
  382. int acpi_dev_runtime_resume(struct device *dev)
  383. {
  384. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  385. int error;
  386. if (!adev)
  387. return 0;
  388. error = acpi_dev_pm_full_power(adev);
  389. __acpi_device_run_wake(adev, false);
  390. return error;
  391. }
  392. EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
  393. /**
  394. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  395. * @dev: Device to suspend.
  396. *
  397. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  398. * it into a runtime low-power state.
  399. */
  400. int acpi_subsys_runtime_suspend(struct device *dev)
  401. {
  402. int ret = pm_generic_runtime_suspend(dev);
  403. return ret ? ret : acpi_dev_runtime_suspend(dev);
  404. }
  405. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  406. /**
  407. * acpi_subsys_runtime_resume - Resume device using ACPI.
  408. * @dev: Device to Resume.
  409. *
  410. * Use ACPI to put the given device into the full-power state and carry out the
  411. * generic runtime resume procedure for it.
  412. */
  413. int acpi_subsys_runtime_resume(struct device *dev)
  414. {
  415. int ret = acpi_dev_runtime_resume(dev);
  416. return ret ? ret : pm_generic_runtime_resume(dev);
  417. }
  418. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  419. #endif /* CONFIG_PM_RUNTIME */
  420. #ifdef CONFIG_PM_SLEEP
  421. /**
  422. * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
  423. * @dev: Device to put into a low-power state.
  424. *
  425. * Put the given device into a low-power state during system transition to a
  426. * sleep state using the standard ACPI mechanism. Set up system wakeup if
  427. * desired, choose the state to put the device into (this checks if system
  428. * wakeup is expected to work too), and set the power state of the device.
  429. */
  430. int acpi_dev_suspend_late(struct device *dev)
  431. {
  432. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  433. u32 target_state;
  434. bool wakeup;
  435. int error;
  436. if (!adev)
  437. return 0;
  438. target_state = acpi_target_system_state();
  439. wakeup = device_may_wakeup(dev);
  440. error = __acpi_device_sleep_wake(adev, target_state, wakeup);
  441. if (wakeup && error)
  442. return error;
  443. error = acpi_dev_pm_low_power(dev, adev, target_state);
  444. if (error)
  445. __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
  446. return error;
  447. }
  448. EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
  449. /**
  450. * acpi_dev_resume_early - Put device into the full-power state using ACPI.
  451. * @dev: Device to put into the full-power state.
  452. *
  453. * Put the given device into the full-power state using the standard ACPI
  454. * mechanism during system transition to the working state. Set the power
  455. * state of the device to ACPI D0 and disable remote wakeup.
  456. */
  457. int acpi_dev_resume_early(struct device *dev)
  458. {
  459. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  460. int error;
  461. if (!adev)
  462. return 0;
  463. error = acpi_dev_pm_full_power(adev);
  464. __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
  465. return error;
  466. }
  467. EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
  468. /**
  469. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  470. * @dev: Device to prepare.
  471. */
  472. int acpi_subsys_prepare(struct device *dev)
  473. {
  474. /*
  475. * Follow PCI and resume devices suspended at run time before running
  476. * their system suspend callbacks.
  477. */
  478. pm_runtime_resume(dev);
  479. return pm_generic_prepare(dev);
  480. }
  481. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  482. /**
  483. * acpi_subsys_suspend_late - Suspend device using ACPI.
  484. * @dev: Device to suspend.
  485. *
  486. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  487. * it into a low-power state during system transition into a sleep state.
  488. */
  489. int acpi_subsys_suspend_late(struct device *dev)
  490. {
  491. int ret = pm_generic_suspend_late(dev);
  492. return ret ? ret : acpi_dev_suspend_late(dev);
  493. }
  494. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  495. /**
  496. * acpi_subsys_resume_early - Resume device using ACPI.
  497. * @dev: Device to Resume.
  498. *
  499. * Use ACPI to put the given device into the full-power state and carry out the
  500. * generic early resume procedure for it during system transition into the
  501. * working state.
  502. */
  503. int acpi_subsys_resume_early(struct device *dev)
  504. {
  505. int ret = acpi_dev_resume_early(dev);
  506. return ret ? ret : pm_generic_resume_early(dev);
  507. }
  508. EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
  509. #endif /* CONFIG_PM_SLEEP */
  510. static struct dev_pm_domain acpi_general_pm_domain = {
  511. .ops = {
  512. #ifdef CONFIG_PM_RUNTIME
  513. .runtime_suspend = acpi_subsys_runtime_suspend,
  514. .runtime_resume = acpi_subsys_runtime_resume,
  515. .runtime_idle = pm_generic_runtime_idle,
  516. #endif
  517. #ifdef CONFIG_PM_SLEEP
  518. .prepare = acpi_subsys_prepare,
  519. .suspend_late = acpi_subsys_suspend_late,
  520. .resume_early = acpi_subsys_resume_early,
  521. .poweroff_late = acpi_subsys_suspend_late,
  522. .restore_early = acpi_subsys_resume_early,
  523. #endif
  524. },
  525. };
  526. /**
  527. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  528. * @dev: Device to prepare.
  529. * @power_on: Whether or not to power on the device.
  530. *
  531. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  532. * attached to it, install a wakeup notification handler for the device and
  533. * add it to the general ACPI PM domain. If @power_on is set, the device will
  534. * be put into the ACPI D0 state before the function returns.
  535. *
  536. * This assumes that the @dev's bus type uses generic power management callbacks
  537. * (or doesn't use any power management callbacks at all).
  538. *
  539. * Callers must ensure proper synchronization of this function with power
  540. * management callbacks.
  541. */
  542. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  543. {
  544. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  545. if (!adev)
  546. return -ENODEV;
  547. if (dev->pm_domain)
  548. return -EEXIST;
  549. acpi_add_pm_notifier(adev, acpi_wakeup_device, dev);
  550. dev->pm_domain = &acpi_general_pm_domain;
  551. if (power_on) {
  552. acpi_dev_pm_full_power(adev);
  553. __acpi_device_run_wake(adev, false);
  554. }
  555. return 0;
  556. }
  557. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  558. /**
  559. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  560. * @dev: Device to take care of.
  561. * @power_off: Whether or not to try to remove power from the device.
  562. *
  563. * Remove the device from the general ACPI PM domain and remove its wakeup
  564. * notifier. If @power_off is set, additionally remove power from the device if
  565. * possible.
  566. *
  567. * Callers must ensure proper synchronization of this function with power
  568. * management callbacks.
  569. */
  570. void acpi_dev_pm_detach(struct device *dev, bool power_off)
  571. {
  572. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  573. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  574. dev->pm_domain = NULL;
  575. acpi_remove_pm_notifier(adev, acpi_wakeup_device);
  576. if (power_off) {
  577. /*
  578. * If the device's PM QoS resume latency limit or flags
  579. * have been exposed to user space, they have to be
  580. * hidden at this point, so that they don't affect the
  581. * choice of the low-power state to put the device into.
  582. */
  583. dev_pm_qos_hide_latency_limit(dev);
  584. dev_pm_qos_hide_flags(dev);
  585. __acpi_device_run_wake(adev, false);
  586. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  587. }
  588. }
  589. }
  590. EXPORT_SYMBOL_GPL(acpi_dev_pm_detach);