device_pm.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. */
  20. #include <linux/acpi.h>
  21. #include <linux/export.h>
  22. #include <linux/mutex.h>
  23. #include <linux/pm_qos.h>
  24. #include <linux/pm_domain.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/suspend.h>
  27. #include "internal.h"
  28. #define _COMPONENT ACPI_POWER_COMPONENT
  29. ACPI_MODULE_NAME("device_pm");
  30. /**
  31. * acpi_power_state_string - String representation of ACPI device power state.
  32. * @state: ACPI device power state to return the string representation of.
  33. */
  34. const char *acpi_power_state_string(int state)
  35. {
  36. switch (state) {
  37. case ACPI_STATE_D0:
  38. return "D0";
  39. case ACPI_STATE_D1:
  40. return "D1";
  41. case ACPI_STATE_D2:
  42. return "D2";
  43. case ACPI_STATE_D3_HOT:
  44. return "D3hot";
  45. case ACPI_STATE_D3_COLD:
  46. return "D3cold";
  47. default:
  48. return "(unknown)";
  49. }
  50. }
  51. /**
  52. * acpi_device_get_power - Get power state of an ACPI device.
  53. * @device: Device to get the power state of.
  54. * @state: Place to store the power state of the device.
  55. *
  56. * This function does not update the device's power.state field, but it may
  57. * update its parent's power.state field (when the parent's power state is
  58. * unknown and the device's power state turns out to be D0).
  59. */
  60. int acpi_device_get_power(struct acpi_device *device, int *state)
  61. {
  62. int result = ACPI_STATE_UNKNOWN;
  63. if (!device || !state)
  64. return -EINVAL;
  65. if (!device->flags.power_manageable) {
  66. /* TBD: Non-recursive algorithm for walking up hierarchy. */
  67. *state = device->parent ?
  68. device->parent->power.state : ACPI_STATE_D0;
  69. goto out;
  70. }
  71. /*
  72. * Get the device's power state from power resources settings and _PSC,
  73. * if available.
  74. */
  75. if (device->power.flags.power_resources) {
  76. int error = acpi_power_get_inferred_state(device, &result);
  77. if (error)
  78. return error;
  79. }
  80. if (device->power.flags.explicit_get) {
  81. acpi_handle handle = device->handle;
  82. unsigned long long psc;
  83. acpi_status status;
  84. status = acpi_evaluate_integer(handle, "_PSC", NULL, &psc);
  85. if (ACPI_FAILURE(status))
  86. return -ENODEV;
  87. /*
  88. * The power resources settings may indicate a power state
  89. * shallower than the actual power state of the device, because
  90. * the same power resources may be referenced by other devices.
  91. *
  92. * For systems predating ACPI 4.0 we assume that D3hot is the
  93. * deepest state that can be supported.
  94. */
  95. if (psc > result && psc < ACPI_STATE_D3_COLD)
  96. result = psc;
  97. else if (result == ACPI_STATE_UNKNOWN)
  98. result = psc > ACPI_STATE_D2 ? ACPI_STATE_D3_HOT : psc;
  99. }
  100. /*
  101. * If we were unsure about the device parent's power state up to this
  102. * point, the fact that the device is in D0 implies that the parent has
  103. * to be in D0 too, except if ignore_parent is set.
  104. */
  105. if (!device->power.flags.ignore_parent && device->parent
  106. && device->parent->power.state == ACPI_STATE_UNKNOWN
  107. && result == ACPI_STATE_D0)
  108. device->parent->power.state = ACPI_STATE_D0;
  109. *state = result;
  110. out:
  111. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is %s\n",
  112. device->pnp.bus_id, acpi_power_state_string(*state)));
  113. return 0;
  114. }
  115. static int acpi_dev_pm_explicit_set(struct acpi_device *adev, int state)
  116. {
  117. if (adev->power.states[state].flags.explicit_set) {
  118. char method[5] = { '_', 'P', 'S', '0' + state, '\0' };
  119. acpi_status status;
  120. status = acpi_evaluate_object(adev->handle, method, NULL, NULL);
  121. if (ACPI_FAILURE(status))
  122. return -ENODEV;
  123. }
  124. return 0;
  125. }
  126. /**
  127. * acpi_device_set_power - Set power state of an ACPI device.
  128. * @device: Device to set the power state of.
  129. * @state: New power state to set.
  130. *
  131. * Callers must ensure that the device is power manageable before using this
  132. * function.
  133. */
  134. int acpi_device_set_power(struct acpi_device *device, int state)
  135. {
  136. int target_state = state;
  137. int result = 0;
  138. if (!device || !device->flags.power_manageable
  139. || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3_COLD))
  140. return -EINVAL;
  141. /* Make sure this is a valid target state */
  142. if (state == device->power.state) {
  143. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already in %s\n",
  144. device->pnp.bus_id,
  145. acpi_power_state_string(state)));
  146. return 0;
  147. }
  148. if (state == ACPI_STATE_D3_COLD) {
  149. /*
  150. * For transitions to D3cold we need to execute _PS3 and then
  151. * possibly drop references to the power resources in use.
  152. */
  153. state = ACPI_STATE_D3_HOT;
  154. /* If _PR3 is not available, use D3hot as the target state. */
  155. if (!device->power.states[ACPI_STATE_D3_COLD].flags.valid)
  156. target_state = state;
  157. } else if (!device->power.states[state].flags.valid) {
  158. dev_warn(&device->dev, "Power state %s not supported\n",
  159. acpi_power_state_string(state));
  160. return -ENODEV;
  161. }
  162. if (!device->power.flags.ignore_parent &&
  163. device->parent && (state < device->parent->power.state)) {
  164. dev_warn(&device->dev,
  165. "Cannot transition to power state %s for parent in %s\n",
  166. acpi_power_state_string(state),
  167. acpi_power_state_string(device->parent->power.state));
  168. return -ENODEV;
  169. }
  170. /*
  171. * Transition Power
  172. * ----------------
  173. * In accordance with ACPI 6, _PSx is executed before manipulating power
  174. * resources, unless the target state is D0, in which case _PS0 is
  175. * supposed to be executed after turning the power resources on.
  176. */
  177. if (state > ACPI_STATE_D0) {
  178. /*
  179. * According to ACPI 6, devices cannot go from lower-power
  180. * (deeper) states to higher-power (shallower) states.
  181. */
  182. if (state < device->power.state) {
  183. dev_warn(&device->dev, "Cannot transition from %s to %s\n",
  184. acpi_power_state_string(device->power.state),
  185. acpi_power_state_string(state));
  186. return -ENODEV;
  187. }
  188. result = acpi_dev_pm_explicit_set(device, state);
  189. if (result)
  190. goto end;
  191. if (device->power.flags.power_resources)
  192. result = acpi_power_transition(device, target_state);
  193. } else {
  194. if (device->power.flags.power_resources) {
  195. result = acpi_power_transition(device, ACPI_STATE_D0);
  196. if (result)
  197. goto end;
  198. }
  199. result = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
  200. }
  201. end:
  202. if (result) {
  203. dev_warn(&device->dev, "Failed to change power state to %s\n",
  204. acpi_power_state_string(state));
  205. } else {
  206. device->power.state = target_state;
  207. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  208. "Device [%s] transitioned to %s\n",
  209. device->pnp.bus_id,
  210. acpi_power_state_string(state)));
  211. }
  212. return result;
  213. }
  214. EXPORT_SYMBOL(acpi_device_set_power);
  215. int acpi_bus_set_power(acpi_handle handle, int state)
  216. {
  217. struct acpi_device *device;
  218. int result;
  219. result = acpi_bus_get_device(handle, &device);
  220. if (result)
  221. return result;
  222. return acpi_device_set_power(device, state);
  223. }
  224. EXPORT_SYMBOL(acpi_bus_set_power);
  225. int acpi_bus_init_power(struct acpi_device *device)
  226. {
  227. int state;
  228. int result;
  229. if (!device)
  230. return -EINVAL;
  231. device->power.state = ACPI_STATE_UNKNOWN;
  232. if (!acpi_device_is_present(device)) {
  233. device->flags.initialized = false;
  234. return -ENXIO;
  235. }
  236. result = acpi_device_get_power(device, &state);
  237. if (result)
  238. return result;
  239. if (state < ACPI_STATE_D3_COLD && device->power.flags.power_resources) {
  240. /* Reference count the power resources. */
  241. result = acpi_power_on_resources(device, state);
  242. if (result)
  243. return result;
  244. if (state == ACPI_STATE_D0) {
  245. /*
  246. * If _PSC is not present and the state inferred from
  247. * power resources appears to be D0, it still may be
  248. * necessary to execute _PS0 at this point, because
  249. * another device using the same power resources may
  250. * have been put into D0 previously and that's why we
  251. * see D0 here.
  252. */
  253. result = acpi_dev_pm_explicit_set(device, state);
  254. if (result)
  255. return result;
  256. }
  257. } else if (state == ACPI_STATE_UNKNOWN) {
  258. /*
  259. * No power resources and missing _PSC? Cross fingers and make
  260. * it D0 in hope that this is what the BIOS put the device into.
  261. * [We tried to force D0 here by executing _PS0, but that broke
  262. * Toshiba P870-303 in a nasty way.]
  263. */
  264. state = ACPI_STATE_D0;
  265. }
  266. device->power.state = state;
  267. return 0;
  268. }
  269. /**
  270. * acpi_device_fix_up_power - Force device with missing _PSC into D0.
  271. * @device: Device object whose power state is to be fixed up.
  272. *
  273. * Devices without power resources and _PSC, but having _PS0 and _PS3 defined,
  274. * are assumed to be put into D0 by the BIOS. However, in some cases that may
  275. * not be the case and this function should be used then.
  276. */
  277. int acpi_device_fix_up_power(struct acpi_device *device)
  278. {
  279. int ret = 0;
  280. if (!device->power.flags.power_resources
  281. && !device->power.flags.explicit_get
  282. && device->power.state == ACPI_STATE_D0)
  283. ret = acpi_dev_pm_explicit_set(device, ACPI_STATE_D0);
  284. return ret;
  285. }
  286. EXPORT_SYMBOL_GPL(acpi_device_fix_up_power);
  287. int acpi_device_update_power(struct acpi_device *device, int *state_p)
  288. {
  289. int state;
  290. int result;
  291. if (device->power.state == ACPI_STATE_UNKNOWN) {
  292. result = acpi_bus_init_power(device);
  293. if (!result && state_p)
  294. *state_p = device->power.state;
  295. return result;
  296. }
  297. result = acpi_device_get_power(device, &state);
  298. if (result)
  299. return result;
  300. if (state == ACPI_STATE_UNKNOWN) {
  301. state = ACPI_STATE_D0;
  302. result = acpi_device_set_power(device, state);
  303. if (result)
  304. return result;
  305. } else {
  306. if (device->power.flags.power_resources) {
  307. /*
  308. * We don't need to really switch the state, bu we need
  309. * to update the power resources' reference counters.
  310. */
  311. result = acpi_power_transition(device, state);
  312. if (result)
  313. return result;
  314. }
  315. device->power.state = state;
  316. }
  317. if (state_p)
  318. *state_p = state;
  319. return 0;
  320. }
  321. EXPORT_SYMBOL_GPL(acpi_device_update_power);
  322. int acpi_bus_update_power(acpi_handle handle, int *state_p)
  323. {
  324. struct acpi_device *device;
  325. int result;
  326. result = acpi_bus_get_device(handle, &device);
  327. return result ? result : acpi_device_update_power(device, state_p);
  328. }
  329. EXPORT_SYMBOL_GPL(acpi_bus_update_power);
  330. bool acpi_bus_power_manageable(acpi_handle handle)
  331. {
  332. struct acpi_device *device;
  333. int result;
  334. result = acpi_bus_get_device(handle, &device);
  335. return result ? false : device->flags.power_manageable;
  336. }
  337. EXPORT_SYMBOL(acpi_bus_power_manageable);
  338. #ifdef CONFIG_PM
  339. static DEFINE_MUTEX(acpi_pm_notifier_lock);
  340. static DEFINE_MUTEX(acpi_pm_notifier_install_lock);
  341. void acpi_pm_wakeup_event(struct device *dev)
  342. {
  343. pm_wakeup_dev_event(dev, 0, acpi_s2idle_wakeup());
  344. }
  345. EXPORT_SYMBOL_GPL(acpi_pm_wakeup_event);
  346. static void acpi_pm_notify_handler(acpi_handle handle, u32 val, void *not_used)
  347. {
  348. struct acpi_device *adev;
  349. if (val != ACPI_NOTIFY_DEVICE_WAKE)
  350. return;
  351. acpi_handle_debug(handle, "Wake notify\n");
  352. adev = acpi_bus_get_acpi_device(handle);
  353. if (!adev)
  354. return;
  355. mutex_lock(&acpi_pm_notifier_lock);
  356. if (adev->wakeup.flags.notifier_present) {
  357. pm_wakeup_ws_event(adev->wakeup.ws, 0, acpi_s2idle_wakeup());
  358. if (adev->wakeup.context.func) {
  359. acpi_handle_debug(handle, "Running %pF for %s\n",
  360. adev->wakeup.context.func,
  361. dev_name(adev->wakeup.context.dev));
  362. adev->wakeup.context.func(&adev->wakeup.context);
  363. }
  364. }
  365. mutex_unlock(&acpi_pm_notifier_lock);
  366. acpi_bus_put_acpi_device(adev);
  367. }
  368. /**
  369. * acpi_add_pm_notifier - Register PM notify handler for given ACPI device.
  370. * @adev: ACPI device to add the notify handler for.
  371. * @dev: Device to generate a wakeup event for while handling the notification.
  372. * @func: Work function to execute when handling the notification.
  373. *
  374. * NOTE: @adev need not be a run-wake or wakeup device to be a valid source of
  375. * PM wakeup events. For example, wakeup events may be generated for bridges
  376. * if one of the devices below the bridge is signaling wakeup, even if the
  377. * bridge itself doesn't have a wakeup GPE associated with it.
  378. */
  379. acpi_status acpi_add_pm_notifier(struct acpi_device *adev, struct device *dev,
  380. void (*func)(struct acpi_device_wakeup_context *context))
  381. {
  382. acpi_status status = AE_ALREADY_EXISTS;
  383. if (!dev && !func)
  384. return AE_BAD_PARAMETER;
  385. mutex_lock(&acpi_pm_notifier_install_lock);
  386. if (adev->wakeup.flags.notifier_present)
  387. goto out;
  388. status = acpi_install_notify_handler(adev->handle, ACPI_SYSTEM_NOTIFY,
  389. acpi_pm_notify_handler, NULL);
  390. if (ACPI_FAILURE(status))
  391. goto out;
  392. mutex_lock(&acpi_pm_notifier_lock);
  393. adev->wakeup.ws = wakeup_source_register(dev_name(&adev->dev));
  394. adev->wakeup.context.dev = dev;
  395. adev->wakeup.context.func = func;
  396. adev->wakeup.flags.notifier_present = true;
  397. mutex_unlock(&acpi_pm_notifier_lock);
  398. out:
  399. mutex_unlock(&acpi_pm_notifier_install_lock);
  400. return status;
  401. }
  402. /**
  403. * acpi_remove_pm_notifier - Unregister PM notifier from given ACPI device.
  404. * @adev: ACPI device to remove the notifier from.
  405. */
  406. acpi_status acpi_remove_pm_notifier(struct acpi_device *adev)
  407. {
  408. acpi_status status = AE_BAD_PARAMETER;
  409. mutex_lock(&acpi_pm_notifier_install_lock);
  410. if (!adev->wakeup.flags.notifier_present)
  411. goto out;
  412. status = acpi_remove_notify_handler(adev->handle,
  413. ACPI_SYSTEM_NOTIFY,
  414. acpi_pm_notify_handler);
  415. if (ACPI_FAILURE(status))
  416. goto out;
  417. mutex_lock(&acpi_pm_notifier_lock);
  418. adev->wakeup.context.func = NULL;
  419. adev->wakeup.context.dev = NULL;
  420. wakeup_source_unregister(adev->wakeup.ws);
  421. adev->wakeup.flags.notifier_present = false;
  422. mutex_unlock(&acpi_pm_notifier_lock);
  423. out:
  424. mutex_unlock(&acpi_pm_notifier_install_lock);
  425. return status;
  426. }
  427. bool acpi_bus_can_wakeup(acpi_handle handle)
  428. {
  429. struct acpi_device *device;
  430. int result;
  431. result = acpi_bus_get_device(handle, &device);
  432. return result ? false : device->wakeup.flags.valid;
  433. }
  434. EXPORT_SYMBOL(acpi_bus_can_wakeup);
  435. bool acpi_pm_device_can_wakeup(struct device *dev)
  436. {
  437. struct acpi_device *adev = ACPI_COMPANION(dev);
  438. return adev ? acpi_device_can_wakeup(adev) : false;
  439. }
  440. /**
  441. * acpi_dev_pm_get_state - Get preferred power state of ACPI device.
  442. * @dev: Device whose preferred target power state to return.
  443. * @adev: ACPI device node corresponding to @dev.
  444. * @target_state: System state to match the resultant device state.
  445. * @d_min_p: Location to store the highest power state available to the device.
  446. * @d_max_p: Location to store the lowest power state available to the device.
  447. *
  448. * Find the lowest power (highest number) and highest power (lowest number) ACPI
  449. * device power states that the device can be in while the system is in the
  450. * state represented by @target_state. Store the integer numbers representing
  451. * those stats in the memory locations pointed to by @d_max_p and @d_min_p,
  452. * respectively.
  453. *
  454. * Callers must ensure that @dev and @adev are valid pointers and that @adev
  455. * actually corresponds to @dev before using this function.
  456. *
  457. * Returns 0 on success or -ENODATA when one of the ACPI methods fails or
  458. * returns a value that doesn't make sense. The memory locations pointed to by
  459. * @d_max_p and @d_min_p are only modified on success.
  460. */
  461. static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev,
  462. u32 target_state, int *d_min_p, int *d_max_p)
  463. {
  464. char method[] = { '_', 'S', '0' + target_state, 'D', '\0' };
  465. acpi_handle handle = adev->handle;
  466. unsigned long long ret;
  467. int d_min, d_max;
  468. bool wakeup = false;
  469. acpi_status status;
  470. /*
  471. * If the system state is S0, the lowest power state the device can be
  472. * in is D3cold, unless the device has _S0W and is supposed to signal
  473. * wakeup, in which case the return value of _S0W has to be used as the
  474. * lowest power state available to the device.
  475. */
  476. d_min = ACPI_STATE_D0;
  477. d_max = ACPI_STATE_D3_COLD;
  478. /*
  479. * If present, _SxD methods return the minimum D-state (highest power
  480. * state) we can use for the corresponding S-states. Otherwise, the
  481. * minimum D-state is D0 (ACPI 3.x).
  482. */
  483. if (target_state > ACPI_STATE_S0) {
  484. /*
  485. * We rely on acpi_evaluate_integer() not clobbering the integer
  486. * provided if AE_NOT_FOUND is returned.
  487. */
  488. ret = d_min;
  489. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  490. if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  491. || ret > ACPI_STATE_D3_COLD)
  492. return -ENODATA;
  493. /*
  494. * We need to handle legacy systems where D3hot and D3cold are
  495. * the same and 3 is returned in both cases, so fall back to
  496. * D3cold if D3hot is not a valid state.
  497. */
  498. if (!adev->power.states[ret].flags.valid) {
  499. if (ret == ACPI_STATE_D3_HOT)
  500. ret = ACPI_STATE_D3_COLD;
  501. else
  502. return -ENODATA;
  503. }
  504. d_min = ret;
  505. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  506. && adev->wakeup.sleep_state >= target_state;
  507. } else {
  508. wakeup = adev->wakeup.flags.valid;
  509. }
  510. /*
  511. * If _PRW says we can wake up the system from the target sleep state,
  512. * the D-state returned by _SxD is sufficient for that (we assume a
  513. * wakeup-aware driver if wake is set). Still, if _SxW exists
  514. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  515. * can wake the system. _S0W may be valid, too.
  516. */
  517. if (wakeup) {
  518. method[3] = 'W';
  519. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  520. if (status == AE_NOT_FOUND) {
  521. if (target_state > ACPI_STATE_S0)
  522. d_max = d_min;
  523. } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
  524. /* Fall back to D3cold if ret is not a valid state. */
  525. if (!adev->power.states[ret].flags.valid)
  526. ret = ACPI_STATE_D3_COLD;
  527. d_max = ret > d_min ? ret : d_min;
  528. } else {
  529. return -ENODATA;
  530. }
  531. }
  532. if (d_min_p)
  533. *d_min_p = d_min;
  534. if (d_max_p)
  535. *d_max_p = d_max;
  536. return 0;
  537. }
  538. /**
  539. * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
  540. * @dev: Device whose preferred target power state to return.
  541. * @d_min_p: Location to store the upper limit of the allowed states range.
  542. * @d_max_in: Deepest low-power state to take into consideration.
  543. * Return value: Preferred power state of the device on success, -ENODEV
  544. * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
  545. * incorrect, or -ENODATA on ACPI method failure.
  546. *
  547. * The caller must ensure that @dev is valid before using this function.
  548. */
  549. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
  550. {
  551. struct acpi_device *adev;
  552. int ret, d_min, d_max;
  553. if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
  554. return -EINVAL;
  555. if (d_max_in > ACPI_STATE_D2) {
  556. enum pm_qos_flags_status stat;
  557. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  558. if (stat == PM_QOS_FLAGS_ALL)
  559. d_max_in = ACPI_STATE_D2;
  560. }
  561. adev = ACPI_COMPANION(dev);
  562. if (!adev) {
  563. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  564. return -ENODEV;
  565. }
  566. ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
  567. &d_min, &d_max);
  568. if (ret)
  569. return ret;
  570. if (d_max_in < d_min)
  571. return -EINVAL;
  572. if (d_max > d_max_in) {
  573. for (d_max = d_max_in; d_max > d_min; d_max--) {
  574. if (adev->power.states[d_max].flags.valid)
  575. break;
  576. }
  577. }
  578. if (d_min_p)
  579. *d_min_p = d_min;
  580. return d_max;
  581. }
  582. EXPORT_SYMBOL(acpi_pm_device_sleep_state);
  583. /**
  584. * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
  585. * @context: Device wakeup context.
  586. */
  587. static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
  588. {
  589. struct device *dev = context->dev;
  590. if (dev) {
  591. pm_wakeup_event(dev, 0);
  592. pm_request_resume(dev);
  593. }
  594. }
  595. static DEFINE_MUTEX(acpi_wakeup_lock);
  596. static int __acpi_device_wakeup_enable(struct acpi_device *adev,
  597. u32 target_state, int max_count)
  598. {
  599. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  600. acpi_status status;
  601. int error = 0;
  602. mutex_lock(&acpi_wakeup_lock);
  603. if (wakeup->enable_count >= max_count)
  604. goto out;
  605. if (wakeup->enable_count > 0)
  606. goto inc;
  607. error = acpi_enable_wakeup_device_power(adev, target_state);
  608. if (error)
  609. goto out;
  610. status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  611. if (ACPI_FAILURE(status)) {
  612. acpi_disable_wakeup_device_power(adev);
  613. error = -EIO;
  614. goto out;
  615. }
  616. inc:
  617. wakeup->enable_count++;
  618. out:
  619. mutex_unlock(&acpi_wakeup_lock);
  620. return error;
  621. }
  622. /**
  623. * acpi_device_wakeup_enable - Enable wakeup functionality for device.
  624. * @adev: ACPI device to enable wakeup functionality for.
  625. * @target_state: State the system is transitioning into.
  626. *
  627. * Enable the GPE associated with @adev so that it can generate wakeup signals
  628. * for the device in response to external (remote) events and enable wakeup
  629. * power for it.
  630. *
  631. * Callers must ensure that @adev is a valid ACPI device node before executing
  632. * this function.
  633. */
  634. static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
  635. {
  636. return __acpi_device_wakeup_enable(adev, target_state, 1);
  637. }
  638. /**
  639. * acpi_device_wakeup_disable - Disable wakeup functionality for device.
  640. * @adev: ACPI device to disable wakeup functionality for.
  641. *
  642. * Disable the GPE associated with @adev and disable wakeup power for it.
  643. *
  644. * Callers must ensure that @adev is a valid ACPI device node before executing
  645. * this function.
  646. */
  647. static void acpi_device_wakeup_disable(struct acpi_device *adev)
  648. {
  649. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  650. mutex_lock(&acpi_wakeup_lock);
  651. if (!wakeup->enable_count)
  652. goto out;
  653. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  654. acpi_disable_wakeup_device_power(adev);
  655. wakeup->enable_count--;
  656. out:
  657. mutex_unlock(&acpi_wakeup_lock);
  658. }
  659. static int __acpi_pm_set_device_wakeup(struct device *dev, bool enable,
  660. int max_count)
  661. {
  662. struct acpi_device *adev;
  663. int error;
  664. adev = ACPI_COMPANION(dev);
  665. if (!adev) {
  666. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  667. return -ENODEV;
  668. }
  669. if (!acpi_device_can_wakeup(adev))
  670. return -EINVAL;
  671. if (!enable) {
  672. acpi_device_wakeup_disable(adev);
  673. dev_dbg(dev, "Wakeup disabled by ACPI\n");
  674. return 0;
  675. }
  676. error = __acpi_device_wakeup_enable(adev, acpi_target_system_state(),
  677. max_count);
  678. if (!error)
  679. dev_dbg(dev, "Wakeup enabled by ACPI\n");
  680. return error;
  681. }
  682. /**
  683. * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.
  684. * @dev: Device to enable/disable to generate wakeup events.
  685. * @enable: Whether to enable or disable the wakeup functionality.
  686. */
  687. int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
  688. {
  689. return __acpi_pm_set_device_wakeup(dev, enable, 1);
  690. }
  691. EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
  692. /**
  693. * acpi_pm_set_bridge_wakeup - Enable/disable remote wakeup for given bridge.
  694. * @dev: Bridge device to enable/disable to generate wakeup events.
  695. * @enable: Whether to enable or disable the wakeup functionality.
  696. */
  697. int acpi_pm_set_bridge_wakeup(struct device *dev, bool enable)
  698. {
  699. return __acpi_pm_set_device_wakeup(dev, enable, INT_MAX);
  700. }
  701. EXPORT_SYMBOL_GPL(acpi_pm_set_bridge_wakeup);
  702. /**
  703. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  704. * @dev: Device to put into a low-power state.
  705. * @adev: ACPI device node corresponding to @dev.
  706. * @system_state: System state to choose the device state for.
  707. */
  708. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  709. u32 system_state)
  710. {
  711. int ret, state;
  712. if (!acpi_device_power_manageable(adev))
  713. return 0;
  714. ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
  715. return ret ? ret : acpi_device_set_power(adev, state);
  716. }
  717. /**
  718. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  719. * @adev: ACPI device node to put into the full-power state.
  720. */
  721. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  722. {
  723. return acpi_device_power_manageable(adev) ?
  724. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  725. }
  726. /**
  727. * acpi_dev_suspend - Put device into a low-power state using ACPI.
  728. * @dev: Device to put into a low-power state.
  729. * @wakeup: Whether or not to enable wakeup for the device.
  730. *
  731. * Put the given device into a low-power state using the standard ACPI
  732. * mechanism. Set up remote wakeup if desired, choose the state to put the
  733. * device into (this checks if remote wakeup is expected to work too), and set
  734. * the power state of the device.
  735. */
  736. int acpi_dev_suspend(struct device *dev, bool wakeup)
  737. {
  738. struct acpi_device *adev = ACPI_COMPANION(dev);
  739. u32 target_state = acpi_target_system_state();
  740. int error;
  741. if (!adev)
  742. return 0;
  743. if (wakeup && acpi_device_can_wakeup(adev)) {
  744. error = acpi_device_wakeup_enable(adev, target_state);
  745. if (error)
  746. return -EAGAIN;
  747. } else {
  748. wakeup = false;
  749. }
  750. error = acpi_dev_pm_low_power(dev, adev, target_state);
  751. if (error && wakeup)
  752. acpi_device_wakeup_disable(adev);
  753. return error;
  754. }
  755. EXPORT_SYMBOL_GPL(acpi_dev_suspend);
  756. /**
  757. * acpi_dev_resume - Put device into the full-power state using ACPI.
  758. * @dev: Device to put into the full-power state.
  759. *
  760. * Put the given device into the full-power state using the standard ACPI
  761. * mechanism. Set the power state of the device to ACPI D0 and disable wakeup.
  762. */
  763. int acpi_dev_resume(struct device *dev)
  764. {
  765. struct acpi_device *adev = ACPI_COMPANION(dev);
  766. int error;
  767. if (!adev)
  768. return 0;
  769. error = acpi_dev_pm_full_power(adev);
  770. acpi_device_wakeup_disable(adev);
  771. return error;
  772. }
  773. EXPORT_SYMBOL_GPL(acpi_dev_resume);
  774. /**
  775. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  776. * @dev: Device to suspend.
  777. *
  778. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  779. * it into a runtime low-power state.
  780. */
  781. int acpi_subsys_runtime_suspend(struct device *dev)
  782. {
  783. int ret = pm_generic_runtime_suspend(dev);
  784. return ret ? ret : acpi_dev_suspend(dev, true);
  785. }
  786. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  787. /**
  788. * acpi_subsys_runtime_resume - Resume device using ACPI.
  789. * @dev: Device to Resume.
  790. *
  791. * Use ACPI to put the given device into the full-power state and carry out the
  792. * generic runtime resume procedure for it.
  793. */
  794. int acpi_subsys_runtime_resume(struct device *dev)
  795. {
  796. int ret = acpi_dev_resume(dev);
  797. return ret ? ret : pm_generic_runtime_resume(dev);
  798. }
  799. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  800. #ifdef CONFIG_PM_SLEEP
  801. static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev)
  802. {
  803. u32 sys_target = acpi_target_system_state();
  804. int ret, state;
  805. if (!pm_runtime_suspended(dev) || !adev ||
  806. device_may_wakeup(dev) != !!adev->wakeup.prepare_count)
  807. return true;
  808. if (sys_target == ACPI_STATE_S0)
  809. return false;
  810. if (adev->power.flags.dsw_present)
  811. return true;
  812. ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
  813. if (ret)
  814. return true;
  815. return state != adev->power.state;
  816. }
  817. /**
  818. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  819. * @dev: Device to prepare.
  820. */
  821. int acpi_subsys_prepare(struct device *dev)
  822. {
  823. struct acpi_device *adev = ACPI_COMPANION(dev);
  824. if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {
  825. int ret = dev->driver->pm->prepare(dev);
  826. if (ret < 0)
  827. return ret;
  828. if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
  829. return 0;
  830. }
  831. return !acpi_dev_needs_resume(dev, adev);
  832. }
  833. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  834. /**
  835. * acpi_subsys_complete - Finalize device's resume during system resume.
  836. * @dev: Device to handle.
  837. */
  838. void acpi_subsys_complete(struct device *dev)
  839. {
  840. pm_generic_complete(dev);
  841. /*
  842. * If the device had been runtime-suspended before the system went into
  843. * the sleep state it is going out of and it has never been resumed till
  844. * now, resume it in case the firmware powered it up.
  845. */
  846. if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
  847. pm_request_resume(dev);
  848. }
  849. EXPORT_SYMBOL_GPL(acpi_subsys_complete);
  850. /**
  851. * acpi_subsys_suspend - Run the device driver's suspend callback.
  852. * @dev: Device to handle.
  853. *
  854. * Follow PCI and resume devices from runtime suspend before running their
  855. * system suspend callbacks, unless the driver can cope with runtime-suspended
  856. * devices during system suspend and there are no ACPI-specific reasons for
  857. * resuming them.
  858. */
  859. int acpi_subsys_suspend(struct device *dev)
  860. {
  861. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
  862. acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
  863. pm_runtime_resume(dev);
  864. return pm_generic_suspend(dev);
  865. }
  866. EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
  867. /**
  868. * acpi_subsys_suspend_late - Suspend device using ACPI.
  869. * @dev: Device to suspend.
  870. *
  871. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  872. * it into a low-power state during system transition into a sleep state.
  873. */
  874. int acpi_subsys_suspend_late(struct device *dev)
  875. {
  876. int ret;
  877. if (dev_pm_smart_suspend_and_suspended(dev))
  878. return 0;
  879. ret = pm_generic_suspend_late(dev);
  880. return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));
  881. }
  882. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  883. /**
  884. * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.
  885. * @dev: Device to suspend.
  886. */
  887. int acpi_subsys_suspend_noirq(struct device *dev)
  888. {
  889. int ret;
  890. if (dev_pm_smart_suspend_and_suspended(dev)) {
  891. dev->power.may_skip_resume = true;
  892. return 0;
  893. }
  894. ret = pm_generic_suspend_noirq(dev);
  895. if (ret)
  896. return ret;
  897. /*
  898. * If the target system sleep state is suspend-to-idle, it is sufficient
  899. * to check whether or not the device's wakeup settings are good for
  900. * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause
  901. * acpi_subsys_complete() to take care of fixing up the device's state
  902. * anyway, if need be.
  903. */
  904. dev->power.may_skip_resume = device_may_wakeup(dev) ||
  905. !device_can_wakeup(dev);
  906. return 0;
  907. }
  908. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq);
  909. /**
  910. * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback.
  911. * @dev: Device to handle.
  912. */
  913. int acpi_subsys_resume_noirq(struct device *dev)
  914. {
  915. if (dev_pm_may_skip_resume(dev))
  916. return 0;
  917. /*
  918. * Devices with DPM_FLAG_SMART_SUSPEND may be left in runtime suspend
  919. * during system suspend, so update their runtime PM status to "active"
  920. * as they will be put into D0 going forward.
  921. */
  922. if (dev_pm_smart_suspend_and_suspended(dev))
  923. pm_runtime_set_active(dev);
  924. return pm_generic_resume_noirq(dev);
  925. }
  926. EXPORT_SYMBOL_GPL(acpi_subsys_resume_noirq);
  927. /**
  928. * acpi_subsys_resume_early - Resume device using ACPI.
  929. * @dev: Device to Resume.
  930. *
  931. * Use ACPI to put the given device into the full-power state and carry out the
  932. * generic early resume procedure for it during system transition into the
  933. * working state.
  934. */
  935. int acpi_subsys_resume_early(struct device *dev)
  936. {
  937. int ret = acpi_dev_resume(dev);
  938. return ret ? ret : pm_generic_resume_early(dev);
  939. }
  940. EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
  941. /**
  942. * acpi_subsys_freeze - Run the device driver's freeze callback.
  943. * @dev: Device to handle.
  944. */
  945. int acpi_subsys_freeze(struct device *dev)
  946. {
  947. /*
  948. * This used to be done in acpi_subsys_prepare() for all devices and
  949. * some drivers may depend on it, so do it here. Ideally, however,
  950. * runtime-suspended devices should not be touched during freeze/thaw
  951. * transitions.
  952. */
  953. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND))
  954. pm_runtime_resume(dev);
  955. return pm_generic_freeze(dev);
  956. }
  957. EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
  958. /**
  959. * acpi_subsys_freeze_late - Run the device driver's "late" freeze callback.
  960. * @dev: Device to handle.
  961. */
  962. int acpi_subsys_freeze_late(struct device *dev)
  963. {
  964. if (dev_pm_smart_suspend_and_suspended(dev))
  965. return 0;
  966. return pm_generic_freeze_late(dev);
  967. }
  968. EXPORT_SYMBOL_GPL(acpi_subsys_freeze_late);
  969. /**
  970. * acpi_subsys_freeze_noirq - Run the device driver's "noirq" freeze callback.
  971. * @dev: Device to handle.
  972. */
  973. int acpi_subsys_freeze_noirq(struct device *dev)
  974. {
  975. if (dev_pm_smart_suspend_and_suspended(dev))
  976. return 0;
  977. return pm_generic_freeze_noirq(dev);
  978. }
  979. EXPORT_SYMBOL_GPL(acpi_subsys_freeze_noirq);
  980. /**
  981. * acpi_subsys_thaw_noirq - Run the device driver's "noirq" thaw callback.
  982. * @dev: Device to handle.
  983. */
  984. int acpi_subsys_thaw_noirq(struct device *dev)
  985. {
  986. /*
  987. * If the device is in runtime suspend, the "thaw" code may not work
  988. * correctly with it, so skip the driver callback and make the PM core
  989. * skip all of the subsequent "thaw" callbacks for the device.
  990. */
  991. if (dev_pm_smart_suspend_and_suspended(dev)) {
  992. dev_pm_skip_next_resume_phases(dev);
  993. return 0;
  994. }
  995. return pm_generic_thaw_noirq(dev);
  996. }
  997. EXPORT_SYMBOL_GPL(acpi_subsys_thaw_noirq);
  998. #endif /* CONFIG_PM_SLEEP */
  999. static struct dev_pm_domain acpi_general_pm_domain = {
  1000. .ops = {
  1001. .runtime_suspend = acpi_subsys_runtime_suspend,
  1002. .runtime_resume = acpi_subsys_runtime_resume,
  1003. #ifdef CONFIG_PM_SLEEP
  1004. .prepare = acpi_subsys_prepare,
  1005. .complete = acpi_subsys_complete,
  1006. .suspend = acpi_subsys_suspend,
  1007. .suspend_late = acpi_subsys_suspend_late,
  1008. .suspend_noirq = acpi_subsys_suspend_noirq,
  1009. .resume_noirq = acpi_subsys_resume_noirq,
  1010. .resume_early = acpi_subsys_resume_early,
  1011. .freeze = acpi_subsys_freeze,
  1012. .freeze_late = acpi_subsys_freeze_late,
  1013. .freeze_noirq = acpi_subsys_freeze_noirq,
  1014. .thaw_noirq = acpi_subsys_thaw_noirq,
  1015. .poweroff = acpi_subsys_suspend,
  1016. .poweroff_late = acpi_subsys_suspend_late,
  1017. .poweroff_noirq = acpi_subsys_suspend_noirq,
  1018. .restore_noirq = acpi_subsys_resume_noirq,
  1019. .restore_early = acpi_subsys_resume_early,
  1020. #endif
  1021. },
  1022. };
  1023. /**
  1024. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  1025. * @dev: Device to take care of.
  1026. * @power_off: Whether or not to try to remove power from the device.
  1027. *
  1028. * Remove the device from the general ACPI PM domain and remove its wakeup
  1029. * notifier. If @power_off is set, additionally remove power from the device if
  1030. * possible.
  1031. *
  1032. * Callers must ensure proper synchronization of this function with power
  1033. * management callbacks.
  1034. */
  1035. static void acpi_dev_pm_detach(struct device *dev, bool power_off)
  1036. {
  1037. struct acpi_device *adev = ACPI_COMPANION(dev);
  1038. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  1039. dev_pm_domain_set(dev, NULL);
  1040. acpi_remove_pm_notifier(adev);
  1041. if (power_off) {
  1042. /*
  1043. * If the device's PM QoS resume latency limit or flags
  1044. * have been exposed to user space, they have to be
  1045. * hidden at this point, so that they don't affect the
  1046. * choice of the low-power state to put the device into.
  1047. */
  1048. dev_pm_qos_hide_latency_limit(dev);
  1049. dev_pm_qos_hide_flags(dev);
  1050. acpi_device_wakeup_disable(adev);
  1051. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  1052. }
  1053. }
  1054. }
  1055. /**
  1056. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  1057. * @dev: Device to prepare.
  1058. * @power_on: Whether or not to power on the device.
  1059. *
  1060. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  1061. * attached to it, install a wakeup notification handler for the device and
  1062. * add it to the general ACPI PM domain. If @power_on is set, the device will
  1063. * be put into the ACPI D0 state before the function returns.
  1064. *
  1065. * This assumes that the @dev's bus type uses generic power management callbacks
  1066. * (or doesn't use any power management callbacks at all).
  1067. *
  1068. * Callers must ensure proper synchronization of this function with power
  1069. * management callbacks.
  1070. */
  1071. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  1072. {
  1073. struct acpi_device *adev = ACPI_COMPANION(dev);
  1074. if (!adev)
  1075. return -ENODEV;
  1076. if (dev->pm_domain)
  1077. return -EEXIST;
  1078. /*
  1079. * Only attach the power domain to the first device if the
  1080. * companion is shared by multiple. This is to prevent doing power
  1081. * management twice.
  1082. */
  1083. if (!acpi_device_is_first_physical_node(adev, dev))
  1084. return -EBUSY;
  1085. acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
  1086. dev_pm_domain_set(dev, &acpi_general_pm_domain);
  1087. if (power_on) {
  1088. acpi_dev_pm_full_power(adev);
  1089. acpi_device_wakeup_disable(adev);
  1090. }
  1091. dev->pm_domain->detach = acpi_dev_pm_detach;
  1092. return 0;
  1093. }
  1094. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  1095. #endif /* CONFIG_PM */