2 * driver.c - driver support
4 * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 * Shaohua Li <shaohua.li@intel.com>
6 * Adam Belay <abelay@novell.com>
8 * This code is licenced under the GPL.
11 #include <linux/mutex.h>
12 #include <linux/module.h>
13 #include <linux/cpuidle.h>
17 static struct cpuidle_driver *cpuidle_curr_driver;
18 DEFINE_SPINLOCK(cpuidle_driver_lock);
19 int cpuidle_driver_refcount;
21 static void set_power_states(struct cpuidle_driver *drv)
26 * cpuidle driver should set the drv->power_specified bit
27 * before registering if the driver provides
28 * power_usage numbers.
30 * If power_specified is not set,
31 * we fill in power_usage with decreasing values as the
32 * cpuidle code has an implicit assumption that state Cn
33 * uses less power than C(n-1).
35 * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
36 * an power value of -1. So we use -2, -3, etc, for other
39 for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++)
40 drv->states[i].power_usage = -1 - i;
44 * cpuidle_register_driver - registers a driver
47 int cpuidle_register_driver(struct cpuidle_driver *drv)
49 if (!drv || !drv->state_count)
52 if (cpuidle_disabled())
55 spin_lock(&cpuidle_driver_lock);
56 if (cpuidle_curr_driver) {
57 spin_unlock(&cpuidle_driver_lock);
61 if (!drv->power_specified)
62 set_power_states(drv);
64 cpuidle_curr_driver = drv;
66 spin_unlock(&cpuidle_driver_lock);
70 EXPORT_SYMBOL_GPL(cpuidle_register_driver);
73 * cpuidle_get_driver - return the current driver
75 struct cpuidle_driver *cpuidle_get_driver(void)
77 return cpuidle_curr_driver;
79 EXPORT_SYMBOL_GPL(cpuidle_get_driver);
82 * cpuidle_unregister_driver - unregisters a driver
85 void cpuidle_unregister_driver(struct cpuidle_driver *drv)
87 if (drv != cpuidle_curr_driver) {
88 WARN(1, "invalid cpuidle_unregister_driver(%s)\n",
93 spin_lock(&cpuidle_driver_lock);
95 if (!WARN_ON(cpuidle_driver_refcount > 0))
96 cpuidle_curr_driver = NULL;
98 spin_unlock(&cpuidle_driver_lock);
100 EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
102 struct cpuidle_driver *cpuidle_driver_ref(void)
104 struct cpuidle_driver *drv;
106 spin_lock(&cpuidle_driver_lock);
108 drv = cpuidle_curr_driver;
109 cpuidle_driver_refcount++;
111 spin_unlock(&cpuidle_driver_lock);
115 void cpuidle_driver_unref(void)
117 spin_lock(&cpuidle_driver_lock);
119 if (!WARN_ON(cpuidle_driver_refcount <= 0))
120 cpuidle_driver_refcount--;
122 spin_unlock(&cpuidle_driver_lock);