cpuidle: move driver's refcount to cpuidle
[deliverable/linux.git] / drivers / cpuidle / driver.c
CommitLineData
4f86d3a8
LB
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
11#include <linux/mutex.h>
12#include <linux/module.h>
13#include <linux/cpuidle.h>
14
15#include "cpuidle.h"
16
752138df 17static struct cpuidle_driver *cpuidle_curr_driver;
4f86d3a8
LB
18DEFINE_SPINLOCK(cpuidle_driver_lock);
19
ed953472 20static void set_power_states(struct cpuidle_driver *drv)
46bcfad7
DD
21{
22 int i;
ed953472 23
46bcfad7
DD
24 /*
25 * cpuidle driver should set the drv->power_specified bit
26 * before registering if the driver provides
27 * power_usage numbers.
28 *
29 * If power_specified is not set,
30 * we fill in power_usage with decreasing values as the
31 * cpuidle code has an implicit assumption that state Cn
32 * uses less power than C(n-1).
33 *
34 * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
35 * an power value of -1. So we use -2, -3, etc, for other
36 * c-states.
37 */
ed953472
DL
38 for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++)
39 drv->states[i].power_usage = -1 - i;
46bcfad7
DD
40}
41
4f86d3a8
LB
42/**
43 * cpuidle_register_driver - registers a driver
44 * @drv: the driver
45 */
46int cpuidle_register_driver(struct cpuidle_driver *drv)
47{
fc850f39 48 if (!drv || !drv->state_count)
4f86d3a8
LB
49 return -EINVAL;
50
62027aea
LB
51 if (cpuidle_disabled())
52 return -ENODEV;
53
4f86d3a8
LB
54 spin_lock(&cpuidle_driver_lock);
55 if (cpuidle_curr_driver) {
56 spin_unlock(&cpuidle_driver_lock);
57 return -EBUSY;
58 }
ed953472
DL
59
60 if (!drv->power_specified)
61 set_power_states(drv);
62
42f67f2a
DL
63 drv->refcnt = 0;
64
4f86d3a8 65 cpuidle_curr_driver = drv;
ed953472 66
4f86d3a8
LB
67 spin_unlock(&cpuidle_driver_lock);
68
69 return 0;
70}
4f86d3a8
LB
71EXPORT_SYMBOL_GPL(cpuidle_register_driver);
72
752138df
LB
73/**
74 * cpuidle_get_driver - return the current driver
75 */
76struct cpuidle_driver *cpuidle_get_driver(void)
77{
78 return cpuidle_curr_driver;
79}
80EXPORT_SYMBOL_GPL(cpuidle_get_driver);
81
4f86d3a8
LB
82/**
83 * cpuidle_unregister_driver - unregisters a driver
84 * @drv: the driver
85 */
86void cpuidle_unregister_driver(struct cpuidle_driver *drv)
87{
c0d64cb0
LB
88 if (drv != cpuidle_curr_driver) {
89 WARN(1, "invalid cpuidle_unregister_driver(%s)\n",
90 drv->name);
4f86d3a8 91 return;
c0d64cb0 92 }
4f86d3a8
LB
93
94 spin_lock(&cpuidle_driver_lock);
6e797a07 95
42f67f2a 96 if (!WARN_ON(drv->refcnt > 0))
6e797a07
RW
97 cpuidle_curr_driver = NULL;
98
4f86d3a8
LB
99 spin_unlock(&cpuidle_driver_lock);
100}
4f86d3a8 101EXPORT_SYMBOL_GPL(cpuidle_unregister_driver);
6e797a07
RW
102
103struct cpuidle_driver *cpuidle_driver_ref(void)
104{
105 struct cpuidle_driver *drv;
106
107 spin_lock(&cpuidle_driver_lock);
108
109 drv = cpuidle_curr_driver;
42f67f2a 110 drv->refcnt++;
6e797a07
RW
111
112 spin_unlock(&cpuidle_driver_lock);
113 return drv;
114}
115
116void cpuidle_driver_unref(void)
117{
42f67f2a
DL
118 struct cpuidle_driver *drv = cpuidle_curr_driver;
119
6e797a07
RW
120 spin_lock(&cpuidle_driver_lock);
121
42f67f2a
DL
122 if (drv && !WARN_ON(drv->refcnt <= 0))
123 drv->refcnt--;
6e797a07
RW
124
125 spin_unlock(&cpuidle_driver_lock);
126}
This page took 0.375853 seconds and 5 git commands to generate.