device_pm.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  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 D3cold is not supported, 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(target_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(target_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)
  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 >= INT_MAX) {
  611. acpi_handle_info(adev->handle, "Wakeup enable count out of bounds!\n");
  612. goto out;
  613. }
  614. if (wakeup->enable_count > 0)
  615. goto inc;
  616. error = acpi_enable_wakeup_device_power(adev, target_state);
  617. if (error)
  618. goto out;
  619. status = acpi_enable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  620. if (ACPI_FAILURE(status)) {
  621. acpi_disable_wakeup_device_power(adev);
  622. error = -EIO;
  623. goto out;
  624. }
  625. inc:
  626. wakeup->enable_count++;
  627. out:
  628. mutex_unlock(&acpi_wakeup_lock);
  629. return error;
  630. }
  631. /**
  632. * acpi_device_wakeup_enable - Enable wakeup functionality for device.
  633. * @adev: ACPI device to enable wakeup functionality for.
  634. * @target_state: State the system is transitioning into.
  635. *
  636. * Enable the GPE associated with @adev so that it can generate wakeup signals
  637. * for the device in response to external (remote) events and enable wakeup
  638. * power for it.
  639. *
  640. * Callers must ensure that @adev is a valid ACPI device node before executing
  641. * this function.
  642. */
  643. static int acpi_device_wakeup_enable(struct acpi_device *adev, u32 target_state)
  644. {
  645. return __acpi_device_wakeup_enable(adev, target_state);
  646. }
  647. /**
  648. * acpi_device_wakeup_disable - Disable wakeup functionality for device.
  649. * @adev: ACPI device to disable wakeup functionality for.
  650. *
  651. * Disable the GPE associated with @adev and disable wakeup power for it.
  652. *
  653. * Callers must ensure that @adev is a valid ACPI device node before executing
  654. * this function.
  655. */
  656. static void acpi_device_wakeup_disable(struct acpi_device *adev)
  657. {
  658. struct acpi_device_wakeup *wakeup = &adev->wakeup;
  659. mutex_lock(&acpi_wakeup_lock);
  660. if (!wakeup->enable_count)
  661. goto out;
  662. acpi_disable_gpe(wakeup->gpe_device, wakeup->gpe_number);
  663. acpi_disable_wakeup_device_power(adev);
  664. wakeup->enable_count--;
  665. out:
  666. mutex_unlock(&acpi_wakeup_lock);
  667. }
  668. /**
  669. * acpi_pm_set_device_wakeup - Enable/disable remote wakeup for given device.
  670. * @dev: Device to enable/disable to generate wakeup events.
  671. * @enable: Whether to enable or disable the wakeup functionality.
  672. */
  673. int acpi_pm_set_device_wakeup(struct device *dev, bool enable)
  674. {
  675. struct acpi_device *adev;
  676. int error;
  677. adev = ACPI_COMPANION(dev);
  678. if (!adev) {
  679. dev_dbg(dev, "ACPI companion missing in %s!\n", __func__);
  680. return -ENODEV;
  681. }
  682. if (!acpi_device_can_wakeup(adev))
  683. return -EINVAL;
  684. if (!enable) {
  685. acpi_device_wakeup_disable(adev);
  686. dev_dbg(dev, "Wakeup disabled by ACPI\n");
  687. return 0;
  688. }
  689. error = __acpi_device_wakeup_enable(adev, acpi_target_system_state());
  690. if (!error)
  691. dev_dbg(dev, "Wakeup enabled by ACPI\n");
  692. return error;
  693. }
  694. EXPORT_SYMBOL_GPL(acpi_pm_set_device_wakeup);
  695. /**
  696. * acpi_dev_pm_low_power - Put ACPI device into a low-power state.
  697. * @dev: Device to put into a low-power state.
  698. * @adev: ACPI device node corresponding to @dev.
  699. * @system_state: System state to choose the device state for.
  700. */
  701. static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev,
  702. u32 system_state)
  703. {
  704. int ret, state;
  705. if (!acpi_device_power_manageable(adev))
  706. return 0;
  707. ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state);
  708. return ret ? ret : acpi_device_set_power(adev, state);
  709. }
  710. /**
  711. * acpi_dev_pm_full_power - Put ACPI device into the full-power state.
  712. * @adev: ACPI device node to put into the full-power state.
  713. */
  714. static int acpi_dev_pm_full_power(struct acpi_device *adev)
  715. {
  716. return acpi_device_power_manageable(adev) ?
  717. acpi_device_set_power(adev, ACPI_STATE_D0) : 0;
  718. }
  719. /**
  720. * acpi_dev_suspend - Put device into a low-power state using ACPI.
  721. * @dev: Device to put into a low-power state.
  722. * @wakeup: Whether or not to enable wakeup for the device.
  723. *
  724. * Put the given device into a low-power state using the standard ACPI
  725. * mechanism. Set up remote wakeup if desired, choose the state to put the
  726. * device into (this checks if remote wakeup is expected to work too), and set
  727. * the power state of the device.
  728. */
  729. int acpi_dev_suspend(struct device *dev, bool wakeup)
  730. {
  731. struct acpi_device *adev = ACPI_COMPANION(dev);
  732. u32 target_state = acpi_target_system_state();
  733. int error;
  734. if (!adev)
  735. return 0;
  736. if (wakeup && acpi_device_can_wakeup(adev)) {
  737. error = acpi_device_wakeup_enable(adev, target_state);
  738. if (error)
  739. return -EAGAIN;
  740. } else {
  741. wakeup = false;
  742. }
  743. error = acpi_dev_pm_low_power(dev, adev, target_state);
  744. if (error && wakeup)
  745. acpi_device_wakeup_disable(adev);
  746. return error;
  747. }
  748. EXPORT_SYMBOL_GPL(acpi_dev_suspend);
  749. /**
  750. * acpi_dev_resume - Put device into the full-power state using ACPI.
  751. * @dev: Device to put into the full-power state.
  752. *
  753. * Put the given device into the full-power state using the standard ACPI
  754. * mechanism. Set the power state of the device to ACPI D0 and disable wakeup.
  755. */
  756. int acpi_dev_resume(struct device *dev)
  757. {
  758. struct acpi_device *adev = ACPI_COMPANION(dev);
  759. int error;
  760. if (!adev)
  761. return 0;
  762. error = acpi_dev_pm_full_power(adev);
  763. acpi_device_wakeup_disable(adev);
  764. return error;
  765. }
  766. EXPORT_SYMBOL_GPL(acpi_dev_resume);
  767. /**
  768. * acpi_subsys_runtime_suspend - Suspend device using ACPI.
  769. * @dev: Device to suspend.
  770. *
  771. * Carry out the generic runtime suspend procedure for @dev and use ACPI to put
  772. * it into a runtime low-power state.
  773. */
  774. int acpi_subsys_runtime_suspend(struct device *dev)
  775. {
  776. int ret = pm_generic_runtime_suspend(dev);
  777. return ret ? ret : acpi_dev_suspend(dev, true);
  778. }
  779. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_suspend);
  780. /**
  781. * acpi_subsys_runtime_resume - Resume device using ACPI.
  782. * @dev: Device to Resume.
  783. *
  784. * Use ACPI to put the given device into the full-power state and carry out the
  785. * generic runtime resume procedure for it.
  786. */
  787. int acpi_subsys_runtime_resume(struct device *dev)
  788. {
  789. int ret = acpi_dev_resume(dev);
  790. return ret ? ret : pm_generic_runtime_resume(dev);
  791. }
  792. EXPORT_SYMBOL_GPL(acpi_subsys_runtime_resume);
  793. #ifdef CONFIG_PM_SLEEP
  794. static bool acpi_dev_needs_resume(struct device *dev, struct acpi_device *adev)
  795. {
  796. u32 sys_target = acpi_target_system_state();
  797. int ret, state;
  798. if (!pm_runtime_suspended(dev) || !adev || (adev->wakeup.flags.valid &&
  799. device_may_wakeup(dev) != !!adev->wakeup.prepare_count))
  800. return true;
  801. if (sys_target == ACPI_STATE_S0)
  802. return false;
  803. if (adev->power.flags.dsw_present)
  804. return true;
  805. ret = acpi_dev_pm_get_state(dev, adev, sys_target, NULL, &state);
  806. if (ret)
  807. return true;
  808. return state != adev->power.state;
  809. }
  810. /**
  811. * acpi_subsys_prepare - Prepare device for system transition to a sleep state.
  812. * @dev: Device to prepare.
  813. */
  814. int acpi_subsys_prepare(struct device *dev)
  815. {
  816. struct acpi_device *adev = ACPI_COMPANION(dev);
  817. if (dev->driver && dev->driver->pm && dev->driver->pm->prepare) {
  818. int ret = dev->driver->pm->prepare(dev);
  819. if (ret < 0)
  820. return ret;
  821. if (!ret && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
  822. return 0;
  823. }
  824. return !acpi_dev_needs_resume(dev, adev);
  825. }
  826. EXPORT_SYMBOL_GPL(acpi_subsys_prepare);
  827. /**
  828. * acpi_subsys_complete - Finalize device's resume during system resume.
  829. * @dev: Device to handle.
  830. */
  831. void acpi_subsys_complete(struct device *dev)
  832. {
  833. pm_generic_complete(dev);
  834. /*
  835. * If the device had been runtime-suspended before the system went into
  836. * the sleep state it is going out of and it has never been resumed till
  837. * now, resume it in case the firmware powered it up.
  838. */
  839. if (pm_runtime_suspended(dev) && pm_resume_via_firmware())
  840. pm_request_resume(dev);
  841. }
  842. EXPORT_SYMBOL_GPL(acpi_subsys_complete);
  843. /**
  844. * acpi_subsys_suspend - Run the device driver's suspend callback.
  845. * @dev: Device to handle.
  846. *
  847. * Follow PCI and resume devices from runtime suspend before running their
  848. * system suspend callbacks, unless the driver can cope with runtime-suspended
  849. * devices during system suspend and there are no ACPI-specific reasons for
  850. * resuming them.
  851. */
  852. int acpi_subsys_suspend(struct device *dev)
  853. {
  854. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
  855. acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
  856. pm_runtime_resume(dev);
  857. return pm_generic_suspend(dev);
  858. }
  859. EXPORT_SYMBOL_GPL(acpi_subsys_suspend);
  860. /**
  861. * acpi_subsys_suspend_late - Suspend device using ACPI.
  862. * @dev: Device to suspend.
  863. *
  864. * Carry out the generic late suspend procedure for @dev and use ACPI to put
  865. * it into a low-power state during system transition into a sleep state.
  866. */
  867. int acpi_subsys_suspend_late(struct device *dev)
  868. {
  869. int ret;
  870. if (dev_pm_smart_suspend_and_suspended(dev))
  871. return 0;
  872. ret = pm_generic_suspend_late(dev);
  873. return ret ? ret : acpi_dev_suspend(dev, device_may_wakeup(dev));
  874. }
  875. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_late);
  876. /**
  877. * acpi_subsys_suspend_noirq - Run the device driver's "noirq" suspend callback.
  878. * @dev: Device to suspend.
  879. */
  880. int acpi_subsys_suspend_noirq(struct device *dev)
  881. {
  882. int ret;
  883. if (dev_pm_smart_suspend_and_suspended(dev)) {
  884. dev->power.may_skip_resume = true;
  885. return 0;
  886. }
  887. ret = pm_generic_suspend_noirq(dev);
  888. if (ret)
  889. return ret;
  890. /*
  891. * If the target system sleep state is suspend-to-idle, it is sufficient
  892. * to check whether or not the device's wakeup settings are good for
  893. * runtime PM. Otherwise, the pm_resume_via_firmware() check will cause
  894. * acpi_subsys_complete() to take care of fixing up the device's state
  895. * anyway, if need be.
  896. */
  897. dev->power.may_skip_resume = device_may_wakeup(dev) ||
  898. !device_can_wakeup(dev);
  899. return 0;
  900. }
  901. EXPORT_SYMBOL_GPL(acpi_subsys_suspend_noirq);
  902. /**
  903. * acpi_subsys_resume_noirq - Run the device driver's "noirq" resume callback.
  904. * @dev: Device to handle.
  905. */
  906. static int acpi_subsys_resume_noirq(struct device *dev)
  907. {
  908. if (dev_pm_may_skip_resume(dev))
  909. return 0;
  910. /*
  911. * Devices with DPM_FLAG_SMART_SUSPEND may be left in runtime suspend
  912. * during system suspend, so update their runtime PM status to "active"
  913. * as they will be put into D0 going forward.
  914. */
  915. if (dev_pm_smart_suspend_and_suspended(dev))
  916. pm_runtime_set_active(dev);
  917. return pm_generic_resume_noirq(dev);
  918. }
  919. /**
  920. * acpi_subsys_resume_early - Resume device using ACPI.
  921. * @dev: Device to Resume.
  922. *
  923. * Use ACPI to put the given device into the full-power state and carry out the
  924. * generic early resume procedure for it during system transition into the
  925. * working state.
  926. */
  927. static int acpi_subsys_resume_early(struct device *dev)
  928. {
  929. int ret = acpi_dev_resume(dev);
  930. return ret ? ret : pm_generic_resume_early(dev);
  931. }
  932. /**
  933. * acpi_subsys_freeze - Run the device driver's freeze callback.
  934. * @dev: Device to handle.
  935. */
  936. int acpi_subsys_freeze(struct device *dev)
  937. {
  938. /*
  939. * Resume all runtime-suspended devices before creating a snapshot
  940. * image of system memory, because the restore kernel generally cannot
  941. * be expected to always handle them consistently and they need to be
  942. * put into the runtime-active metastate during system resume anyway,
  943. * so it is better to ensure that the state saved in the image will be
  944. * always consistent with that.
  945. */
  946. pm_runtime_resume(dev);
  947. return pm_generic_freeze(dev);
  948. }
  949. EXPORT_SYMBOL_GPL(acpi_subsys_freeze);
  950. /**
  951. * acpi_subsys_restore_early - Restore device using ACPI.
  952. * @dev: Device to restore.
  953. */
  954. int acpi_subsys_restore_early(struct device *dev)
  955. {
  956. int ret = acpi_dev_resume(dev);
  957. return ret ? ret : pm_generic_restore_early(dev);
  958. }
  959. EXPORT_SYMBOL_GPL(acpi_subsys_restore_early);
  960. /**
  961. * acpi_subsys_poweroff - Run the device driver's poweroff callback.
  962. * @dev: Device to handle.
  963. *
  964. * Follow PCI and resume devices from runtime suspend before running their
  965. * system poweroff callbacks, unless the driver can cope with runtime-suspended
  966. * devices during system suspend and there are no ACPI-specific reasons for
  967. * resuming them.
  968. */
  969. int acpi_subsys_poweroff(struct device *dev)
  970. {
  971. if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
  972. acpi_dev_needs_resume(dev, ACPI_COMPANION(dev)))
  973. pm_runtime_resume(dev);
  974. return pm_generic_poweroff(dev);
  975. }
  976. EXPORT_SYMBOL_GPL(acpi_subsys_poweroff);
  977. /**
  978. * acpi_subsys_poweroff_late - Run the device driver's poweroff callback.
  979. * @dev: Device to handle.
  980. *
  981. * Carry out the generic late poweroff procedure for @dev and use ACPI to put
  982. * it into a low-power state during system transition into a sleep state.
  983. */
  984. static int acpi_subsys_poweroff_late(struct device *dev)
  985. {
  986. int ret;
  987. if (dev_pm_smart_suspend_and_suspended(dev))
  988. return 0;
  989. ret = pm_generic_poweroff_late(dev);
  990. if (ret)
  991. return ret;
  992. return acpi_dev_suspend(dev, device_may_wakeup(dev));
  993. }
  994. /**
  995. * acpi_subsys_poweroff_noirq - Run the driver's "noirq" poweroff callback.
  996. * @dev: Device to suspend.
  997. */
  998. static int acpi_subsys_poweroff_noirq(struct device *dev)
  999. {
  1000. if (dev_pm_smart_suspend_and_suspended(dev))
  1001. return 0;
  1002. return pm_generic_poweroff_noirq(dev);
  1003. }
  1004. #endif /* CONFIG_PM_SLEEP */
  1005. static struct dev_pm_domain acpi_general_pm_domain = {
  1006. .ops = {
  1007. .runtime_suspend = acpi_subsys_runtime_suspend,
  1008. .runtime_resume = acpi_subsys_runtime_resume,
  1009. #ifdef CONFIG_PM_SLEEP
  1010. .prepare = acpi_subsys_prepare,
  1011. .complete = acpi_subsys_complete,
  1012. .suspend = acpi_subsys_suspend,
  1013. .suspend_late = acpi_subsys_suspend_late,
  1014. .suspend_noirq = acpi_subsys_suspend_noirq,
  1015. .resume_noirq = acpi_subsys_resume_noirq,
  1016. .resume_early = acpi_subsys_resume_early,
  1017. .freeze = acpi_subsys_freeze,
  1018. .poweroff = acpi_subsys_poweroff,
  1019. .poweroff_late = acpi_subsys_poweroff_late,
  1020. .poweroff_noirq = acpi_subsys_poweroff_noirq,
  1021. .restore_early = acpi_subsys_restore_early,
  1022. #endif
  1023. },
  1024. };
  1025. /**
  1026. * acpi_dev_pm_detach - Remove ACPI power management from the device.
  1027. * @dev: Device to take care of.
  1028. * @power_off: Whether or not to try to remove power from the device.
  1029. *
  1030. * Remove the device from the general ACPI PM domain and remove its wakeup
  1031. * notifier. If @power_off is set, additionally remove power from the device if
  1032. * possible.
  1033. *
  1034. * Callers must ensure proper synchronization of this function with power
  1035. * management callbacks.
  1036. */
  1037. static void acpi_dev_pm_detach(struct device *dev, bool power_off)
  1038. {
  1039. struct acpi_device *adev = ACPI_COMPANION(dev);
  1040. if (adev && dev->pm_domain == &acpi_general_pm_domain) {
  1041. dev_pm_domain_set(dev, NULL);
  1042. acpi_remove_pm_notifier(adev);
  1043. if (power_off) {
  1044. /*
  1045. * If the device's PM QoS resume latency limit or flags
  1046. * have been exposed to user space, they have to be
  1047. * hidden at this point, so that they don't affect the
  1048. * choice of the low-power state to put the device into.
  1049. */
  1050. dev_pm_qos_hide_latency_limit(dev);
  1051. dev_pm_qos_hide_flags(dev);
  1052. acpi_device_wakeup_disable(adev);
  1053. acpi_dev_pm_low_power(dev, adev, ACPI_STATE_S0);
  1054. }
  1055. }
  1056. }
  1057. /**
  1058. * acpi_dev_pm_attach - Prepare device for ACPI power management.
  1059. * @dev: Device to prepare.
  1060. * @power_on: Whether or not to power on the device.
  1061. *
  1062. * If @dev has a valid ACPI handle that has a valid struct acpi_device object
  1063. * attached to it, install a wakeup notification handler for the device and
  1064. * add it to the general ACPI PM domain. If @power_on is set, the device will
  1065. * be put into the ACPI D0 state before the function returns.
  1066. *
  1067. * This assumes that the @dev's bus type uses generic power management callbacks
  1068. * (or doesn't use any power management callbacks at all).
  1069. *
  1070. * Callers must ensure proper synchronization of this function with power
  1071. * management callbacks.
  1072. */
  1073. int acpi_dev_pm_attach(struct device *dev, bool power_on)
  1074. {
  1075. /*
  1076. * Skip devices whose ACPI companions match the device IDs below,
  1077. * because they require special power management handling incompatible
  1078. * with the generic ACPI PM domain.
  1079. */
  1080. static const struct acpi_device_id special_pm_ids[] = {
  1081. {"PNP0C0B", }, /* Generic ACPI fan */
  1082. {"INT3404", }, /* Fan */
  1083. {}
  1084. };
  1085. struct acpi_device *adev = ACPI_COMPANION(dev);
  1086. if (!adev || !acpi_match_device_ids(adev, special_pm_ids))
  1087. return 0;
  1088. /*
  1089. * Only attach the power domain to the first device if the
  1090. * companion is shared by multiple. This is to prevent doing power
  1091. * management twice.
  1092. */
  1093. if (!acpi_device_is_first_physical_node(adev, dev))
  1094. return 0;
  1095. acpi_add_pm_notifier(adev, dev, acpi_pm_notify_work_func);
  1096. dev_pm_domain_set(dev, &acpi_general_pm_domain);
  1097. if (power_on) {
  1098. acpi_dev_pm_full_power(adev);
  1099. acpi_device_wakeup_disable(adev);
  1100. }
  1101. dev->pm_domain->detach = acpi_dev_pm_detach;
  1102. return 1;
  1103. }
  1104. EXPORT_SYMBOL_GPL(acpi_dev_pm_attach);
  1105. #endif /* CONFIG_PM */