common.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 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 negative error code.
  86. */
  87. int dev_pm_domain_attach(struct device *dev, bool power_on)
  88. {
  89. int ret;
  90. ret = acpi_dev_pm_attach(dev, power_on);
  91. if (ret)
  92. ret = genpd_dev_pm_attach(dev);
  93. return ret;
  94. }
  95. EXPORT_SYMBOL_GPL(dev_pm_domain_attach);
  96. /**
  97. * dev_pm_domain_detach - Detach a device from its PM domain.
  98. * @dev: Device to attach.
  99. * @power_off: Used to indicate whether we should power off the device.
  100. *
  101. * This functions will reverse the actions from dev_pm_domain_attach() and thus
  102. * try to detach the @dev from its PM domain. Typically it should be invoked
  103. * from subsystem level code during the remove phase.
  104. *
  105. * Callers must ensure proper synchronization of this function with power
  106. * management callbacks.
  107. */
  108. void dev_pm_domain_detach(struct device *dev, bool power_off)
  109. {
  110. if (dev->pm_domain && dev->pm_domain->detach)
  111. dev->pm_domain->detach(dev, power_off);
  112. }
  113. EXPORT_SYMBOL_GPL(dev_pm_domain_detach);