common.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. /**
  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 1 if a new object has been created, otherwise
  21. * return 0 or 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. Return 1 if that happens or 0
  51. * otherwise.
  52. */
  53. int dev_pm_put_subsys_data(struct device *dev)
  54. {
  55. struct pm_subsys_data *psd;
  56. int ret = 1;
  57. spin_lock_irq(&dev->power.lock);
  58. psd = dev_to_psd(dev);
  59. if (!psd)
  60. goto out;
  61. if (--psd->refcount == 0) {
  62. dev->power.subsys_data = NULL;
  63. } else {
  64. psd = NULL;
  65. ret = 0;
  66. }
  67. out:
  68. spin_unlock_irq(&dev->power.lock);
  69. kfree(psd);
  70. return ret;
  71. }
  72. EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);
  73. /**
  74. * dev_pm_domain_attach - Attach a device to its PM domain.
  75. * @dev: Device to attach.
  76. * @power_on: Used to indicate whether we should power on the device.
  77. *
  78. * The @dev may only be attached to a single PM domain. By iterating through
  79. * the available alternatives we try to find a valid PM domain for the device.
  80. * As attachment succeeds, the ->detach() callback in the struct dev_pm_domain
  81. * should be assigned by the corresponding attach function.
  82. *
  83. * This function should typically be invoked from subsystem level code during
  84. * the probe phase. Especially for those that holds devices which requires
  85. * power management through PM domains.
  86. *
  87. * Callers must ensure proper synchronization of this function with power
  88. * management callbacks.
  89. *
  90. * Returns 0 on successfully attached PM domain or negative error code.
  91. */
  92. int dev_pm_domain_attach(struct device *dev, bool power_on)
  93. {
  94. int ret;
  95. ret = acpi_dev_pm_attach(dev, power_on);
  96. if (ret)
  97. ret = genpd_dev_pm_attach(dev);
  98. return ret;
  99. }
  100. EXPORT_SYMBOL_GPL(dev_pm_domain_attach);
  101. /**
  102. * dev_pm_domain_detach - Detach a device from its PM domain.
  103. * @dev: Device to attach.
  104. * @power_off: Used to indicate whether we should power off the device.
  105. *
  106. * This functions will reverse the actions from dev_pm_domain_attach() and thus
  107. * try to detach the @dev from its PM domain. Typically it should be invoked
  108. * from subsystem level code during the remove phase.
  109. *
  110. * Callers must ensure proper synchronization of this function with power
  111. * management callbacks.
  112. */
  113. void dev_pm_domain_detach(struct device *dev, bool power_off)
  114. {
  115. if (dev->pm_domain && dev->pm_domain->detach)
  116. dev->pm_domain->detach(dev, power_off);
  117. }
  118. EXPORT_SYMBOL_GPL(dev_pm_domain_detach);