common.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * drivers/base/power/common.c - Common device power management code.
  3. *
  4. * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
  5. *
  6. * This file is released under the GPLv2.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/device.h>
  10. #include <linux/export.h>
  11. #include <linux/slab.h>
  12. #include <linux/pm_clock.h>
  13. #include <linux/acpi.h>
  14. #include <linux/pm_domain.h>
  15. #include "power.h"
  16. /**
  17. * dev_pm_get_subsys_data - Create or refcount power.subsys_data for device.
  18. * @dev: Device to handle.
  19. *
  20. * If power.subsys_data is NULL, point it to a new object, otherwise increment
  21. * its reference counter. Return 0 if new object has been created or refcount
  22. * increased, otherwise negative error code.
  23. */
  24. int dev_pm_get_subsys_data(struct device *dev)
  25. {
  26. struct pm_subsys_data *psd;
  27. psd = kzalloc(sizeof(*psd), GFP_KERNEL);
  28. if (!psd)
  29. return -ENOMEM;
  30. spin_lock_irq(&dev->power.lock);
  31. if (dev->power.subsys_data) {
  32. dev->power.subsys_data->refcount++;
  33. } else {
  34. spin_lock_init(&psd->lock);
  35. psd->refcount = 1;
  36. dev->power.subsys_data = psd;
  37. pm_clk_init(dev);
  38. psd = NULL;
  39. }
  40. spin_unlock_irq(&dev->power.lock);
  41. /* kfree() verifies that its argument is nonzero. */
  42. kfree(psd);
  43. return 0;
  44. }
  45. EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data);
  46. /**
  47. * dev_pm_put_subsys_data - Drop reference to power.subsys_data.
  48. * @dev: Device to handle.
  49. *
  50. * If the reference counter of power.subsys_data is zero after dropping the
  51. * reference, power.subsys_data is removed.
  52. */
  53. void dev_pm_put_subsys_data(struct device *dev)
  54. {
  55. struct pm_subsys_data *psd;
  56. spin_lock_irq(&dev->power.lock);
  57. psd = dev_to_psd(dev);
  58. if (!psd)
  59. goto out;
  60. if (--psd->refcount == 0)
  61. dev->power.subsys_data = NULL;
  62. else
  63. psd = NULL;
  64. out:
  65. spin_unlock_irq(&dev->power.lock);
  66. kfree(psd);
  67. }
  68. EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);
  69. /**
  70. * dev_pm_domain_attach - Attach a device to its PM domain.
  71. * @dev: Device to attach.
  72. * @power_on: Used to indicate whether we should power on the device.
  73. *
  74. * The @dev may only be attached to a single PM domain. By iterating through
  75. * the available alternatives we try to find a valid PM domain for the device.
  76. * As attachment succeeds, the ->detach() callback in the struct dev_pm_domain
  77. * should be assigned by the corresponding attach function.
  78. *
  79. * This function should typically be invoked from subsystem level code during
  80. * the probe phase. Especially for those that holds devices which requires
  81. * power management through PM domains.
  82. *
  83. * Callers must ensure proper synchronization of this function with power
  84. * management callbacks.
  85. *
  86. * Returns 0 on successfully attached PM domain or negative error code.
  87. */
  88. int dev_pm_domain_attach(struct device *dev, bool power_on)
  89. {
  90. int ret;
  91. ret = acpi_dev_pm_attach(dev, power_on);
  92. if (ret)
  93. ret = genpd_dev_pm_attach(dev);
  94. return ret;
  95. }
  96. EXPORT_SYMBOL_GPL(dev_pm_domain_attach);
  97. /**
  98. * dev_pm_domain_detach - Detach a device from its PM domain.
  99. * @dev: Device to detach.
  100. * @power_off: Used to indicate whether we should power off the device.
  101. *
  102. * This functions will reverse the actions from dev_pm_domain_attach() and thus
  103. * try to detach the @dev from its PM domain. Typically it should be invoked
  104. * from subsystem level code during the remove phase.
  105. *
  106. * Callers must ensure proper synchronization of this function with power
  107. * management callbacks.
  108. */
  109. void dev_pm_domain_detach(struct device *dev, bool power_off)
  110. {
  111. if (dev->pm_domain && dev->pm_domain->detach)
  112. dev->pm_domain->detach(dev, power_off);
  113. }
  114. EXPORT_SYMBOL_GPL(dev_pm_domain_detach);
  115. /**
  116. * dev_pm_domain_set - Set PM domain of a device.
  117. * @dev: Device whose PM domain is to be set.
  118. * @pd: PM domain to be set, or NULL.
  119. *
  120. * Sets the PM domain the device belongs to. The PM domain of a device needs
  121. * to be set before its probe finishes (it's bound to a driver).
  122. *
  123. * This function must be called with the device lock held.
  124. */
  125. void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd)
  126. {
  127. if (dev->pm_domain == pd)
  128. return;
  129. WARN(pd && device_is_bound(dev),
  130. "PM domains can only be changed for unbound devices\n");
  131. dev->pm_domain = pd;
  132. device_pm_check_callbacks(dev);
  133. }
  134. EXPORT_SYMBOL_GPL(dev_pm_domain_set);