device_pm.c 29 KB

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