MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/devfreq/governor_simpleondemand.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Samsung Electronics |
| 5 | * MyungJoo Ham <myungjoo.ham@samsung.com> |
| 6 | * |
| 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. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/devfreq.h> |
| 14 | #include <linux/math64.h> |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 15 | #include "governor.h" |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 16 | |
| 17 | /* Default constants for DevFreq-Simple-Ondemand (DFSO) */ |
| 18 | #define DFSO_UPTHRESHOLD (90) |
| 19 | #define DFSO_DOWNDIFFERENCTIAL (5) |
| 20 | static int devfreq_simple_ondemand_func(struct devfreq *df, |
| 21 | unsigned long *freq) |
| 22 | { |
| 23 | struct devfreq_dev_status stat; |
| 24 | int err = df->profile->get_dev_status(df->dev.parent, &stat); |
| 25 | unsigned long long a, b; |
| 26 | unsigned int dfso_upthreshold = DFSO_UPTHRESHOLD; |
| 27 | unsigned int dfso_downdifferential = DFSO_DOWNDIFFERENCTIAL; |
| 28 | struct devfreq_simple_ondemand_data *data = df->data; |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 29 | unsigned long max = (df->max_freq) ? df->max_freq : UINT_MAX; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 30 | |
| 31 | if (err) |
| 32 | return err; |
| 33 | |
| 34 | if (data) { |
| 35 | if (data->upthreshold) |
| 36 | dfso_upthreshold = data->upthreshold; |
| 37 | if (data->downdifferential) |
| 38 | dfso_downdifferential = data->downdifferential; |
| 39 | } |
| 40 | if (dfso_upthreshold > 100 || |
| 41 | dfso_upthreshold < dfso_downdifferential) |
| 42 | return -EINVAL; |
| 43 | |
| 44 | /* Assume MAX if it is going to be divided by zero */ |
| 45 | if (stat.total_time == 0) { |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 46 | *freq = max; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | /* Prevent overflow */ |
| 51 | if (stat.busy_time >= (1 << 24) || stat.total_time >= (1 << 24)) { |
| 52 | stat.busy_time >>= 7; |
| 53 | stat.total_time >>= 7; |
| 54 | } |
| 55 | |
| 56 | /* Set MAX if it's busy enough */ |
| 57 | if (stat.busy_time * 100 > |
| 58 | stat.total_time * dfso_upthreshold) { |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 59 | *freq = max; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | /* Set MAX if we do not know the initial frequency */ |
| 64 | if (stat.current_frequency == 0) { |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 65 | *freq = max; |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /* Keep the current frequency */ |
| 70 | if (stat.busy_time * 100 > |
| 71 | stat.total_time * (dfso_upthreshold - dfso_downdifferential)) { |
| 72 | *freq = stat.current_frequency; |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* Set the desired frequency based on the load */ |
| 77 | a = stat.busy_time; |
| 78 | a *= stat.current_frequency; |
| 79 | b = div_u64(a, stat.total_time); |
| 80 | b *= 100; |
| 81 | b = div_u64(b, (dfso_upthreshold - dfso_downdifferential / 2)); |
| 82 | *freq = (unsigned long) b; |
| 83 | |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 84 | if (df->min_freq && *freq < df->min_freq) |
| 85 | *freq = df->min_freq; |
| 86 | if (df->max_freq && *freq > df->max_freq) |
| 87 | *freq = df->max_freq; |
| 88 | |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 92 | static int devfreq_simple_ondemand_handler(struct devfreq *devfreq, |
| 93 | unsigned int event, void *data) |
| 94 | { |
| 95 | switch (event) { |
| 96 | case DEVFREQ_GOV_START: |
| 97 | devfreq_monitor_start(devfreq); |
| 98 | break; |
| 99 | |
| 100 | case DEVFREQ_GOV_STOP: |
| 101 | devfreq_monitor_stop(devfreq); |
| 102 | break; |
| 103 | |
| 104 | case DEVFREQ_GOV_INTERVAL: |
| 105 | devfreq_interval_update(devfreq, (unsigned int *)data); |
| 106 | break; |
Rajagopal Venkat | 206c30c | 2012-10-26 01:50:18 +0200 | [diff] [blame] | 107 | |
| 108 | case DEVFREQ_GOV_SUSPEND: |
| 109 | devfreq_monitor_suspend(devfreq); |
| 110 | break; |
| 111 | |
| 112 | case DEVFREQ_GOV_RESUME: |
| 113 | devfreq_monitor_resume(devfreq); |
| 114 | break; |
| 115 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 116 | default: |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 123 | const struct devfreq_governor devfreq_simple_ondemand = { |
| 124 | .name = "simple_ondemand", |
| 125 | .get_target_freq = devfreq_simple_ondemand_func, |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 126 | .event_handler = devfreq_simple_ondemand_handler, |
MyungJoo Ham | ce26c5b | 2011-10-02 00:19:34 +0200 | [diff] [blame] | 127 | }; |
Nishanth Menon | 83116e6 | 2012-10-29 15:01:44 -0500 | [diff] [blame^] | 128 | |
| 129 | static int __init devfreq_simple_ondemand_init(void) |
| 130 | { |
| 131 | return devfreq_add_governor(&devfreq_simple_ondemand); |
| 132 | } |
| 133 | subsys_initcall(devfreq_simple_ondemand_init); |
| 134 | |
| 135 | static void __exit devfreq_simple_ondemand_exit(void) |
| 136 | { |
| 137 | int ret; |
| 138 | |
| 139 | ret = devfreq_remove_governor(&devfreq_simple_ondemand); |
| 140 | if (ret) |
| 141 | pr_err("%s: failed remove governor %d\n", __func__, ret); |
| 142 | |
| 143 | return; |
| 144 | } |
| 145 | module_exit(devfreq_simple_ondemand_exit); |