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