sysfs.c 21 KB

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