[CPUFREQ] Lots of whitespace & CodingStyle cleanup.
[linux-2.6/mini2440.git] / drivers / cpufreq / cpufreq.c
blobcb7d6e0db7591440ebe0aea732171f54bed377d9
1 /*
2 * linux/drivers/cpufreq/cpufreq.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
7 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
8 * Added handling for CPU hotplug
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
16 #include <linux/config.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/notifier.h>
21 #include <linux/cpufreq.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
24 #include <linux/spinlock.h>
25 #include <linux/device.h>
26 #include <linux/slab.h>
27 #include <linux/cpu.h>
28 #include <linux/completion.h>
29 #include <linux/mutex.h>
31 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
33 /**
34 * The "cpufreq driver" - the arch- or hardware-dependend low
35 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
38 static struct cpufreq_driver *cpufreq_driver;
39 static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
40 static DEFINE_SPINLOCK(cpufreq_driver_lock);
42 /* internal prototypes */
43 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
44 static void handle_update(void *data);
46 /**
47 * Two notifier lists: the "policy" list is involved in the
48 * validation process for a new CPU frequency policy; the
49 * "transition" list for kernel code that needs to handle
50 * changes to devices when the CPU clock speed changes.
51 * The mutex locks both lists.
53 static struct notifier_block *cpufreq_policy_notifier_list;
54 static struct notifier_block *cpufreq_transition_notifier_list;
55 static DECLARE_RWSEM (cpufreq_notifier_rwsem);
58 static LIST_HEAD(cpufreq_governor_list);
59 static DEFINE_MUTEX (cpufreq_governor_mutex);
61 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
63 struct cpufreq_policy *data;
64 unsigned long flags;
66 if (cpu >= NR_CPUS)
67 goto err_out;
69 /* get the cpufreq driver */
70 spin_lock_irqsave(&cpufreq_driver_lock, flags);
72 if (!cpufreq_driver)
73 goto err_out_unlock;
75 if (!try_module_get(cpufreq_driver->owner))
76 goto err_out_unlock;
79 /* get the CPU */
80 data = cpufreq_cpu_data[cpu];
82 if (!data)
83 goto err_out_put_module;
85 if (!kobject_get(&data->kobj))
86 goto err_out_put_module;
88 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
89 return data;
91 err_out_put_module:
92 module_put(cpufreq_driver->owner);
93 err_out_unlock:
94 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
95 err_out:
96 return NULL;
98 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
101 void cpufreq_cpu_put(struct cpufreq_policy *data)
103 kobject_put(&data->kobj);
104 module_put(cpufreq_driver->owner);
106 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
109 /*********************************************************************
110 * UNIFIED DEBUG HELPERS *
111 *********************************************************************/
112 #ifdef CONFIG_CPU_FREQ_DEBUG
114 /* what part(s) of the CPUfreq subsystem are debugged? */
115 static unsigned int debug;
117 /* is the debug output ratelimit'ed using printk_ratelimit? User can
118 * set or modify this value.
120 static unsigned int debug_ratelimit = 1;
122 /* is the printk_ratelimit'ing enabled? It's enabled after a successful
123 * loading of a cpufreq driver, temporarily disabled when a new policy
124 * is set, and disabled upon cpufreq driver removal
126 static unsigned int disable_ratelimit = 1;
127 static DEFINE_SPINLOCK(disable_ratelimit_lock);
129 static void cpufreq_debug_enable_ratelimit(void)
131 unsigned long flags;
133 spin_lock_irqsave(&disable_ratelimit_lock, flags);
134 if (disable_ratelimit)
135 disable_ratelimit--;
136 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
139 static void cpufreq_debug_disable_ratelimit(void)
141 unsigned long flags;
143 spin_lock_irqsave(&disable_ratelimit_lock, flags);
144 disable_ratelimit++;
145 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
148 void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
150 char s[256];
151 va_list args;
152 unsigned int len;
153 unsigned long flags;
155 WARN_ON(!prefix);
156 if (type & debug) {
157 spin_lock_irqsave(&disable_ratelimit_lock, flags);
158 if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) {
159 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
160 return;
162 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
164 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
166 va_start(args, fmt);
167 len += vsnprintf(&s[len], (256 - len), fmt, args);
168 va_end(args);
170 printk(s);
172 WARN_ON(len < 5);
175 EXPORT_SYMBOL(cpufreq_debug_printk);
178 module_param(debug, uint, 0644);
179 MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
181 module_param(debug_ratelimit, uint, 0644);
182 MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
184 #else /* !CONFIG_CPU_FREQ_DEBUG */
186 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
187 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
189 #endif /* CONFIG_CPU_FREQ_DEBUG */
192 /*********************************************************************
193 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
194 *********************************************************************/
197 * adjust_jiffies - adjust the system "loops_per_jiffy"
199 * This function alters the system "loops_per_jiffy" for the clock
200 * speed change. Note that loops_per_jiffy cannot be updated on SMP
201 * systems as each CPU might be scaled differently. So, use the arch
202 * per-CPU loops_per_jiffy value wherever possible.
204 #ifndef CONFIG_SMP
205 static unsigned long l_p_j_ref;
206 static unsigned int l_p_j_ref_freq;
208 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
210 if (ci->flags & CPUFREQ_CONST_LOOPS)
211 return;
213 if (!l_p_j_ref_freq) {
214 l_p_j_ref = loops_per_jiffy;
215 l_p_j_ref_freq = ci->old;
216 dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
218 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
219 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
220 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
221 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
222 dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new);
225 #else
226 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
227 #endif
231 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
232 * on frequency transition.
234 * This function calls the transition notifiers and the "adjust_jiffies"
235 * function. It is called twice on all CPU frequency changes that have
236 * external effects.
238 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
240 struct cpufreq_policy *policy;
242 BUG_ON(irqs_disabled());
244 freqs->flags = cpufreq_driver->flags;
245 dprintk("notification %u of frequency transition to %u kHz\n",
246 state, freqs->new);
248 down_read(&cpufreq_notifier_rwsem);
250 policy = cpufreq_cpu_data[freqs->cpu];
251 switch (state) {
253 case CPUFREQ_PRECHANGE:
254 /* detect if the driver reported a value as "old frequency"
255 * which is not equal to what the cpufreq core thinks is
256 * "old frequency".
258 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
259 if ((policy) && (policy->cpu == freqs->cpu) &&
260 (policy->cur) && (policy->cur != freqs->old)) {
261 dprintk(KERN_WARNING "Warning: CPU frequency is"
262 " %u, cpufreq assumed %u kHz.\n",
263 freqs->old, policy->cur);
264 freqs->old = policy->cur;
267 notifier_call_chain(&cpufreq_transition_notifier_list,
268 CPUFREQ_PRECHANGE, freqs);
269 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
270 break;
272 case CPUFREQ_POSTCHANGE:
273 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
274 notifier_call_chain(&cpufreq_transition_notifier_list,
275 CPUFREQ_POSTCHANGE, freqs);
276 if (likely(policy) && likely(policy->cpu == freqs->cpu))
277 policy->cur = freqs->new;
278 break;
280 up_read(&cpufreq_notifier_rwsem);
282 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
286 /*********************************************************************
287 * SYSFS INTERFACE *
288 *********************************************************************/
291 * cpufreq_parse_governor - parse a governor string
293 static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
294 struct cpufreq_governor **governor)
296 if (!cpufreq_driver)
297 return -EINVAL;
298 if (cpufreq_driver->setpolicy) {
299 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
300 *policy = CPUFREQ_POLICY_PERFORMANCE;
301 return 0;
302 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
303 *policy = CPUFREQ_POLICY_POWERSAVE;
304 return 0;
306 return -EINVAL;
307 } else {
308 struct cpufreq_governor *t;
309 mutex_lock(&cpufreq_governor_mutex);
310 if (!cpufreq_driver || !cpufreq_driver->target)
311 goto out;
312 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
313 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
314 *governor = t;
315 mutex_unlock(&cpufreq_governor_mutex);
316 return 0;
319 out:
320 mutex_unlock(&cpufreq_governor_mutex);
322 return -EINVAL;
324 EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
327 /* drivers/base/cpu.c */
328 extern struct sysdev_class cpu_sysdev_class;
332 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
334 * Write out information from cpufreq_driver->policy[cpu]; object must be
335 * "unsigned int".
338 #define show_one(file_name, object) \
339 static ssize_t show_##file_name \
340 (struct cpufreq_policy * policy, char *buf) \
342 return sprintf (buf, "%u\n", policy->object); \
345 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
346 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
347 show_one(scaling_min_freq, min);
348 show_one(scaling_max_freq, max);
349 show_one(scaling_cur_freq, cur);
352 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
354 #define store_one(file_name, object) \
355 static ssize_t store_##file_name \
356 (struct cpufreq_policy * policy, const char *buf, size_t count) \
358 unsigned int ret = -EINVAL; \
359 struct cpufreq_policy new_policy; \
361 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
362 if (ret) \
363 return -EINVAL; \
365 ret = sscanf (buf, "%u", &new_policy.object); \
366 if (ret != 1) \
367 return -EINVAL; \
369 ret = cpufreq_set_policy(&new_policy); \
371 return ret ? ret : count; \
374 store_one(scaling_min_freq,min);
375 store_one(scaling_max_freq,max);
378 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
380 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
382 unsigned int cur_freq = cpufreq_get(policy->cpu);
383 if (!cur_freq)
384 return sprintf(buf, "<unknown>");
385 return sprintf(buf, "%u\n", cur_freq);
390 * show_scaling_governor - show the current policy for the specified CPU
392 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
394 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
395 return sprintf(buf, "powersave\n");
396 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
397 return sprintf(buf, "performance\n");
398 else if (policy->governor)
399 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
400 return -EINVAL;
405 * store_scaling_governor - store policy for the specified CPU
407 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
408 const char *buf, size_t count)
410 unsigned int ret = -EINVAL;
411 char str_governor[16];
412 struct cpufreq_policy new_policy;
414 ret = cpufreq_get_policy(&new_policy, policy->cpu);
415 if (ret)
416 return ret;
418 ret = sscanf (buf, "%15s", str_governor);
419 if (ret != 1)
420 return -EINVAL;
422 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
423 return -EINVAL;
425 ret = cpufreq_set_policy(&new_policy);
426 return ret ? ret : count;
430 * show_scaling_driver - show the cpufreq driver currently loaded
432 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
434 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
438 * show_scaling_available_governors - show the available CPUfreq governors
440 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
441 char *buf)
443 ssize_t i = 0;
444 struct cpufreq_governor *t;
446 if (!cpufreq_driver->target) {
447 i += sprintf(buf, "performance powersave");
448 goto out;
451 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
452 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
453 goto out;
454 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
456 out:
457 i += sprintf(&buf[i], "\n");
458 return i;
461 * show_affected_cpus - show the CPUs affected by each transition
463 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
465 ssize_t i = 0;
466 unsigned int cpu;
468 for_each_cpu_mask(cpu, policy->cpus) {
469 if (i)
470 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
471 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
472 if (i >= (PAGE_SIZE - 5))
473 break;
475 i += sprintf(&buf[i], "\n");
476 return i;
480 #define define_one_ro(_name) \
481 static struct freq_attr _name = \
482 __ATTR(_name, 0444, show_##_name, NULL)
484 #define define_one_ro0400(_name) \
485 static struct freq_attr _name = \
486 __ATTR(_name, 0400, show_##_name, NULL)
488 #define define_one_rw(_name) \
489 static struct freq_attr _name = \
490 __ATTR(_name, 0644, show_##_name, store_##_name)
492 define_one_ro0400(cpuinfo_cur_freq);
493 define_one_ro(cpuinfo_min_freq);
494 define_one_ro(cpuinfo_max_freq);
495 define_one_ro(scaling_available_governors);
496 define_one_ro(scaling_driver);
497 define_one_ro(scaling_cur_freq);
498 define_one_ro(affected_cpus);
499 define_one_rw(scaling_min_freq);
500 define_one_rw(scaling_max_freq);
501 define_one_rw(scaling_governor);
503 static struct attribute * default_attrs[] = {
504 &cpuinfo_min_freq.attr,
505 &cpuinfo_max_freq.attr,
506 &scaling_min_freq.attr,
507 &scaling_max_freq.attr,
508 &affected_cpus.attr,
509 &scaling_governor.attr,
510 &scaling_driver.attr,
511 &scaling_available_governors.attr,
512 NULL
515 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
516 #define to_attr(a) container_of(a,struct freq_attr,attr)
518 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
520 struct cpufreq_policy * policy = to_policy(kobj);
521 struct freq_attr * fattr = to_attr(attr);
522 ssize_t ret;
523 policy = cpufreq_cpu_get(policy->cpu);
524 if (!policy)
525 return -EINVAL;
526 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
527 cpufreq_cpu_put(policy);
528 return ret;
531 static ssize_t store(struct kobject * kobj, struct attribute * attr,
532 const char * buf, size_t count)
534 struct cpufreq_policy * policy = to_policy(kobj);
535 struct freq_attr * fattr = to_attr(attr);
536 ssize_t ret;
537 policy = cpufreq_cpu_get(policy->cpu);
538 if (!policy)
539 return -EINVAL;
540 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
541 cpufreq_cpu_put(policy);
542 return ret;
545 static void cpufreq_sysfs_release(struct kobject * kobj)
547 struct cpufreq_policy * policy = to_policy(kobj);
548 dprintk("last reference is dropped\n");
549 complete(&policy->kobj_unregister);
552 static struct sysfs_ops sysfs_ops = {
553 .show = show,
554 .store = store,
557 static struct kobj_type ktype_cpufreq = {
558 .sysfs_ops = &sysfs_ops,
559 .default_attrs = default_attrs,
560 .release = cpufreq_sysfs_release,
565 * cpufreq_add_dev - add a CPU device
567 * Adds the cpufreq interface for a CPU device.
569 static int cpufreq_add_dev (struct sys_device * sys_dev)
571 unsigned int cpu = sys_dev->id;
572 int ret = 0;
573 struct cpufreq_policy new_policy;
574 struct cpufreq_policy *policy;
575 struct freq_attr **drv_attr;
576 unsigned long flags;
577 unsigned int j;
579 if (cpu_is_offline(cpu))
580 return 0;
582 cpufreq_debug_disable_ratelimit();
583 dprintk("adding CPU %u\n", cpu);
585 #ifdef CONFIG_SMP
586 /* check whether a different CPU already registered this
587 * CPU because it is in the same boat. */
588 policy = cpufreq_cpu_get(cpu);
589 if (unlikely(policy)) {
590 dprintk("CPU already managed, adding link\n");
591 sysfs_create_link(&sys_dev->kobj, &policy->kobj, "cpufreq");
592 cpufreq_debug_enable_ratelimit();
593 return 0;
595 #endif
597 if (!try_module_get(cpufreq_driver->owner)) {
598 ret = -EINVAL;
599 goto module_out;
602 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
603 if (!policy) {
604 ret = -ENOMEM;
605 goto nomem_out;
608 policy->cpu = cpu;
609 policy->cpus = cpumask_of_cpu(cpu);
611 mutex_init(&policy->lock);
612 mutex_lock(&policy->lock);
613 init_completion(&policy->kobj_unregister);
614 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
616 /* call driver. From then on the cpufreq must be able
617 * to accept all calls to ->verify and ->setpolicy for this CPU
619 ret = cpufreq_driver->init(policy);
620 if (ret) {
621 dprintk("initialization failed\n");
622 mutex_unlock(&policy->lock);
623 goto err_out;
626 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
628 /* prepare interface data */
629 policy->kobj.parent = &sys_dev->kobj;
630 policy->kobj.ktype = &ktype_cpufreq;
631 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
633 ret = kobject_register(&policy->kobj);
634 if (ret) {
635 mutex_unlock(&policy->lock);
636 goto err_out_driver_exit;
638 /* set up files for this cpu device */
639 drv_attr = cpufreq_driver->attr;
640 while ((drv_attr) && (*drv_attr)) {
641 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
642 drv_attr++;
644 if (cpufreq_driver->get)
645 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
646 if (cpufreq_driver->target)
647 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
649 spin_lock_irqsave(&cpufreq_driver_lock, flags);
650 for_each_cpu_mask(j, policy->cpus)
651 cpufreq_cpu_data[j] = policy;
652 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
653 policy->governor = NULL; /* to assure that the starting sequence is
654 * run in cpufreq_set_policy */
655 mutex_unlock(&policy->lock);
657 /* set default policy */
659 ret = cpufreq_set_policy(&new_policy);
660 if (ret) {
661 dprintk("setting policy failed\n");
662 goto err_out_unregister;
665 module_put(cpufreq_driver->owner);
666 dprintk("initialization complete\n");
667 cpufreq_debug_enable_ratelimit();
669 return 0;
672 err_out_unregister:
673 spin_lock_irqsave(&cpufreq_driver_lock, flags);
674 for_each_cpu_mask(j, policy->cpus)
675 cpufreq_cpu_data[j] = NULL;
676 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
678 kobject_unregister(&policy->kobj);
679 wait_for_completion(&policy->kobj_unregister);
681 err_out_driver_exit:
682 if (cpufreq_driver->exit)
683 cpufreq_driver->exit(policy);
685 err_out:
686 kfree(policy);
688 nomem_out:
689 module_put(cpufreq_driver->owner);
690 module_out:
691 cpufreq_debug_enable_ratelimit();
692 return ret;
697 * cpufreq_remove_dev - remove a CPU device
699 * Removes the cpufreq interface for a CPU device.
701 static int cpufreq_remove_dev (struct sys_device * sys_dev)
703 unsigned int cpu = sys_dev->id;
704 unsigned long flags;
705 struct cpufreq_policy *data;
706 #ifdef CONFIG_SMP
707 struct sys_device *cpu_sys_dev;
708 unsigned int j;
709 #endif
711 cpufreq_debug_disable_ratelimit();
712 dprintk("unregistering CPU %u\n", cpu);
714 spin_lock_irqsave(&cpufreq_driver_lock, flags);
715 data = cpufreq_cpu_data[cpu];
717 if (!data) {
718 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
719 cpufreq_debug_enable_ratelimit();
720 return -EINVAL;
722 cpufreq_cpu_data[cpu] = NULL;
725 #ifdef CONFIG_SMP
726 /* if this isn't the CPU which is the parent of the kobj, we
727 * only need to unlink, put and exit
729 if (unlikely(cpu != data->cpu)) {
730 dprintk("removing link\n");
731 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
732 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
733 cpufreq_cpu_put(data);
734 cpufreq_debug_enable_ratelimit();
735 return 0;
737 #endif
740 if (!kobject_get(&data->kobj)) {
741 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
742 cpufreq_debug_enable_ratelimit();
743 return -EFAULT;
746 #ifdef CONFIG_SMP
747 /* if we have other CPUs still registered, we need to unlink them,
748 * or else wait_for_completion below will lock up. Clean the
749 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
750 * links afterwards.
752 if (unlikely(cpus_weight(data->cpus) > 1)) {
753 for_each_cpu_mask(j, data->cpus) {
754 if (j == cpu)
755 continue;
756 cpufreq_cpu_data[j] = NULL;
760 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
762 if (unlikely(cpus_weight(data->cpus) > 1)) {
763 for_each_cpu_mask(j, data->cpus) {
764 if (j == cpu)
765 continue;
766 dprintk("removing link for cpu %u\n", j);
767 cpu_sys_dev = get_cpu_sysdev(j);
768 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
769 cpufreq_cpu_put(data);
772 #else
773 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
774 #endif
776 mutex_lock(&data->lock);
777 if (cpufreq_driver->target)
778 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
779 mutex_unlock(&data->lock);
781 kobject_unregister(&data->kobj);
783 kobject_put(&data->kobj);
785 /* we need to make sure that the underlying kobj is actually
786 * not referenced anymore by anybody before we proceed with
787 * unloading.
789 dprintk("waiting for dropping of refcount\n");
790 wait_for_completion(&data->kobj_unregister);
791 dprintk("wait complete\n");
793 if (cpufreq_driver->exit)
794 cpufreq_driver->exit(data);
796 kfree(data);
798 cpufreq_debug_enable_ratelimit();
799 return 0;
803 static void handle_update(void *data)
805 unsigned int cpu = (unsigned int)(long)data;
806 dprintk("handle_update for cpu %u called\n", cpu);
807 cpufreq_update_policy(cpu);
811 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
812 * @cpu: cpu number
813 * @old_freq: CPU frequency the kernel thinks the CPU runs at
814 * @new_freq: CPU frequency the CPU actually runs at
816 * We adjust to current frequency first, and need to clean up later. So either call
817 * to cpufreq_update_policy() or schedule handle_update()).
819 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
821 struct cpufreq_freqs freqs;
823 dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
824 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
826 freqs.cpu = cpu;
827 freqs.old = old_freq;
828 freqs.new = new_freq;
829 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
830 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
835 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
836 * @cpu: CPU number
838 * This is the last known freq, without actually getting it from the driver.
839 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
841 unsigned int cpufreq_quick_get(unsigned int cpu)
843 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
844 unsigned int ret = 0;
846 if (policy) {
847 mutex_lock(&policy->lock);
848 ret = policy->cur;
849 mutex_unlock(&policy->lock);
850 cpufreq_cpu_put(policy);
853 return (ret);
855 EXPORT_SYMBOL(cpufreq_quick_get);
859 * cpufreq_get - get the current CPU frequency (in kHz)
860 * @cpu: CPU number
862 * Get the CPU current (static) CPU frequency
864 unsigned int cpufreq_get(unsigned int cpu)
866 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
867 unsigned int ret = 0;
869 if (!policy)
870 return 0;
872 if (!cpufreq_driver->get)
873 goto out;
875 mutex_lock(&policy->lock);
877 ret = cpufreq_driver->get(cpu);
879 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
880 /* verify no discrepancy between actual and saved value exists */
881 if (unlikely(ret != policy->cur)) {
882 cpufreq_out_of_sync(cpu, policy->cur, ret);
883 schedule_work(&policy->update);
887 mutex_unlock(&policy->lock);
889 out:
890 cpufreq_cpu_put(policy);
892 return (ret);
894 EXPORT_SYMBOL(cpufreq_get);
898 * cpufreq_suspend - let the low level driver prepare for suspend
901 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
903 int cpu = sysdev->id;
904 unsigned int ret = 0;
905 unsigned int cur_freq = 0;
906 struct cpufreq_policy *cpu_policy;
908 dprintk("resuming cpu %u\n", cpu);
910 if (!cpu_online(cpu))
911 return 0;
913 /* we may be lax here as interrupts are off. Nonetheless
914 * we need to grab the correct cpu policy, as to check
915 * whether we really run on this CPU.
918 cpu_policy = cpufreq_cpu_get(cpu);
919 if (!cpu_policy)
920 return -EINVAL;
922 /* only handle each CPU group once */
923 if (unlikely(cpu_policy->cpu != cpu)) {
924 cpufreq_cpu_put(cpu_policy);
925 return 0;
928 if (cpufreq_driver->suspend) {
929 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
930 if (ret) {
931 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
932 "step on CPU %u\n", cpu_policy->cpu);
933 cpufreq_cpu_put(cpu_policy);
934 return ret;
939 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
940 goto out;
942 if (cpufreq_driver->get)
943 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
945 if (!cur_freq || !cpu_policy->cur) {
946 printk(KERN_ERR "cpufreq: suspend failed to assert current "
947 "frequency is what timing core thinks it is.\n");
948 goto out;
951 if (unlikely(cur_freq != cpu_policy->cur)) {
952 struct cpufreq_freqs freqs;
954 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
955 dprintk(KERN_DEBUG "Warning: CPU frequency is %u, "
956 "cpufreq assumed %u kHz.\n",
957 cur_freq, cpu_policy->cur);
959 freqs.cpu = cpu;
960 freqs.old = cpu_policy->cur;
961 freqs.new = cur_freq;
963 notifier_call_chain(&cpufreq_transition_notifier_list,
964 CPUFREQ_SUSPENDCHANGE, &freqs);
965 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
967 cpu_policy->cur = cur_freq;
970 out:
971 cpufreq_cpu_put(cpu_policy);
972 return 0;
976 * cpufreq_resume - restore proper CPU frequency handling after resume
978 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
979 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
980 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
981 * restored.
983 static int cpufreq_resume(struct sys_device * sysdev)
985 int cpu = sysdev->id;
986 unsigned int ret = 0;
987 struct cpufreq_policy *cpu_policy;
989 dprintk("resuming cpu %u\n", cpu);
991 if (!cpu_online(cpu))
992 return 0;
994 /* we may be lax here as interrupts are off. Nonetheless
995 * we need to grab the correct cpu policy, as to check
996 * whether we really run on this CPU.
999 cpu_policy = cpufreq_cpu_get(cpu);
1000 if (!cpu_policy)
1001 return -EINVAL;
1003 /* only handle each CPU group once */
1004 if (unlikely(cpu_policy->cpu != cpu)) {
1005 cpufreq_cpu_put(cpu_policy);
1006 return 0;
1009 if (cpufreq_driver->resume) {
1010 ret = cpufreq_driver->resume(cpu_policy);
1011 if (ret) {
1012 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1013 "step on CPU %u\n", cpu_policy->cpu);
1014 cpufreq_cpu_put(cpu_policy);
1015 return ret;
1019 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1020 unsigned int cur_freq = 0;
1022 if (cpufreq_driver->get)
1023 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1025 if (!cur_freq || !cpu_policy->cur) {
1026 printk(KERN_ERR "cpufreq: resume failed to assert "
1027 "current frequency is what timing core "
1028 "thinks it is.\n");
1029 goto out;
1032 if (unlikely(cur_freq != cpu_policy->cur)) {
1033 struct cpufreq_freqs freqs;
1035 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1036 dprintk(KERN_WARNING "Warning: CPU frequency"
1037 "is %u, cpufreq assumed %u kHz.\n",
1038 cur_freq, cpu_policy->cur);
1040 freqs.cpu = cpu;
1041 freqs.old = cpu_policy->cur;
1042 freqs.new = cur_freq;
1044 notifier_call_chain(&cpufreq_transition_notifier_list,
1045 CPUFREQ_RESUMECHANGE, &freqs);
1046 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1048 cpu_policy->cur = cur_freq;
1052 out:
1053 schedule_work(&cpu_policy->update);
1054 cpufreq_cpu_put(cpu_policy);
1055 return ret;
1058 static struct sysdev_driver cpufreq_sysdev_driver = {
1059 .add = cpufreq_add_dev,
1060 .remove = cpufreq_remove_dev,
1061 .suspend = cpufreq_suspend,
1062 .resume = cpufreq_resume,
1066 /*********************************************************************
1067 * NOTIFIER LISTS INTERFACE *
1068 *********************************************************************/
1071 * cpufreq_register_notifier - register a driver with cpufreq
1072 * @nb: notifier function to register
1073 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1075 * Add a driver to one of two lists: either a list of drivers that
1076 * are notified about clock rate changes (once before and once after
1077 * the transition), or a list of drivers that are notified about
1078 * changes in cpufreq policy.
1080 * This function may sleep, and has the same return conditions as
1081 * notifier_chain_register.
1083 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1085 int ret;
1087 down_write(&cpufreq_notifier_rwsem);
1088 switch (list) {
1089 case CPUFREQ_TRANSITION_NOTIFIER:
1090 ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb);
1091 break;
1092 case CPUFREQ_POLICY_NOTIFIER:
1093 ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb);
1094 break;
1095 default:
1096 ret = -EINVAL;
1098 up_write(&cpufreq_notifier_rwsem);
1100 return ret;
1102 EXPORT_SYMBOL(cpufreq_register_notifier);
1106 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1107 * @nb: notifier block to be unregistered
1108 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1110 * Remove a driver from the CPU frequency notifier list.
1112 * This function may sleep, and has the same return conditions as
1113 * notifier_chain_unregister.
1115 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1117 int ret;
1119 down_write(&cpufreq_notifier_rwsem);
1120 switch (list) {
1121 case CPUFREQ_TRANSITION_NOTIFIER:
1122 ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb);
1123 break;
1124 case CPUFREQ_POLICY_NOTIFIER:
1125 ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb);
1126 break;
1127 default:
1128 ret = -EINVAL;
1130 up_write(&cpufreq_notifier_rwsem);
1132 return ret;
1134 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1137 /*********************************************************************
1138 * GOVERNORS *
1139 *********************************************************************/
1142 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1143 unsigned int target_freq,
1144 unsigned int relation)
1146 int retval = -EINVAL;
1148 lock_cpu_hotplug();
1149 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1150 target_freq, relation);
1151 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1152 retval = cpufreq_driver->target(policy, target_freq, relation);
1154 unlock_cpu_hotplug();
1156 return retval;
1158 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1160 int cpufreq_driver_target(struct cpufreq_policy *policy,
1161 unsigned int target_freq,
1162 unsigned int relation)
1164 int ret;
1166 policy = cpufreq_cpu_get(policy->cpu);
1167 if (!policy)
1168 return -EINVAL;
1170 mutex_lock(&policy->lock);
1172 ret = __cpufreq_driver_target(policy, target_freq, relation);
1174 mutex_unlock(&policy->lock);
1176 cpufreq_cpu_put(policy);
1177 return ret;
1179 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1182 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1184 int ret;
1186 if (!try_module_get(policy->governor->owner))
1187 return -EINVAL;
1189 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1190 ret = policy->governor->governor(policy, event);
1192 /* we keep one module reference alive for each CPU governed by this CPU */
1193 if ((event != CPUFREQ_GOV_START) || ret)
1194 module_put(policy->governor->owner);
1195 if ((event == CPUFREQ_GOV_STOP) && !ret)
1196 module_put(policy->governor->owner);
1198 return ret;
1202 int cpufreq_governor(unsigned int cpu, unsigned int event)
1204 int ret = 0;
1205 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1207 if (!policy)
1208 return -EINVAL;
1210 mutex_lock(&policy->lock);
1211 ret = __cpufreq_governor(policy, event);
1212 mutex_unlock(&policy->lock);
1214 cpufreq_cpu_put(policy);
1215 return ret;
1217 EXPORT_SYMBOL_GPL(cpufreq_governor);
1220 int cpufreq_register_governor(struct cpufreq_governor *governor)
1222 struct cpufreq_governor *t;
1224 if (!governor)
1225 return -EINVAL;
1227 mutex_lock(&cpufreq_governor_mutex);
1229 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1230 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
1231 mutex_unlock(&cpufreq_governor_mutex);
1232 return -EBUSY;
1235 list_add(&governor->governor_list, &cpufreq_governor_list);
1237 mutex_unlock(&cpufreq_governor_mutex);
1238 return 0;
1240 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1243 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1245 if (!governor)
1246 return;
1248 mutex_lock(&cpufreq_governor_mutex);
1249 list_del(&governor->governor_list);
1250 mutex_unlock(&cpufreq_governor_mutex);
1251 return;
1253 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1257 /*********************************************************************
1258 * POLICY INTERFACE *
1259 *********************************************************************/
1262 * cpufreq_get_policy - get the current cpufreq_policy
1263 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1265 * Reads the current cpufreq policy.
1267 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1269 struct cpufreq_policy *cpu_policy;
1270 if (!policy)
1271 return -EINVAL;
1273 cpu_policy = cpufreq_cpu_get(cpu);
1274 if (!cpu_policy)
1275 return -EINVAL;
1277 mutex_lock(&cpu_policy->lock);
1278 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1279 mutex_unlock(&cpu_policy->lock);
1281 cpufreq_cpu_put(cpu_policy);
1282 return 0;
1284 EXPORT_SYMBOL(cpufreq_get_policy);
1287 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1289 int ret = 0;
1291 cpufreq_debug_disable_ratelimit();
1292 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1293 policy->min, policy->max);
1295 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
1297 /* verify the cpu speed can be set within this limit */
1298 ret = cpufreq_driver->verify(policy);
1299 if (ret)
1300 goto error_out;
1302 down_read(&cpufreq_notifier_rwsem);
1304 /* adjust if necessary - all reasons */
1305 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
1306 policy);
1308 /* adjust if necessary - hardware incompatibility*/
1309 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
1310 policy);
1312 /* verify the cpu speed can be set within this limit,
1313 which might be different to the first one */
1314 ret = cpufreq_driver->verify(policy);
1315 if (ret) {
1316 up_read(&cpufreq_notifier_rwsem);
1317 goto error_out;
1320 /* notification of the new policy */
1321 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
1322 policy);
1324 up_read(&cpufreq_notifier_rwsem);
1326 data->min = policy->min;
1327 data->max = policy->max;
1329 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1331 if (cpufreq_driver->setpolicy) {
1332 data->policy = policy->policy;
1333 dprintk("setting range\n");
1334 ret = cpufreq_driver->setpolicy(policy);
1335 } else {
1336 if (policy->governor != data->governor) {
1337 /* save old, working values */
1338 struct cpufreq_governor *old_gov = data->governor;
1340 dprintk("governor switch\n");
1342 /* end old governor */
1343 if (data->governor)
1344 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1346 /* start new governor */
1347 data->governor = policy->governor;
1348 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1349 /* new governor failed, so re-start old one */
1350 dprintk("starting governor %s failed\n", data->governor->name);
1351 if (old_gov) {
1352 data->governor = old_gov;
1353 __cpufreq_governor(data, CPUFREQ_GOV_START);
1355 ret = -EINVAL;
1356 goto error_out;
1358 /* might be a policy change, too, so fall through */
1360 dprintk("governor: change or update limits\n");
1361 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1364 error_out:
1365 cpufreq_debug_enable_ratelimit();
1366 return ret;
1370 * cpufreq_set_policy - set a new CPUFreq policy
1371 * @policy: policy to be set.
1373 * Sets a new CPU frequency and voltage scaling policy.
1375 int cpufreq_set_policy(struct cpufreq_policy *policy)
1377 int ret = 0;
1378 struct cpufreq_policy *data;
1380 if (!policy)
1381 return -EINVAL;
1383 data = cpufreq_cpu_get(policy->cpu);
1384 if (!data)
1385 return -EINVAL;
1387 /* lock this CPU */
1388 mutex_lock(&data->lock);
1390 ret = __cpufreq_set_policy(data, policy);
1391 data->user_policy.min = data->min;
1392 data->user_policy.max = data->max;
1393 data->user_policy.policy = data->policy;
1394 data->user_policy.governor = data->governor;
1396 mutex_unlock(&data->lock);
1397 cpufreq_cpu_put(data);
1399 return ret;
1401 EXPORT_SYMBOL(cpufreq_set_policy);
1405 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1406 * @cpu: CPU which shall be re-evaluated
1408 * Usefull for policy notifiers which have different necessities
1409 * at different times.
1411 int cpufreq_update_policy(unsigned int cpu)
1413 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1414 struct cpufreq_policy policy;
1415 int ret = 0;
1417 if (!data)
1418 return -ENODEV;
1420 mutex_lock(&data->lock);
1422 dprintk("updating policy for CPU %u\n", cpu);
1423 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1424 policy.min = data->user_policy.min;
1425 policy.max = data->user_policy.max;
1426 policy.policy = data->user_policy.policy;
1427 policy.governor = data->user_policy.governor;
1429 /* BIOS might change freq behind our back
1430 -> ask driver for current freq and notify governors about a change */
1431 if (cpufreq_driver->get) {
1432 policy.cur = cpufreq_driver->get(cpu);
1433 if (!data->cur) {
1434 dprintk("Driver did not initialize current freq");
1435 data->cur = policy.cur;
1436 } else {
1437 if (data->cur != policy.cur)
1438 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1442 ret = __cpufreq_set_policy(data, &policy);
1444 mutex_unlock(&data->lock);
1446 cpufreq_cpu_put(data);
1447 return ret;
1449 EXPORT_SYMBOL(cpufreq_update_policy);
1451 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1452 unsigned long action, void *hcpu)
1454 unsigned int cpu = (unsigned long)hcpu;
1455 struct cpufreq_policy *policy;
1456 struct sys_device *sys_dev;
1458 sys_dev = get_cpu_sysdev(cpu);
1460 if (sys_dev) {
1461 switch (action) {
1462 case CPU_ONLINE:
1463 cpufreq_add_dev(sys_dev);
1464 break;
1465 case CPU_DOWN_PREPARE:
1467 * We attempt to put this cpu in lowest frequency
1468 * possible before going down. This will permit
1469 * hardware-managed P-State to switch other related
1470 * threads to min or higher speeds if possible.
1472 policy = cpufreq_cpu_data[cpu];
1473 if (policy) {
1474 cpufreq_driver_target(policy, policy->min,
1475 CPUFREQ_RELATION_H);
1477 break;
1478 case CPU_DEAD:
1479 cpufreq_remove_dev(sys_dev);
1480 break;
1483 return NOTIFY_OK;
1486 static struct notifier_block cpufreq_cpu_notifier =
1488 .notifier_call = cpufreq_cpu_callback,
1491 /*********************************************************************
1492 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1493 *********************************************************************/
1496 * cpufreq_register_driver - register a CPU Frequency driver
1497 * @driver_data: A struct cpufreq_driver containing the values#
1498 * submitted by the CPU Frequency driver.
1500 * Registers a CPU Frequency driver to this core code. This code
1501 * returns zero on success, -EBUSY when another driver got here first
1502 * (and isn't unregistered in the meantime).
1505 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1507 unsigned long flags;
1508 int ret;
1510 if (!driver_data || !driver_data->verify || !driver_data->init ||
1511 ((!driver_data->setpolicy) && (!driver_data->target)))
1512 return -EINVAL;
1514 dprintk("trying to register driver %s\n", driver_data->name);
1516 if (driver_data->setpolicy)
1517 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1519 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1520 if (cpufreq_driver) {
1521 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1522 return -EBUSY;
1524 cpufreq_driver = driver_data;
1525 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1527 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1529 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1530 int i;
1531 ret = -ENODEV;
1533 /* check for at least one working CPU */
1534 for (i=0; i<NR_CPUS; i++)
1535 if (cpufreq_cpu_data[i])
1536 ret = 0;
1538 /* if all ->init() calls failed, unregister */
1539 if (ret) {
1540 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1541 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1543 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1544 cpufreq_driver = NULL;
1545 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1549 if (!ret) {
1550 register_cpu_notifier(&cpufreq_cpu_notifier);
1551 dprintk("driver %s up and running\n", driver_data->name);
1552 cpufreq_debug_enable_ratelimit();
1555 return (ret);
1557 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1561 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1563 * Unregister the current CPUFreq driver. Only call this if you have
1564 * the right to do so, i.e. if you have succeeded in initialising before!
1565 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1566 * currently not initialised.
1568 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1570 unsigned long flags;
1572 cpufreq_debug_disable_ratelimit();
1574 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1575 cpufreq_debug_enable_ratelimit();
1576 return -EINVAL;
1579 dprintk("unregistering driver %s\n", driver->name);
1581 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1582 unregister_cpu_notifier(&cpufreq_cpu_notifier);
1584 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1585 cpufreq_driver = NULL;
1586 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1588 return 0;
1590 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);