main.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * drivers/base/power/main.c - Where the driver meets power management.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Open Source Development Lab
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. *
  10. * The driver model core calls device_pm_add() when a device is registered.
  11. * This will intialize the embedded device_pm_info object in the device
  12. * and add it to the list of power-controlled devices. sysfs entries for
  13. * controlling device power management will also be added.
  14. *
  15. * A different set of lists than the global subsystem list are used to
  16. * keep track of power info because we use different lists to hold
  17. * devices based on what stage of the power management process they
  18. * are in. The power domain dependencies may also differ from the
  19. * ancestral dependencies that the subsystem list maintains.
  20. */
  21. #include <linux/config.h>
  22. #include <linux/device.h>
  23. #include "power.h"
  24. LIST_HEAD(dpm_active);
  25. LIST_HEAD(dpm_off);
  26. LIST_HEAD(dpm_off_irq);
  27. DECLARE_MUTEX(dpm_sem);
  28. DECLARE_MUTEX(dpm_list_sem);
  29. /*
  30. * PM Reference Counting.
  31. */
  32. static inline void device_pm_hold(struct device * dev)
  33. {
  34. if (dev)
  35. atomic_inc(&dev->power.pm_users);
  36. }
  37. static inline void device_pm_release(struct device * dev)
  38. {
  39. if (dev)
  40. atomic_dec(&dev->power.pm_users);
  41. }
  42. /**
  43. * device_pm_set_parent - Specify power dependency.
  44. * @dev: Device who needs power.
  45. * @parent: Device that supplies power.
  46. *
  47. * This function is used to manually describe a power-dependency
  48. * relationship. It may be used to specify a transversal relationship
  49. * (where the power supplier is not the physical (or electrical)
  50. * ancestor of a specific device.
  51. * The effect of this is that the supplier will not be powered down
  52. * before the power dependent.
  53. */
  54. void device_pm_set_parent(struct device * dev, struct device * parent)
  55. {
  56. struct device * old_parent = dev->power.pm_parent;
  57. device_pm_release(old_parent);
  58. dev->power.pm_parent = parent;
  59. device_pm_hold(parent);
  60. }
  61. EXPORT_SYMBOL_GPL(device_pm_set_parent);
  62. int device_pm_add(struct device * dev)
  63. {
  64. int error;
  65. pr_debug("PM: Adding info for %s:%s\n",
  66. dev->bus ? dev->bus->name : "No Bus", dev->kobj.name);
  67. atomic_set(&dev->power.pm_users, 0);
  68. down(&dpm_list_sem);
  69. list_add_tail(&dev->power.entry, &dpm_active);
  70. device_pm_set_parent(dev, dev->parent);
  71. if ((error = dpm_sysfs_add(dev)))
  72. list_del(&dev->power.entry);
  73. up(&dpm_list_sem);
  74. return error;
  75. }
  76. void device_pm_remove(struct device * dev)
  77. {
  78. pr_debug("PM: Removing info for %s:%s\n",
  79. dev->bus ? dev->bus->name : "No Bus", dev->kobj.name);
  80. down(&dpm_list_sem);
  81. dpm_sysfs_remove(dev);
  82. device_pm_release(dev->power.pm_parent);
  83. list_del_init(&dev->power.entry);
  84. up(&dpm_list_sem);
  85. }