sysfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* sysfs entries for device PM */
  3. #include <linux/device.h>
  4. #include <linux/kobject.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 sysfs_emit(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,
  118. char *buf)
  119. {
  120. u64 tmp = pm_runtime_active_time(dev);
  121. do_div(tmp, NSEC_PER_MSEC);
  122. return sysfs_emit(buf, "%llu\n", tmp);
  123. }
  124. static DEVICE_ATTR_RO(runtime_active_time);
  125. static ssize_t runtime_suspended_time_show(struct device *dev,
  126. struct device_attribute *attr,
  127. char *buf)
  128. {
  129. u64 tmp = pm_runtime_suspended_time(dev);
  130. do_div(tmp, NSEC_PER_MSEC);
  131. return sysfs_emit(buf, "%llu\n", tmp);
  132. }
  133. static DEVICE_ATTR_RO(runtime_suspended_time);
  134. static ssize_t runtime_status_show(struct device *dev,
  135. struct device_attribute *attr, char *buf)
  136. {
  137. const char *output;
  138. if (dev->power.runtime_error) {
  139. output = "error";
  140. } else if (dev->power.disable_depth) {
  141. output = "unsupported";
  142. } else {
  143. switch (dev->power.runtime_status) {
  144. case RPM_SUSPENDED:
  145. output = "suspended";
  146. break;
  147. case RPM_SUSPENDING:
  148. output = "suspending";
  149. break;
  150. case RPM_RESUMING:
  151. output = "resuming";
  152. break;
  153. case RPM_ACTIVE:
  154. output = "active";
  155. break;
  156. default:
  157. return -EIO;
  158. }
  159. }
  160. return sysfs_emit(buf, "%s\n", output);
  161. }
  162. static DEVICE_ATTR_RO(runtime_status);
  163. static ssize_t autosuspend_delay_ms_show(struct device *dev,
  164. struct device_attribute *attr,
  165. char *buf)
  166. {
  167. if (!dev->power.use_autosuspend)
  168. return -EIO;
  169. return sysfs_emit(buf, "%d\n", dev->power.autosuspend_delay);
  170. }
  171. static ssize_t autosuspend_delay_ms_store(struct device *dev,
  172. struct device_attribute *attr, const char *buf, size_t n)
  173. {
  174. long delay;
  175. if (!dev->power.use_autosuspend)
  176. return -EIO;
  177. if (kstrtol(buf, 10, &delay) != 0 || delay != (int) delay)
  178. return -EINVAL;
  179. device_lock(dev);
  180. pm_runtime_set_autosuspend_delay(dev, delay);
  181. device_unlock(dev);
  182. return n;
  183. }
  184. static DEVICE_ATTR_RW(autosuspend_delay_ms);
  185. static ssize_t pm_qos_resume_latency_us_show(struct device *dev,
  186. struct device_attribute *attr,
  187. char *buf)
  188. {
  189. s32 value = dev_pm_qos_requested_resume_latency(dev);
  190. if (value == 0)
  191. return sysfs_emit(buf, "n/a\n");
  192. if (value == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
  193. value = 0;
  194. return sysfs_emit(buf, "%d\n", value);
  195. }
  196. static ssize_t pm_qos_resume_latency_us_store(struct device *dev,
  197. struct device_attribute *attr,
  198. const char *buf, size_t n)
  199. {
  200. s32 value;
  201. int ret;
  202. if (!kstrtos32(buf, 0, &value)) {
  203. /*
  204. * Prevent users from writing negative or "no constraint" values
  205. * directly.
  206. */
  207. if (value < 0 || value == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT)
  208. return -EINVAL;
  209. if (value == 0)
  210. value = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT;
  211. } else if (sysfs_streq(buf, "n/a")) {
  212. value = 0;
  213. } else {
  214. return -EINVAL;
  215. }
  216. ret = dev_pm_qos_update_request(dev->power.qos->resume_latency_req,
  217. value);
  218. return ret < 0 ? ret : n;
  219. }
  220. static DEVICE_ATTR_RW(pm_qos_resume_latency_us);
  221. static ssize_t pm_qos_latency_tolerance_us_show(struct device *dev,
  222. struct device_attribute *attr,
  223. char *buf)
  224. {
  225. s32 value = dev_pm_qos_get_user_latency_tolerance(dev);
  226. if (value < 0)
  227. return sysfs_emit(buf, "%s\n", "auto");
  228. if (value == PM_QOS_LATENCY_ANY)
  229. return sysfs_emit(buf, "%s\n", "any");
  230. return sysfs_emit(buf, "%d\n", value);
  231. }
  232. static ssize_t pm_qos_latency_tolerance_us_store(struct device *dev,
  233. struct device_attribute *attr,
  234. const char *buf, size_t n)
  235. {
  236. s32 value;
  237. int ret;
  238. if (kstrtos32(buf, 0, &value) == 0) {
  239. /* Users can't write negative values directly */
  240. if (value < 0)
  241. return -EINVAL;
  242. } else {
  243. if (sysfs_streq(buf, "auto"))
  244. value = PM_QOS_LATENCY_TOLERANCE_NO_CONSTRAINT;
  245. else if (sysfs_streq(buf, "any"))
  246. value = PM_QOS_LATENCY_ANY;
  247. else
  248. return -EINVAL;
  249. }
  250. ret = dev_pm_qos_update_user_latency_tolerance(dev, value);
  251. return ret < 0 ? ret : n;
  252. }
  253. static DEVICE_ATTR_RW(pm_qos_latency_tolerance_us);
  254. static ssize_t pm_qos_no_power_off_show(struct device *dev,
  255. struct device_attribute *attr,
  256. char *buf)
  257. {
  258. return sysfs_emit(buf, "%d\n", !!(dev_pm_qos_requested_flags(dev)
  259. & PM_QOS_FLAG_NO_POWER_OFF));
  260. }
  261. static ssize_t pm_qos_no_power_off_store(struct device *dev,
  262. struct device_attribute *attr,
  263. const char *buf, size_t n)
  264. {
  265. int ret;
  266. if (kstrtoint(buf, 0, &ret))
  267. return -EINVAL;
  268. if (ret != 0 && ret != 1)
  269. return -EINVAL;
  270. ret = dev_pm_qos_update_flags(dev, PM_QOS_FLAG_NO_POWER_OFF, ret);
  271. return ret < 0 ? ret : n;
  272. }
  273. static DEVICE_ATTR_RW(pm_qos_no_power_off);
  274. #ifdef CONFIG_PM_SLEEP
  275. static const char _enabled[] = "enabled";
  276. static const char _disabled[] = "disabled";
  277. static ssize_t wakeup_show(struct device *dev, struct device_attribute *attr,
  278. char *buf)
  279. {
  280. return sysfs_emit(buf, "%s\n", device_can_wakeup(dev)
  281. ? (device_may_wakeup(dev) ? _enabled : _disabled)
  282. : "");
  283. }
  284. static ssize_t wakeup_store(struct device *dev, struct device_attribute *attr,
  285. const char *buf, size_t n)
  286. {
  287. if (!device_can_wakeup(dev))
  288. return -EINVAL;
  289. if (sysfs_streq(buf, _enabled))
  290. device_set_wakeup_enable(dev, 1);
  291. else if (sysfs_streq(buf, _disabled))
  292. device_set_wakeup_enable(dev, 0);
  293. else
  294. return -EINVAL;
  295. return n;
  296. }
  297. static DEVICE_ATTR_RW(wakeup);
  298. static ssize_t wakeup_count_show(struct device *dev,
  299. struct device_attribute *attr, char *buf)
  300. {
  301. unsigned long count;
  302. bool enabled = false;
  303. spin_lock_irq(&dev->power.lock);
  304. if (dev->power.wakeup) {
  305. count = dev->power.wakeup->wakeup_count;
  306. enabled = true;
  307. }
  308. spin_unlock_irq(&dev->power.lock);
  309. if (!enabled)
  310. return sysfs_emit(buf, "\n");
  311. return sysfs_emit(buf, "%lu\n", count);
  312. }
  313. static DEVICE_ATTR_RO(wakeup_count);
  314. static ssize_t wakeup_active_count_show(struct device *dev,
  315. struct device_attribute *attr,
  316. char *buf)
  317. {
  318. unsigned long count;
  319. bool enabled = false;
  320. spin_lock_irq(&dev->power.lock);
  321. if (dev->power.wakeup) {
  322. count = dev->power.wakeup->active_count;
  323. enabled = true;
  324. }
  325. spin_unlock_irq(&dev->power.lock);
  326. if (!enabled)
  327. return sysfs_emit(buf, "\n");
  328. return sysfs_emit(buf, "%lu\n", count);
  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;
  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. if (!enabled)
  344. return sysfs_emit(buf, "\n");
  345. return sysfs_emit(buf, "%lu\n", count);
  346. }
  347. static DEVICE_ATTR_RO(wakeup_abort_count);
  348. static ssize_t wakeup_expire_count_show(struct device *dev,
  349. struct device_attribute *attr,
  350. char *buf)
  351. {
  352. unsigned long count;
  353. bool enabled = false;
  354. spin_lock_irq(&dev->power.lock);
  355. if (dev->power.wakeup) {
  356. count = dev->power.wakeup->expire_count;
  357. enabled = true;
  358. }
  359. spin_unlock_irq(&dev->power.lock);
  360. if (!enabled)
  361. return sysfs_emit(buf, "\n");
  362. return sysfs_emit(buf, "%lu\n", count);
  363. }
  364. static DEVICE_ATTR_RO(wakeup_expire_count);
  365. static ssize_t wakeup_active_show(struct device *dev,
  366. struct device_attribute *attr, char *buf)
  367. {
  368. unsigned int active;
  369. bool enabled = false;
  370. spin_lock_irq(&dev->power.lock);
  371. if (dev->power.wakeup) {
  372. active = dev->power.wakeup->active;
  373. enabled = true;
  374. }
  375. spin_unlock_irq(&dev->power.lock);
  376. if (!enabled)
  377. return sysfs_emit(buf, "\n");
  378. return sysfs_emit(buf, "%u\n", active);
  379. }
  380. static DEVICE_ATTR_RO(wakeup_active);
  381. static ssize_t wakeup_total_time_ms_show(struct device *dev,
  382. struct device_attribute *attr,
  383. char *buf)
  384. {
  385. s64 msec;
  386. bool enabled = false;
  387. spin_lock_irq(&dev->power.lock);
  388. if (dev->power.wakeup) {
  389. msec = ktime_to_ms(dev->power.wakeup->total_time);
  390. enabled = true;
  391. }
  392. spin_unlock_irq(&dev->power.lock);
  393. if (!enabled)
  394. return sysfs_emit(buf, "\n");
  395. return sysfs_emit(buf, "%lld\n", msec);
  396. }
  397. static DEVICE_ATTR_RO(wakeup_total_time_ms);
  398. static ssize_t wakeup_max_time_ms_show(struct device *dev,
  399. struct device_attribute *attr, char *buf)
  400. {
  401. s64 msec;
  402. bool enabled = false;
  403. spin_lock_irq(&dev->power.lock);
  404. if (dev->power.wakeup) {
  405. msec = ktime_to_ms(dev->power.wakeup->max_time);
  406. enabled = true;
  407. }
  408. spin_unlock_irq(&dev->power.lock);
  409. if (!enabled)
  410. return sysfs_emit(buf, "\n");
  411. return sysfs_emit(buf, "%lld\n", msec);
  412. }
  413. static DEVICE_ATTR_RO(wakeup_max_time_ms);
  414. static ssize_t wakeup_last_time_ms_show(struct device *dev,
  415. struct device_attribute *attr,
  416. char *buf)
  417. {
  418. s64 msec;
  419. bool enabled = false;
  420. spin_lock_irq(&dev->power.lock);
  421. if (dev->power.wakeup) {
  422. msec = ktime_to_ms(dev->power.wakeup->last_time);
  423. enabled = true;
  424. }
  425. spin_unlock_irq(&dev->power.lock);
  426. if (!enabled)
  427. return sysfs_emit(buf, "\n");
  428. return sysfs_emit(buf, "%lld\n", msec);
  429. }
  430. static DEVICE_ATTR_RO(wakeup_last_time_ms);
  431. #ifdef CONFIG_PM_AUTOSLEEP
  432. static ssize_t wakeup_prevent_sleep_time_ms_show(struct device *dev,
  433. struct device_attribute *attr,
  434. char *buf)
  435. {
  436. s64 msec;
  437. bool enabled = false;
  438. spin_lock_irq(&dev->power.lock);
  439. if (dev->power.wakeup) {
  440. msec = ktime_to_ms(dev->power.wakeup->prevent_sleep_time);
  441. enabled = true;
  442. }
  443. spin_unlock_irq(&dev->power.lock);
  444. if (!enabled)
  445. return sysfs_emit(buf, "\n");
  446. return sysfs_emit(buf, "%lld\n", msec);
  447. }
  448. static DEVICE_ATTR_RO(wakeup_prevent_sleep_time_ms);
  449. #endif /* CONFIG_PM_AUTOSLEEP */
  450. static inline int dpm_sysfs_wakeup_change_owner(struct device *dev, kuid_t kuid,
  451. kgid_t kgid)
  452. {
  453. if (dev->power.wakeup && dev->power.wakeup->dev)
  454. return device_change_owner(dev->power.wakeup->dev, kuid, kgid);
  455. return 0;
  456. }
  457. #else /* CONFIG_PM_SLEEP */
  458. static inline int dpm_sysfs_wakeup_change_owner(struct device *dev, kuid_t kuid,
  459. kgid_t kgid)
  460. {
  461. return 0;
  462. }
  463. #endif
  464. #ifdef CONFIG_PM_ADVANCED_DEBUG
  465. static ssize_t runtime_usage_show(struct device *dev,
  466. struct device_attribute *attr, char *buf)
  467. {
  468. return sysfs_emit(buf, "%d\n", atomic_read(&dev->power.usage_count));
  469. }
  470. static DEVICE_ATTR_RO(runtime_usage);
  471. static ssize_t runtime_active_kids_show(struct device *dev,
  472. struct device_attribute *attr,
  473. char *buf)
  474. {
  475. return sysfs_emit(buf, "%d\n", dev->power.ignore_children ?
  476. 0 : atomic_read(&dev->power.child_count));
  477. }
  478. static DEVICE_ATTR_RO(runtime_active_kids);
  479. static ssize_t runtime_enabled_show(struct device *dev,
  480. struct device_attribute *attr, char *buf)
  481. {
  482. const char *output;
  483. if (dev->power.disable_depth && !dev->power.runtime_auto)
  484. output = "disabled & forbidden";
  485. else if (dev->power.disable_depth)
  486. output = "disabled";
  487. else if (!dev->power.runtime_auto)
  488. output = "forbidden";
  489. else
  490. output = "enabled";
  491. return sysfs_emit(buf, "%s\n", output);
  492. }
  493. static DEVICE_ATTR_RO(runtime_enabled);
  494. #ifdef CONFIG_PM_SLEEP
  495. static ssize_t async_show(struct device *dev, struct device_attribute *attr,
  496. char *buf)
  497. {
  498. return sysfs_emit(buf, "%s\n",
  499. device_async_suspend_enabled(dev) ?
  500. _enabled : _disabled);
  501. }
  502. static ssize_t async_store(struct device *dev, struct device_attribute *attr,
  503. const char *buf, size_t n)
  504. {
  505. if (sysfs_streq(buf, _enabled))
  506. device_enable_async_suspend(dev);
  507. else if (sysfs_streq(buf, _disabled))
  508. device_disable_async_suspend(dev);
  509. else
  510. return -EINVAL;
  511. return n;
  512. }
  513. static DEVICE_ATTR_RW(async);
  514. #endif /* CONFIG_PM_SLEEP */
  515. #endif /* CONFIG_PM_ADVANCED_DEBUG */
  516. static struct attribute *power_attrs[] = {
  517. #if defined(CONFIG_PM_ADVANCED_DEBUG) && defined(CONFIG_PM_SLEEP)
  518. &dev_attr_async.attr,
  519. #endif
  520. NULL,
  521. };
  522. static const struct attribute_group pm_attr_group = {
  523. .name = power_group_name,
  524. .attrs = power_attrs,
  525. };
  526. static struct attribute *wakeup_attrs[] = {
  527. #ifdef CONFIG_PM_SLEEP
  528. &dev_attr_wakeup.attr,
  529. &dev_attr_wakeup_count.attr,
  530. &dev_attr_wakeup_active_count.attr,
  531. &dev_attr_wakeup_abort_count.attr,
  532. &dev_attr_wakeup_expire_count.attr,
  533. &dev_attr_wakeup_active.attr,
  534. &dev_attr_wakeup_total_time_ms.attr,
  535. &dev_attr_wakeup_max_time_ms.attr,
  536. &dev_attr_wakeup_last_time_ms.attr,
  537. #ifdef CONFIG_PM_AUTOSLEEP
  538. &dev_attr_wakeup_prevent_sleep_time_ms.attr,
  539. #endif
  540. #endif
  541. NULL,
  542. };
  543. static const struct attribute_group pm_wakeup_attr_group = {
  544. .name = power_group_name,
  545. .attrs = wakeup_attrs,
  546. };
  547. static struct attribute *runtime_attrs[] = {
  548. &dev_attr_runtime_status.attr,
  549. &dev_attr_control.attr,
  550. &dev_attr_runtime_suspended_time.attr,
  551. &dev_attr_runtime_active_time.attr,
  552. &dev_attr_autosuspend_delay_ms.attr,
  553. #ifdef CONFIG_PM_ADVANCED_DEBUG
  554. &dev_attr_runtime_usage.attr,
  555. &dev_attr_runtime_active_kids.attr,
  556. &dev_attr_runtime_enabled.attr,
  557. #endif
  558. NULL,
  559. };
  560. static const struct attribute_group pm_runtime_attr_group = {
  561. .name = power_group_name,
  562. .attrs = runtime_attrs,
  563. };
  564. static struct attribute *pm_qos_resume_latency_attrs[] = {
  565. &dev_attr_pm_qos_resume_latency_us.attr,
  566. NULL,
  567. };
  568. static const struct attribute_group pm_qos_resume_latency_attr_group = {
  569. .name = power_group_name,
  570. .attrs = pm_qos_resume_latency_attrs,
  571. };
  572. static struct attribute *pm_qos_latency_tolerance_attrs[] = {
  573. &dev_attr_pm_qos_latency_tolerance_us.attr,
  574. NULL,
  575. };
  576. static const struct attribute_group pm_qos_latency_tolerance_attr_group = {
  577. .name = power_group_name,
  578. .attrs = pm_qos_latency_tolerance_attrs,
  579. };
  580. static struct attribute *pm_qos_flags_attrs[] = {
  581. &dev_attr_pm_qos_no_power_off.attr,
  582. NULL,
  583. };
  584. static const struct attribute_group pm_qos_flags_attr_group = {
  585. .name = power_group_name,
  586. .attrs = pm_qos_flags_attrs,
  587. };
  588. int dpm_sysfs_add(struct device *dev)
  589. {
  590. int rc;
  591. /* No need to create PM sysfs if explicitly disabled. */
  592. if (device_pm_not_required(dev))
  593. return 0;
  594. rc = sysfs_create_group(&dev->kobj, &pm_attr_group);
  595. if (rc)
  596. return rc;
  597. if (!pm_runtime_has_no_callbacks(dev)) {
  598. rc = sysfs_merge_group(&dev->kobj, &pm_runtime_attr_group);
  599. if (rc)
  600. goto err_out;
  601. }
  602. if (device_can_wakeup(dev)) {
  603. rc = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
  604. if (rc)
  605. goto err_runtime;
  606. }
  607. if (dev->power.set_latency_tolerance) {
  608. rc = sysfs_merge_group(&dev->kobj,
  609. &pm_qos_latency_tolerance_attr_group);
  610. if (rc)
  611. goto err_wakeup;
  612. }
  613. rc = pm_wakeup_source_sysfs_add(dev);
  614. if (rc)
  615. goto err_latency;
  616. return 0;
  617. err_latency:
  618. sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
  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 dpm_sysfs_change_owner(struct device *dev, kuid_t kuid, kgid_t kgid)
  628. {
  629. int rc;
  630. if (device_pm_not_required(dev))
  631. return 0;
  632. rc = sysfs_group_change_owner(&dev->kobj, &pm_attr_group, kuid, kgid);
  633. if (rc)
  634. return rc;
  635. if (!pm_runtime_has_no_callbacks(dev)) {
  636. rc = sysfs_group_change_owner(
  637. &dev->kobj, &pm_runtime_attr_group, kuid, kgid);
  638. if (rc)
  639. return rc;
  640. }
  641. if (device_can_wakeup(dev)) {
  642. rc = sysfs_group_change_owner(&dev->kobj, &pm_wakeup_attr_group,
  643. kuid, kgid);
  644. if (rc)
  645. return rc;
  646. rc = dpm_sysfs_wakeup_change_owner(dev, kuid, kgid);
  647. if (rc)
  648. return rc;
  649. }
  650. if (dev->power.set_latency_tolerance) {
  651. rc = sysfs_group_change_owner(
  652. &dev->kobj, &pm_qos_latency_tolerance_attr_group, kuid,
  653. kgid);
  654. if (rc)
  655. return rc;
  656. }
  657. return 0;
  658. }
  659. int wakeup_sysfs_add(struct device *dev)
  660. {
  661. int ret = sysfs_merge_group(&dev->kobj, &pm_wakeup_attr_group);
  662. if (!ret)
  663. kobject_uevent(&dev->kobj, KOBJ_CHANGE);
  664. return ret;
  665. }
  666. void wakeup_sysfs_remove(struct device *dev)
  667. {
  668. sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
  669. kobject_uevent(&dev->kobj, KOBJ_CHANGE);
  670. }
  671. int pm_qos_sysfs_add_resume_latency(struct device *dev)
  672. {
  673. return sysfs_merge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
  674. }
  675. void pm_qos_sysfs_remove_resume_latency(struct device *dev)
  676. {
  677. sysfs_unmerge_group(&dev->kobj, &pm_qos_resume_latency_attr_group);
  678. }
  679. int pm_qos_sysfs_add_flags(struct device *dev)
  680. {
  681. return sysfs_merge_group(&dev->kobj, &pm_qos_flags_attr_group);
  682. }
  683. void pm_qos_sysfs_remove_flags(struct device *dev)
  684. {
  685. sysfs_unmerge_group(&dev->kobj, &pm_qos_flags_attr_group);
  686. }
  687. int pm_qos_sysfs_add_latency_tolerance(struct device *dev)
  688. {
  689. return sysfs_merge_group(&dev->kobj,
  690. &pm_qos_latency_tolerance_attr_group);
  691. }
  692. void pm_qos_sysfs_remove_latency_tolerance(struct device *dev)
  693. {
  694. sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
  695. }
  696. void rpm_sysfs_remove(struct device *dev)
  697. {
  698. sysfs_unmerge_group(&dev->kobj, &pm_runtime_attr_group);
  699. }
  700. void dpm_sysfs_remove(struct device *dev)
  701. {
  702. if (device_pm_not_required(dev))
  703. return;
  704. sysfs_unmerge_group(&dev->kobj, &pm_qos_latency_tolerance_attr_group);
  705. dev_pm_qos_constraints_destroy(dev);
  706. rpm_sysfs_remove(dev);
  707. sysfs_unmerge_group(&dev->kobj, &pm_wakeup_attr_group);
  708. sysfs_remove_group(&dev->kobj, &pm_attr_group);
  709. }