device_pm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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_bus_get_device(handle, &adev) ? adev : NULL;
  317. }
  318. /**
  319. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  320. * @dev: Device to put into a low-power state.
  321. * @adev: ACPI device node corresponding to @dev.
  322. * @system_state: System state to choose the device state for.
  323. */
  324. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  325. u32 system_state)
  326. {
  327. int power_state;
  328. if (!acpi_device_power_manageable(adev))
  329. return 0;
  330. power_state = acpi_device_power_state(dev, adev, system_state,
  331. ACPI_STATE_D3, NULL);
  332. if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3)
  333. return -EIO;
  334. return acpi_device_set_power(adev, power_state);
  335. }
  336. /**
  337. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  338. * @adev: ACPI device node to put into the full-power state.
  339. */
  340. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  341. {
  342. return acpi_device_power_manageable(adev) ?
  343. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  344. }
  345. #ifdef CONFIG_PM_RUNTIME
  346. /**
  347. * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
  348. * @dev: Device to put into a low-power state.
  349. *
  350. * Put the given device into a runtime low-power state using the standard ACPI
  351. * mechanism. Set up remote wakeup if desired, choose the state to put the
  352. * device into (this checks if remote wakeup is expected to work too), and set
  353. * the power state of the device.
  354. */
  355. int acpi_dev_runtime_suspend(struct device *dev)
  356. {
  357. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  358. bool remote_wakeup;
  359. int error;
  360. if (!adev)
  361. return 0;
  362. remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
  363. PM_QOS_FLAGS_NONE;
  364. error = __acpi_device_run_wake(adev, remote_wakeup);
  365. if (remote_wakeup && error)
  366. return -EAGAIN;
  367. error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  368. if (error)
  369. __acpi_device_run_wake(adev, false);
  370. return error;
  371. }
  372. EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
  373. /**
  374. * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
  375. * @dev: Device to put into the full-power state.
  376. *
  377. * Put the given device into the full-power state using the standard ACPI
  378. * mechanism at run time. Set the power state of the device to ACPI D0 and
  379. * disable remote wakeup.
  380. */
  381. int acpi_dev_runtime_resume(struct device *dev)
  382. {
  383. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  384. int error;
  385. if (!adev)
  386. return 0;
  387. error = acpi_dev_pm_full_power(adev);
  388. __acpi_device_run_wake(adev, false);
  389. return error;
  390. }
  391. EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
  392. /**
  393. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  394. * @dev: Device to suspend.
  395. *
  396. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  397. * it into a runtime low-power state.
  398. */
  399. int acpi_subsys_runtime_suspend(struct device *dev)
  400. {
  401. int ret = pm_generic_runtime_suspend(dev);
  402. return ret ? ret : acpi_dev_runtime_suspend(dev);
  403. }
  404. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  405. /**
  406. * acpi_subsys_runtime_resume - Resume device using ACPI.
  407. * @dev: Device to Resume.
  408. *
  409. * Use ACPI to put the given device into the full-power state and carry out the
  410. * generic runtime resume procedure for it.
  411. */
  412. int acpi_subsys_runtime_resume(struct device *dev)
  413. {
  414. int ret = acpi_dev_runtime_resume(dev);
  415. return ret ? ret : pm_generic_runtime_resume(dev);
  416. }
  417. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  418. #endif /* CONFIG_PM_RUNTIME */
  419. #ifdef CONFIG_PM_SLEEP
  420. /**
  421. * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
  422. * @dev: Device to put into a low-power state.
  423. *
  424. * Put the given device into a low-power state during system transition to a
  425. * sleep state using the standard ACPI mechanism. Set up system wakeup if
  426. * desired, choose the state to put the device into (this checks if system
  427. * wakeup is expected to work too), and set the power state of the device.
  428. */
  429. int acpi_dev_suspend_late(struct device *dev)
  430. {
  431. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  432. u32 target_state;
  433. bool wakeup;
  434. int error;
  435. if (!adev)
  436. return 0;
  437. target_state = acpi_target_system_state();
  438. wakeup = device_may_wakeup(dev);
  439. error = __acpi_device_sleep_wake(adev, target_state, wakeup);
  440. if (wakeup && error)
  441. return error;
  442. error = acpi_dev_pm_low_power(dev, adev, target_state);
  443. if (error)
  444. __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
  445. return error;
  446. }
  447. EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
  448. /**
  449. * acpi_dev_resume_early - Put device into the full-power state using ACPI.
  450. * @dev: Device to put into the full-power state.
  451. *
  452. * Put the given device into the full-power state using the standard ACPI
  453. * mechanism during system transition to the working state. Set the power
  454. * state of the device to ACPI D0 and disable remote wakeup.
  455. */
  456. int acpi_dev_resume_early(struct device *dev)
  457. {
  458. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  459. int error;
  460. if (!adev)
  461. return 0;
  462. error = acpi_dev_pm_full_power(adev);
  463. __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
  464. return error;
  465. }
  466. EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
  467. /**
  468. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  469. * @dev: Device to prepare.
  470. */
  471. int acpi_subsys_prepare(struct device *dev)
  472. {
  473. /*
  474. * Follow PCI and resume devices suspended at run time before running
  475. * their system suspend callbacks.
  476. */
  477. pm_runtime_resume(dev);
  478. return pm_generic_prepare(dev);
  479. }
  480. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  481. /**
  482. * acpi_subsys_suspend_late - Suspend device using ACPI.
  483. * @dev: Device to suspend.
  484. *
  485. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  486. * it into a low-power state during system transition into a sleep state.
  487. */
  488. int acpi_subsys_suspend_late(struct device *dev)
  489. {
  490. int ret = pm_generic_suspend_late(dev);
  491. return ret ? ret : acpi_dev_suspend_late(dev);
  492. }
  493. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  494. /**
  495. * acpi_subsys_resume_early - Resume device using ACPI.
  496. * @dev: Device to Resume.
  497. *
  498. * Use ACPI to put the given device into the full-power state and carry out the
  499. * generic early resume procedure for it during system transition into the
  500. * working state.
  501. */
  502. int acpi_subsys_resume_early(struct device *dev)
  503. {
  504. int ret = acpi_dev_resume_early(dev);
  505. return ret ? ret : pm_generic_resume_early(dev);
  506. }
  507. EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
  508. #endif /* CONFIG_PM_SLEEP */
  509. static struct dev_pm_domain acpi_general_pm_domain = {
  510. .ops = {
  511. #ifdef CONFIG_PM_RUNTIME
  512. .runtime_suspend = acpi_subsys_runtime_suspend,
  513. .runtime_resume = acpi_subsys_runtime_resume,
  514. .runtime_idle = pm_generic_runtime_idle,
  515. #endif
  516. #ifdef CONFIG_PM_SLEEP
  517. .prepare = acpi_subsys_prepare,
  518. .suspend_late = acpi_subsys_suspend_late,
  519. .resume_early = acpi_subsys_resume_early,
  520. .poweroff_late = acpi_subsys_suspend_late,
  521. .restore_early = acpi_subsys_resume_early,
  522. #endif
  523. },
  524. };
  525. /**
  526. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  527. * @dev: Device to prepare.
  528. * @power_on: Whether or not to power on the device.
  529. *
  530. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  531. * attached to it, install a wakeup notification handler for the device and
  532. * add it to the general ACPI PM domain. If @power_on is set, the device will
  533. * be put into the ACPI D0 state before the function returns.
  534. *
  535. * This assumes that the @dev's bus type uses generic power management callbacks
  536. * (or doesn't use any power management callbacks at all).
  537. *
  538. * Callers must ensure proper synchronization of this function with power
  539. * management callbacks.
  540. */
  541. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  542. {
  543. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  544. if (!adev)
  545. return -ENODEV;
  546. if (dev->pm_domain)
  547. return -EEXIST;
  548. acpi_add_pm_notifier(adev, acpi_wakeup_device, dev);
  549. dev->pm_domain = &acpi_general_pm_domain;
  550. if (power_on) {
  551. acpi_dev_pm_full_power(adev);
  552. __acpi_device_run_wake(adev, false);
  553. }
  554. return 0;
  555. }
  556. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  557. /**
  558. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  559. * @dev: Device to take care of.
  560. * @power_off: Whether or not to try to remove power from the device.
  561. *
  562. * Remove the device from the general ACPI PM domain and remove its wakeup
  563. * notifier. If @power_off is set, additionally remove power from the device if
  564. * possible.
  565. *
  566. * Callers must ensure proper synchronization of this function with power
  567. * management callbacks.
  568. */
  569. void acpi_dev_pm_detach(struct device *dev, bool power_off)
  570. {
  571. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  572. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  573. dev->pm_domain = NULL;
  574. acpi_remove_pm_notifier(adev, acpi_wakeup_device);
  575. if (power_off) {
  576. /*
  577. * If the device's PM QoS resume latency limit or flags
  578. * have been exposed to user space, they have to be
  579. * hidden at this point, so that they don't affect the
  580. * choice of the low-power state to put the device into.
  581. */
  582. dev_pm_qos_hide_latency_limit(dev);
  583. dev_pm_qos_hide_flags(dev);
  584. __acpi_device_run_wake(adev, false);
  585. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  586. }
  587. }
  588. }
  589. EXPORT_SYMBOL_GPL(acpi_dev_pm_detach);