| Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 1 | #include <linux/types.h> | 
|  | 2 | #include <linux/errno.h> | 
|  | 3 | #include <linux/kernel.h> | 
|  | 4 | #include <linux/delay.h> | 
|  | 5 | #include <linux/slab.h> | 
|  | 6 | #include <linux/init.h> | 
|  | 7 | #include <linux/wait.h> | 
|  | 8 | #include <linux/cpufreq.h> | 
|  | 9 |  | 
| Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 10 | #include <asm/prom.h> | 
|  | 11 |  | 
| Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 12 | #include "windfarm.h" | 
|  | 13 |  | 
|  | 14 | #define VERSION "0.3" | 
|  | 15 |  | 
|  | 16 | static int clamped; | 
|  | 17 | static struct wf_control *clamp_control; | 
|  | 18 |  | 
|  | 19 | static int clamp_notifier_call(struct notifier_block *self, | 
|  | 20 | unsigned long event, void *data) | 
|  | 21 | { | 
|  | 22 | struct cpufreq_policy *p = data; | 
|  | 23 | unsigned long max_freq; | 
|  | 24 |  | 
|  | 25 | if (event != CPUFREQ_ADJUST) | 
|  | 26 | return 0; | 
|  | 27 |  | 
|  | 28 | max_freq = clamped ? (p->cpuinfo.min_freq) : (p->cpuinfo.max_freq); | 
|  | 29 | cpufreq_verify_within_limits(p, 0, max_freq); | 
|  | 30 |  | 
|  | 31 | return 0; | 
|  | 32 | } | 
|  | 33 |  | 
|  | 34 | static struct notifier_block clamp_notifier = { | 
|  | 35 | .notifier_call = clamp_notifier_call, | 
|  | 36 | }; | 
|  | 37 |  | 
|  | 38 | static int clamp_set(struct wf_control *ct, s32 value) | 
|  | 39 | { | 
|  | 40 | if (value) | 
|  | 41 | printk(KERN_INFO "windfarm: Clamping CPU frequency to " | 
|  | 42 | "minimum !\n"); | 
|  | 43 | else | 
|  | 44 | printk(KERN_INFO "windfarm: CPU frequency unclamped !\n"); | 
|  | 45 | clamped = value; | 
|  | 46 | cpufreq_update_policy(0); | 
|  | 47 | return 0; | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | static int clamp_get(struct wf_control *ct, s32 *value) | 
|  | 51 | { | 
|  | 52 | *value = clamped; | 
|  | 53 | return 0; | 
|  | 54 | } | 
|  | 55 |  | 
|  | 56 | static s32 clamp_min(struct wf_control *ct) | 
|  | 57 | { | 
|  | 58 | return 0; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | static s32 clamp_max(struct wf_control *ct) | 
|  | 62 | { | 
|  | 63 | return 1; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | static struct wf_control_ops clamp_ops = { | 
|  | 67 | .set_value	= clamp_set, | 
|  | 68 | .get_value	= clamp_get, | 
|  | 69 | .get_min	= clamp_min, | 
|  | 70 | .get_max	= clamp_max, | 
|  | 71 | .owner		= THIS_MODULE, | 
|  | 72 | }; | 
|  | 73 |  | 
|  | 74 | static int __init wf_cpufreq_clamp_init(void) | 
|  | 75 | { | 
|  | 76 | struct wf_control *clamp; | 
|  | 77 |  | 
| Benjamin Herrenschmidt | b55fafc | 2006-03-03 17:03:21 +1100 | [diff] [blame] | 78 | /* Don't register on old machines that use therm_pm72 for now */ | 
|  | 79 | if (machine_is_compatible("PowerMac7,2") || | 
|  | 80 | machine_is_compatible("PowerMac7,3") || | 
|  | 81 | machine_is_compatible("RackMac3,1")) | 
|  | 82 | return -ENODEV; | 
|  | 83 |  | 
| Benjamin Herrenschmidt | 75722d3 | 2005-11-07 16:08:17 +1100 | [diff] [blame] | 84 | clamp = kmalloc(sizeof(struct wf_control), GFP_KERNEL); | 
|  | 85 | if (clamp == NULL) | 
|  | 86 | return -ENOMEM; | 
|  | 87 | cpufreq_register_notifier(&clamp_notifier, CPUFREQ_POLICY_NOTIFIER); | 
|  | 88 | clamp->ops = &clamp_ops; | 
|  | 89 | clamp->name = "cpufreq-clamp"; | 
|  | 90 | if (wf_register_control(clamp)) | 
|  | 91 | goto fail; | 
|  | 92 | clamp_control = clamp; | 
|  | 93 | return 0; | 
|  | 94 | fail: | 
|  | 95 | kfree(clamp); | 
|  | 96 | return -ENODEV; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | static void __exit wf_cpufreq_clamp_exit(void) | 
|  | 100 | { | 
|  | 101 | if (clamp_control) | 
|  | 102 | wf_unregister_control(clamp_control); | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 |  | 
|  | 106 | module_init(wf_cpufreq_clamp_init); | 
|  | 107 | module_exit(wf_cpufreq_clamp_exit); | 
|  | 108 |  | 
|  | 109 | MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>"); | 
|  | 110 | MODULE_DESCRIPTION("CPU frequency clamp for PowerMacs thermal control"); | 
|  | 111 | MODULE_LICENSE("GPL"); | 
|  | 112 |  |