sysfs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  1. /*
  2. * drivers/base/power/sysfs.c - sysfs entries for device PM
  3. */
  4. #include <linux/device.h>
  5. #include <linux/string.h>
  6. #include <linux/export.h>
  7. #include <linux/pm_qos.h>
  8. #include <linux/pm_runtime.h>
  9. #include <linux/atomic.h>
  10. #include <linux/jiffies.h>
  11. #include "power.h"
  12. /*
  13. * control - Report/change current runtime PM setting of the device
  14. *
  15. * Runtime power management of a device can be blocked with the help of
  16. * this attribute. All devices have one of the following two values for
  17. * the power/control file:
  18. *
  19. * + "auto\n" to allow the device to be power managed at run time;
  20. * + "on\n" to prevent the device from being power managed at run time;
  21. *
  22. * The default for all devices is "auto", which means that devices may be
  23. * subject to automatic power management, depending on their drivers.
  24. * Changing this attribute to "on" prevents the driver from power managing
  25. * the device at run time. Doing that while the device is suspended causes
  26. * it to be woken up.
  27. *
  28. * wakeup - Report/change current wakeup option for device
  29. *
  30. * Some devices support "wakeup" events, which are hardware signals
  31. * used to activate devices from suspended or low power states. Such
  32. * devices have one of three values for the sysfs power/wakeup file:
  33. *
  34. * + "enabled\n" to issue the events;
  35. * + "disabled\n" not to do so; or
  36. * + "\n" for temporary or permanent inability to issue wakeup.
  37. *
  38. * (For example, unconfigured USB devices can't issue wakeups.)
  39. *
  40. * Familiar examples of devices that can issue wakeup events include
  41. * keyboards and mice (both PS2 and USB styles), power buttons, modems,
  42. * "Wake-On-LAN" Ethernet links, GPIO lines, and more. Some events
  43. * will wake the entire system from a suspend state; others may just
  44. * wake up the device (if the system as a whole is already active).
  45. * Some wakeup events use normal IRQ lines; other use special out
  46. * of band signaling.
  47. *
  48. * It is the responsibility of device drivers to enable (or disable)
  49. * wakeup signaling as part of changing device power states, respecting
  50. * the policy choices provided through the driver model.
  51. *
  52. * Devices may not be able to generate wakeup events from all power
  53. * states. Also, the events may be ignored in some configurations;
  54. * for example, they might need help from other devices that aren't
  55. * active, or which may have wakeup disabled. Some drivers rely on
  56. * wakeup events internally (unless they are disabled), keeping
  57. * their hardware in low power modes whenever they're unused. This
  58. * saves runtime power, without requiring system-wide sleep states.
  59. *
  60. * async - Report/change current async suspend setting for the device
  61. *
  62. * Asynchronous suspend and resume of the device during system-wide power
  63. * state transitions can be enabled by writing "enabled" to this file.
  64. * Analogously, if "disabled" is written to this file, the device will be
  65. * suspended and resumed synchronously.
  66. *
  67. * All devices have one of the following two values for power/async:
  68. *
  69. * + "enabled\n" to permit the asynchronous suspend/resume of the device;
  70. * + "disabled\n" to forbid it;
  71. *
  72. * NOTE: It generally is unsafe to permit the asynchronous suspend/resume
  73. * of a device unless it is certain that all of the PM dependencies of the
  74. * device are known to the PM core. However, for some devices this
  75. * attribute is set to "enabled" by bus type code or device drivers and in
  76. * that cases it should be safe to leave the default value.
  77. *
  78. * autosuspend_delay_ms - Report/change a device's autosuspend_delay value
  79. *
  80. * Some drivers don't want to carry out a runtime suspend as soon as a
  81. * device becomes idle; they want it always to remain idle for some period
  82. * of time before suspending it. This period is the autosuspend_delay
  83. * value (expressed in milliseconds) and it can be controlled by the user.
  84. * If the value is negative then the device will never be runtime
  85. * suspended.
  86. *
  87. * NOTE: The autosuspend_delay_ms attribute and the autosuspend_delay
  88. * value are used only if the driver calls pm_runtime_use_autosuspend().
  89. *
  90. * wakeup_count - Report the number of wakeup events related to the device
  91. */
  92. const char power_group_name[] = "power";
  93. EXPORT_SYMBOL_GPL(power_group_name);
  94. static const char ctrl_auto[] = "auto";
  95. static const char ctrl_on[] = "on";
  96. static ssize_t control_show(struct device *dev, struct device_attribute *attr,
  97. char *buf)
  98. {
  99. return sprintf(buf, "%s\n",
  100. dev->power.runtime_auto ? ctrl_auto : ctrl_on);
  101. }
  102. static ssize_t control_store(struct device * dev, struct device_attribute *attr,
  103. const char * buf, size_t n)
  104. {
  105. char *cp;
  106. int len = n;
  107. cp = memchr(buf, '\n', n);
  108. if (cp)
  109. len = cp - buf;
  110. device_lock(dev);
  111. if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
  112. pm_runtime_allow(dev);
  113. else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
  114. pm_runtime_forbid(dev);
  115. else
  116. n = -EINVAL;
  117. device_unlock(dev);
  118. return n;
  119. }
  120. static DEVICE_ATTR(control, 0644, control_show, control_store);
  121. static ssize_t rtpm_active_time_show(struct device *dev,
  122. struct device_attribute *attr, char *buf)
  123. {
  124. int ret;
  125. spin_lock_irq(&dev->power.lock);
  126. update_pm_runtime_accounting(dev);
  127. ret = sprintf(buf, "%i\n", jiffies_to_msecs(dev->power.active_jiffies));
  128. spin_unlock_irq(&dev->power.lock);
  129. return ret;
  130. }
  131. static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
  132. static ssize_t rtpm_suspended_time_show(struct device *dev,
  133. struct device_attribute *attr, char *buf)
  134. {
  135. int ret;
  136. spin_lock_irq(&dev->power.lock);
  137. update_pm_runtime_accounting(dev);
  138. ret = sprintf(buf, "%i\n",
  139. jiffies_to_msecs(dev->power.suspended_jiffies));
  140. spin_unlock_irq(&dev->power.lock);
  141. return ret;
  142. }
  143. static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
  144. static ssize_t rtpm_status_show(struct device *dev,
  145. struct device_attribute *attr, char *buf)
  146. {
  147. const char *p;
  148. if (dev->power.runtime_error) {
  149. p = "error\n";
  150. } else if (dev->power.disable_depth) {
  151. p = "unsupported\n";
  152. } else {
  153. switch (dev->power.runtime_status) {
  154. case RPM_SUSPENDED:
  155. p = "suspended\n";
  156. break;
  157. case RPM_SUSPENDING:
  158. p = "suspending\n";
  159. break;
  160. case RPM_RESUMING:
  161. p = "resuming\n";
  162. break;
  163. case RPM_ACTIVE:
  164. p = "active\n";
  165. break;
  166. default:
  167. return -EIO;
  168. }
  169. }
  170. return sprintf(buf, p);
  171. }
  172. static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
  173. static ssize_t autosuspend_delay_ms_show(struct device *dev,
  174. struct device_attribute *attr, char *buf)
  175. {
  176. if (!dev->power.use_autosuspend)
  177. return -EIO;
  178. return sprintf(buf, "%d\n", dev->power.autosuspend_delay);
  179. }
  180. static ssize_t autosuspend_delay_ms_store(struct device *dev,
  181. struct device_attribute *attr, const char *buf, size_t n)
  182. {
  183. long delay;
  184. if (!dev->power.use_autosuspend)
  185. return -EIO;
  186. if (kstrtol(buf, 10, &delay) != 0 || delay != (int) delay)
  187. return -EINVAL;
  188. device_lock(dev);
  189. pm_runtime_set_autosuspend_delay(dev, delay);
  190. device_unlock(dev);
  191. return n;
  192. }
  193. static DEVICE_ATTR(autosuspend_delay_ms, 0644, autosuspend_delay_ms_show,
  194. autosuspend_delay_ms_store);
  195. static ssize_t pm_qos_resume_latency_show(struct device *dev,
  196. struct device_attribute *attr,
  197. char *buf)
  198. {
  199. s32 value = dev_pm_qos_requested_resume_latency(dev);
  200. if (value == 0)
  201. return sprintf(buf, "n/a\n");
  202. else if (value == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
  203. value = 0;
  204. return sprintf(buf, "%d\n", value);
  205. }
  206. static ssize_t pm_qos_resume_latency_store(struct device *dev,
  207. struct device_attribute *attr,
  208. const char *buf, size_t n)
  209. {
  210. s32 value;
  211. int ret;
  212. if (!kstrtos32(buf, 0, &value)) {
  213. /*
  214. * Prevent users from writing negative or "no constraint" values
  215. * directly.
  216. */
  217. if (value < 0 || value == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
  218. return -EINVAL;
  219. if (value == 0)
  220. value = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
  221. } else if (!strcmp(buf, "n/a") || !strcmp(buf, "n/a\n")) {
  222. value = 0;
  223. } else {
  224. return -EINVAL;
  225. }
  226. ret = dev_pm_qos_update_request(dev->power.qos->resume_latency_req,
  227. value);
  228. return ret < 0 ? ret : n;
  229. }
  230. static DEVICE_ATTR(pm_qos_resume_latency_us, 0644,
  231. pm_qos_resume_latency_show, pm_qos_resume_latency_store);
  232. static ssize_t pm_qos_latency_tolerance_show(struct device *dev,
  233. struct device_attribute *attr,
  234. char *buf)
  235. {
  236. s32 value = dev_pm_qos_get_user_latency_tolerance(dev);
  237. if (value < 0)
  238. return sprintf(buf, "auto\n");
  239. else if (value == PM_QOS_LATENCY_ANY)
  240. return sprintf(buf, "any\n");
  241. return sprintf(buf, "%d\n", value);
  242. }
  243. static ssize_t pm_qos_latency_tolerance_store(struct device *dev,
  244. struct device_attribute *attr,
  245. const char *buf, size_t n)
  246. {
  247. s32 value;
  248. int ret;
  249. if (kstrtos32(buf, 0, &value) == 0) {
  250. /* Users can't write negative values directly */
  251. if (value < 0)
  252. return -EINVAL;
  253. } else {
  254. if (!strcmp(buf, "auto") || !strcmp(buf, "auto\n"))
  255. value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
  256. else if (!strcmp(buf, "any") || !strcmp(buf, "any\n"))
  257. value = PM_QOS_LATENCY_ANY;
  258. else
  259. return -EINVAL;
  260. }
  261. ret = dev_pm_qos_update_user_latency_tolerance(dev, value);
  262. return ret < 0 ? ret : n;
  263. }
  264. static DEVICE_ATTR(pm_qos_latency_tolerance_us, 0644,
  265. pm_qos_latency_tolerance_show, pm_qos_latency_tolerance_store);
  266. static ssize_t pm_qos_no_power_off_show(struct device *dev,
  267. struct device_attribute *attr,
  268. char *buf)
  269. {
  270. return sprintf(buf, "%d\n", !!(dev_pm_qos_requested_flags(dev)
  271. & PM_QOS_FLAG_NO_POWER_OFF));
  272. }
  273. static ssize_t pm_qos_no_power_off_store(struct device *dev,
  274. struct device_attribute *attr,
  275. const char *buf, size_t n)
  276. {
  277. int ret;
  278. if (kstrtoint(buf, 0, &ret))
  279. return -EINVAL;
  280. if (ret != 0 && ret != 1)
  281. return -EINVAL;
  282. ret = dev_pm_qos_update_flags(dev, PM_QOS_FLAG_NO_POWER_OFF, ret);
  283. return ret < 0 ? ret : n;
  284. }
  285. static DEVICE_ATTR(pm_qos_no_power_off, 0644,
  286. pm_qos_no_power_off_show, pm_qos_no_power_off_store);
  287. static ssize_t pm_qos_remote_wakeup_show(struct device *dev,
  288. struct device_attribute *attr,
  289. char *buf)
  290. {
  291. return sprintf(buf, "%d\n", !!(dev_pm_qos_requested_flags(dev)
  292. & PM_QOS_FLAG_REMOTE_WAKEUP));
  293. }
  294. static ssize_t pm_qos_remote_wakeup_store(struct device *dev,
  295. struct device_attribute *attr,
  296. const char *buf, size_t n)
  297. {
  298. int ret;
  299. if (kstrtoint(buf, 0, &ret))
  300. return -EINVAL;
  301. if (ret != 0 && ret != 1)
  302. return -EINVAL;
  303. ret = dev_pm_qos_update_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP, ret);
  304. return ret < 0 ? ret : n;
  305. }
  306. static DEVICE_ATTR(pm_qos_remote_wakeup, 0644,
  307. pm_qos_remote_wakeup_show, pm_qos_remote_wakeup_store);
  308. #ifdef CONFIG_PM_SLEEP
  309. static const char _enabled[] = "enabled";
  310. static const char _disabled[] = "disabled";
  311. static ssize_t
  312. wake_show(struct device * dev, struct device_attribute *attr, char * buf)
  313. {
  314. return sprintf(buf, "%s\n", device_can_wakeup(dev)
  315. ? (device_may_wakeup(dev) ? _enabled : _disabled)
  316. : "");
  317. }
  318. static ssize_t
  319. wake_store(struct device * dev, struct device_attribute *attr,
  320. const char * buf, size_t n)
  321. {
  322. char *cp;
  323. int len = n;
  324. if (!device_can_wakeup(dev))
  325. return -EINVAL;
  326. cp = memchr(buf, '\n', n);
  327. if (cp)
  328. len = cp - buf;
  329. if (len == sizeof _enabled - 1
  330. && strncmp(buf, _enabled, sizeof _enabled - 1) == 0)
  331. device_set_wakeup_enable(dev, 1);
  332. else if (len == sizeof _disabled - 1
  333. && strncmp(buf, _disabled, sizeof _disabled - 1) == 0)
  334. device_set_wakeup_enable(dev, 0);
  335. else
  336. return -EINVAL;
  337. return n;
  338. }
  339. static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
  340. static ssize_t wakeup_count_show(struct device *dev,
  341. struct device_attribute *attr, char *buf)
  342. {
  343. unsigned long count = 0;
  344. bool enabled = false;
  345. spin_lock_irq(&dev->power.lock);
  346. if (dev->power.wakeup) {
  347. count = dev->power.wakeup->event_count;
  348. enabled = true;
  349. }
  350. spin_unlock_irq(&dev->power.lock);
  351. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  352. }
  353. static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
  354. static ssize_t wakeup_active_count_show(struct device *dev,
  355. struct device_attribute *attr, char *buf)
  356. {
  357. unsigned long count = 0;
  358. bool enabled = false;
  359. spin_lock_irq(&dev->power.lock);
  360. if (dev->power.wakeup) {
  361. count = dev->power.wakeup->active_count;
  362. enabled = true;
  363. }
  364. spin_unlock_irq(&dev->power.lock);
  365. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  366. }
  367. static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
  368. static ssize_t wakeup_abort_count_show(struct device *dev,
  369. struct device_attribute *attr,
  370. char *buf)
  371. {
  372. unsigned long count = 0;
  373. bool enabled = false;
  374. spin_lock_irq(&dev->power.lock);
  375. if (dev->power.wakeup) {
  376. count = dev->power.wakeup->wakeup_count;
  377. enabled = true;
  378. }
  379. spin_unlock_irq(&dev->power.lock);
  380. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  381. }
  382. static DEVICE_ATTR(wakeup_abort_count, 0444, wakeup_abort_count_show, NULL);
  383. static ssize_t wakeup_expire_count_show(struct device *dev,
  384. struct device_attribute *attr,
  385. char *buf)
  386. {
  387. unsigned long count = 0;
  388. bool enabled = false;
  389. spin_lock_irq(&dev->power.lock);
  390. if (dev->power.wakeup) {
  391. count = dev->power.wakeup->expire_count;
  392. enabled = true;
  393. }
  394. spin_unlock_irq(&dev->power.lock);
  395. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  396. }
  397. static DEVICE_ATTR(wakeup_expire_count, 0444, wakeup_expire_count_show, NULL);
  398. static ssize_t wakeup_active_show(struct device *dev,
  399. struct device_attribute *attr, char *buf)
  400. {
  401. unsigned int active = 0;
  402. bool enabled = false;
  403. spin_lock_irq(&dev->power.lock);
  404. if (dev->power.wakeup) {
  405. active = dev->power.wakeup->active;
  406. enabled = true;
  407. }
  408. spin_unlock_irq(&dev->power.lock);
  409. return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
  410. }
  411. static DEVICE_ATTR(wakeup_active, 0444, wakeup_active_show, NULL);
  412. static ssize_t wakeup_total_time_show(struct device *dev,
  413. struct device_attribute *attr, char *buf)
  414. {
  415. s64 msec = 0;
  416. bool enabled = false;
  417. spin_lock_irq(&dev->power.lock);
  418. if (dev->power.wakeup) {
  419. msec = ktime_to_ms(dev->power.wakeup->total_time);
  420. enabled = true;
  421. }
  422. spin_unlock_irq(&dev->power.lock);
  423. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  424. }
  425. static DEVICE_ATTR(wakeup_total_time_ms, 0444, wakeup_total_time_show, NULL);
  426. static ssize_t wakeup_max_time_show(struct device *dev,
  427. struct device_attribute *attr, char *buf)
  428. {
  429. s64 msec = 0;
  430. bool enabled = false;
  431. spin_lock_irq(&dev->power.lock);
  432. if (dev->power.wakeup) {
  433. msec = ktime_to_ms(dev->power.wakeup->max_time);
  434. enabled = true;
  435. }
  436. spin_unlock_irq(&dev->power.lock);
  437. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  438. }
  439. static DEVICE_ATTR(wakeup_max_time_ms, 0444, wakeup_max_time_show, NULL);
  440. static ssize_t wakeup_last_time_show(struct device *dev,
  441. struct device_attribute *attr, char *buf)
  442. {
  443. s64 msec = 0;
  444. bool enabled = false;
  445. spin_lock_irq(&dev->power.lock);
  446. if (dev->power.wakeup) {
  447. msec = ktime_to_ms(dev->power.wakeup->last_time);
  448. enabled = true;
  449. }
  450. spin_unlock_irq(&dev->power.lock);
  451. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  452. }
  453. static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
  454. #ifdef CONFIG_PM_AUTOSLEEP
  455. static ssize_t wakeup_prevent_sleep_time_show(struct device *dev,
  456. struct device_attribute *attr,
  457. char *buf)
  458. {
  459. s64 msec = 0;
  460. bool enabled = false;
  461. spin_lock_irq(&dev->power.lock);
  462. if (dev->power.wakeup) {
  463. msec = ktime_to_ms(dev->power.wakeup->prevent_sleep_time);
  464. enabled = true;
  465. }
  466. spin_unlock_irq(&dev->power.lock);
  467. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  468. }
  469. static DEVICE_ATTR(wakeup_prevent_sleep_time_ms, 0444,
  470. wakeup_prevent_sleep_time_show, NULL);
  471. #endif /* CONFIG_PM_AUTOSLEEP */
  472. #endif /* CONFIG_PM_SLEEP */
  473. #ifdef CONFIG_PM_ADVANCED_DEBUG
  474. static ssize_t rtpm_usagecount_show(struct device *dev,
  475. struct device_attribute *attr, char *buf)
  476. {
  477. return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
  478. }
  479. static ssize_t rtpm_children_show(struct device *dev,
  480. struct device_attribute *attr, char *buf)
  481. {
  482. return sprintf(buf, "%d\n", dev->power.ignore_children ?
  483. 0 : atomic_read(&dev->power.child_count));
  484. }
  485. static ssize_t rtpm_enabled_show(struct device *dev,
  486. struct device_attribute *attr, char *buf)
  487. {
  488. if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
  489. return sprintf(buf, "disabled & forbidden\n");
  490. else if (dev->power.disable_depth)
  491. return sprintf(buf, "disabled\n");
  492. else if (dev->power.runtime_auto == false)
  493. return sprintf(buf, "forbidden\n");
  494. return sprintf(buf, "enabled\n");
  495. }
  496. static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
  497. static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
  498. static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
  499. #ifdef CONFIG_PM_SLEEP
  500. static ssize_t async_show(struct device *dev, struct device_attribute *attr,
  501. char *buf)
  502. {
  503. return sprintf(buf, "%s\n",
  504. device_async_suspend_enabled(dev) ?
  505. _enabled : _disabled);
  506. }
  507. static ssize_t async_store(struct device *dev, struct device_attribute *attr,
  508. const char *buf, size_t n)
  509. {
  510. char *cp;
  511. int len = n;
  512. cp = memchr(buf, '\n', n);
  513. if (cp)
  514. len = cp - buf;
  515. if (len == sizeof _enabled - 1 && strncmp(buf, _enabled, len) == 0)
  516. device_enable_async_suspend(dev);
  517. else if (len == sizeof _disabled - 1 &&
  518. strncmp(buf, _disabled, len) == 0)
  519. device_disable_async_suspend(dev);
  520. else
  521. return -EINVAL;
  522. return n;
  523. }
  524. static DEVICE_ATTR(async, 0644, async_show, async_store);
  525. #endif /* CONFIG_PM_SLEEP */
  526. #endif /* CONFIG_PM_ADVANCED_DEBUG */
  527. static struct attribute *power_attrs[] = {
  528. #ifdef CONFIG_PM_ADVANCED_DEBUG
  529. #ifdef CONFIG_PM_SLEEP
  530. &dev_attr_async.attr,
  531. #endif
  532. &dev_attr_runtime_status.attr,
  533. &dev_attr_runtime_usage.attr,
  534. &dev_attr_runtime_active_kids.attr,
  535. &dev_attr_runtime_enabled.attr,
  536. #endif /* CONFIG_PM_ADVANCED_DEBUG */
  537. NULL,
  538. };
  539. static const struct attribute_group pm_attr_group = {
  540. .name = power_group_name,
  541. .attrs = power_attrs,
  542. };
  543. static struct attribute *wakeup_attrs[] = {
  544. #ifdef CONFIG_PM_SLEEP
  545. &dev_attr_wakeup.attr,
  546. &dev_attr_wakeup_count.attr,
  547. &dev_attr_wakeup_active_count.attr,
  548. &dev_attr_wakeup_abort_count.attr,
  549. &dev_attr_wakeup_expire_count.attr,
  550. &dev_attr_wakeup_active.attr,
  551. &dev_attr_wakeup_total_time_ms.attr,
  552. &dev_attr_wakeup_max_time_ms.attr,
  553. &dev_attr_wakeup_last_time_ms.attr,
  554. #ifdef CONFIG_PM_AUTOSLEEP
  555. &dev_attr_wakeup_prevent_sleep_time_ms.attr,
  556. #endif
  557. #endif
  558. NULL,
  559. };
  560. static const struct attribute_group pm_wakeup_attr_group = {
  561. .name = power_group_name,
  562. .attrs = wakeup_attrs,
  563. };
  564. static struct attribute *runtime_attrs[] = {
  565. #ifndef CONFIG_PM_ADVANCED_DEBUG
  566. &dev_attr_runtime_status.attr,
  567. #endif
  568. &dev_attr_control.attr,
  569. &dev_attr_runtime_suspended_time.attr,
  570. &dev_attr_runtime_active_time.attr,
  571. &dev_attr_autosuspend_delay_ms.attr,
  572. NULL,
  573. };
  574. static const struct attribute_group pm_runtime_attr_group = {
  575. .name = power_group_name,
  576. .attrs = runtime_attrs,
  577. };
  578. static struct attribute *pm_qos_resume_latency_attrs[] = {
  579. &dev_attr_pm_qos_resume_latency_us.attr,
  580. NULL,
  581. };
  582. static const struct attribute_group pm_qos_resume_latency_attr_group = {
  583. .name = power_group_name,
  584. .attrs = pm_qos_resume_latency_attrs,
  585. };
  586. static struct attribute *pm_qos_latency_tolerance_attrs[] = {
  587. &dev_attr_pm_qos_latency_tolerance_us.attr,
  588. NULL,
  589. };
  590. static const struct attribute_group pm_qos_latency_tolerance_attr_group = {
  591. .name = power_group_name,
  592. .attrs = pm_qos_latency_tolerance_attrs,
  593. };
  594. static struct attribute *pm_qos_flags_attrs[] = {
  595. &dev_attr_pm_qos_no_power_off.attr,
  596. &dev_attr_pm_qos_remote_wakeup.attr,
  597. NULL,
  598. };
  599. static const struct attribute_group pm_qos_flags_attr_group = {
  600. .name = power_group_name,
  601. .attrs = pm_qos_flags_attrs,
  602. };
  603. int dpm_sysfs_add(struct device *dev)
  604. {
  605. int rc;
  606. rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
  607. if (rc)
  608. return rc;
  609. if (pm_runtime_callbacks_present(dev)) {
  610. rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
  611. if (rc)
  612. goto err_out;
  613. }
  614. if (device_can_wakeup(dev)) {
  615. rc = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
  616. if (rc)
  617. goto err_runtime;
  618. }
  619. if (dev->power.set_latency_tolerance) {
  620. rc = sysfs_merge_group(&dev->kobj,
  621. &pm_qos_latency_tolerance_attr_group);
  622. if (rc)
  623. goto err_wakeup;
  624. }
  625. return 0;
  626. err_wakeup:
  627. sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
  628. err_runtime:
  629. sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
  630. err_out:
  631. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  632. return rc;
  633. }
  634. int wakeup_sysfs_add(struct device *dev)
  635. {
  636. return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
  637. }
  638. void wakeup_sysfs_remove(struct device *dev)
  639. {
  640. sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
  641. }
  642. int pm_qos_sysfs_add_resume_latency(struct device *dev)
  643. {
  644. return sysfs_merge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
  645. }
  646. void pm_qos_sysfs_remove_resume_latency(struct device *dev)
  647. {
  648. sysfs_unmerge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
  649. }
  650. int pm_qos_sysfs_add_flags(struct device *dev)
  651. {
  652. return sysfs_merge_group(&dev->kobj, &pm_qos_flags_attr_group);
  653. }
  654. void pm_qos_sysfs_remove_flags(struct device *dev)
  655. {
  656. sysfs_unmerge_group(&dev->kobj, &pm_qos_flags_attr_group);
  657. }
  658. int pm_qos_sysfs_add_latency_tolerance(struct device *dev)
  659. {
  660. return sysfs_merge_group(&dev->kobj,
  661. &pm_qos_latency_tolerance_attr_group);
  662. }
  663. void pm_qos_sysfs_remove_latency_tolerance(struct device *dev)
  664. {
  665. sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
  666. }
  667. void rpm_sysfs_remove(struct device *dev)
  668. {
  669. sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
  670. }
  671. void dpm_sysfs_remove(struct device *dev)
  672. {
  673. sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
  674. dev_pm_qos_constraints_destroy(dev);
  675. rpm_sysfs_remove(dev);
  676. sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
  677. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  678. }