common.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 when it is found that the
  87. * device doesn't need a PM domain, else a negative error code.
  88. */
  89. int dev_pm_domain_attach(struct device *dev, bool power_on)
  90. {
  91. int ret;
  92. if (dev->pm_domain)
  93. return 0;
  94. ret = acpi_dev_pm_attach(dev, power_on);
  95. if (!ret)
  96. ret = genpd_dev_pm_attach(dev);
  97. return ret < 0 ? ret : 0;
  98. }
  99. EXPORT_SYMBOL_GPL(dev_pm_domain_attach);
  100. /**
  101. * dev_pm_domain_attach_by_id - Associate a device with one of its PM domains.
  102. * @dev: The device used to lookup the PM domain.
  103. * @index: The index of the PM domain.
  104. *
  105. * As @dev may only be attached to a single PM domain, the backend PM domain
  106. * provider creates a virtual device to attach instead. If attachment succeeds,
  107. * the ->detach() callback in the struct dev_pm_domain are assigned by the
  108. * corresponding backend attach function, as to deal with detaching of the
  109. * created virtual device.
  110. *
  111. * This function should typically be invoked by a driver during the probe phase,
  112. * in case its device requires power management through multiple PM domains. The
  113. * driver may benefit from using the received device, to configure device-links
  114. * towards its original device. Depending on the use-case and if needed, the
  115. * links may be dynamically changed by the driver, which allows it to control
  116. * the power to the PM domains independently from each other.
  117. *
  118. * Callers must ensure proper synchronization of this function with power
  119. * management callbacks.
  120. *
  121. * Returns the virtual created device when successfully attached to its PM
  122. * domain, NULL in case @dev don't need a PM domain, else an ERR_PTR().
  123. * Note that, to detach the returned virtual device, the driver shall call
  124. * dev_pm_domain_detach() on it, typically during the remove phase.
  125. */
  126. struct device *dev_pm_domain_attach_by_id(struct device *dev,
  127. unsigned int index)
  128. {
  129. if (dev->pm_domain)
  130. return ERR_PTR(-EEXIST);
  131. return genpd_dev_pm_attach_by_id(dev, index);
  132. }
  133. EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_id);
  134. /**
  135. * dev_pm_domain_attach_by_name - Associate a device with one of its PM domains.
  136. * @dev: The device used to lookup the PM domain.
  137. * @name: The name of the PM domain.
  138. *
  139. * For a detailed function description, see dev_pm_domain_attach_by_id().
  140. */
  141. struct device *dev_pm_domain_attach_by_name(struct device *dev,
  142. char *name)
  143. {
  144. if (dev->pm_domain)
  145. return ERR_PTR(-EEXIST);
  146. return genpd_dev_pm_attach_by_name(dev, name);
  147. }
  148. EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_name);
  149. /**
  150. * dev_pm_domain_detach - Detach a device from its PM domain.
  151. * @dev: Device to detach.
  152. * @power_off: Used to indicate whether we should power off the device.
  153. *
  154. * This functions will reverse the actions from dev_pm_domain_attach() and
  155. * dev_pm_domain_attach_by_id(), thus it detaches @dev from its PM domain.
  156. * Typically it should be invoked during the remove phase, either from
  157. * subsystem level code or from drivers.
  158. *
  159. * Callers must ensure proper synchronization of this function with power
  160. * management callbacks.
  161. */
  162. void dev_pm_domain_detach(struct device *dev, bool power_off)
  163. {
  164. if (dev->pm_domain && dev->pm_domain->detach)
  165. dev->pm_domain->detach(dev, power_off);
  166. }
  167. EXPORT_SYMBOL_GPL(dev_pm_domain_detach);
  168. /**
  169. * dev_pm_domain_set - Set PM domain of a device.
  170. * @dev: Device whose PM domain is to be set.
  171. * @pd: PM domain to be set, or NULL.
  172. *
  173. * Sets the PM domain the device belongs to. The PM domain of a device needs
  174. * to be set before its probe finishes (it's bound to a driver).
  175. *
  176. * This function must be called with the device lock held.
  177. */
  178. void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd)
  179. {
  180. if (dev->pm_domain == pd)
  181. return;
  182. WARN(pd && device_is_bound(dev),
  183. "PM domains can only be changed for unbound devices\n");
  184. dev->pm_domain = pd;
  185. device_pm_check_callbacks(dev);
  186. }
  187. EXPORT_SYMBOL_GPL(dev_pm_domain_set);