common.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/device.h>
  11. #include <linux/export.h>
  12. #include <linux/slab.h>
  13. #include <linux/pm_clock.h>
  14. /**
  15. * dev_pm_get_subsys_data - Create or refcount power.subsys_data for device.
  16. * @dev: Device to handle.
  17. *
  18. * If power.subsys_data is NULL, point it to a new object, otherwise increment
  19. * its reference counter. Return 1 if a new object has been created, otherwise
  20. * return 0 or error code.
  21. */
  22. int dev_pm_get_subsys_data(struct device *dev)
  23. {
  24. struct pm_subsys_data *psd;
  25. int ret = 0;
  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. ret = 1;
  39. }
  40. spin_unlock_irq(&dev->power.lock);
  41. /* kfree() verifies that its argument is nonzero. */
  42. kfree(psd);
  43. return ret;
  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. Return 1 if that happens or 0
  52. * otherwise.
  53. */
  54. int dev_pm_put_subsys_data(struct device *dev)
  55. {
  56. struct pm_subsys_data *psd;
  57. int ret = 0;
  58. spin_lock_irq(&dev->power.lock);
  59. psd = dev_to_psd(dev);
  60. if (!psd) {
  61. ret = -EINVAL;
  62. goto out;
  63. }
  64. if (--psd->refcount == 0) {
  65. dev->power.subsys_data = NULL;
  66. kfree(psd);
  67. ret = 1;
  68. }
  69. out:
  70. spin_unlock_irq(&dev->power.lock);
  71. return ret;
  72. }
  73. EXPORT_SYMBOL_GPL(dev_pm_put_subsys_data);