device_pm.c 28 KB

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