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