1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/types.h>
3 #include <linux/errno.h>
4 #include <linux/kernel.h>
5 #include <linux/delay.h>
6 #include <linux/pm_qos.h>
7 #include <linux/slab.h>
8 #include <linux/init.h>
9 #include <linux/wait.h>
10 #include <linux/cpu.h>
11 #include <linux/cpufreq.h>
20 static struct wf_control
*clamp_control
;
21 static struct freq_qos_request qos_req
;
22 static unsigned int min_freq
, max_freq
;
24 static int clamp_set(struct wf_control
*ct
, s32 value
)
30 printk(KERN_INFO
"windfarm: Clamping CPU frequency to "
34 printk(KERN_INFO
"windfarm: CPU frequency unclamped !\n");
38 return freq_qos_update_request(&qos_req
, freq
);
41 static int clamp_get(struct wf_control
*ct
, s32
*value
)
47 static s32
clamp_min(struct wf_control
*ct
)
52 static s32
clamp_max(struct wf_control
*ct
)
57 static const struct wf_control_ops clamp_ops
= {
58 .set_value
= clamp_set
,
59 .get_value
= clamp_get
,
65 static int __init
wf_cpufreq_clamp_init(void)
67 struct cpufreq_policy
*policy
;
68 struct wf_control
*clamp
;
72 policy
= cpufreq_cpu_get(0);
74 pr_warn("%s: cpufreq policy not found cpu0\n", __func__
);
78 min_freq
= policy
->cpuinfo
.min_freq
;
79 max_freq
= policy
->cpuinfo
.max_freq
;
81 ret
= freq_qos_add_request(&policy
->constraints
, &qos_req
, FREQ_QOS_MAX
,
84 cpufreq_cpu_put(policy
);
87 pr_err("%s: Failed to add freq constraint (%d)\n", __func__
,
92 dev
= get_cpu_device(0);
94 pr_warn("%s: No cpu device for cpu0\n", __func__
);
99 clamp
= kmalloc(sizeof(struct wf_control
), GFP_KERNEL
);
105 clamp
->ops
= &clamp_ops
;
106 clamp
->name
= "cpufreq-clamp";
107 ret
= wf_register_control(clamp
);
111 clamp_control
= clamp
;
117 freq_qos_remove_request(&qos_req
);
121 static void __exit
wf_cpufreq_clamp_exit(void)
124 wf_unregister_control(clamp_control
);
125 freq_qos_remove_request(&qos_req
);
130 module_init(wf_cpufreq_clamp_init
);
131 module_exit(wf_cpufreq_clamp_exit
);
133 MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
134 MODULE_DESCRIPTION("CPU frequency clamp for PowerMacs thermal control");
135 MODULE_LICENSE("GPL");