device_pm.c 29 KB

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