Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/pm_qos.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/cpufreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <asm/prom.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "windfarm.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #define VERSION "0.3"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static int clamped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) static struct wf_control *clamp_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) static struct freq_qos_request qos_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) static unsigned int min_freq, max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) static int clamp_set(struct wf_control *ct, s32 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	unsigned int freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	if (value) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		freq = min_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 		printk(KERN_INFO "windfarm: Clamping CPU frequency to "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		       "minimum !\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		freq = max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		printk(KERN_INFO "windfarm: CPU frequency unclamped !\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	clamped = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	return freq_qos_update_request(&qos_req, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static int clamp_get(struct wf_control *ct, s32 *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	*value = clamped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static s32 clamp_min(struct wf_control *ct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static s32 clamp_max(struct wf_control *ct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) static const struct wf_control_ops clamp_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	.set_value	= clamp_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	.get_value	= clamp_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	.get_min	= clamp_min,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	.get_max	= clamp_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static int __init wf_cpufreq_clamp_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	struct cpufreq_policy *policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	struct wf_control *clamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	policy = cpufreq_cpu_get(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	if (!policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		pr_warn("%s: cpufreq policy not found cpu0\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	min_freq = policy->cpuinfo.min_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	max_freq = policy->cpuinfo.max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	ret = freq_qos_add_request(&policy->constraints, &qos_req, FREQ_QOS_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 				   max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	cpufreq_cpu_put(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		pr_err("%s: Failed to add freq constraint (%d)\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		       ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	dev = get_cpu_device(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (unlikely(!dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		pr_warn("%s: No cpu device for cpu0\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	clamp = kmalloc(sizeof(struct wf_control), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	if (clamp == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	clamp->ops = &clamp_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	clamp->name = "cpufreq-clamp";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	ret = wf_register_control(clamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	clamp_control = clamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	kfree(clamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	freq_qos_remove_request(&qos_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static void __exit wf_cpufreq_clamp_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	if (clamp_control) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		wf_unregister_control(clamp_control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		freq_qos_remove_request(&qos_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) module_init(wf_cpufreq_clamp_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) module_exit(wf_cpufreq_clamp_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) MODULE_AUTHOR("Benjamin Herrenschmidt <benh@kernel.crashing.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) MODULE_DESCRIPTION("CPU frequency clamp for PowerMacs thermal control");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)