device_pm.c 28 KB

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