common.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/base/power/common.c - Common device power management code.
  4. *
  5. * Copyright (C) 2011 Rafael J. Wysocki <rjw@sisk.pl>, Renesas Electronics Corp.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/device.h>
  9. #include <linux/export.h>
  10. #include <linux/slab.h>
  11. #include <linux/pm_clock.h>
  12. #include <linux/acpi.h>
  13. #include <linux/pm_domain.h>
  14. #include "power.h"
  15. /**
  16. * dev_pm_get_subsys_data - Create or refcount power.subsys_data for device.
  17. * @dev: Device to handle.
  18. *
  19. * If power.subsys_data is NULL, point it to a new object, otherwise increment
  20. * its reference counter. Return 0 if new object has been created or refcount
  21. * increased, otherwise negative error code.
  22. */
  23. int dev_pm_get_subsys_data(struct device *dev)
  24. {
  25. struct pm_subsys_data *psd;
  26. psd = kzalloc(sizeof(*psd), GFP_KERNEL);
  27. if (!psd)
  28. return -ENOMEM;
  29. spin_lock_irq(&dev->power.lock);
  30. if (dev->power.subsys_data) {
  31. dev->power.subsys_data->refcount++;
  32. } else {
  33. spin_lock_init(&psd->lock);
  34. psd->refcount = 1;
  35. dev->power.subsys_data = psd;
  36. pm_clk_init(dev);
  37. psd = NULL;
  38. }
  39. spin_unlock_irq(&dev->power.lock);
  40. /* kfree() verifies that its argument is nonzero. */
  41. kfree(psd);
  42. return 0;
  43. }
  44. EXPORT_SYMBOL_GPL(dev_pm_get_subsys_data);
  45. /**
  46. * dev_pm_put_subsys_data - Drop reference to power.subsys_data.
  47. * @dev: Device to handle.
  48. *
  49. * If the reference counter of power.subsys_data is zero after dropping the
  50. * reference, power.subsys_data is removed.
  51. */
  52. void dev_pm_put_subsys_data(struct device *dev)
  53. {
  54. struct pm_subsys_data *psd;
  55. spin_lock_irq(&dev->power.lock);
  56. psd = dev_to_psd(dev);
  57. if (!psd)
  58. goto out;
  59. if (--psd->refcount == 0)
  60. dev->power.subsys_data = NULL;
  61. else
  62. psd = NULL;
  63. out:
  64. spin_unlock_irq(&dev->power.lock);
  65. kfree(psd);
  66. }
  67. EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);
  68. /**
  69. * dev_pm_domain_attach - Attach a device to its PM domain.
  70. * @dev: Device to attach.
  71. * @power_on: Used to indicate whether we should power on the device.
  72. *
  73. * The @dev may only be attached to a single PM domain. By iterating through
  74. * the available alternatives we try to find a valid PM domain for the device.
  75. * As attachment succeeds, the ->detach() callback in the struct dev_pm_domain
  76. * should be assigned by the corresponding attach function.
  77. *
  78. * This function should typically be invoked from subsystem level code during
  79. * the probe phase. Especially for those that holds devices which requires
  80. * power management through PM domains.
  81. *
  82. * Callers must ensure proper synchronization of this function with power
  83. * management callbacks.
  84. *
  85. * Returns 0 on successfully attached PM domain, or when it is found that the
  86. * device doesn't need a PM domain, else a negative error code.
  87. */
  88. int dev_pm_domain_attach(struct device *dev, bool power_on)
  89. {
  90. int ret;
  91. if (dev->pm_domain)
  92. return 0;
  93. ret = acpi_dev_pm_attach(dev, power_on);
  94. if (!ret)
  95. ret = genpd_dev_pm_attach(dev);
  96. return ret < 0 ? ret : 0;
  97. }
  98. EXPORT_SYMBOL_GPL(dev_pm_domain_attach);
  99. /**
  100. * dev_pm_domain_attach_by_id - Associate a device with one of its PM domains.
  101. * @dev: The device used to lookup the PM domain.
  102. * @index: The index of the PM domain.
  103. *
  104. * As @dev may only be attached to a single PM domain, the backend PM domain
  105. * provider creates a virtual device to attach instead. If attachment succeeds,
  106. * the ->detach() callback in the struct dev_pm_domain are assigned by the
  107. * corresponding backend attach function, as to deal with detaching of the
  108. * created virtual device.
  109. *
  110. * This function should typically be invoked by a driver during the probe phase,
  111. * in case its device requires power management through multiple PM domains. The
  112. * driver may benefit from using the received device, to configure device-links
  113. * towards its original device. Depending on the use-case and if needed, the
  114. * links may be dynamically changed by the driver, which allows it to control
  115. * the power to the PM domains independently from each other.
  116. *
  117. * Callers must ensure proper synchronization of this function with power
  118. * management callbacks.
  119. *
  120. * Returns the virtual created device when successfully attached to its PM
  121. * domain, NULL in case @dev don't need a PM domain, else an ERR_PTR().
  122. * Note that, to detach the returned virtual device, the driver shall call
  123. * dev_pm_domain_detach() on it, typically during the remove phase.
  124. */
  125. struct device *dev_pm_domain_attach_by_id(struct device *dev,
  126. unsigned int index)
  127. {
  128. if (dev->pm_domain)
  129. return ERR_PTR(-EEXIST);
  130. return genpd_dev_pm_attach_by_id(dev, index);
  131. }
  132. EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_id);
  133. /**
  134. * dev_pm_domain_attach_by_name - Associate a device with one of its PM domains.
  135. * @dev: The device used to lookup the PM domain.
  136. * @name: The name of the PM domain.
  137. *
  138. * For a detailed function description, see dev_pm_domain_attach_by_id().
  139. */
  140. struct device *dev_pm_domain_attach_by_name(struct device *dev,
  141. const char *name)
  142. {
  143. if (dev->pm_domain)
  144. return ERR_PTR(-EEXIST);
  145. return genpd_dev_pm_attach_by_name(dev, name);
  146. }
  147. EXPORT_SYMBOL_GPL(dev_pm_domain_attach_by_name);
  148. /**
  149. * dev_pm_domain_detach - Detach a device from its PM domain.
  150. * @dev: Device to detach.
  151. * @power_off: Used to indicate whether we should power off the device.
  152. *
  153. * This functions will reverse the actions from dev_pm_domain_attach() and
  154. * dev_pm_domain_attach_by_id(), thus it detaches @dev from its PM domain.
  155. * Typically it should be invoked during the remove phase, either from
  156. * subsystem level code or from drivers.
  157. *
  158. * Callers must ensure proper synchronization of this function with power
  159. * management callbacks.
  160. */
  161. void dev_pm_domain_detach(struct device *dev, bool power_off)
  162. {
  163. if (dev->pm_domain && dev->pm_domain->detach)
  164. dev->pm_domain->detach(dev, power_off);
  165. }
  166. EXPORT_SYMBOL_GPL(dev_pm_domain_detach);
  167. /**
  168. * dev_pm_domain_start - Start the device through its PM domain.
  169. * @dev: Device to start.
  170. *
  171. * This function should typically be called during probe by a subsystem/driver,
  172. * when it needs to start its device from the PM domain's perspective. Note
  173. * that, it's assumed that the PM domain is already powered on when this
  174. * function is called.
  175. *
  176. * Returns 0 on success and negative error values on failures.
  177. */
  178. int dev_pm_domain_start(struct device *dev)
  179. {
  180. if (dev->pm_domain && dev->pm_domain->start)
  181. return dev->pm_domain->start(dev);
  182. return 0;
  183. }
  184. EXPORT_SYMBOL_GPL(dev_pm_domain_start);
  185. /**
  186. * dev_pm_domain_set - Set PM domain of a device.
  187. * @dev: Device whose PM domain is to be set.
  188. * @pd: PM domain to be set, or NULL.
  189. *
  190. * Sets the PM domain the device belongs to. The PM domain of a device needs
  191. * to be set before its probe finishes (it's bound to a driver).
  192. *
  193. * This function must be called with the device lock held.
  194. */
  195. void dev_pm_domain_set(struct device *dev, struct dev_pm_domain *pd)
  196. {
  197. if (dev->pm_domain == pd)
  198. return;
  199. WARN(pd && device_is_bound(dev),
  200. "PM domains can only be changed for unbound devices\n");
  201. dev->pm_domain = pd;
  202. device_pm_check_callbacks(dev);
  203. }
  204. EXPORT_SYMBOL_GPL(dev_pm_domain_set);