sysfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. #ifdef CONFIG_PM_RUNTIME
  95. static const char ctrl_auto[] = "auto";
  96. static const char ctrl_on[] = "on";
  97. static ssize_t control_show(struct device *dev, struct device_attribute *attr,
  98. char *buf)
  99. {
  100. return sprintf(buf, "%s\n",
  101. dev->power.runtime_auto ? ctrl_auto : ctrl_on);
  102. }
  103. static ssize_t control_store(struct device * dev, struct device_attribute *attr,
  104. const char * buf, size_t n)
  105. {
  106. char *cp;
  107. int len = n;
  108. cp = memchr(buf, '\n', n);
  109. if (cp)
  110. len = cp - buf;
  111. device_lock(dev);
  112. if (len == sizeof ctrl_auto - 1 && strncmp(buf, ctrl_auto, len) == 0)
  113. pm_runtime_allow(dev);
  114. else if (len == sizeof ctrl_on - 1 && strncmp(buf, ctrl_on, len) == 0)
  115. pm_runtime_forbid(dev);
  116. else
  117. n = -EINVAL;
  118. device_unlock(dev);
  119. return n;
  120. }
  121. static DEVICE_ATTR(control, 0644, control_show, control_store);
  122. static ssize_t rtpm_active_time_show(struct device *dev,
  123. struct device_attribute *attr, char *buf)
  124. {
  125. int ret;
  126. spin_lock_irq(&dev->power.lock);
  127. update_pm_runtime_accounting(dev);
  128. ret = sprintf(buf, "%i\n", jiffies_to_msecs(dev->power.active_jiffies));
  129. spin_unlock_irq(&dev->power.lock);
  130. return ret;
  131. }
  132. static DEVICE_ATTR(runtime_active_time, 0444, rtpm_active_time_show, NULL);
  133. static ssize_t rtpm_suspended_time_show(struct device *dev,
  134. struct device_attribute *attr, char *buf)
  135. {
  136. int ret;
  137. spin_lock_irq(&dev->power.lock);
  138. update_pm_runtime_accounting(dev);
  139. ret = sprintf(buf, "%i\n",
  140. jiffies_to_msecs(dev->power.suspended_jiffies));
  141. spin_unlock_irq(&dev->power.lock);
  142. return ret;
  143. }
  144. static DEVICE_ATTR(runtime_suspended_time, 0444, rtpm_suspended_time_show, NULL);
  145. static ssize_t rtpm_status_show(struct device *dev,
  146. struct device_attribute *attr, char *buf)
  147. {
  148. const char *p;
  149. if (dev->power.runtime_error) {
  150. p = "error\n";
  151. } else if (dev->power.disable_depth) {
  152. p = "unsupported\n";
  153. } else {
  154. switch (dev->power.runtime_status) {
  155. case RPM_SUSPENDED:
  156. p = "suspended\n";
  157. break;
  158. case RPM_SUSPENDING:
  159. p = "suspending\n";
  160. break;
  161. case RPM_RESUMING:
  162. p = "resuming\n";
  163. break;
  164. case RPM_ACTIVE:
  165. p = "active\n";
  166. break;
  167. default:
  168. return -EIO;
  169. }
  170. }
  171. return sprintf(buf, p);
  172. }
  173. static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL);
  174. static ssize_t autosuspend_delay_ms_show(struct device *dev,
  175. struct device_attribute *attr, char *buf)
  176. {
  177. if (!dev->power.use_autosuspend)
  178. return -EIO;
  179. return sprintf(buf, "%d\n", dev->power.autosuspend_delay);
  180. }
  181. static ssize_t autosuspend_delay_ms_store(struct device *dev,
  182. struct device_attribute *attr, const char *buf, size_t n)
  183. {
  184. long delay;
  185. if (!dev->power.use_autosuspend)
  186. return -EIO;
  187. if (kstrtol(buf, 10, &delay) != 0 || delay != (int) delay)
  188. return -EINVAL;
  189. device_lock(dev);
  190. pm_runtime_set_autosuspend_delay(dev, delay);
  191. device_unlock(dev);
  192. return n;
  193. }
  194. static DEVICE_ATTR(autosuspend_delay_ms, 0644, autosuspend_delay_ms_show,
  195. autosuspend_delay_ms_store);
  196. static ssize_t pm_qos_resume_latency_show(struct device *dev,
  197. struct device_attribute *attr,
  198. char *buf)
  199. {
  200. return sprintf(buf, "%d\n", dev_pm_qos_requested_resume_latency(dev));
  201. }
  202. static ssize_t pm_qos_resume_latency_store(struct device *dev,
  203. struct device_attribute *attr,
  204. const char *buf, size_t n)
  205. {
  206. s32 value;
  207. int ret;
  208. if (kstrtos32(buf, 0, &value))
  209. return -EINVAL;
  210. if (value < 0)
  211. return -EINVAL;
  212. ret = dev_pm_qos_update_request(dev->power.qos->resume_latency_req,
  213. value);
  214. return ret < 0 ? ret : n;
  215. }
  216. static DEVICE_ATTR(pm_qos_resume_latency_us, 0644,
  217. pm_qos_resume_latency_show, pm_qos_resume_latency_store);
  218. static ssize_t pm_qos_latency_tolerance_show(struct device *dev,
  219. struct device_attribute *attr,
  220. char *buf)
  221. {
  222. s32 value = dev_pm_qos_get_user_latency_tolerance(dev);
  223. if (value < 0)
  224. return sprintf(buf, "auto\n");
  225. else if (value == PM_QOS_LATENCY_ANY)
  226. return sprintf(buf, "any\n");
  227. return sprintf(buf, "%d\n", value);
  228. }
  229. static ssize_t pm_qos_latency_tolerance_store(struct device *dev,
  230. struct device_attribute *attr,
  231. const char *buf, size_t n)
  232. {
  233. s32 value;
  234. int ret;
  235. if (kstrtos32(buf, 0, &value)) {
  236. if (!strcmp(buf, "auto") || !strcmp(buf, "auto\n"))
  237. value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
  238. else if (!strcmp(buf, "any") || !strcmp(buf, "any\n"))
  239. value = PM_QOS_LATENCY_ANY;
  240. }
  241. ret = dev_pm_qos_update_user_latency_tolerance(dev, value);
  242. return ret < 0 ? ret : n;
  243. }
  244. static DEVICE_ATTR(pm_qos_latency_tolerance_us, 0644,
  245. pm_qos_latency_tolerance_show, pm_qos_latency_tolerance_store);
  246. static ssize_t pm_qos_no_power_off_show(struct device *dev,
  247. struct device_attribute *attr,
  248. char *buf)
  249. {
  250. return sprintf(buf, "%d\n", !!(dev_pm_qos_requested_flags(dev)
  251. & PM_QOS_FLAG_NO_POWER_OFF));
  252. }
  253. static ssize_t pm_qos_no_power_off_store(struct device *dev,
  254. struct device_attribute *attr,
  255. const char *buf, size_t n)
  256. {
  257. int ret;
  258. if (kstrtoint(buf, 0, &ret))
  259. return -EINVAL;
  260. if (ret != 0 && ret != 1)
  261. return -EINVAL;
  262. ret = dev_pm_qos_update_flags(dev, PM_QOS_FLAG_NO_POWER_OFF, ret);
  263. return ret < 0 ? ret : n;
  264. }
  265. static DEVICE_ATTR(pm_qos_no_power_off, 0644,
  266. pm_qos_no_power_off_show, pm_qos_no_power_off_store);
  267. static ssize_t pm_qos_remote_wakeup_show(struct device *dev,
  268. struct device_attribute *attr,
  269. char *buf)
  270. {
  271. return sprintf(buf, "%d\n", !!(dev_pm_qos_requested_flags(dev)
  272. & PM_QOS_FLAG_REMOTE_WAKEUP));
  273. }
  274. static ssize_t pm_qos_remote_wakeup_store(struct device *dev,
  275. struct device_attribute *attr,
  276. const char *buf, size_t n)
  277. {
  278. int ret;
  279. if (kstrtoint(buf, 0, &ret))
  280. return -EINVAL;
  281. if (ret != 0 && ret != 1)
  282. return -EINVAL;
  283. ret = dev_pm_qos_update_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP, ret);
  284. return ret < 0 ? ret : n;
  285. }
  286. static DEVICE_ATTR(pm_qos_remote_wakeup, 0644,
  287. pm_qos_remote_wakeup_show, pm_qos_remote_wakeup_store);
  288. #endif /* CONFIG_PM_RUNTIME */
  289. #ifdef CONFIG_PM_SLEEP
  290. static const char _enabled[] = "enabled";
  291. static const char _disabled[] = "disabled";
  292. static ssize_t
  293. wake_show(struct device * dev, struct device_attribute *attr, char * buf)
  294. {
  295. return sprintf(buf, "%s\n", device_can_wakeup(dev)
  296. ? (device_may_wakeup(dev) ? _enabled : _disabled)
  297. : "");
  298. }
  299. static ssize_t
  300. wake_store(struct device * dev, struct device_attribute *attr,
  301. const char * buf, size_t n)
  302. {
  303. char *cp;
  304. int len = n;
  305. if (!device_can_wakeup(dev))
  306. return -EINVAL;
  307. cp = memchr(buf, '\n', n);
  308. if (cp)
  309. len = cp - buf;
  310. if (len == sizeof _enabled - 1
  311. && strncmp(buf, _enabled, sizeof _enabled - 1) == 0)
  312. device_set_wakeup_enable(dev, 1);
  313. else if (len == sizeof _disabled - 1
  314. && strncmp(buf, _disabled, sizeof _disabled - 1) == 0)
  315. device_set_wakeup_enable(dev, 0);
  316. else
  317. return -EINVAL;
  318. return n;
  319. }
  320. static DEVICE_ATTR(wakeup, 0644, wake_show, wake_store);
  321. static ssize_t wakeup_count_show(struct device *dev,
  322. struct device_attribute *attr, char *buf)
  323. {
  324. unsigned long count = 0;
  325. bool enabled = false;
  326. spin_lock_irq(&dev->power.lock);
  327. if (dev->power.wakeup) {
  328. count = dev->power.wakeup->event_count;
  329. enabled = true;
  330. }
  331. spin_unlock_irq(&dev->power.lock);
  332. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  333. }
  334. static DEVICE_ATTR(wakeup_count, 0444, wakeup_count_show, NULL);
  335. static ssize_t wakeup_active_count_show(struct device *dev,
  336. struct device_attribute *attr, char *buf)
  337. {
  338. unsigned long count = 0;
  339. bool enabled = false;
  340. spin_lock_irq(&dev->power.lock);
  341. if (dev->power.wakeup) {
  342. count = dev->power.wakeup->active_count;
  343. enabled = true;
  344. }
  345. spin_unlock_irq(&dev->power.lock);
  346. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  347. }
  348. static DEVICE_ATTR(wakeup_active_count, 0444, wakeup_active_count_show, NULL);
  349. static ssize_t wakeup_abort_count_show(struct device *dev,
  350. struct device_attribute *attr,
  351. char *buf)
  352. {
  353. unsigned long count = 0;
  354. bool enabled = false;
  355. spin_lock_irq(&dev->power.lock);
  356. if (dev->power.wakeup) {
  357. count = dev->power.wakeup->wakeup_count;
  358. enabled = true;
  359. }
  360. spin_unlock_irq(&dev->power.lock);
  361. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  362. }
  363. static DEVICE_ATTR(wakeup_abort_count, 0444, wakeup_abort_count_show, NULL);
  364. static ssize_t wakeup_expire_count_show(struct device *dev,
  365. struct device_attribute *attr,
  366. char *buf)
  367. {
  368. unsigned long count = 0;
  369. bool enabled = false;
  370. spin_lock_irq(&dev->power.lock);
  371. if (dev->power.wakeup) {
  372. count = dev->power.wakeup->expire_count;
  373. enabled = true;
  374. }
  375. spin_unlock_irq(&dev->power.lock);
  376. return enabled ? sprintf(buf, "%lu\n", count) : sprintf(buf, "\n");
  377. }
  378. static DEVICE_ATTR(wakeup_expire_count, 0444, wakeup_expire_count_show, NULL);
  379. static ssize_t wakeup_active_show(struct device *dev,
  380. struct device_attribute *attr, char *buf)
  381. {
  382. unsigned int active = 0;
  383. bool enabled = false;
  384. spin_lock_irq(&dev->power.lock);
  385. if (dev->power.wakeup) {
  386. active = dev->power.wakeup->active;
  387. enabled = true;
  388. }
  389. spin_unlock_irq(&dev->power.lock);
  390. return enabled ? sprintf(buf, "%u\n", active) : sprintf(buf, "\n");
  391. }
  392. static DEVICE_ATTR(wakeup_active, 0444, wakeup_active_show, NULL);
  393. static ssize_t wakeup_total_time_show(struct device *dev,
  394. struct device_attribute *attr, char *buf)
  395. {
  396. s64 msec = 0;
  397. bool enabled = false;
  398. spin_lock_irq(&dev->power.lock);
  399. if (dev->power.wakeup) {
  400. msec = ktime_to_ms(dev->power.wakeup->total_time);
  401. enabled = true;
  402. }
  403. spin_unlock_irq(&dev->power.lock);
  404. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  405. }
  406. static DEVICE_ATTR(wakeup_total_time_ms, 0444, wakeup_total_time_show, NULL);
  407. static ssize_t wakeup_max_time_show(struct device *dev,
  408. struct device_attribute *attr, char *buf)
  409. {
  410. s64 msec = 0;
  411. bool enabled = false;
  412. spin_lock_irq(&dev->power.lock);
  413. if (dev->power.wakeup) {
  414. msec = ktime_to_ms(dev->power.wakeup->max_time);
  415. enabled = true;
  416. }
  417. spin_unlock_irq(&dev->power.lock);
  418. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  419. }
  420. static DEVICE_ATTR(wakeup_max_time_ms, 0444, wakeup_max_time_show, NULL);
  421. static ssize_t wakeup_last_time_show(struct device *dev,
  422. struct device_attribute *attr, char *buf)
  423. {
  424. s64 msec = 0;
  425. bool enabled = false;
  426. spin_lock_irq(&dev->power.lock);
  427. if (dev->power.wakeup) {
  428. msec = ktime_to_ms(dev->power.wakeup->last_time);
  429. enabled = true;
  430. }
  431. spin_unlock_irq(&dev->power.lock);
  432. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  433. }
  434. static DEVICE_ATTR(wakeup_last_time_ms, 0444, wakeup_last_time_show, NULL);
  435. #ifdef CONFIG_PM_AUTOSLEEP
  436. static ssize_t wakeup_prevent_sleep_time_show(struct device *dev,
  437. struct device_attribute *attr,
  438. char *buf)
  439. {
  440. s64 msec = 0;
  441. bool enabled = false;
  442. spin_lock_irq(&dev->power.lock);
  443. if (dev->power.wakeup) {
  444. msec = ktime_to_ms(dev->power.wakeup->prevent_sleep_time);
  445. enabled = true;
  446. }
  447. spin_unlock_irq(&dev->power.lock);
  448. return enabled ? sprintf(buf, "%lld\n", msec) : sprintf(buf, "\n");
  449. }
  450. static DEVICE_ATTR(wakeup_prevent_sleep_time_ms, 0444,
  451. wakeup_prevent_sleep_time_show, NULL);
  452. #endif /* CONFIG_PM_AUTOSLEEP */
  453. #endif /* CONFIG_PM_SLEEP */
  454. #ifdef CONFIG_PM_ADVANCED_DEBUG
  455. #ifdef CONFIG_PM_RUNTIME
  456. static ssize_t rtpm_usagecount_show(struct device *dev,
  457. struct device_attribute *attr, char *buf)
  458. {
  459. return sprintf(buf, "%d\n", atomic_read(&dev->power.usage_count));
  460. }
  461. static ssize_t rtpm_children_show(struct device *dev,
  462. struct device_attribute *attr, char *buf)
  463. {
  464. return sprintf(buf, "%d\n", dev->power.ignore_children ?
  465. 0 : atomic_read(&dev->power.child_count));
  466. }
  467. static ssize_t rtpm_enabled_show(struct device *dev,
  468. struct device_attribute *attr, char *buf)
  469. {
  470. if ((dev->power.disable_depth) && (dev->power.runtime_auto == false))
  471. return sprintf(buf, "disabled & forbidden\n");
  472. else if (dev->power.disable_depth)
  473. return sprintf(buf, "disabled\n");
  474. else if (dev->power.runtime_auto == false)
  475. return sprintf(buf, "forbidden\n");
  476. return sprintf(buf, "enabled\n");
  477. }
  478. static DEVICE_ATTR(runtime_usage, 0444, rtpm_usagecount_show, NULL);
  479. static DEVICE_ATTR(runtime_active_kids, 0444, rtpm_children_show, NULL);
  480. static DEVICE_ATTR(runtime_enabled, 0444, rtpm_enabled_show, NULL);
  481. #endif
  482. #ifdef CONFIG_PM_SLEEP
  483. static ssize_t async_show(struct device *dev, struct device_attribute *attr,
  484. char *buf)
  485. {
  486. return sprintf(buf, "%s\n",
  487. device_async_suspend_enabled(dev) ?
  488. _enabled : _disabled);
  489. }
  490. static ssize_t async_store(struct device *dev, struct device_attribute *attr,
  491. const char *buf, size_t n)
  492. {
  493. char *cp;
  494. int len = n;
  495. cp = memchr(buf, '\n', n);
  496. if (cp)
  497. len = cp - buf;
  498. if (len == sizeof _enabled - 1 && strncmp(buf, _enabled, len) == 0)
  499. device_enable_async_suspend(dev);
  500. else if (len == sizeof _disabled - 1 &&
  501. strncmp(buf, _disabled, len) == 0)
  502. device_disable_async_suspend(dev);
  503. else
  504. return -EINVAL;
  505. return n;
  506. }
  507. static DEVICE_ATTR(async, 0644, async_show, async_store);
  508. #endif
  509. #endif /* CONFIG_PM_ADVANCED_DEBUG */
  510. static struct attribute *power_attrs[] = {
  511. #ifdef CONFIG_PM_ADVANCED_DEBUG
  512. #ifdef CONFIG_PM_SLEEP
  513. &dev_attr_async.attr,
  514. #endif
  515. #ifdef CONFIG_PM_RUNTIME
  516. &dev_attr_runtime_status.attr,
  517. &dev_attr_runtime_usage.attr,
  518. &dev_attr_runtime_active_kids.attr,
  519. &dev_attr_runtime_enabled.attr,
  520. #endif
  521. #endif /* CONFIG_PM_ADVANCED_DEBUG */
  522. NULL,
  523. };
  524. static struct attribute_group pm_attr_group = {
  525. .name = power_group_name,
  526. .attrs = power_attrs,
  527. };
  528. static struct attribute *wakeup_attrs[] = {
  529. #ifdef CONFIG_PM_SLEEP
  530. &dev_attr_wakeup.attr,
  531. &dev_attr_wakeup_count.attr,
  532. &dev_attr_wakeup_active_count.attr,
  533. &dev_attr_wakeup_abort_count.attr,
  534. &dev_attr_wakeup_expire_count.attr,
  535. &dev_attr_wakeup_active.attr,
  536. &dev_attr_wakeup_total_time_ms.attr,
  537. &dev_attr_wakeup_max_time_ms.attr,
  538. &dev_attr_wakeup_last_time_ms.attr,
  539. #ifdef CONFIG_PM_AUTOSLEEP
  540. &dev_attr_wakeup_prevent_sleep_time_ms.attr,
  541. #endif
  542. #endif
  543. NULL,
  544. };
  545. static struct attribute_group pm_wakeup_attr_group = {
  546. .name = power_group_name,
  547. .attrs = wakeup_attrs,
  548. };
  549. static struct attribute *runtime_attrs[] = {
  550. #ifdef CONFIG_PM_RUNTIME
  551. #ifndef CONFIG_PM_ADVANCED_DEBUG
  552. &dev_attr_runtime_status.attr,
  553. #endif
  554. &dev_attr_control.attr,
  555. &dev_attr_runtime_suspended_time.attr,
  556. &dev_attr_runtime_active_time.attr,
  557. &dev_attr_autosuspend_delay_ms.attr,
  558. #endif /* CONFIG_PM_RUNTIME */
  559. NULL,
  560. };
  561. static struct attribute_group pm_runtime_attr_group = {
  562. .name = power_group_name,
  563. .attrs = runtime_attrs,
  564. };
  565. static struct attribute *pm_qos_resume_latency_attrs[] = {
  566. #ifdef CONFIG_PM_RUNTIME
  567. &dev_attr_pm_qos_resume_latency_us.attr,
  568. #endif /* CONFIG_PM_RUNTIME */
  569. NULL,
  570. };
  571. static struct attribute_group pm_qos_resume_latency_attr_group = {
  572. .name = power_group_name,
  573. .attrs = pm_qos_resume_latency_attrs,
  574. };
  575. static struct attribute *pm_qos_latency_tolerance_attrs[] = {
  576. #ifdef CONFIG_PM_RUNTIME
  577. &dev_attr_pm_qos_latency_tolerance_us.attr,
  578. #endif /* CONFIG_PM_RUNTIME */
  579. NULL,
  580. };
  581. static struct attribute_group pm_qos_latency_tolerance_attr_group = {
  582. .name = power_group_name,
  583. .attrs = pm_qos_latency_tolerance_attrs,
  584. };
  585. static struct attribute *pm_qos_flags_attrs[] = {
  586. #ifdef CONFIG_PM_RUNTIME
  587. &dev_attr_pm_qos_no_power_off.attr,
  588. &dev_attr_pm_qos_remote_wakeup.attr,
  589. #endif /* CONFIG_PM_RUNTIME */
  590. NULL,
  591. };
  592. static struct attribute_group pm_qos_flags_attr_group = {
  593. .name = power_group_name,
  594. .attrs = pm_qos_flags_attrs,
  595. };
  596. int dpm_sysfs_add(struct device *dev)
  597. {
  598. int rc;
  599. rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
  600. if (rc)
  601. return rc;
  602. if (pm_runtime_callbacks_present(dev)) {
  603. rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
  604. if (rc)
  605. goto err_out;
  606. }
  607. if (device_can_wakeup(dev)) {
  608. rc = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
  609. if (rc)
  610. goto err_runtime;
  611. }
  612. if (dev->power.set_latency_tolerance) {
  613. rc = sysfs_merge_group(&dev->kobj,
  614. &pm_qos_latency_tolerance_attr_group);
  615. if (rc)
  616. goto err_wakeup;
  617. }
  618. return 0;
  619. err_wakeup:
  620. sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
  621. err_runtime:
  622. sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
  623. err_out:
  624. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  625. return rc;
  626. }
  627. int wakeup_sysfs_add(struct device *dev)
  628. {
  629. return sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
  630. }
  631. void wakeup_sysfs_remove(struct device *dev)
  632. {
  633. sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
  634. }
  635. int pm_qos_sysfs_add_resume_latency(struct device *dev)
  636. {
  637. return sysfs_merge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
  638. }
  639. void pm_qos_sysfs_remove_resume_latency(struct device *dev)
  640. {
  641. sysfs_unmerge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
  642. }
  643. int pm_qos_sysfs_add_flags(struct device *dev)
  644. {
  645. return sysfs_merge_group(&dev->kobj, &pm_qos_flags_attr_group);
  646. }
  647. void pm_qos_sysfs_remove_flags(struct device *dev)
  648. {
  649. sysfs_unmerge_group(&dev->kobj, &pm_qos_flags_attr_group);
  650. }
  651. void rpm_sysfs_remove(struct device *dev)
  652. {
  653. sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
  654. }
  655. void dpm_sysfs_remove(struct device *dev)
  656. {
  657. sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
  658. dev_pm_qos_constraints_destroy(dev);
  659. rpm_sysfs_remove(dev);
  660. sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
  661. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  662. }