device_pm.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  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. bool has_sxd = false;
  470. acpi_status status;
  471. /*
  472. * If the system state is S0, the lowest power state the device can be
  473. * in is D3cold, unless the device has _S0W and is supposed to signal
  474. * wakeup, in which case the return value of _S0W has to be used as the
  475. * lowest power state available to the device.
  476. */
  477. d_min = ACPI_STATE_D0;
  478. d_max = ACPI_STATE_D3_COLD;
  479. /*
  480. * If present, _SxD methods return the minimum D-state (highest power
  481. * state) we can use for the corresponding S-states. Otherwise, the
  482. * minimum D-state is D0 (ACPI 3.x).
  483. */
  484. if (target_state > ACPI_STATE_S0) {
  485. /*
  486. * We rely on acpi_evaluate_integer() not clobbering the integer
  487. * provided if AE_NOT_FOUND is returned.
  488. */
  489. ret = d_min;
  490. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  491. if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND)
  492. || ret > ACPI_STATE_D3_COLD)
  493. return -ENODATA;
  494. /*
  495. * We need to handle legacy systems where D3hot and D3cold are
  496. * the same and 3 is returned in both cases, so fall back to
  497. * D3cold if D3hot is not a valid state.
  498. */
  499. if (!adev->power.states[ret].flags.valid) {
  500. if (ret == ACPI_STATE_D3_HOT)
  501. ret = ACPI_STATE_D3_COLD;
  502. else
  503. return -ENODATA;
  504. }
  505. if (status == AE_OK)
  506. has_sxd = true;
  507. d_min = ret;
  508. wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid
  509. && adev->wakeup.sleep_state >= target_state;
  510. } else {
  511. wakeup = adev->wakeup.flags.valid;
  512. }
  513. /*
  514. * If _PRW says we can wake up the system from the target sleep state,
  515. * the D-state returned by _SxD is sufficient for that (we assume a
  516. * wakeup-aware driver if wake is set). Still, if _SxW exists
  517. * (ACPI 3.x), it should return the maximum (lowest power) D-state that
  518. * can wake the system. _S0W may be valid, too.
  519. */
  520. if (wakeup) {
  521. method[3] = 'W';
  522. status = acpi_evaluate_integer(handle, method, NULL, &ret);
  523. if (status == AE_NOT_FOUND) {
  524. /* No _SxW. In this case, the ACPI spec says that we
  525. * must not go into any power state deeper than the
  526. * value returned from _SxD.
  527. */
  528. if (has_sxd && target_state > ACPI_STATE_S0)
  529. d_max = d_min;
  530. } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) {
  531. /* Fall back to D3cold if ret is not a valid state. */
  532. if (!adev->power.states[ret].flags.valid)
  533. ret = ACPI_STATE_D3_COLD;
  534. d_max = ret > d_min ? ret : d_min;
  535. } else {
  536. return -ENODATA;
  537. }
  538. }
  539. if (d_min_p)
  540. *d_min_p = d_min;
  541. if (d_max_p)
  542. *d_max_p = d_max;
  543. return 0;
  544. }
  545. /**
  546. * acpi_pm_device_sleep_state - Get preferred power state of ACPI device.
  547. * @dev: Device whose preferred target power state to return.
  548. * @d_min_p: Location to store the upper limit of the allowed states range.
  549. * @d_max_in: Deepest low-power state to take into consideration.
  550. * Return value: Preferred power state of the device on success, -ENODEV
  551. * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is
  552. * incorrect, or -ENODATA on ACPI method failure.
  553. *
  554. * The caller must ensure that @dev is valid before using this function.
  555. */
  556. int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in)
  557. {
  558. struct acpi_device *adev;
  559. int ret, d_min, d_max;
  560. if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD)
  561. return -EINVAL;
  562. if (d_max_in > ACPI_STATE_D2) {
  563. enum pm_qos_flags_status stat;
  564. stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF);
  565. if (stat == PM_QOS_FLAGS_ALL)
  566. d_max_in = ACPI_STATE_D2;
  567. }
  568. adev = ACPI_COMPANION(dev);
  569. if (!adev) {
  570. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  571. return -ENODEV;
  572. }
  573. ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(),
  574. &d_min, &d_max);
  575. if (ret)
  576. return ret;
  577. if (d_max_in < d_min)
  578. return -EINVAL;
  579. if (d_max > d_max_in) {
  580. for (d_max = d_max_in; d_max > d_min; d_max--) {
  581. if (adev->power.states[d_max].flags.valid)
  582. break;
  583. }
  584. }
  585. if (d_min_p)
  586. *d_min_p = d_min;
  587. return d_max;
  588. }
  589. EXPORT_SYMBOL(acpi_pm_device_sleep_state);
  590. /**
  591. * acpi_pm_notify_work_func - ACPI devices wakeup notification work function.
  592. * @context: Device wakeup context.
  593. */
  594. static void acpi_pm_notify_work_func(struct acpi_device_wakeup_context *context)
  595. {
  596. struct device *dev = context->dev;
  597. if (dev) {
  598. pm_wakeup_event(dev, 0);
  599. pm_request_resume(dev);
  600. }
  601. }
  602. static DEFINE_MUTEX(acpi_wakeup_lock);
  603. static int __acpi_device_wakeup_enable(struct acpi_device *adev,
  604. u32 target_state, int max_count)
  605. {
  606. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  607. acpi_status status;
  608. int error = 0;
  609. mutex_lock(&acpi_wakeup_lock);
  610. if (wakeup->enable_count >= max_count)
  611. goto out;
  612. if (wakeup->enable_count > 0)
  613. goto inc;
  614. error = acpi_enable_wakeup_device_power(adev, target_state);
  615. if (error)
  616. goto out;
  617. status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  618. if (ACPI_FAILURE(status)) {
  619. acpi_disable_wakeup_device_power(adev);
  620. error = -EIO;
  621. goto out;
  622. }
  623. inc:
  624. wakeup->enable_count++;
  625. out:
  626. mutex_unlock(&acpi_wakeup_lock);
  627. return error;
  628. }
  629. /**
  630. * acpi_device_wakeup_enable - Enable wakeup functionality for device.
  631. * @adev: ACPI device to enable wakeup functionality for.
  632. * @target_state: State the system is transitioning into.
  633. *
  634. * Enable the GPE associated with @adev so that it can generate wakeup signals
  635. * for the device in response to external (remote) events and enable wakeup
  636. * power for it.
  637. *
  638. * Callers must ensure that @adev is a valid ACPI device node before executing
  639. * this function.
  640. */
  641. static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
  642. {
  643. return __acpi_device_wakeup_enable(adev, target_state, 1);
  644. }
  645. /**
  646. * acpi_device_wakeup_disable - Disable wakeup functionality for device.
  647. * @adev: ACPI device to disable wakeup functionality for.
  648. *
  649. * Disable the GPE associated with @adev and disable wakeup power for it.
  650. *
  651. * Callers must ensure that @adev is a valid ACPI device node before executing
  652. * this function.
  653. */
  654. static void acpi_device_wakeup_disable(struct acpi_device *adev)
  655. {
  656. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  657. mutex_lock(&acpi_wakeup_lock);
  658. if (!wakeup->enable_count)
  659. goto out;
  660. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  661. acpi_disable_wakeup_device_power(adev);
  662. wakeup->enable_count--;
  663. out:
  664. mutex_unlock(&acpi_wakeup_lock);
  665. }
  666. static int __acpi_pm_set_device_wakeup(struct device *dev, bool enable,
  667. int max_count)
  668. {
  669. struct acpi_device *adev;
  670. int error;
  671. adev = ACPI_COMPANION(dev);
  672. if (!adev) {
  673. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  674. return -ENODEV;
  675. }
  676. if (!acpi_device_can_wakeup(adev))
  677. return -EINVAL;
  678. if (!enable) {
  679. acpi_device_wakeup_disable(adev);
  680. dev_dbg(dev, "Wakeup disabled by ACPI\n");
  681. return 0;
  682. }
  683. error = __acpi_device_wakeup_enable(adev, acpi_target_system_state(),
  684. max_count);
  685. if (!error)
  686. dev_dbg(dev, "Wakeup enabled by ACPI\n");
  687. return error;
  688. }
  689. /**
  690. * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.
  691. * @dev: Device to enable/disable to generate wakeup events.
  692. * @enable: Whether to enable or disable the wakeup functionality.
  693. */
  694. int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
  695. {
  696. return __acpi_pm_set_device_wakeup(dev, enable, 1);
  697. }
  698. EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
  699. /**
  700. * acpi_pm_set_bridge_wakeup - Enable/disable remote wakeup for given bridge.
  701. * @dev: Bridge device to enable/disable to generate wakeup events.
  702. * @enable: Whether to enable or disable the wakeup functionality.
  703. */
  704. int acpi_pm_set_bridge_wakeup(struct device *dev, bool enable)
  705. {
  706. return __acpi_pm_set_device_wakeup(dev, enable, INT_MAX);
  707. }
  708. EXPORT_SYMBOL_GPL(acpi_pm_set_bridge_wakeup);
  709. /**
  710. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  711. * @dev: Device to put into a low-power state.
  712. * @adev: ACPI device node corresponding to @dev.
  713. * @system_state: System state to choose the device state for.
  714. */
  715. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  716. u32 system_state)
  717. {
  718. int ret, state;
  719. if (!acpi_device_power_manageable(adev))
  720. return 0;
  721. ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
  722. return ret ? ret : acpi_device_set_power(adev, state);
  723. }
  724. /**
  725. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  726. * @adev: ACPI device node to put into the full-power state.
  727. */
  728. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  729. {
  730. return acpi_device_power_manageable(adev) ?
  731. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  732. }
  733. /**
  734. * acpi_dev_suspend - Put device into a low-power state using ACPI.
  735. * @dev: Device to put into a low-power state.
  736. * @wakeup: Whether or not to enable wakeup for the device.
  737. *
  738. * Put the given device into a low-power state using the standard ACPI
  739. * mechanism. Set up remote wakeup if desired, choose the state to put the
  740. * device into (this checks if remote wakeup is expected to work too), and set
  741. * the power state of the device.
  742. */
  743. int acpi_dev_suspend(struct device *dev, bool wakeup)
  744. {
  745. struct acpi_device *adev = ACPI_COMPANION(dev);
  746. u32 target_state = acpi_target_system_state();
  747. int error;
  748. if (!adev)
  749. return 0;
  750. if (wakeup && acpi_device_can_wakeup(adev)) {
  751. error = acpi_device_wakeup_enable(adev, target_state);
  752. if (error)
  753. return -EAGAIN;
  754. } else {
  755. wakeup = false;
  756. }
  757. error = acpi_dev_pm_low_power(dev, adev, target_state);
  758. if (error && wakeup)
  759. acpi_device_wakeup_disable(adev);
  760. return error;
  761. }
  762. EXPORT_SYMBOL_GPL(acpi_dev_suspend);
  763. /**
  764. * acpi_dev_resume - Put device into the full-power state using ACPI.
  765. * @dev: Device to put into the full-power state.
  766. *
  767. * Put the given device into the full-power state using the standard ACPI
  768. * mechanism. Set the power state of the device to ACPI D0 and disable wakeup.
  769. */
  770. int acpi_dev_resume(struct device *dev)
  771. {
  772. struct acpi_device *adev = ACPI_COMPANION(dev);
  773. int error;
  774. if (!adev)
  775. return 0;
  776. error = acpi_dev_pm_full_power(adev);
  777. acpi_device_wakeup_disable(adev);
  778. return error;
  779. }
  780. EXPORT_SYMBOL_GPL(acpi_dev_resume);
  781. /**
  782. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  783. * @dev: Device to suspend.
  784. *
  785. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  786. * it into a runtime low-power state.
  787. */
  788. int acpi_subsys_runtime_suspend(struct device *dev)
  789. {
  790. int ret = pm_generic_runtime_suspend(dev);
  791. return ret ? ret : acpi_dev_suspend(dev, true);
  792. }
  793. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  794. /**
  795. * acpi_subsys_runtime_resume - Resume device using ACPI.
  796. * @dev: Device to Resume.
  797. *
  798. * Use ACPI to put the given device into the full-power state and carry out the
  799. * generic runtime resume procedure for it.
  800. */
  801. int acpi_subsys_runtime_resume(struct device *dev)
  802. {
  803. int ret = acpi_dev_resume(dev);
  804. return ret ? ret : pm_generic_runtime_resume(dev);
  805. }
  806. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  807. #ifdef CONFIG_PM_SLEEP
  808. static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev)
  809. {
  810. u32 sys_target = acpi_target_system_state();
  811. int ret, state;
  812. if (!pm_runtime_suspended(dev) || !adev ||
  813. device_may_wakeup(dev) != !!adev->wakeup.prepare_count)
  814. return true;
  815. if (sys_target == ACPI_STATE_S0)
  816. return false;
  817. if (adev->power.flags.dsw_present)
  818. return true;
  819. ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
  820. if (ret)
  821. return true;
  822. return state != adev->power.state;
  823. }
  824. /**
  825. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  826. * @dev: Device to prepare.
  827. */
  828. int acpi_subsys_prepare(struct device *dev)
  829. {
  830. struct acpi_device *adev = ACPI_COMPANION(dev);
  831. if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {
  832. int ret = dev->driver->pm->prepare(dev);
  833. if (ret < 0)
  834. return ret;
  835. if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
  836. return 0;
  837. }
  838. return !acpi_dev_needs_resume(dev, adev);
  839. }
  840. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  841. /**
  842. * acpi_subsys_complete - Finalize device's resume during system resume.
  843. * @dev: Device to handle.
  844. */
  845. void acpi_subsys_complete(struct device *dev)
  846. {
  847. pm_generic_complete(dev);
  848. /*
  849. * If the device had been runtime-suspended before the system went into
  850. * the sleep state it is going out of and it has never been resumed till
  851. * now, resume it in case the firmware powered it up.
  852. */
  853. if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
  854. pm_request_resume(dev);
  855. }
  856. EXPORT_SYMBOL_GPL(acpi_subsys_complete);
  857. /**
  858. * acpi_subsys_suspend - Run the device driver's suspend callback.
  859. * @dev: Device to handle.
  860. *
  861. * Follow PCI and resume devices from runtime suspend before running their
  862. * system suspend callbacks, unless the driver can cope with runtime-suspended
  863. * devices during system suspend and there are no ACPI-specific reasons for
  864. * resuming them.
  865. */
  866. int acpi_subsys_suspend(struct device *dev)
  867. {
  868. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
  869. acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
  870. pm_runtime_resume(dev);
  871. return pm_generic_suspend(dev);
  872. }
  873. EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
  874. /**
  875. * acpi_subsys_suspend_late - Suspend device using ACPI.
  876. * @dev: Device to suspend.
  877. *
  878. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  879. * it into a low-power state during system transition into a sleep state.
  880. */
  881. int acpi_subsys_suspend_late(struct device *dev)
  882. {
  883. int ret;
  884. if (dev_pm_smart_suspend_and_suspended(dev))
  885. return 0;
  886. ret = pm_generic_suspend_late(dev);
  887. return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));
  888. }
  889. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  890. /**
  891. * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.
  892. * @dev: Device to suspend.
  893. */
  894. int acpi_subsys_suspend_noirq(struct device *dev)
  895. {
  896. int ret;
  897. if (dev_pm_smart_suspend_and_suspended(dev)) {
  898. dev->power.may_skip_resume = true;
  899. return 0;
  900. }
  901. ret = pm_generic_suspend_noirq(dev);
  902. if (ret)
  903. return ret;
  904. /*
  905. * If the target system sleep state is suspend-to-idle, it is sufficient
  906. * to check whether or not the device's wakeup settings are good for
  907. * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause
  908. * acpi_subsys_complete() to take care of fixing up the device's state
  909. * anyway, if need be.
  910. */
  911. dev->power.may_skip_resume = device_may_wakeup(dev) ||
  912. !device_can_wakeup(dev);
  913. return 0;
  914. }
  915. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq);
  916. /**
  917. * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback.
  918. * @dev: Device to handle.
  919. */
  920. int acpi_subsys_resume_noirq(struct device *dev)
  921. {
  922. if (dev_pm_may_skip_resume(dev))
  923. return 0;
  924. /*
  925. * Devices with DPM_FLAG_SMART_SUSPEND may be left in runtime suspend
  926. * during system suspend, so update their runtime PM status to "active"
  927. * as they will be put into D0 going forward.
  928. */
  929. if (dev_pm_smart_suspend_and_suspended(dev))
  930. pm_runtime_set_active(dev);
  931. return pm_generic_resume_noirq(dev);
  932. }
  933. EXPORT_SYMBOL_GPL(acpi_subsys_resume_noirq);
  934. /**
  935. * acpi_subsys_resume_early - Resume device using ACPI.
  936. * @dev: Device to Resume.
  937. *
  938. * Use ACPI to put the given device into the full-power state and carry out the
  939. * generic early resume procedure for it during system transition into the
  940. * working state.
  941. */
  942. int acpi_subsys_resume_early(struct device *dev)
  943. {
  944. int ret = acpi_dev_resume(dev);
  945. return ret ? ret : pm_generic_resume_early(dev);
  946. }
  947. EXPORT_SYMBOL_GPL(acpi_subsys_resume_early);
  948. /**
  949. * acpi_subsys_freeze - Run the device driver's freeze callback.
  950. * @dev: Device to handle.
  951. */
  952. int acpi_subsys_freeze(struct device *dev)
  953. {
  954. /*
  955. * This used to be done in acpi_subsys_prepare() for all devices and
  956. * some drivers may depend on it, so do it here. Ideally, however,
  957. * runtime-suspended devices should not be touched during freeze/thaw
  958. * transitions.
  959. */
  960. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND))
  961. pm_runtime_resume(dev);
  962. return pm_generic_freeze(dev);
  963. }
  964. EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
  965. /**
  966. * acpi_subsys_freeze_late - Run the device driver's "late" freeze callback.
  967. * @dev: Device to handle.
  968. */
  969. int acpi_subsys_freeze_late(struct device *dev)
  970. {
  971. if (dev_pm_smart_suspend_and_suspended(dev))
  972. return 0;
  973. return pm_generic_freeze_late(dev);
  974. }
  975. EXPORT_SYMBOL_GPL(acpi_subsys_freeze_late);
  976. /**
  977. * acpi_subsys_freeze_noirq - Run the device driver's "noirq" freeze callback.
  978. * @dev: Device to handle.
  979. */
  980. int acpi_subsys_freeze_noirq(struct device *dev)
  981. {
  982. if (dev_pm_smart_suspend_and_suspended(dev))
  983. return 0;
  984. return pm_generic_freeze_noirq(dev);
  985. }
  986. EXPORT_SYMBOL_GPL(acpi_subsys_freeze_noirq);
  987. /**
  988. * acpi_subsys_thaw_noirq - Run the device driver's "noirq" thaw callback.
  989. * @dev: Device to handle.
  990. */
  991. int acpi_subsys_thaw_noirq(struct device *dev)
  992. {
  993. /*
  994. * If the device is in runtime suspend, the "thaw" code may not work
  995. * correctly with it, so skip the driver callback and make the PM core
  996. * skip all of the subsequent "thaw" callbacks for the device.
  997. */
  998. if (dev_pm_smart_suspend_and_suspended(dev)) {
  999. dev_pm_skip_next_resume_phases(dev);
  1000. return 0;
  1001. }
  1002. return pm_generic_thaw_noirq(dev);
  1003. }
  1004. EXPORT_SYMBOL_GPL(acpi_subsys_thaw_noirq);
  1005. #endif /* CONFIG_PM_SLEEP */
  1006. static struct dev_pm_domain acpi_general_pm_domain = {
  1007. .ops = {
  1008. .runtime_suspend = acpi_subsys_runtime_suspend,
  1009. .runtime_resume = acpi_subsys_runtime_resume,
  1010. #ifdef CONFIG_PM_SLEEP
  1011. .prepare = acpi_subsys_prepare,
  1012. .complete = acpi_subsys_complete,
  1013. .suspend = acpi_subsys_suspend,
  1014. .suspend_late = acpi_subsys_suspend_late,
  1015. .suspend_noirq = acpi_subsys_suspend_noirq,
  1016. .resume_noirq = acpi_subsys_resume_noirq,
  1017. .resume_early = acpi_subsys_resume_early,
  1018. .freeze = acpi_subsys_freeze,
  1019. .freeze_late = acpi_subsys_freeze_late,
  1020. .freeze_noirq = acpi_subsys_freeze_noirq,
  1021. .thaw_noirq = acpi_subsys_thaw_noirq,
  1022. .poweroff = acpi_subsys_suspend,
  1023. .poweroff_late = acpi_subsys_suspend_late,
  1024. .poweroff_noirq = acpi_subsys_suspend_noirq,
  1025. .restore_noirq = acpi_subsys_resume_noirq,
  1026. .restore_early = acpi_subsys_resume_early,
  1027. #endif
  1028. },
  1029. };
  1030. /**
  1031. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  1032. * @dev: Device to take care of.
  1033. * @power_off: Whether or not to try to remove power from the device.
  1034. *
  1035. * Remove the device from the general ACPI PM domain and remove its wakeup
  1036. * notifier. If @power_off is set, additionally remove power from the device if
  1037. * possible.
  1038. *
  1039. * Callers must ensure proper synchronization of this function with power
  1040. * management callbacks.
  1041. */
  1042. static void acpi_dev_pm_detach(struct device *dev, bool power_off)
  1043. {
  1044. struct acpi_device *adev = ACPI_COMPANION(dev);
  1045. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  1046. dev_pm_domain_set(dev, NULL);
  1047. acpi_remove_pm_notifier(adev);
  1048. if (power_off) {
  1049. /*
  1050. * If the device's PM QoS resume latency limit or flags
  1051. * have been exposed to user space, they have to be
  1052. * hidden at this point, so that they don't affect the
  1053. * choice of the low-power state to put the device into.
  1054. */
  1055. dev_pm_qos_hide_latency_limit(dev);
  1056. dev_pm_qos_hide_flags(dev);
  1057. acpi_device_wakeup_disable(adev);
  1058. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  1059. }
  1060. }
  1061. }
  1062. /**
  1063. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  1064. * @dev: Device to prepare.
  1065. * @power_on: Whether or not to power on the device.
  1066. *
  1067. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  1068. * attached to it, install a wakeup notification handler for the device and
  1069. * add it to the general ACPI PM domain. If @power_on is set, the device will
  1070. * be put into the ACPI D0 state before the function returns.
  1071. *
  1072. * This assumes that the @dev's bus type uses generic power management callbacks
  1073. * (or doesn't use any power management callbacks at all).
  1074. *
  1075. * Callers must ensure proper synchronization of this function with power
  1076. * management callbacks.
  1077. */
  1078. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  1079. {
  1080. struct acpi_device *adev = ACPI_COMPANION(dev);
  1081. if (!adev)
  1082. return 0;
  1083. /*
  1084. * Only attach the power domain to the first device if the
  1085. * companion is shared by multiple. This is to prevent doing power
  1086. * management twice.
  1087. */
  1088. if (!acpi_device_is_first_physical_node(adev, dev))
  1089. return 0;
  1090. acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
  1091. dev_pm_domain_set(dev, &acpi_general_pm_domain);
  1092. if (power_on) {
  1093. acpi_dev_pm_full_power(adev);
  1094. acpi_device_wakeup_disable(adev);
  1095. }
  1096. dev->pm_domain->detach = acpi_dev_pm_detach;
  1097. return 1;
  1098. }
  1099. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  1100. #endif /* CONFIG_PM */