device_pm.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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. #include <acpi/acpi_drivers.h>
  32. #include "internal.h"
  33. #define _COMPONENT ACPI_POWER_COMPONENT
  34. ACPI_MODULE_NAME("device_pm");
  35. static DEFINE_MUTEX(acpi_pm_notifier_lock);
  36. /**
  37. * acpi_add_pm_notifier - Register PM notifier for given ACPI device.
  38. * @adev: ACPI device to add the notifier for.
  39. * @context: Context information to pass to the notifier routine.
  40. *
  41. * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
  42. * PM wakeup events. For example, wakeup events may be generated for bridges
  43. * if one of the devices below the bridge is signaling wakeup, even if the
  44. * bridge itself doesn't have a wakeup GPE associated with it.
  45. */
  46. acpi_status acpi_add_pm_notifier(struct acpi_device *adev,
  47. acpi_notify_handler handler, void *context)
  48. {
  49. acpi_status status = AE_ALREADY_EXISTS;
  50. mutex_lock(&acpi_pm_notifier_lock);
  51. if (adev->wakeup.flags.notifier_present)
  52. goto out;
  53. status = acpi_install_notify_handler(adev->handle,
  54. ACPI_SYSTEM_NOTIFY,
  55. handler, context);
  56. if (ACPI_FAILURE(status))
  57. goto out;
  58. adev->wakeup.flags.notifier_present = true;
  59. out:
  60. mutex_unlock(&acpi_pm_notifier_lock);
  61. return status;
  62. }
  63. /**
  64. * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
  65. * @adev: ACPI device to remove the notifier from.
  66. */
  67. acpi_status acpi_remove_pm_notifier(struct acpi_device *adev,
  68. acpi_notify_handler handler)
  69. {
  70. acpi_status status = AE_BAD_PARAMETER;
  71. mutex_lock(&acpi_pm_notifier_lock);
  72. if (!adev->wakeup.flags.notifier_present)
  73. goto out;
  74. status = acpi_remove_notify_handler(adev->handle,
  75. ACPI_SYSTEM_NOTIFY,
  76. handler);
  77. if (ACPI_FAILURE(status))
  78. goto out;
  79. adev->wakeup.flags.notifier_present = false;
  80. out:
  81. mutex_unlock(&acpi_pm_notifier_lock);
  82. return status;
  83. }
  84. /**
  85. * acpi_power_state_string - String representation of ACPI device power state.
  86. * @state: ACPI device power state to return the string representation of.
  87. */
  88. const char *acpi_power_state_string(int state)
  89. {
  90. switch (state) {
  91. case ACPI_STATE_D0:
  92. return "D0";
  93. case ACPI_STATE_D1:
  94. return "D1";
  95. case ACPI_STATE_D2:
  96. return "D2";
  97. case ACPI_STATE_D3_HOT:
  98. return "D3hot";
  99. case ACPI_STATE_D3_COLD:
  100. return "D3";
  101. default:
  102. return "(unknown)";
  103. }
  104. }
  105. /**
  106. * acpi_device_get_power - Get power state of an ACPI device.
  107. * @device: Device to get the power state of.
  108. * @state: Place to store the power state of the device.
  109. *
  110. * This function does not update the device's power.state field, but it may
  111. * update its parent's power.state field (when the parent's power state is
  112. * unknown and the device's power state turns out to be D0).
  113. */
  114. int acpi_device_get_power(struct acpi_device *device, int *state)
  115. {
  116. int result = ACPI_STATE_UNKNOWN;
  117. if (!device || !state)
  118. return -EINVAL;
  119. if (!device->flags.power_manageable) {
  120. /* TBD: Non-recursive algorithm for walking up hierarchy. */
  121. *state = device->parent ?
  122. device->parent->power.state : ACPI_STATE_D0;
  123. goto out;
  124. }
  125. /*
  126. * Get the device's power state either directly (via _PSC) or
  127. * indirectly (via power resources).
  128. */
  129. if (device->power.flags.explicit_get) {
  130. unsigned long long psc;
  131. acpi_status status = acpi_evaluate_integer(device->handle,
  132. "_PSC", NULL, &psc);
  133. if (ACPI_FAILURE(status))
  134. return -ENODEV;
  135. result = psc;
  136. }
  137. /* The test below covers ACPI_STATE_UNKNOWN too. */
  138. if (result <= ACPI_STATE_D2) {
  139. ; /* Do nothing. */
  140. } else if (device->power.flags.power_resources) {
  141. int error = acpi_power_get_inferred_state(device, &result);
  142. if (error)
  143. return error;
  144. } else if (result == ACPI_STATE_D3_HOT) {
  145. result = ACPI_STATE_D3;
  146. }
  147. /*
  148. * If we were unsure about the device parent's power state up to this
  149. * point, the fact that the device is in D0 implies that the parent has
  150. * to be in D0 too.
  151. */
  152. if (device->parent && device->parent->power.state == ACPI_STATE_UNKNOWN
  153. && result == ACPI_STATE_D0)
  154. device->parent->power.state = ACPI_STATE_D0;
  155. *state = result;
  156. out:
  157. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
  158. device->pnp.bus_id, acpi_power_state_string(*state)));
  159. return 0;
  160. }
  161. /**
  162. * acpi_device_set_power - Set power state of an ACPI device.
  163. * @device: Device to set the power state of.
  164. * @state: New power state to set.
  165. *
  166. * Callers must ensure that the device is power manageable before using this
  167. * function.
  168. */
  169. int acpi_device_set_power(struct acpi_device *device, int state)
  170. {
  171. int result = 0;
  172. acpi_status status = AE_OK;
  173. char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
  174. bool cut_power = false;
  175. if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
  176. return -EINVAL;
  177. /* Make sure this is a valid target state */
  178. if (state == device->power.state) {
  179. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at %s\n",
  180. acpi_power_state_string(state)));
  181. return 0;
  182. }
  183. if (!device->power.states[state].flags.valid) {
  184. printk(KERN_WARNING PREFIX "Device does not support %s\n",
  185. acpi_power_state_string(state));
  186. return -ENODEV;
  187. }
  188. if (device->parent && (state < device->parent->power.state)) {
  189. printk(KERN_WARNING PREFIX
  190. "Cannot set device to a higher-powered"
  191. " state than parent\n");
  192. return -ENODEV;
  193. }
  194. /* For D3cold we should first transition into D3hot. */
  195. if (state == ACPI_STATE_D3_COLD
  196. && device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible) {
  197. state = ACPI_STATE_D3_HOT;
  198. object_name[3] = '3';
  199. cut_power = true;
  200. }
  201. /*
  202. * Transition Power
  203. * ----------------
  204. * On transitions to a high-powered state we first apply power (via
  205. * power resources) then evalute _PSx. Conversly for transitions to
  206. * a lower-powered state.
  207. */
  208. if (state < device->power.state) {
  209. if (device->power.state >= ACPI_STATE_D3_HOT &&
  210. state != ACPI_STATE_D0) {
  211. printk(KERN_WARNING PREFIX
  212. "Cannot transition to non-D0 state from D3\n");
  213. return -ENODEV;
  214. }
  215. if (device->power.flags.power_resources) {
  216. result = acpi_power_transition(device, state);
  217. if (result)
  218. goto end;
  219. }
  220. if (device->power.states[state].flags.explicit_set) {
  221. status = acpi_evaluate_object(device->handle,
  222. object_name, NULL, NULL);
  223. if (ACPI_FAILURE(status)) {
  224. result = -ENODEV;
  225. goto end;
  226. }
  227. }
  228. } else {
  229. if (device->power.states[state].flags.explicit_set) {
  230. status = acpi_evaluate_object(device->handle,
  231. object_name, NULL, NULL);
  232. if (ACPI_FAILURE(status)) {
  233. result = -ENODEV;
  234. goto end;
  235. }
  236. }
  237. if (device->power.flags.power_resources) {
  238. result = acpi_power_transition(device, state);
  239. if (result)
  240. goto end;
  241. }
  242. }
  243. if (cut_power)
  244. result = acpi_power_transition(device, ACPI_STATE_D3_COLD);
  245. end:
  246. if (result)
  247. printk(KERN_WARNING PREFIX
  248. "Device [%s] failed to transition to %s\n",
  249. device->pnp.bus_id,
  250. acpi_power_state_string(state));
  251. else {
  252. device->power.state = state;
  253. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  254. "Device [%s] transitioned to %s\n",
  255. device->pnp.bus_id,
  256. acpi_power_state_string(state)));
  257. }
  258. return result;
  259. }
  260. EXPORT_SYMBOL(acpi_device_set_power);
  261. int acpi_bus_set_power(acpi_handle handle, int state)
  262. {
  263. struct acpi_device *device;
  264. int result;
  265. result = acpi_bus_get_device(handle, &device);
  266. if (result)
  267. return result;
  268. if (!device->flags.power_manageable) {
  269. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  270. "Device [%s] is not power manageable\n",
  271. dev_name(&device->dev)));
  272. return -ENODEV;
  273. }
  274. return acpi_device_set_power(device, state);
  275. }
  276. EXPORT_SYMBOL(acpi_bus_set_power);
  277. int acpi_bus_init_power(struct acpi_device *device)
  278. {
  279. int state;
  280. int result;
  281. if (!device)
  282. return -EINVAL;
  283. device->power.state = ACPI_STATE_UNKNOWN;
  284. result = acpi_device_get_power(device, &state);
  285. if (result)
  286. return result;
  287. if (device->power.flags.power_resources)
  288. result = acpi_power_on_resources(device, state);
  289. if (!result)
  290. device->power.state = state;
  291. return result;
  292. }
  293. int acpi_bus_update_power(acpi_handle handle, int *state_p)
  294. {
  295. struct acpi_device *device;
  296. int state;
  297. int result;
  298. result = acpi_bus_get_device(handle, &device);
  299. if (result)
  300. return result;
  301. result = acpi_device_get_power(device, &state);
  302. if (result)
  303. return result;
  304. result = acpi_device_set_power(device, state);
  305. if (!result && state_p)
  306. *state_p = state;
  307. return result;
  308. }
  309. EXPORT_SYMBOL_GPL(acpi_bus_update_power);
  310. bool acpi_bus_power_manageable(acpi_handle handle)
  311. {
  312. struct acpi_device *device;
  313. int result;
  314. result = acpi_bus_get_device(handle, &device);
  315. return result ? false : device->flags.power_manageable;
  316. }
  317. EXPORT_SYMBOL(acpi_bus_power_manageable);
  318. bool acpi_bus_can_wakeup(acpi_handle handle)
  319. {
  320. struct acpi_device *device;
  321. int result;
  322. result = acpi_bus_get_device(handle, &device);
  323. return result ? false : device->wakeup.flags.valid;
  324. }
  325. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  326. /**
  327. * acpi_device_power_state - Get preferred power state of ACPI device.
  328. * @dev: Device whose preferred target power state to return.
  329. * @adev: ACPI device node corresponding to @dev.
  330. * @target_state: System state to match the resultant device state.
  331. * @d_max_in: Deepest low-power state to take into consideration.
  332. * @d_min_p: Location to store the upper limit of the allowed states range.
  333. * Return value: Preferred power state of the device on success, -ENODEV
  334. * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
  335. *
  336. * Find the lowest power (highest number) ACPI device power state that the
  337. * device can be in while the system is in the state represented by
  338. * @target_state. If @d_min_p is set, the highest power (lowest number) device
  339. * power state that @dev can be in for the given system sleep state is stored
  340. * at the location pointed to by it.
  341. *
  342. * Callers must ensure that @dev and @adev are valid pointers and that @adev
  343. * actually corresponds to @dev before using this function.
  344. */
  345. int acpi_device_power_state(struct device *dev, struct acpi_device *adev,
  346. u32 target_state, int d_max_in, int *d_min_p)
  347. {
  348. char acpi_method[] = "_SxD";
  349. unsigned long long d_min, d_max;
  350. bool wakeup = false;
  351. if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3)
  352. return -EINVAL;
  353. if (d_max_in > ACPI_STATE_D3_HOT) {
  354. enum pm_qos_flags_status stat;
  355. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  356. if (stat == PM_QOS_FLAGS_ALL)
  357. d_max_in = ACPI_STATE_D3_HOT;
  358. }
  359. acpi_method[2] = '0' + target_state;
  360. /*
  361. * If the sleep state is S0, the lowest limit from ACPI is D3,
  362. * but if the device has _S0W, we will use the value from _S0W
  363. * as the lowest limit from ACPI. Finally, we will constrain
  364. * the lowest limit with the specified one.
  365. */
  366. d_min = ACPI_STATE_D0;
  367. d_max = ACPI_STATE_D3;
  368. /*
  369. * If present, _SxD methods return the minimum D-state (highest power
  370. * state) we can use for the corresponding S-states. Otherwise, the
  371. * minimum D-state is D0 (ACPI 3.x).
  372. *
  373. * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer
  374. * provided -- that's our fault recovery, we ignore retval.
  375. */
  376. if (target_state > ACPI_STATE_S0) {
  377. acpi_evaluate_integer(adev->handle, acpi_method, NULL, &d_min);
  378. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  379. && adev->wakeup.sleep_state >= target_state;
  380. } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) !=
  381. PM_QOS_FLAGS_NONE) {
  382. wakeup = adev->wakeup.flags.valid;
  383. }
  384. /*
  385. * If _PRW says we can wake up the system from the target sleep state,
  386. * the D-state returned by _SxD is sufficient for that (we assume a
  387. * wakeup-aware driver if wake is set). Still, if _SxW exists
  388. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  389. * can wake the system. _S0W may be valid, too.
  390. */
  391. if (wakeup) {
  392. acpi_status status;
  393. acpi_method[3] = 'W';
  394. status = acpi_evaluate_integer(adev->handle, acpi_method, NULL,
  395. &d_max);
  396. if (ACPI_FAILURE(status)) {
  397. if (target_state != ACPI_STATE_S0 ||
  398. status != AE_NOT_FOUND)
  399. d_max = d_min;
  400. } else if (d_max < d_min) {
  401. /* Warn the user of the broken DSDT */
  402. printk(KERN_WARNING "ACPI: Wrong value from %s\n",
  403. acpi_method);
  404. /* Sanitize it */
  405. d_min = d_max;
  406. }
  407. }
  408. if (d_max_in < d_min)
  409. return -EINVAL;
  410. if (d_min_p)
  411. *d_min_p = d_min;
  412. /* constrain d_max with specified lowest limit (max number) */
  413. if (d_max > d_max_in) {
  414. for (d_max = d_max_in; d_max > d_min; d_max--) {
  415. if (adev->power.states[d_max].flags.valid)
  416. break;
  417. }
  418. }
  419. return d_max;
  420. }
  421. EXPORT_SYMBOL_GPL(acpi_device_power_state);
  422. /**
  423. * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
  424. * @dev: Device whose preferred target power state to return.
  425. * @d_min_p: Location to store the upper limit of the allowed states range.
  426. * @d_max_in: Deepest low-power state to take into consideration.
  427. * Return value: Preferred power state of the device on success, -ENODEV
  428. * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure
  429. *
  430. * The caller must ensure that @dev is valid before using this function.
  431. */
  432. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
  433. {
  434. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  435. struct acpi_device *adev;
  436. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  437. dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
  438. return -ENODEV;
  439. }
  440. return acpi_device_power_state(dev, adev, acpi_target_system_state(),
  441. d_max_in, d_min_p);
  442. }
  443. EXPORT_SYMBOL(acpi_pm_device_sleep_state);
  444. #ifdef CONFIG_PM_RUNTIME
  445. /**
  446. * acpi_wakeup_device - Wakeup notification handler for ACPI devices.
  447. * @handle: ACPI handle of the device the notification is for.
  448. * @event: Type of the signaled event.
  449. * @context: Device corresponding to @handle.
  450. */
  451. static void acpi_wakeup_device(acpi_handle handle, u32 event, void *context)
  452. {
  453. struct device *dev = context;
  454. if (event == ACPI_NOTIFY_DEVICE_WAKE && dev) {
  455. pm_wakeup_event(dev, 0);
  456. pm_runtime_resume(dev);
  457. }
  458. }
  459. /**
  460. * __acpi_device_run_wake - Enable/disable runtime remote wakeup for device.
  461. * @adev: ACPI device to enable/disable the remote wakeup for.
  462. * @enable: Whether to enable or disable the wakeup functionality.
  463. *
  464. * Enable/disable the GPE associated with @adev so that it can generate
  465. * wakeup signals for the device in response to external (remote) events and
  466. * enable/disable device wakeup power.
  467. *
  468. * Callers must ensure that @adev is a valid ACPI device node before executing
  469. * this function.
  470. */
  471. int __acpi_device_run_wake(struct acpi_device *adev, bool enable)
  472. {
  473. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  474. if (enable) {
  475. acpi_status res;
  476. int error;
  477. error = acpi_enable_wakeup_device_power(adev, ACPI_STATE_S0);
  478. if (error)
  479. return error;
  480. res = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  481. if (ACPI_FAILURE(res)) {
  482. acpi_disable_wakeup_device_power(adev);
  483. return -EIO;
  484. }
  485. } else {
  486. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  487. acpi_disable_wakeup_device_power(adev);
  488. }
  489. return 0;
  490. }
  491. /**
  492. * acpi_pm_device_run_wake - Enable/disable remote wakeup for given device.
  493. * @dev: Device to enable/disable the platform to wake up.
  494. * @enable: Whether to enable or disable the wakeup functionality.
  495. */
  496. int acpi_pm_device_run_wake(struct device *phys_dev, bool enable)
  497. {
  498. struct acpi_device *adev;
  499. acpi_handle handle;
  500. if (!device_run_wake(phys_dev))
  501. return -EINVAL;
  502. handle = DEVICE_ACPI_HANDLE(phys_dev);
  503. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  504. dev_dbg(phys_dev, "ACPI handle without context in %s!\n",
  505. __func__);
  506. return -ENODEV;
  507. }
  508. return __acpi_device_run_wake(adev, enable);
  509. }
  510. EXPORT_SYMBOL(acpi_pm_device_run_wake);
  511. #else
  512. static inline void acpi_wakeup_device(acpi_handle handle, u32 event,
  513. void *context) {}
  514. #endif /* CONFIG_PM_RUNTIME */
  515. #ifdef CONFIG_PM_SLEEP
  516. /**
  517. * __acpi_device_sleep_wake - Enable or disable device to wake up the system.
  518. * @dev: Device to enable/desible to wake up the system.
  519. * @target_state: System state the device is supposed to wake up from.
  520. * @enable: Whether to enable or disable @dev to wake up the system.
  521. */
  522. int __acpi_device_sleep_wake(struct acpi_device *adev, u32 target_state,
  523. bool enable)
  524. {
  525. return enable ?
  526. acpi_enable_wakeup_device_power(adev, target_state) :
  527. acpi_disable_wakeup_device_power(adev);
  528. }
  529. /**
  530. * acpi_pm_device_sleep_wake - Enable or disable device to wake up the system.
  531. * @dev: Device to enable/desible to wake up the system from sleep states.
  532. * @enable: Whether to enable or disable @dev to wake up the system.
  533. */
  534. int acpi_pm_device_sleep_wake(struct device *dev, bool enable)
  535. {
  536. acpi_handle handle;
  537. struct acpi_device *adev;
  538. int error;
  539. if (!device_can_wakeup(dev))
  540. return -EINVAL;
  541. handle = DEVICE_ACPI_HANDLE(dev);
  542. if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) {
  543. dev_dbg(dev, "ACPI handle without context in %s!\n", __func__);
  544. return -ENODEV;
  545. }
  546. error = __acpi_device_sleep_wake(adev, acpi_target_system_state(),
  547. enable);
  548. if (!error)
  549. dev_info(dev, "System wakeup %s by ACPI\n",
  550. enable ? "enabled" : "disabled");
  551. return error;
  552. }
  553. #endif /* CONFIG_PM_SLEEP */
  554. /**
  555. * acpi_dev_pm_get_node - Get ACPI device node for the given physical device.
  556. * @dev: Device to get the ACPI node for.
  557. */
  558. struct acpi_device *acpi_dev_pm_get_node(struct device *dev)
  559. {
  560. acpi_handle handle = DEVICE_ACPI_HANDLE(dev);
  561. struct acpi_device *adev;
  562. return handle && !acpi_bus_get_device(handle, &adev) ? adev : NULL;
  563. }
  564. /**
  565. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  566. * @dev: Device to put into a low-power state.
  567. * @adev: ACPI device node corresponding to @dev.
  568. * @system_state: System state to choose the device state for.
  569. */
  570. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  571. u32 system_state)
  572. {
  573. int power_state;
  574. if (!acpi_device_power_manageable(adev))
  575. return 0;
  576. power_state = acpi_device_power_state(dev, adev, system_state,
  577. ACPI_STATE_D3, NULL);
  578. if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3)
  579. return -EIO;
  580. return acpi_device_set_power(adev, power_state);
  581. }
  582. /**
  583. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  584. * @adev: ACPI device node to put into the full-power state.
  585. */
  586. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  587. {
  588. return acpi_device_power_manageable(adev) ?
  589. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  590. }
  591. #ifdef CONFIG_PM_RUNTIME
  592. /**
  593. * acpi_dev_runtime_suspend - Put device into a low-power state using ACPI.
  594. * @dev: Device to put into a low-power state.
  595. *
  596. * Put the given device into a runtime low-power state using the standard ACPI
  597. * mechanism. Set up remote wakeup if desired, choose the state to put the
  598. * device into (this checks if remote wakeup is expected to work too), and set
  599. * the power state of the device.
  600. */
  601. int acpi_dev_runtime_suspend(struct device *dev)
  602. {
  603. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  604. bool remote_wakeup;
  605. int error;
  606. if (!adev)
  607. return 0;
  608. remote_wakeup = dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) >
  609. PM_QOS_FLAGS_NONE;
  610. error = __acpi_device_run_wake(adev, remote_wakeup);
  611. if (remote_wakeup && error)
  612. return -EAGAIN;
  613. error = acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  614. if (error)
  615. __acpi_device_run_wake(adev, false);
  616. return error;
  617. }
  618. EXPORT_SYMBOL_GPL(acpi_dev_runtime_suspend);
  619. /**
  620. * acpi_dev_runtime_resume - Put device into the full-power state using ACPI.
  621. * @dev: Device to put into the full-power state.
  622. *
  623. * Put the given device into the full-power state using the standard ACPI
  624. * mechanism at run time. Set the power state of the device to ACPI D0 and
  625. * disable remote wakeup.
  626. */
  627. int acpi_dev_runtime_resume(struct device *dev)
  628. {
  629. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  630. int error;
  631. if (!adev)
  632. return 0;
  633. error = acpi_dev_pm_full_power(adev);
  634. __acpi_device_run_wake(adev, false);
  635. return error;
  636. }
  637. EXPORT_SYMBOL_GPL(acpi_dev_runtime_resume);
  638. /**
  639. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  640. * @dev: Device to suspend.
  641. *
  642. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  643. * it into a runtime low-power state.
  644. */
  645. int acpi_subsys_runtime_suspend(struct device *dev)
  646. {
  647. int ret = pm_generic_runtime_suspend(dev);
  648. return ret ? ret : acpi_dev_runtime_suspend(dev);
  649. }
  650. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  651. /**
  652. * acpi_subsys_runtime_resume - Resume device using ACPI.
  653. * @dev: Device to Resume.
  654. *
  655. * Use ACPI to put the given device into the full-power state and carry out the
  656. * generic runtime resume procedure for it.
  657. */
  658. int acpi_subsys_runtime_resume(struct device *dev)
  659. {
  660. int ret = acpi_dev_runtime_resume(dev);
  661. return ret ? ret : pm_generic_runtime_resume(dev);
  662. }
  663. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  664. #endif /* CONFIG_PM_RUNTIME */
  665. #ifdef CONFIG_PM_SLEEP
  666. /**
  667. * acpi_dev_suspend_late - Put device into a low-power state using ACPI.
  668. * @dev: Device to put into a low-power state.
  669. *
  670. * Put the given device into a low-power state during system transition to a
  671. * sleep state using the standard ACPI mechanism. Set up system wakeup if
  672. * desired, choose the state to put the device into (this checks if system
  673. * wakeup is expected to work too), and set the power state of the device.
  674. */
  675. int acpi_dev_suspend_late(struct device *dev)
  676. {
  677. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  678. u32 target_state;
  679. bool wakeup;
  680. int error;
  681. if (!adev)
  682. return 0;
  683. target_state = acpi_target_system_state();
  684. wakeup = device_may_wakeup(dev);
  685. error = __acpi_device_sleep_wake(adev, target_state, wakeup);
  686. if (wakeup && error)
  687. return error;
  688. error = acpi_dev_pm_low_power(dev, adev, target_state);
  689. if (error)
  690. __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
  691. return error;
  692. }
  693. EXPORT_SYMBOL_GPL(acpi_dev_suspend_late);
  694. /**
  695. * acpi_dev_resume_early - Put device into the full-power state using ACPI.
  696. * @dev: Device to put into the full-power state.
  697. *
  698. * Put the given device into the full-power state using the standard ACPI
  699. * mechanism during system transition to the working state. Set the power
  700. * state of the device to ACPI D0 and disable remote wakeup.
  701. */
  702. int acpi_dev_resume_early(struct device *dev)
  703. {
  704. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  705. int error;
  706. if (!adev)
  707. return 0;
  708. error = acpi_dev_pm_full_power(adev);
  709. __acpi_device_sleep_wake(adev, ACPI_STATE_UNKNOWN, false);
  710. return error;
  711. }
  712. EXPORT_SYMBOL_GPL(acpi_dev_resume_early);
  713. /**
  714. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  715. * @dev: Device to prepare.
  716. */
  717. int acpi_subsys_prepare(struct device *dev)
  718. {
  719. /*
  720. * Follow PCI and resume devices suspended at run time before running
  721. * their system suspend callbacks.
  722. */
  723. pm_runtime_resume(dev);
  724. return pm_generic_prepare(dev);
  725. }
  726. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  727. /**
  728. * acpi_subsys_suspend_late - Suspend device using ACPI.
  729. * @dev: Device to suspend.
  730. *
  731. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  732. * it into a low-power state during system transition into a sleep state.
  733. */
  734. int acpi_subsys_suspend_late(struct device *dev)
  735. {
  736. int ret = pm_generic_suspend_late(dev);
  737. return ret ? ret : acpi_dev_suspend_late(dev);
  738. }
  739. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  740. /**
  741. * acpi_subsys_resume_early - Resume device using ACPI.
  742. * @dev: Device to Resume.
  743. *
  744. * Use ACPI to put the given device into the full-power state and carry out the
  745. * generic early resume procedure for it during system transition into the
  746. * working state.
  747. */
  748. int acpi_subsys_resume_early(struct device *dev)
  749. {
  750. int ret = acpi_dev_resume_early(dev);
  751. return ret ? ret : pm_generic_resume_early(dev);
  752. }
  753. EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
  754. #endif /* CONFIG_PM_SLEEP */
  755. static struct dev_pm_domain acpi_general_pm_domain = {
  756. .ops = {
  757. #ifdef CONFIG_PM_RUNTIME
  758. .runtime_suspend = acpi_subsys_runtime_suspend,
  759. .runtime_resume = acpi_subsys_runtime_resume,
  760. .runtime_idle = pm_generic_runtime_idle,
  761. #endif
  762. #ifdef CONFIG_PM_SLEEP
  763. .prepare = acpi_subsys_prepare,
  764. .suspend_late = acpi_subsys_suspend_late,
  765. .resume_early = acpi_subsys_resume_early,
  766. .poweroff_late = acpi_subsys_suspend_late,
  767. .restore_early = acpi_subsys_resume_early,
  768. #endif
  769. },
  770. };
  771. /**
  772. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  773. * @dev: Device to prepare.
  774. * @power_on: Whether or not to power on the device.
  775. *
  776. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  777. * attached to it, install a wakeup notification handler for the device and
  778. * add it to the general ACPI PM domain. If @power_on is set, the device will
  779. * be put into the ACPI D0 state before the function returns.
  780. *
  781. * This assumes that the @dev's bus type uses generic power management callbacks
  782. * (or doesn't use any power management callbacks at all).
  783. *
  784. * Callers must ensure proper synchronization of this function with power
  785. * management callbacks.
  786. */
  787. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  788. {
  789. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  790. if (!adev)
  791. return -ENODEV;
  792. if (dev->pm_domain)
  793. return -EEXIST;
  794. acpi_add_pm_notifier(adev, acpi_wakeup_device, dev);
  795. dev->pm_domain = &acpi_general_pm_domain;
  796. if (power_on) {
  797. acpi_dev_pm_full_power(adev);
  798. __acpi_device_run_wake(adev, false);
  799. }
  800. return 0;
  801. }
  802. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  803. /**
  804. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  805. * @dev: Device to take care of.
  806. * @power_off: Whether or not to try to remove power from the device.
  807. *
  808. * Remove the device from the general ACPI PM domain and remove its wakeup
  809. * notifier. If @power_off is set, additionally remove power from the device if
  810. * possible.
  811. *
  812. * Callers must ensure proper synchronization of this function with power
  813. * management callbacks.
  814. */
  815. void acpi_dev_pm_detach(struct device *dev, bool power_off)
  816. {
  817. struct acpi_device *adev = acpi_dev_pm_get_node(dev);
  818. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  819. dev->pm_domain = NULL;
  820. acpi_remove_pm_notifier(adev, acpi_wakeup_device);
  821. if (power_off) {
  822. /*
  823. * If the device's PM QoS resume latency limit or flags
  824. * have been exposed to user space, they have to be
  825. * hidden at this point, so that they don't affect the
  826. * choice of the low-power state to put the device into.
  827. */
  828. dev_pm_qos_hide_latency_limit(dev);
  829. dev_pm_qos_hide_flags(dev);
  830. __acpi_device_run_wake(adev, false);
  831. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  832. }
  833. }
  834. }
  835. EXPORT_SYMBOL_GPL(acpi_dev_pm_detach);
  836. /**
  837. * acpi_dev_pm_add_dependent - Add physical device depending for PM.
  838. * @handle: Handle of ACPI device node.
  839. * @depdev: Device depending on that node for PM.
  840. */
  841. void acpi_dev_pm_add_dependent(acpi_handle handle, struct device *depdev)
  842. {
  843. struct acpi_device_physical_node *dep;
  844. struct acpi_device *adev;
  845. if (!depdev || acpi_bus_get_device(handle, &adev))
  846. return;
  847. mutex_lock(&adev->physical_node_lock);
  848. list_for_each_entry(dep, &adev->power_dependent, node)
  849. if (dep->dev == depdev)
  850. goto out;
  851. dep = kzalloc(sizeof(*dep), GFP_KERNEL);
  852. if (dep) {
  853. dep->dev = depdev;
  854. list_add_tail(&dep->node, &adev->power_dependent);
  855. }
  856. out:
  857. mutex_unlock(&adev->physical_node_lock);
  858. }
  859. EXPORT_SYMBOL_GPL(acpi_dev_pm_add_dependent);
  860. /**
  861. * acpi_dev_pm_remove_dependent - Remove physical device depending for PM.
  862. * @handle: Handle of ACPI device node.
  863. * @depdev: Device depending on that node for PM.
  864. */
  865. void acpi_dev_pm_remove_dependent(acpi_handle handle, struct device *depdev)
  866. {
  867. struct acpi_device_physical_node *dep;
  868. struct acpi_device *adev;
  869. if (!depdev || acpi_bus_get_device(handle, &adev))
  870. return;
  871. mutex_lock(&adev->physical_node_lock);
  872. list_for_each_entry(dep, &adev->power_dependent, node)
  873. if (dep->dev == depdev) {
  874. list_del(&dep->node);
  875. kfree(dep);
  876. break;
  877. }
  878. mutex_unlock(&adev->physical_node_lock);
  879. }
  880. EXPORT_SYMBOL_GPL(acpi_dev_pm_remove_dependent);