driver.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * driver.c - driver support
  3. *
  4. * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  5. * Shaohua Li <shaohua.li@intel.com>
  6. * Adam Belay <abelay@novell.com>
  7. *
  8. * This code is licenced under the GPL.
  9. */
  10. #include <linux/mutex.h>
  11. #include <linux/module.h>
  12. #include <linux/cpuidle.h>
  13. #include "cpuidle.h"
  14. static struct cpuidle_driver *cpuidle_curr_driver;
  15. DEFINE_SPINLOCK(cpuidle_driver_lock);
  16. static void __cpuidle_register_driver(struct cpuidle_driver *drv)
  17. {
  18. int i;
  19. /*
  20. * cpuidle driver should set the drv->power_specified bit
  21. * before registering if the driver provides
  22. * power_usage numbers.
  23. *
  24. * If power_specified is not set,
  25. * we fill in power_usage with decreasing values as the
  26. * cpuidle code has an implicit assumption that state Cn
  27. * uses less power than C(n-1).
  28. *
  29. * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
  30. * an power value of -1. So we use -2, -3, etc, for other
  31. * c-states.
  32. */
  33. if (!drv->power_specified) {
  34. for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++)
  35. drv->states[i].power_usage = -1 - i;
  36. }
  37. }
  38. /**
  39. * cpuidle_register_driver - registers a driver
  40. * @drv: the driver
  41. */
  42. int cpuidle_register_driver(struct cpuidle_driver *drv)
  43. {
  44. if (!drv || !drv->state_count)
  45. return -EINVAL;
  46. if (cpuidle_disabled())
  47. return -ENODEV;
  48. spin_lock(&cpuidle_driver_lock);
  49. if (cpuidle_curr_driver) {
  50. spin_unlock(&cpuidle_driver_lock);
  51. return -EBUSY;
  52. }
  53. __cpuidle_register_driver(drv);
  54. cpuidle_curr_driver = drv;
  55. spin_unlock(&cpuidle_driver_lock);
  56. return 0;
  57. }
  58. EXPORT_SYMBOL_GPL(cpuidle_register_driver);
  59. /**
  60. * cpuidle_get_driver - return the current driver
  61. */
  62. struct cpuidle_driver *cpuidle_get_driver(void)
  63. {
  64. return cpuidle_curr_driver;
  65. }
  66. EXPORT_SYMBOL_GPL(cpuidle_get_driver);
  67. /**
  68. * cpuidle_unregister_driver - unregisters a driver
  69. * @drv: the driver
  70. */
  71. void cpuidle_unregister_driver(struct cpuidle_driver *drv)
  72. {
  73. if (drv != cpuidle_curr_driver) {
  74. WARN(1, "invalid cpuidle_unregister_driver(%s)\n",
  75. drv->name);
  76. return;
  77. }
  78. spin_lock(&cpuidle_driver_lock);
  79. cpuidle_curr_driver = NULL;
  80. spin_unlock(&cpuidle_driver_lock);
  81. }
  82. EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);