device_pm.c 35 KB

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