Linux 4.19.133
[linux/fpc-iii.git] / drivers / devfreq / governor_simpleondemand.c
blob28e0f2de7100ee0cee83b1e1179c750694b942af
1 /*
2 * linux/drivers/devfreq/governor_simpleondemand.c
4 * Copyright (C) 2011 Samsung Electronics
5 * MyungJoo Ham <myungjoo.ham@samsung.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/errno.h>
13 #include <linux/module.h>
14 #include <linux/devfreq.h>
15 #include <linux/math64.h>
16 #include "governor.h"
18 /* Default constants for DevFreq-Simple-Ondemand (DFSO) */
19 #define DFSO_UPTHRESHOLD (90)
20 #define DFSO_DOWNDIFFERENCTIAL (5)
21 static int devfreq_simple_ondemand_func(struct devfreq *df,
22 unsigned long *freq)
24 int err;
25 struct devfreq_dev_status *stat;
26 unsigned long long a, b;
27 unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD;
28 unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL;
29 struct devfreq_simple_ondemand_data *data = df->data;
30 unsigned long max = (df->max_freq) ? df->max_freq : UINT_MAX;
32 err = devfreq_update_stats(df);
33 if (err)
34 return err;
36 stat = &df->last_status;
38 if (data) {
39 if (data->upthreshold)
40 dfso_upthreshold = data->upthreshold;
41 if (data->downdifferential)
42 dfso_downdifferential = data->downdifferential;
44 if (dfso_upthreshold > 100 ||
45 dfso_upthreshold < dfso_downdifferential)
46 return -EINVAL;
48 /* Assume MAX if it is going to be divided by zero */
49 if (stat->total_time == 0) {
50 *freq = max;
51 return 0;
54 /* Prevent overflow */
55 if (stat->busy_time >= (1 << 24) || stat->total_time >= (1 << 24)) {
56 stat->busy_time >>= 7;
57 stat->total_time >>= 7;
60 /* Set MAX if it's busy enough */
61 if (stat->busy_time * 100 >
62 stat->total_time * dfso_upthreshold) {
63 *freq = max;
64 return 0;
67 /* Set MAX if we do not know the initial frequency */
68 if (stat->current_frequency == 0) {
69 *freq = max;
70 return 0;
73 /* Keep the current frequency */
74 if (stat->busy_time * 100 >
75 stat->total_time * (dfso_upthreshold - dfso_downdifferential)) {
76 *freq = stat->current_frequency;
77 return 0;
80 /* Set the desired frequency based on the load */
81 a = stat->busy_time;
82 a *= stat->current_frequency;
83 b = div_u64(a, stat->total_time);
84 b *= 100;
85 b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2));
86 *freq = (unsigned long) b;
88 if (df->min_freq && *freq < df->min_freq)
89 *freq = df->min_freq;
90 if (df->max_freq && *freq > df->max_freq)
91 *freq = df->max_freq;
93 return 0;
96 static int devfreq_simple_ondemand_handler(struct devfreq *devfreq,
97 unsigned int event, void *data)
99 switch (event) {
100 case DEVFREQ_GOV_START:
101 devfreq_monitor_start(devfreq);
102 break;
104 case DEVFREQ_GOV_STOP:
105 devfreq_monitor_stop(devfreq);
106 break;
108 case DEVFREQ_GOV_INTERVAL:
109 devfreq_interval_update(devfreq, (unsigned int *)data);
110 break;
112 case DEVFREQ_GOV_SUSPEND:
113 devfreq_monitor_suspend(devfreq);
114 break;
116 case DEVFREQ_GOV_RESUME:
117 devfreq_monitor_resume(devfreq);
118 break;
120 default:
121 break;
124 return 0;
127 static struct devfreq_governor devfreq_simple_ondemand = {
128 .name = DEVFREQ_GOV_SIMPLE_ONDEMAND,
129 .get_target_freq = devfreq_simple_ondemand_func,
130 .event_handler = devfreq_simple_ondemand_handler,
133 static int __init devfreq_simple_ondemand_init(void)
135 return devfreq_add_governor(&devfreq_simple_ondemand);
137 subsys_initcall(devfreq_simple_ondemand_init);
139 static void __exit devfreq_simple_ondemand_exit(void)
141 int ret;
143 ret = devfreq_remove_governor(&devfreq_simple_ondemand);
144 if (ret)
145 pr_err("%s: failed remove governor %d\n", __func__, ret);
147 return;
149 module_exit(devfreq_simple_ondemand_exit);
150 MODULE_LICENSE("GPL");