sysfs.c 20 KB

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