Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 2 | /* |
| 3 | * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework |
| 4 | * for Non-CPU Devices. |
| 5 | * |
| 6 | * Copyright (C) 2011 Samsung Electronics |
| 7 | * MyungJoo Ham <myungjoo.ham@samsung.com> |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/kernel.h> |
Enric Balletbo i Serra | 23c7b54 | 2018-07-04 10:45:50 +0200 | [diff] [blame] | 11 | #include <linux/kmod.h> |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 12 | #include <linux/sched.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/init.h> |
Paul Gortmaker | 417dc4b | 2016-06-26 03:43:47 +0900 | [diff] [blame] | 16 | #include <linux/export.h> |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 17 | #include <linux/slab.h> |
MyungJoo Ham | 952f6d1 | 2011-11-10 10:16:23 +0100 | [diff] [blame] | 18 | #include <linux/stat.h> |
Nishanth Menon | e4db1c7 | 2013-09-19 16:03:52 -0500 | [diff] [blame] | 19 | #include <linux/pm_opp.h> |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 20 | #include <linux/devfreq.h> |
| 21 | #include <linux/workqueue.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/printk.h> |
| 25 | #include <linux/hrtimer.h> |
Chanwoo Choi | 8f510ae | 2015-11-10 20:31:07 +0900 | [diff] [blame] | 26 | #include <linux/of.h> |
Leonard Crestez | 05d7ae1 | 2019-12-05 12:05:06 +0200 | [diff] [blame] | 27 | #include <linux/pm_qos.h> |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 28 | #include "governor.h" |
| 29 | |
Lukasz Luba | cf451ad | 2019-02-18 19:21:09 +0100 | [diff] [blame] | 30 | #define CREATE_TRACE_POINTS |
| 31 | #include <trace/events/devfreq.h> |
| 32 | |
Leonard Crestez | 05d7ae1 | 2019-12-05 12:05:06 +0200 | [diff] [blame] | 33 | #define HZ_PER_KHZ 1000 |
| 34 | |
Nishanth Menon | 1a1357e | 2012-10-26 01:50:53 +0200 | [diff] [blame] | 35 | static struct class *devfreq_class; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 36 | |
| 37 | /* |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 38 | * devfreq core provides delayed work based load monitoring helper |
| 39 | * functions. Governors can use these or can implement their own |
| 40 | * monitoring mechanism. |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 41 | */ |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 42 | static struct workqueue_struct *devfreq_wq; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 43 | |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 44 | /* The list of all device-devfreq governors */ |
| 45 | static LIST_HEAD(devfreq_governor_list); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 46 | /* The list of all device-devfreq */ |
| 47 | static LIST_HEAD(devfreq_list); |
| 48 | static DEFINE_MUTEX(devfreq_list_lock); |
| 49 | |
| 50 | /** |
| 51 | * find_device_devfreq() - find devfreq struct using device pointer |
| 52 | * @dev: device pointer used to lookup device devfreq. |
| 53 | * |
| 54 | * Search the list of device devfreqs and return the matched device's |
| 55 | * devfreq info. devfreq_list_lock should be held by the caller. |
| 56 | */ |
| 57 | static struct devfreq *find_device_devfreq(struct device *dev) |
| 58 | { |
| 59 | struct devfreq *tmp_devfreq; |
| 60 | |
Viresh Kumar | 9348da2 | 2015-08-10 11:42:25 +0530 | [diff] [blame] | 61 | if (IS_ERR_OR_NULL(dev)) { |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 62 | pr_err("DEVFREQ: %s: Invalid parameters\n", __func__); |
| 63 | return ERR_PTR(-EINVAL); |
| 64 | } |
| 65 | WARN(!mutex_is_locked(&devfreq_list_lock), |
| 66 | "devfreq_list_lock must be locked."); |
| 67 | |
| 68 | list_for_each_entry(tmp_devfreq, &devfreq_list, node) { |
| 69 | if (tmp_devfreq->dev.parent == dev) |
| 70 | return tmp_devfreq; |
| 71 | } |
| 72 | |
| 73 | return ERR_PTR(-ENODEV); |
| 74 | } |
| 75 | |
Chanwoo Choi | ab8f58a | 2017-10-23 10:32:06 +0900 | [diff] [blame] | 76 | static unsigned long find_available_min_freq(struct devfreq *devfreq) |
| 77 | { |
| 78 | struct dev_pm_opp *opp; |
| 79 | unsigned long min_freq = 0; |
| 80 | |
| 81 | opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq); |
| 82 | if (IS_ERR(opp)) |
| 83 | min_freq = 0; |
| 84 | else |
| 85 | dev_pm_opp_put(opp); |
| 86 | |
| 87 | return min_freq; |
| 88 | } |
| 89 | |
| 90 | static unsigned long find_available_max_freq(struct devfreq *devfreq) |
| 91 | { |
| 92 | struct dev_pm_opp *opp; |
| 93 | unsigned long max_freq = ULONG_MAX; |
| 94 | |
| 95 | opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq); |
| 96 | if (IS_ERR(opp)) |
| 97 | max_freq = 0; |
| 98 | else |
| 99 | dev_pm_opp_put(opp); |
| 100 | |
| 101 | return max_freq; |
| 102 | } |
| 103 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 104 | /** |
Leonard Crestez | 46cecc0 | 2019-10-31 23:34:26 +0200 | [diff] [blame] | 105 | * get_freq_range() - Get the current freq range |
| 106 | * @devfreq: the devfreq instance |
| 107 | * @min_freq: the min frequency |
| 108 | * @max_freq: the max frequency |
| 109 | * |
| 110 | * This takes into consideration all constraints. |
| 111 | */ |
| 112 | static void get_freq_range(struct devfreq *devfreq, |
| 113 | unsigned long *min_freq, |
| 114 | unsigned long *max_freq) |
| 115 | { |
| 116 | unsigned long *freq_table = devfreq->profile->freq_table; |
Leonard Crestez | 05d7ae1 | 2019-12-05 12:05:06 +0200 | [diff] [blame] | 117 | s32 qos_min_freq, qos_max_freq; |
Leonard Crestez | 46cecc0 | 2019-10-31 23:34:26 +0200 | [diff] [blame] | 118 | |
| 119 | lockdep_assert_held(&devfreq->lock); |
| 120 | |
| 121 | /* |
| 122 | * Initialize minimum/maximum frequency from freq table. |
| 123 | * The devfreq drivers can initialize this in either ascending or |
| 124 | * descending order and devfreq core supports both. |
| 125 | */ |
| 126 | if (freq_table[0] < freq_table[devfreq->profile->max_state - 1]) { |
| 127 | *min_freq = freq_table[0]; |
| 128 | *max_freq = freq_table[devfreq->profile->max_state - 1]; |
| 129 | } else { |
| 130 | *min_freq = freq_table[devfreq->profile->max_state - 1]; |
| 131 | *max_freq = freq_table[0]; |
| 132 | } |
| 133 | |
Leonard Crestez | 05d7ae1 | 2019-12-05 12:05:06 +0200 | [diff] [blame] | 134 | /* Apply constraints from PM QoS */ |
| 135 | qos_min_freq = dev_pm_qos_read_value(devfreq->dev.parent, |
| 136 | DEV_PM_QOS_MIN_FREQUENCY); |
| 137 | qos_max_freq = dev_pm_qos_read_value(devfreq->dev.parent, |
| 138 | DEV_PM_QOS_MAX_FREQUENCY); |
| 139 | *min_freq = max(*min_freq, (unsigned long)HZ_PER_KHZ * qos_min_freq); |
| 140 | if (qos_max_freq != PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE) |
| 141 | *max_freq = min(*max_freq, |
| 142 | (unsigned long)HZ_PER_KHZ * qos_max_freq); |
| 143 | |
Leonard Crestez | 46cecc0 | 2019-10-31 23:34:26 +0200 | [diff] [blame] | 144 | /* Apply constraints from OPP interface */ |
| 145 | *min_freq = max(*min_freq, devfreq->scaling_min_freq); |
| 146 | *max_freq = min(*max_freq, devfreq->scaling_max_freq); |
| 147 | |
| 148 | if (*min_freq > *max_freq) |
| 149 | *min_freq = *max_freq; |
| 150 | } |
| 151 | |
| 152 | /** |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 153 | * devfreq_get_freq_level() - Lookup freq_table for the frequency |
| 154 | * @devfreq: the devfreq instance |
| 155 | * @freq: the target frequency |
| 156 | */ |
| 157 | static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq) |
| 158 | { |
| 159 | int lev; |
| 160 | |
| 161 | for (lev = 0; lev < devfreq->profile->max_state; lev++) |
| 162 | if (freq == devfreq->profile->freq_table[lev]) |
| 163 | return lev; |
| 164 | |
| 165 | return -EINVAL; |
| 166 | } |
| 167 | |
Chanwoo Choi | ea572f8 | 2017-10-23 10:32:09 +0900 | [diff] [blame] | 168 | static int set_freq_table(struct devfreq *devfreq) |
Chanwoo Choi | 0ec09ac | 2015-11-18 14:49:02 +0900 | [diff] [blame] | 169 | { |
| 170 | struct devfreq_dev_profile *profile = devfreq->profile; |
| 171 | struct dev_pm_opp *opp; |
| 172 | unsigned long freq; |
| 173 | int i, count; |
| 174 | |
| 175 | /* Initialize the freq_table from OPP table */ |
| 176 | count = dev_pm_opp_get_opp_count(devfreq->dev.parent); |
| 177 | if (count <= 0) |
Chanwoo Choi | ea572f8 | 2017-10-23 10:32:09 +0900 | [diff] [blame] | 178 | return -EINVAL; |
Chanwoo Choi | 0ec09ac | 2015-11-18 14:49:02 +0900 | [diff] [blame] | 179 | |
| 180 | profile->max_state = count; |
| 181 | profile->freq_table = devm_kcalloc(devfreq->dev.parent, |
| 182 | profile->max_state, |
| 183 | sizeof(*profile->freq_table), |
| 184 | GFP_KERNEL); |
| 185 | if (!profile->freq_table) { |
| 186 | profile->max_state = 0; |
Chanwoo Choi | ea572f8 | 2017-10-23 10:32:09 +0900 | [diff] [blame] | 187 | return -ENOMEM; |
Chanwoo Choi | 0ec09ac | 2015-11-18 14:49:02 +0900 | [diff] [blame] | 188 | } |
| 189 | |
Chanwoo Choi | 0ec09ac | 2015-11-18 14:49:02 +0900 | [diff] [blame] | 190 | for (i = 0, freq = 0; i < profile->max_state; i++, freq++) { |
| 191 | opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq); |
| 192 | if (IS_ERR(opp)) { |
| 193 | devm_kfree(devfreq->dev.parent, profile->freq_table); |
| 194 | profile->max_state = 0; |
Chanwoo Choi | ea572f8 | 2017-10-23 10:32:09 +0900 | [diff] [blame] | 195 | return PTR_ERR(opp); |
Chanwoo Choi | 0ec09ac | 2015-11-18 14:49:02 +0900 | [diff] [blame] | 196 | } |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 197 | dev_pm_opp_put(opp); |
Chanwoo Choi | 0ec09ac | 2015-11-18 14:49:02 +0900 | [diff] [blame] | 198 | profile->freq_table[i] = freq; |
| 199 | } |
Chanwoo Choi | ea572f8 | 2017-10-23 10:32:09 +0900 | [diff] [blame] | 200 | |
| 201 | return 0; |
Chanwoo Choi | 0ec09ac | 2015-11-18 14:49:02 +0900 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 205 | * devfreq_update_status() - Update statistics of devfreq behavior |
| 206 | * @devfreq: the devfreq instance |
| 207 | * @freq: the update target frequency |
| 208 | */ |
Chanwoo Choi | 30582c2 | 2017-01-31 15:38:17 +0900 | [diff] [blame] | 209 | int devfreq_update_status(struct devfreq *devfreq, unsigned long freq) |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 210 | { |
Saravana Kannan | e35d35a | 2014-02-27 19:38:57 -0800 | [diff] [blame] | 211 | int lev, prev_lev, ret = 0; |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 212 | unsigned long cur_time; |
| 213 | |
Leonard Crestez | 2abb0d5 | 2019-09-24 10:52:23 +0300 | [diff] [blame] | 214 | lockdep_assert_held(&devfreq->lock); |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 215 | cur_time = jiffies; |
Saravana Kannan | e35d35a | 2014-02-27 19:38:57 -0800 | [diff] [blame] | 216 | |
Tobias Jakobi | d0563a0 | 2016-09-29 14:36:36 +0200 | [diff] [blame] | 217 | /* Immediately exit if previous_freq is not initialized yet. */ |
| 218 | if (!devfreq->previous_freq) |
| 219 | goto out; |
| 220 | |
Saravana Kannan | e35d35a | 2014-02-27 19:38:57 -0800 | [diff] [blame] | 221 | prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq); |
| 222 | if (prev_lev < 0) { |
| 223 | ret = prev_lev; |
| 224 | goto out; |
| 225 | } |
| 226 | |
| 227 | devfreq->time_in_state[prev_lev] += |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 228 | cur_time - devfreq->last_stat_updated; |
Saravana Kannan | e35d35a | 2014-02-27 19:38:57 -0800 | [diff] [blame] | 229 | |
| 230 | lev = devfreq_get_freq_level(devfreq, freq); |
| 231 | if (lev < 0) { |
| 232 | ret = lev; |
| 233 | goto out; |
| 234 | } |
| 235 | |
| 236 | if (lev != prev_lev) { |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 237 | devfreq->trans_table[(prev_lev * |
| 238 | devfreq->profile->max_state) + lev]++; |
| 239 | devfreq->total_trans++; |
| 240 | } |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 241 | |
Saravana Kannan | e35d35a | 2014-02-27 19:38:57 -0800 | [diff] [blame] | 242 | out: |
| 243 | devfreq->last_stat_updated = cur_time; |
| 244 | return ret; |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 245 | } |
Chanwoo Choi | 30582c2 | 2017-01-31 15:38:17 +0900 | [diff] [blame] | 246 | EXPORT_SYMBOL(devfreq_update_status); |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 247 | |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 248 | /** |
| 249 | * find_devfreq_governor() - find devfreq governor from name |
| 250 | * @name: name of the governor |
| 251 | * |
| 252 | * Search the list of devfreq governors and return the matched |
| 253 | * governor's pointer. devfreq_list_lock should be held by the caller. |
| 254 | */ |
| 255 | static struct devfreq_governor *find_devfreq_governor(const char *name) |
| 256 | { |
| 257 | struct devfreq_governor *tmp_governor; |
| 258 | |
Viresh Kumar | 9348da2 | 2015-08-10 11:42:25 +0530 | [diff] [blame] | 259 | if (IS_ERR_OR_NULL(name)) { |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 260 | pr_err("DEVFREQ: %s: Invalid parameters\n", __func__); |
| 261 | return ERR_PTR(-EINVAL); |
| 262 | } |
| 263 | WARN(!mutex_is_locked(&devfreq_list_lock), |
| 264 | "devfreq_list_lock must be locked."); |
| 265 | |
| 266 | list_for_each_entry(tmp_governor, &devfreq_governor_list, node) { |
| 267 | if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN)) |
| 268 | return tmp_governor; |
| 269 | } |
| 270 | |
| 271 | return ERR_PTR(-ENODEV); |
| 272 | } |
| 273 | |
Enric Balletbo i Serra | 23c7b54 | 2018-07-04 10:45:50 +0200 | [diff] [blame] | 274 | /** |
| 275 | * try_then_request_governor() - Try to find the governor and request the |
| 276 | * module if is not found. |
| 277 | * @name: name of the governor |
| 278 | * |
| 279 | * Search the list of devfreq governors and request the module and try again |
| 280 | * if is not found. This can happen when both drivers (the governor driver |
| 281 | * and the driver that call devfreq_add_device) are built as modules. |
| 282 | * devfreq_list_lock should be held by the caller. Returns the matched |
Enric Balletbo i Serra | b53b012 | 2019-03-13 13:22:53 +0100 | [diff] [blame] | 283 | * governor's pointer or an error pointer. |
Enric Balletbo i Serra | 23c7b54 | 2018-07-04 10:45:50 +0200 | [diff] [blame] | 284 | */ |
| 285 | static struct devfreq_governor *try_then_request_governor(const char *name) |
| 286 | { |
| 287 | struct devfreq_governor *governor; |
| 288 | int err = 0; |
| 289 | |
| 290 | if (IS_ERR_OR_NULL(name)) { |
| 291 | pr_err("DEVFREQ: %s: Invalid parameters\n", __func__); |
| 292 | return ERR_PTR(-EINVAL); |
| 293 | } |
| 294 | WARN(!mutex_is_locked(&devfreq_list_lock), |
| 295 | "devfreq_list_lock must be locked."); |
| 296 | |
| 297 | governor = find_devfreq_governor(name); |
| 298 | if (IS_ERR(governor)) { |
| 299 | mutex_unlock(&devfreq_list_lock); |
| 300 | |
| 301 | if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND, |
| 302 | DEVFREQ_NAME_LEN)) |
| 303 | err = request_module("governor_%s", "simpleondemand"); |
| 304 | else |
| 305 | err = request_module("governor_%s", name); |
| 306 | /* Restore previous state before return */ |
| 307 | mutex_lock(&devfreq_list_lock); |
| 308 | if (err) |
Ezequiel Garcia | 7544fd7 | 2019-06-21 18:39:49 -0300 | [diff] [blame] | 309 | return (err < 0) ? ERR_PTR(err) : ERR_PTR(-EINVAL); |
Enric Balletbo i Serra | 23c7b54 | 2018-07-04 10:45:50 +0200 | [diff] [blame] | 310 | |
| 311 | governor = find_devfreq_governor(name); |
| 312 | } |
| 313 | |
| 314 | return governor; |
| 315 | } |
| 316 | |
Chanwoo Choi | 0fe3a66 | 2016-01-26 13:21:26 +0900 | [diff] [blame] | 317 | static int devfreq_notify_transition(struct devfreq *devfreq, |
| 318 | struct devfreq_freqs *freqs, unsigned int state) |
| 319 | { |
| 320 | if (!devfreq) |
| 321 | return -EINVAL; |
| 322 | |
| 323 | switch (state) { |
| 324 | case DEVFREQ_PRECHANGE: |
| 325 | srcu_notifier_call_chain(&devfreq->transition_notifier_list, |
| 326 | DEVFREQ_PRECHANGE, freqs); |
| 327 | break; |
| 328 | |
| 329 | case DEVFREQ_POSTCHANGE: |
| 330 | srcu_notifier_call_chain(&devfreq->transition_notifier_list, |
| 331 | DEVFREQ_POSTCHANGE, freqs); |
| 332 | break; |
| 333 | default: |
| 334 | return -EINVAL; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Lukasz Luba | 6331417 | 2018-12-05 12:05:52 +0100 | [diff] [blame] | 340 | static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq, |
| 341 | u32 flags) |
| 342 | { |
| 343 | struct devfreq_freqs freqs; |
| 344 | unsigned long cur_freq; |
| 345 | int err = 0; |
| 346 | |
| 347 | if (devfreq->profile->get_cur_freq) |
| 348 | devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq); |
| 349 | else |
| 350 | cur_freq = devfreq->previous_freq; |
| 351 | |
| 352 | freqs.old = cur_freq; |
| 353 | freqs.new = new_freq; |
| 354 | devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE); |
| 355 | |
| 356 | err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags); |
| 357 | if (err) { |
| 358 | freqs.new = cur_freq; |
| 359 | devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE); |
| 360 | return err; |
| 361 | } |
| 362 | |
| 363 | freqs.new = new_freq; |
| 364 | devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE); |
| 365 | |
| 366 | if (devfreq_update_status(devfreq, new_freq)) |
| 367 | dev_err(&devfreq->dev, |
| 368 | "Couldn't update frequency transition information.\n"); |
| 369 | |
| 370 | devfreq->previous_freq = new_freq; |
Lukasz Luba | 83f8ca4 | 2018-12-05 12:05:53 +0100 | [diff] [blame] | 371 | |
| 372 | if (devfreq->suspend_freq) |
| 373 | devfreq->resume_freq = cur_freq; |
| 374 | |
Lukasz Luba | 6331417 | 2018-12-05 12:05:52 +0100 | [diff] [blame] | 375 | return err; |
| 376 | } |
| 377 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 378 | /* Load monitoring helper functions for governors use */ |
| 379 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 380 | /** |
| 381 | * update_devfreq() - Reevaluate the device and configure frequency. |
| 382 | * @devfreq: the devfreq instance. |
| 383 | * |
| 384 | * Note: Lock devfreq->lock before calling update_devfreq |
| 385 | * This function is exported for governors. |
| 386 | */ |
| 387 | int update_devfreq(struct devfreq *devfreq) |
| 388 | { |
Lukasz Luba | 6331417 | 2018-12-05 12:05:52 +0100 | [diff] [blame] | 389 | unsigned long freq, min_freq, max_freq; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 390 | int err = 0; |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 391 | u32 flags = 0; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 392 | |
| 393 | if (!mutex_is_locked(&devfreq->lock)) { |
| 394 | WARN(true, "devfreq->lock must be locked by the caller.\n"); |
| 395 | return -EINVAL; |
| 396 | } |
| 397 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 398 | if (!devfreq->governor) |
| 399 | return -EINVAL; |
| 400 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 401 | /* Reevaluate the proper frequency */ |
| 402 | err = devfreq->governor->get_target_freq(devfreq, &freq); |
| 403 | if (err) |
| 404 | return err; |
Leonard Crestez | 46cecc0 | 2019-10-31 23:34:26 +0200 | [diff] [blame] | 405 | get_freq_range(devfreq, &min_freq, &max_freq); |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 406 | |
Matthias Kaehlcke | df5cf4a | 2018-08-03 13:05:09 -0700 | [diff] [blame] | 407 | if (freq < min_freq) { |
Chanwoo Choi | f1d981e | 2017-10-23 10:32:08 +0900 | [diff] [blame] | 408 | freq = min_freq; |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 409 | flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */ |
| 410 | } |
Matthias Kaehlcke | df5cf4a | 2018-08-03 13:05:09 -0700 | [diff] [blame] | 411 | if (freq > max_freq) { |
Chanwoo Choi | f1d981e | 2017-10-23 10:32:08 +0900 | [diff] [blame] | 412 | freq = max_freq; |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 413 | flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */ |
| 414 | } |
| 415 | |
Lukasz Luba | 6331417 | 2018-12-05 12:05:52 +0100 | [diff] [blame] | 416 | return devfreq_set_target(devfreq, freq, flags); |
Chanwoo Choi | 0fe3a66 | 2016-01-26 13:21:26 +0900 | [diff] [blame] | 417 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 418 | } |
Nishanth Menon | 2df5021 | 2012-10-29 15:01:42 -0500 | [diff] [blame] | 419 | EXPORT_SYMBOL(update_devfreq); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 420 | |
| 421 | /** |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 422 | * devfreq_monitor() - Periodically poll devfreq objects. |
| 423 | * @work: the work struct used to run devfreq_monitor periodically. |
| 424 | * |
| 425 | */ |
| 426 | static void devfreq_monitor(struct work_struct *work) |
| 427 | { |
| 428 | int err; |
| 429 | struct devfreq *devfreq = container_of(work, |
| 430 | struct devfreq, work.work); |
| 431 | |
| 432 | mutex_lock(&devfreq->lock); |
| 433 | err = update_devfreq(devfreq); |
| 434 | if (err) |
| 435 | dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err); |
| 436 | |
| 437 | queue_delayed_work(devfreq_wq, &devfreq->work, |
| 438 | msecs_to_jiffies(devfreq->profile->polling_ms)); |
| 439 | mutex_unlock(&devfreq->lock); |
Lukasz Luba | cf451ad | 2019-02-18 19:21:09 +0100 | [diff] [blame] | 440 | |
| 441 | trace_devfreq_monitor(devfreq); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /** |
| 445 | * devfreq_monitor_start() - Start load monitoring of devfreq instance |
| 446 | * @devfreq: the devfreq instance. |
| 447 | * |
Gaël PORTAY | 2c09083 | 2019-05-02 14:27:36 -0400 | [diff] [blame] | 448 | * Helper function for starting devfreq device load monitoring. By |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 449 | * default delayed work based monitoring is supported. Function |
| 450 | * to be called from governor in response to DEVFREQ_GOV_START |
| 451 | * event when device is added to devfreq framework. |
| 452 | */ |
| 453 | void devfreq_monitor_start(struct devfreq *devfreq) |
| 454 | { |
Dmitry Osipenko | 5c0f6c7 | 2019-11-05 00:56:14 +0300 | [diff] [blame] | 455 | if (devfreq->governor->interrupt_driven) |
| 456 | return; |
| 457 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 458 | INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor); |
| 459 | if (devfreq->profile->polling_ms) |
| 460 | queue_delayed_work(devfreq_wq, &devfreq->work, |
| 461 | msecs_to_jiffies(devfreq->profile->polling_ms)); |
| 462 | } |
MyungJoo Ham | 6dcdd8e | 2012-11-28 20:29:17 +0100 | [diff] [blame] | 463 | EXPORT_SYMBOL(devfreq_monitor_start); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 464 | |
| 465 | /** |
| 466 | * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance |
| 467 | * @devfreq: the devfreq instance. |
| 468 | * |
Gaël PORTAY | 2c09083 | 2019-05-02 14:27:36 -0400 | [diff] [blame] | 469 | * Helper function to stop devfreq device load monitoring. Function |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 470 | * to be called from governor in response to DEVFREQ_GOV_STOP |
| 471 | * event when device is removed from devfreq framework. |
| 472 | */ |
| 473 | void devfreq_monitor_stop(struct devfreq *devfreq) |
| 474 | { |
Dmitry Osipenko | 5c0f6c7 | 2019-11-05 00:56:14 +0300 | [diff] [blame] | 475 | if (devfreq->governor->interrupt_driven) |
| 476 | return; |
| 477 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 478 | cancel_delayed_work_sync(&devfreq->work); |
| 479 | } |
MyungJoo Ham | 6dcdd8e | 2012-11-28 20:29:17 +0100 | [diff] [blame] | 480 | EXPORT_SYMBOL(devfreq_monitor_stop); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 481 | |
| 482 | /** |
| 483 | * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance |
| 484 | * @devfreq: the devfreq instance. |
| 485 | * |
Gaël PORTAY | 2c09083 | 2019-05-02 14:27:36 -0400 | [diff] [blame] | 486 | * Helper function to suspend devfreq device load monitoring. Function |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 487 | * to be called from governor in response to DEVFREQ_GOV_SUSPEND |
| 488 | * event or when polling interval is set to zero. |
| 489 | * |
| 490 | * Note: Though this function is same as devfreq_monitor_stop(), |
| 491 | * intentionally kept separate to provide hooks for collecting |
| 492 | * transition statistics. |
| 493 | */ |
| 494 | void devfreq_monitor_suspend(struct devfreq *devfreq) |
| 495 | { |
| 496 | mutex_lock(&devfreq->lock); |
| 497 | if (devfreq->stop_polling) { |
| 498 | mutex_unlock(&devfreq->lock); |
| 499 | return; |
| 500 | } |
| 501 | |
Rajagopal Venkat | 39688ce6 | 2013-01-08 11:20:39 +0530 | [diff] [blame] | 502 | devfreq_update_status(devfreq, devfreq->previous_freq); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 503 | devfreq->stop_polling = true; |
| 504 | mutex_unlock(&devfreq->lock); |
Dmitry Osipenko | 5c0f6c7 | 2019-11-05 00:56:14 +0300 | [diff] [blame] | 505 | |
| 506 | if (devfreq->governor->interrupt_driven) |
| 507 | return; |
| 508 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 509 | cancel_delayed_work_sync(&devfreq->work); |
| 510 | } |
MyungJoo Ham | 6dcdd8e | 2012-11-28 20:29:17 +0100 | [diff] [blame] | 511 | EXPORT_SYMBOL(devfreq_monitor_suspend); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 512 | |
| 513 | /** |
| 514 | * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance |
| 515 | * @devfreq: the devfreq instance. |
| 516 | * |
Gaël PORTAY | 2c09083 | 2019-05-02 14:27:36 -0400 | [diff] [blame] | 517 | * Helper function to resume devfreq device load monitoring. Function |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 518 | * to be called from governor in response to DEVFREQ_GOV_RESUME |
| 519 | * event or when polling interval is set to non-zero. |
| 520 | */ |
| 521 | void devfreq_monitor_resume(struct devfreq *devfreq) |
| 522 | { |
Rajagopal Venkat | 39688ce6 | 2013-01-08 11:20:39 +0530 | [diff] [blame] | 523 | unsigned long freq; |
| 524 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 525 | mutex_lock(&devfreq->lock); |
| 526 | if (!devfreq->stop_polling) |
| 527 | goto out; |
| 528 | |
Dmitry Osipenko | 5c0f6c7 | 2019-11-05 00:56:14 +0300 | [diff] [blame] | 529 | if (devfreq->governor->interrupt_driven) |
| 530 | goto out_update; |
| 531 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 532 | if (!delayed_work_pending(&devfreq->work) && |
| 533 | devfreq->profile->polling_ms) |
| 534 | queue_delayed_work(devfreq_wq, &devfreq->work, |
| 535 | msecs_to_jiffies(devfreq->profile->polling_ms)); |
Rajagopal Venkat | 39688ce6 | 2013-01-08 11:20:39 +0530 | [diff] [blame] | 536 | |
Dmitry Osipenko | 5c0f6c7 | 2019-11-05 00:56:14 +0300 | [diff] [blame] | 537 | out_update: |
Rajagopal Venkat | 39688ce6 | 2013-01-08 11:20:39 +0530 | [diff] [blame] | 538 | devfreq->last_stat_updated = jiffies; |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 539 | devfreq->stop_polling = false; |
| 540 | |
Rajagopal Venkat | 39688ce6 | 2013-01-08 11:20:39 +0530 | [diff] [blame] | 541 | if (devfreq->profile->get_cur_freq && |
| 542 | !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq)) |
| 543 | devfreq->previous_freq = freq; |
| 544 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 545 | out: |
| 546 | mutex_unlock(&devfreq->lock); |
| 547 | } |
MyungJoo Ham | 6dcdd8e | 2012-11-28 20:29:17 +0100 | [diff] [blame] | 548 | EXPORT_SYMBOL(devfreq_monitor_resume); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 549 | |
| 550 | /** |
| 551 | * devfreq_interval_update() - Update device devfreq monitoring interval |
| 552 | * @devfreq: the devfreq instance. |
| 553 | * @delay: new polling interval to be set. |
| 554 | * |
| 555 | * Helper function to set new load monitoring polling interval. Function |
| 556 | * to be called from governor in response to DEVFREQ_GOV_INTERVAL event. |
| 557 | */ |
| 558 | void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay) |
| 559 | { |
| 560 | unsigned int cur_delay = devfreq->profile->polling_ms; |
| 561 | unsigned int new_delay = *delay; |
| 562 | |
| 563 | mutex_lock(&devfreq->lock); |
| 564 | devfreq->profile->polling_ms = new_delay; |
| 565 | |
| 566 | if (devfreq->stop_polling) |
| 567 | goto out; |
| 568 | |
Dmitry Osipenko | 5c0f6c7 | 2019-11-05 00:56:14 +0300 | [diff] [blame] | 569 | if (devfreq->governor->interrupt_driven) |
| 570 | goto out; |
| 571 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 572 | /* if new delay is zero, stop polling */ |
| 573 | if (!new_delay) { |
| 574 | mutex_unlock(&devfreq->lock); |
| 575 | cancel_delayed_work_sync(&devfreq->work); |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | /* if current delay is zero, start polling with new delay */ |
| 580 | if (!cur_delay) { |
| 581 | queue_delayed_work(devfreq_wq, &devfreq->work, |
| 582 | msecs_to_jiffies(devfreq->profile->polling_ms)); |
| 583 | goto out; |
| 584 | } |
| 585 | |
| 586 | /* if current delay is greater than new delay, restart polling */ |
| 587 | if (cur_delay > new_delay) { |
| 588 | mutex_unlock(&devfreq->lock); |
| 589 | cancel_delayed_work_sync(&devfreq->work); |
| 590 | mutex_lock(&devfreq->lock); |
| 591 | if (!devfreq->stop_polling) |
| 592 | queue_delayed_work(devfreq_wq, &devfreq->work, |
MyungJoo Ham | 6d690f7 | 2019-01-21 11:11:07 +0900 | [diff] [blame] | 593 | msecs_to_jiffies(devfreq->profile->polling_ms)); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 594 | } |
| 595 | out: |
| 596 | mutex_unlock(&devfreq->lock); |
| 597 | } |
MyungJoo Ham | 6dcdd8e | 2012-11-28 20:29:17 +0100 | [diff] [blame] | 598 | EXPORT_SYMBOL(devfreq_interval_update); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 599 | |
| 600 | /** |
| 601 | * devfreq_notifier_call() - Notify that the device frequency requirements |
MyungJoo Ham | 6d690f7 | 2019-01-21 11:11:07 +0900 | [diff] [blame] | 602 | * has been changed out of devfreq framework. |
Nishanth Menon | c5b4a1c1 | 2012-10-26 01:50:35 +0200 | [diff] [blame] | 603 | * @nb: the notifier_block (supposed to be devfreq->nb) |
| 604 | * @type: not used |
| 605 | * @devp: not used |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 606 | * |
| 607 | * Called by a notifier that uses devfreq->nb. |
| 608 | */ |
| 609 | static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type, |
| 610 | void *devp) |
| 611 | { |
| 612 | struct devfreq *devfreq = container_of(nb, struct devfreq, nb); |
Leonard Crestez | e876e71 | 2019-10-31 23:34:18 +0200 | [diff] [blame] | 613 | int err = -EINVAL; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 614 | |
| 615 | mutex_lock(&devfreq->lock); |
Chanwoo Choi | f1d981e | 2017-10-23 10:32:08 +0900 | [diff] [blame] | 616 | |
| 617 | devfreq->scaling_min_freq = find_available_min_freq(devfreq); |
Leonard Crestez | e876e71 | 2019-10-31 23:34:18 +0200 | [diff] [blame] | 618 | if (!devfreq->scaling_min_freq) |
| 619 | goto out; |
Chanwoo Choi | f1d981e | 2017-10-23 10:32:08 +0900 | [diff] [blame] | 620 | |
| 621 | devfreq->scaling_max_freq = find_available_max_freq(devfreq); |
Leonard Crestez | e7cc792 | 2019-10-31 23:34:19 +0200 | [diff] [blame] | 622 | if (!devfreq->scaling_max_freq) { |
| 623 | devfreq->scaling_max_freq = ULONG_MAX; |
Leonard Crestez | e876e71 | 2019-10-31 23:34:18 +0200 | [diff] [blame] | 624 | goto out; |
Leonard Crestez | e7cc792 | 2019-10-31 23:34:19 +0200 | [diff] [blame] | 625 | } |
Chanwoo Choi | f1d981e | 2017-10-23 10:32:08 +0900 | [diff] [blame] | 626 | |
Leonard Crestez | e876e71 | 2019-10-31 23:34:18 +0200 | [diff] [blame] | 627 | err = update_devfreq(devfreq); |
| 628 | |
| 629 | out: |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 630 | mutex_unlock(&devfreq->lock); |
Leonard Crestez | e876e71 | 2019-10-31 23:34:18 +0200 | [diff] [blame] | 631 | if (err) |
| 632 | dev_err(devfreq->dev.parent, |
| 633 | "failed to update frequency from OPP notifier (%d)\n", |
| 634 | err); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 635 | |
Leonard Crestez | e876e71 | 2019-10-31 23:34:18 +0200 | [diff] [blame] | 636 | return NOTIFY_OK; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /** |
Leonard Crestez | 05d7ae1 | 2019-12-05 12:05:06 +0200 | [diff] [blame] | 640 | * qos_notifier_call() - Common handler for QoS constraints. |
| 641 | * @devfreq: the devfreq instance. |
| 642 | */ |
| 643 | static int qos_notifier_call(struct devfreq *devfreq) |
| 644 | { |
| 645 | int err; |
| 646 | |
| 647 | mutex_lock(&devfreq->lock); |
| 648 | err = update_devfreq(devfreq); |
| 649 | mutex_unlock(&devfreq->lock); |
| 650 | if (err) |
| 651 | dev_err(devfreq->dev.parent, |
| 652 | "failed to update frequency from PM QoS (%d)\n", |
| 653 | err); |
| 654 | |
| 655 | return NOTIFY_OK; |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * qos_min_notifier_call() - Callback for QoS min_freq changes. |
| 660 | * @nb: Should be devfreq->nb_min |
| 661 | */ |
| 662 | static int qos_min_notifier_call(struct notifier_block *nb, |
| 663 | unsigned long val, void *ptr) |
| 664 | { |
| 665 | return qos_notifier_call(container_of(nb, struct devfreq, nb_min)); |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * qos_max_notifier_call() - Callback for QoS max_freq changes. |
| 670 | * @nb: Should be devfreq->nb_max |
| 671 | */ |
| 672 | static int qos_max_notifier_call(struct notifier_block *nb, |
| 673 | unsigned long val, void *ptr) |
| 674 | { |
| 675 | return qos_notifier_call(container_of(nb, struct devfreq, nb_max)); |
| 676 | } |
| 677 | |
| 678 | /** |
Chanwoo Choi | 29b6968 | 2017-01-31 15:38:18 +0900 | [diff] [blame] | 679 | * devfreq_dev_release() - Callback for struct device to release the device. |
| 680 | * @dev: the devfreq device |
| 681 | * |
| 682 | * Remove devfreq from the list and release its resources. |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 683 | */ |
Chanwoo Choi | 29b6968 | 2017-01-31 15:38:18 +0900 | [diff] [blame] | 684 | static void devfreq_dev_release(struct device *dev) |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 685 | { |
Chanwoo Choi | 29b6968 | 2017-01-31 15:38:18 +0900 | [diff] [blame] | 686 | struct devfreq *devfreq = to_devfreq(dev); |
Leonard Crestez | 05d7ae1 | 2019-12-05 12:05:06 +0200 | [diff] [blame] | 687 | int err; |
Chanwoo Choi | 29b6968 | 2017-01-31 15:38:18 +0900 | [diff] [blame] | 688 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 689 | mutex_lock(&devfreq_list_lock); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 690 | list_del(&devfreq->node); |
| 691 | mutex_unlock(&devfreq_list_lock); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 692 | |
Leonard Crestez | 05d7ae1 | 2019-12-05 12:05:06 +0200 | [diff] [blame] | 693 | err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_max, |
| 694 | DEV_PM_QOS_MAX_FREQUENCY); |
| 695 | if (err && err != -ENOENT) |
| 696 | dev_warn(dev->parent, |
| 697 | "Failed to remove max_freq notifier: %d\n", err); |
| 698 | err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_min, |
| 699 | DEV_PM_QOS_MIN_FREQUENCY); |
| 700 | if (err && err != -ENOENT) |
| 701 | dev_warn(dev->parent, |
| 702 | "Failed to remove min_freq notifier: %d\n", err); |
| 703 | |
Leonard Crestez | 27dbc54 | 2019-12-05 12:05:07 +0200 | [diff] [blame^] | 704 | if (dev_pm_qos_request_active(&devfreq->user_max_freq_req)) { |
| 705 | err = dev_pm_qos_remove_request(&devfreq->user_max_freq_req); |
| 706 | if (err) |
| 707 | dev_warn(dev->parent, |
| 708 | "Failed to remove max_freq request: %d\n", err); |
| 709 | } |
| 710 | if (dev_pm_qos_request_active(&devfreq->user_min_freq_req)) { |
| 711 | err = dev_pm_qos_remove_request(&devfreq->user_min_freq_req); |
| 712 | if (err) |
| 713 | dev_warn(dev->parent, |
| 714 | "Failed to remove min_freq request: %d\n", err); |
| 715 | } |
| 716 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 717 | if (devfreq->profile->exit) |
| 718 | devfreq->profile->exit(devfreq->dev.parent); |
| 719 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 720 | mutex_destroy(&devfreq->lock); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 721 | kfree(devfreq); |
| 722 | } |
| 723 | |
| 724 | /** |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 725 | * devfreq_add_device() - Add devfreq feature to the device |
| 726 | * @dev: the device to add devfreq feature. |
| 727 | * @profile: device-specific profile to run devfreq. |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 728 | * @governor_name: name of the policy to choose frequency. |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 729 | * @data: private data for the governor. The devfreq framework does not |
| 730 | * touch this value. |
| 731 | */ |
| 732 | struct devfreq *devfreq_add_device(struct device *dev, |
| 733 | struct devfreq_dev_profile *profile, |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 734 | const char *governor_name, |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 735 | void *data) |
| 736 | { |
| 737 | struct devfreq *devfreq; |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 738 | struct devfreq_governor *governor; |
Chanwoo Choi | 4585fbc | 2017-01-31 16:47:57 +0900 | [diff] [blame] | 739 | static atomic_t devfreq_no = ATOMIC_INIT(-1); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 740 | int err = 0; |
| 741 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 742 | if (!dev || !profile || !governor_name) { |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 743 | dev_err(dev, "%s: Invalid parameters.\n", __func__); |
| 744 | return ERR_PTR(-EINVAL); |
| 745 | } |
| 746 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 747 | mutex_lock(&devfreq_list_lock); |
| 748 | devfreq = find_device_devfreq(dev); |
| 749 | mutex_unlock(&devfreq_list_lock); |
| 750 | if (!IS_ERR(devfreq)) { |
Matthias Kaehlcke | df4d7b1 | 2019-09-18 17:09:46 -0700 | [diff] [blame] | 751 | dev_err(dev, "%s: devfreq device already exists!\n", |
Chanwoo Choi | 9d0109b | 2016-11-19 22:47:36 +0900 | [diff] [blame] | 752 | __func__); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 753 | err = -EINVAL; |
| 754 | goto err_out; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL); |
| 758 | if (!devfreq) { |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 759 | err = -ENOMEM; |
Axel Lin | 3f19f08 | 2011-11-15 21:59:09 +0100 | [diff] [blame] | 760 | goto err_out; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 761 | } |
| 762 | |
| 763 | mutex_init(&devfreq->lock); |
| 764 | mutex_lock(&devfreq->lock); |
| 765 | devfreq->dev.parent = dev; |
| 766 | devfreq->dev.class = devfreq_class; |
| 767 | devfreq->dev.release = devfreq_dev_release; |
Leonard Crestez | 42a6b25 | 2019-11-14 01:21:31 +0200 | [diff] [blame] | 768 | INIT_LIST_HEAD(&devfreq->node); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 769 | devfreq->profile = profile; |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 770 | strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 771 | devfreq->previous_freq = profile->initial_freq; |
Lukasz Luba | 8d39fc08 | 2016-05-31 11:25:09 +0100 | [diff] [blame] | 772 | devfreq->last_status.current_frequency = profile->initial_freq; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 773 | devfreq->data = data; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 774 | devfreq->nb.notifier_call = devfreq_notifier_call; |
| 775 | |
Chanwoo Choi | 0ec09ac | 2015-11-18 14:49:02 +0900 | [diff] [blame] | 776 | if (!devfreq->profile->max_state && !devfreq->profile->freq_table) { |
| 777 | mutex_unlock(&devfreq->lock); |
Chanwoo Choi | ea572f8 | 2017-10-23 10:32:09 +0900 | [diff] [blame] | 778 | err = set_freq_table(devfreq); |
| 779 | if (err < 0) |
Yangtao Li | a948791 | 2019-01-19 11:04:54 -0500 | [diff] [blame] | 780 | goto err_dev; |
Chanwoo Choi | 0ec09ac | 2015-11-18 14:49:02 +0900 | [diff] [blame] | 781 | mutex_lock(&devfreq->lock); |
| 782 | } |
| 783 | |
Matthias Kaehlcke | 2c2cb1e6 | 2018-05-25 13:30:33 -0700 | [diff] [blame] | 784 | devfreq->scaling_min_freq = find_available_min_freq(devfreq); |
| 785 | if (!devfreq->scaling_min_freq) { |
Chanwoo Choi | ab8f58a | 2017-10-23 10:32:06 +0900 | [diff] [blame] | 786 | mutex_unlock(&devfreq->lock); |
| 787 | err = -EINVAL; |
| 788 | goto err_dev; |
| 789 | } |
| 790 | |
Matthias Kaehlcke | 2c2cb1e6 | 2018-05-25 13:30:33 -0700 | [diff] [blame] | 791 | devfreq->scaling_max_freq = find_available_max_freq(devfreq); |
| 792 | if (!devfreq->scaling_max_freq) { |
Chanwoo Choi | ab8f58a | 2017-10-23 10:32:06 +0900 | [diff] [blame] | 793 | mutex_unlock(&devfreq->lock); |
| 794 | err = -EINVAL; |
| 795 | goto err_dev; |
| 796 | } |
| 797 | |
Lukasz Luba | 83f8ca4 | 2018-12-05 12:05:53 +0100 | [diff] [blame] | 798 | devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev); |
| 799 | atomic_set(&devfreq->suspend_count, 0); |
| 800 | |
Chanwoo Choi | 4585fbc | 2017-01-31 16:47:57 +0900 | [diff] [blame] | 801 | dev_set_name(&devfreq->dev, "devfreq%d", |
| 802 | atomic_inc_return(&devfreq_no)); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 803 | err = device_register(&devfreq->dev); |
| 804 | if (err) { |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 805 | mutex_unlock(&devfreq->lock); |
Arvind Yadav | 2d803dc | 2018-03-30 17:14:03 +0530 | [diff] [blame] | 806 | put_device(&devfreq->dev); |
| 807 | goto err_out; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 808 | } |
| 809 | |
MyungJoo Ham | 6d690f7 | 2019-01-21 11:11:07 +0900 | [diff] [blame] | 810 | devfreq->trans_table = devm_kzalloc(&devfreq->dev, |
| 811 | array3_size(sizeof(unsigned int), |
| 812 | devfreq->profile->max_state, |
| 813 | devfreq->profile->max_state), |
| 814 | GFP_KERNEL); |
Yangtao Li | 25846fa | 2019-01-19 11:04:53 -0500 | [diff] [blame] | 815 | if (!devfreq->trans_table) { |
| 816 | mutex_unlock(&devfreq->lock); |
| 817 | err = -ENOMEM; |
| 818 | goto err_devfreq; |
| 819 | } |
| 820 | |
Kees Cook | a86854d | 2018-06-12 14:07:58 -0700 | [diff] [blame] | 821 | devfreq->time_in_state = devm_kcalloc(&devfreq->dev, |
MyungJoo Ham | 6d690f7 | 2019-01-21 11:11:07 +0900 | [diff] [blame] | 822 | devfreq->profile->max_state, |
| 823 | sizeof(unsigned long), |
| 824 | GFP_KERNEL); |
Yangtao Li | 25846fa | 2019-01-19 11:04:53 -0500 | [diff] [blame] | 825 | if (!devfreq->time_in_state) { |
| 826 | mutex_unlock(&devfreq->lock); |
| 827 | err = -ENOMEM; |
| 828 | goto err_devfreq; |
| 829 | } |
| 830 | |
MyungJoo Ham | 3e1d7fb | 2015-10-02 12:39:23 +0900 | [diff] [blame] | 831 | devfreq->last_stat_updated = jiffies; |
| 832 | |
Chanwoo Choi | 0fe3a66 | 2016-01-26 13:21:26 +0900 | [diff] [blame] | 833 | srcu_init_notifier_head(&devfreq->transition_notifier_list); |
| 834 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 835 | mutex_unlock(&devfreq->lock); |
| 836 | |
Leonard Crestez | 27dbc54 | 2019-12-05 12:05:07 +0200 | [diff] [blame^] | 837 | err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req, |
| 838 | DEV_PM_QOS_MIN_FREQUENCY, 0); |
| 839 | if (err < 0) |
| 840 | goto err_devfreq; |
| 841 | err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req, |
| 842 | DEV_PM_QOS_MAX_FREQUENCY, |
| 843 | PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE); |
| 844 | if (err < 0) |
| 845 | goto err_devfreq; |
| 846 | |
Leonard Crestez | 05d7ae1 | 2019-12-05 12:05:06 +0200 | [diff] [blame] | 847 | devfreq->nb_min.notifier_call = qos_min_notifier_call; |
| 848 | err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_min, |
| 849 | DEV_PM_QOS_MIN_FREQUENCY); |
| 850 | if (err) |
| 851 | goto err_devfreq; |
| 852 | |
| 853 | devfreq->nb_max.notifier_call = qos_max_notifier_call; |
| 854 | err = dev_pm_qos_add_notifier(devfreq->dev.parent, &devfreq->nb_max, |
| 855 | DEV_PM_QOS_MAX_FREQUENCY); |
| 856 | if (err) |
| 857 | goto err_devfreq; |
| 858 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 859 | mutex_lock(&devfreq_list_lock); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 860 | |
Enric Balletbo i Serra | 23c7b54 | 2018-07-04 10:45:50 +0200 | [diff] [blame] | 861 | governor = try_then_request_governor(devfreq->governor_name); |
Chanwoo Choi | 73613b1 | 2016-12-28 20:52:35 +0900 | [diff] [blame] | 862 | if (IS_ERR(governor)) { |
| 863 | dev_err(dev, "%s: Unable to find governor for the device\n", |
| 864 | __func__); |
| 865 | err = PTR_ERR(governor); |
| 866 | goto err_init; |
| 867 | } |
| 868 | |
| 869 | devfreq->governor = governor; |
| 870 | err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START, |
| 871 | NULL); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 872 | if (err) { |
| 873 | dev_err(dev, "%s: Unable to start governor for the device\n", |
| 874 | __func__); |
| 875 | goto err_init; |
| 876 | } |
Enric Balletbo i Serra | 23c7b54 | 2018-07-04 10:45:50 +0200 | [diff] [blame] | 877 | |
| 878 | list_add(&devfreq->node, &devfreq_list); |
| 879 | |
Axel Lin | 0f376c9 | 2016-09-29 10:13:28 +0800 | [diff] [blame] | 880 | mutex_unlock(&devfreq_list_lock); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 881 | |
Axel Lin | 3f19f08 | 2011-11-15 21:59:09 +0100 | [diff] [blame] | 882 | return devfreq; |
| 883 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 884 | err_init: |
Axel Lin | 0f376c9 | 2016-09-29 10:13:28 +0800 | [diff] [blame] | 885 | mutex_unlock(&devfreq_list_lock); |
Yangtao Li | 25846fa | 2019-01-19 11:04:53 -0500 | [diff] [blame] | 886 | err_devfreq: |
Vincent Donnefort | 2f061fd | 2018-09-03 09:02:07 +0900 | [diff] [blame] | 887 | devfreq_remove_device(devfreq); |
Arvind Yadav | 2d803dc | 2018-03-30 17:14:03 +0530 | [diff] [blame] | 888 | devfreq = NULL; |
Chanwoo Choi | 9e14de1 | 2017-08-24 10:42:48 +0900 | [diff] [blame] | 889 | err_dev: |
zhong jiang | 8188b15 | 2018-09-21 21:18:43 +0800 | [diff] [blame] | 890 | kfree(devfreq); |
Axel Lin | 3f19f08 | 2011-11-15 21:59:09 +0100 | [diff] [blame] | 891 | err_out: |
| 892 | return ERR_PTR(err); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 893 | } |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 894 | EXPORT_SYMBOL(devfreq_add_device); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 895 | |
| 896 | /** |
| 897 | * devfreq_remove_device() - Remove devfreq feature from a device. |
Nishanth Menon | c5b4a1c1 | 2012-10-26 01:50:35 +0200 | [diff] [blame] | 898 | * @devfreq: the devfreq instance to be removed |
MyungJoo Ham | de9c739 | 2013-02-05 18:40:17 +0900 | [diff] [blame] | 899 | * |
| 900 | * The opposite of devfreq_add_device(). |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 901 | */ |
| 902 | int devfreq_remove_device(struct devfreq *devfreq) |
| 903 | { |
| 904 | if (!devfreq) |
| 905 | return -EINVAL; |
| 906 | |
Vincent Donnefort | 2f061fd | 2018-09-03 09:02:07 +0900 | [diff] [blame] | 907 | if (devfreq->governor) |
| 908 | devfreq->governor->event_handler(devfreq, |
| 909 | DEVFREQ_GOV_STOP, NULL); |
Chanwoo Choi | 585fc83 | 2014-05-09 16:43:07 +0900 | [diff] [blame] | 910 | device_unregister(&devfreq->dev); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 911 | |
| 912 | return 0; |
| 913 | } |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 914 | EXPORT_SYMBOL(devfreq_remove_device); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 915 | |
Chanwoo Choi | 8cd8409 | 2014-05-09 16:43:08 +0900 | [diff] [blame] | 916 | static int devm_devfreq_dev_match(struct device *dev, void *res, void *data) |
| 917 | { |
| 918 | struct devfreq **r = res; |
| 919 | |
| 920 | if (WARN_ON(!r || !*r)) |
| 921 | return 0; |
| 922 | |
| 923 | return *r == data; |
| 924 | } |
| 925 | |
| 926 | static void devm_devfreq_dev_release(struct device *dev, void *res) |
| 927 | { |
| 928 | devfreq_remove_device(*(struct devfreq **)res); |
| 929 | } |
| 930 | |
| 931 | /** |
| 932 | * devm_devfreq_add_device() - Resource-managed devfreq_add_device() |
| 933 | * @dev: the device to add devfreq feature. |
| 934 | * @profile: device-specific profile to run devfreq. |
| 935 | * @governor_name: name of the policy to choose frequency. |
| 936 | * @data: private data for the governor. The devfreq framework does not |
| 937 | * touch this value. |
| 938 | * |
| 939 | * This function manages automatically the memory of devfreq device using device |
| 940 | * resource management and simplify the free operation for memory of devfreq |
| 941 | * device. |
| 942 | */ |
| 943 | struct devfreq *devm_devfreq_add_device(struct device *dev, |
| 944 | struct devfreq_dev_profile *profile, |
| 945 | const char *governor_name, |
| 946 | void *data) |
| 947 | { |
| 948 | struct devfreq **ptr, *devfreq; |
| 949 | |
| 950 | ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL); |
| 951 | if (!ptr) |
| 952 | return ERR_PTR(-ENOMEM); |
| 953 | |
| 954 | devfreq = devfreq_add_device(dev, profile, governor_name, data); |
| 955 | if (IS_ERR(devfreq)) { |
| 956 | devres_free(ptr); |
Bjorn Andersson | d1bf2d30 | 2017-11-05 21:27:41 -0800 | [diff] [blame] | 957 | return devfreq; |
Chanwoo Choi | 8cd8409 | 2014-05-09 16:43:08 +0900 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | *ptr = devfreq; |
| 961 | devres_add(dev, ptr); |
| 962 | |
| 963 | return devfreq; |
| 964 | } |
| 965 | EXPORT_SYMBOL(devm_devfreq_add_device); |
| 966 | |
Chanwoo Choi | 8f510ae | 2015-11-10 20:31:07 +0900 | [diff] [blame] | 967 | #ifdef CONFIG_OF |
| 968 | /* |
| 969 | * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree |
| 970 | * @dev - instance to the given device |
| 971 | * @index - index into list of devfreq |
| 972 | * |
| 973 | * return the instance of devfreq device |
| 974 | */ |
| 975 | struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index) |
| 976 | { |
| 977 | struct device_node *node; |
| 978 | struct devfreq *devfreq; |
| 979 | |
| 980 | if (!dev) |
| 981 | return ERR_PTR(-EINVAL); |
| 982 | |
| 983 | if (!dev->of_node) |
| 984 | return ERR_PTR(-EINVAL); |
| 985 | |
| 986 | node = of_parse_phandle(dev->of_node, "devfreq", index); |
| 987 | if (!node) |
| 988 | return ERR_PTR(-ENODEV); |
| 989 | |
| 990 | mutex_lock(&devfreq_list_lock); |
| 991 | list_for_each_entry(devfreq, &devfreq_list, node) { |
| 992 | if (devfreq->dev.parent |
| 993 | && devfreq->dev.parent->of_node == node) { |
| 994 | mutex_unlock(&devfreq_list_lock); |
Peter Chen | 3427c6f | 2016-07-01 17:42:00 +0800 | [diff] [blame] | 995 | of_node_put(node); |
Chanwoo Choi | 8f510ae | 2015-11-10 20:31:07 +0900 | [diff] [blame] | 996 | return devfreq; |
| 997 | } |
| 998 | } |
| 999 | mutex_unlock(&devfreq_list_lock); |
Peter Chen | 3427c6f | 2016-07-01 17:42:00 +0800 | [diff] [blame] | 1000 | of_node_put(node); |
Chanwoo Choi | 8f510ae | 2015-11-10 20:31:07 +0900 | [diff] [blame] | 1001 | |
| 1002 | return ERR_PTR(-EPROBE_DEFER); |
| 1003 | } |
| 1004 | #else |
| 1005 | struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, int index) |
| 1006 | { |
| 1007 | return ERR_PTR(-ENODEV); |
| 1008 | } |
| 1009 | #endif /* CONFIG_OF */ |
| 1010 | EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle); |
| 1011 | |
Chanwoo Choi | 8cd8409 | 2014-05-09 16:43:08 +0900 | [diff] [blame] | 1012 | /** |
| 1013 | * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device() |
Krzysztof Kozlowski | e2fc167 | 2019-07-24 20:59:53 +0200 | [diff] [blame] | 1014 | * @dev: the device from which to remove devfreq feature. |
Chanwoo Choi | 8cd8409 | 2014-05-09 16:43:08 +0900 | [diff] [blame] | 1015 | * @devfreq: the devfreq instance to be removed |
| 1016 | */ |
| 1017 | void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq) |
| 1018 | { |
| 1019 | WARN_ON(devres_release(dev, devm_devfreq_dev_release, |
| 1020 | devm_devfreq_dev_match, devfreq)); |
| 1021 | } |
| 1022 | EXPORT_SYMBOL(devm_devfreq_remove_device); |
| 1023 | |
Rajagopal Venkat | 206c30c | 2012-10-26 01:50:18 +0200 | [diff] [blame] | 1024 | /** |
| 1025 | * devfreq_suspend_device() - Suspend devfreq of a device. |
| 1026 | * @devfreq: the devfreq instance to be suspended |
MyungJoo Ham | de9c739 | 2013-02-05 18:40:17 +0900 | [diff] [blame] | 1027 | * |
| 1028 | * This function is intended to be called by the pm callbacks |
| 1029 | * (e.g., runtime_suspend, suspend) of the device driver that |
| 1030 | * holds the devfreq. |
Rajagopal Venkat | 206c30c | 2012-10-26 01:50:18 +0200 | [diff] [blame] | 1031 | */ |
| 1032 | int devfreq_suspend_device(struct devfreq *devfreq) |
| 1033 | { |
Lukasz Luba | 83f8ca4 | 2018-12-05 12:05:53 +0100 | [diff] [blame] | 1034 | int ret; |
| 1035 | |
Rajagopal Venkat | 206c30c | 2012-10-26 01:50:18 +0200 | [diff] [blame] | 1036 | if (!devfreq) |
| 1037 | return -EINVAL; |
| 1038 | |
Lukasz Luba | 83f8ca4 | 2018-12-05 12:05:53 +0100 | [diff] [blame] | 1039 | if (atomic_inc_return(&devfreq->suspend_count) > 1) |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 1040 | return 0; |
| 1041 | |
Lukasz Luba | 83f8ca4 | 2018-12-05 12:05:53 +0100 | [diff] [blame] | 1042 | if (devfreq->governor) { |
| 1043 | ret = devfreq->governor->event_handler(devfreq, |
| 1044 | DEVFREQ_GOV_SUSPEND, NULL); |
| 1045 | if (ret) |
| 1046 | return ret; |
| 1047 | } |
| 1048 | |
| 1049 | if (devfreq->suspend_freq) { |
Marek Szyprowski | e1e047a | 2019-11-12 11:47:34 +0100 | [diff] [blame] | 1050 | mutex_lock(&devfreq->lock); |
Lukasz Luba | 83f8ca4 | 2018-12-05 12:05:53 +0100 | [diff] [blame] | 1051 | ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0); |
Marek Szyprowski | e1e047a | 2019-11-12 11:47:34 +0100 | [diff] [blame] | 1052 | mutex_unlock(&devfreq->lock); |
Lukasz Luba | 83f8ca4 | 2018-12-05 12:05:53 +0100 | [diff] [blame] | 1053 | if (ret) |
| 1054 | return ret; |
| 1055 | } |
| 1056 | |
| 1057 | return 0; |
Rajagopal Venkat | 206c30c | 2012-10-26 01:50:18 +0200 | [diff] [blame] | 1058 | } |
| 1059 | EXPORT_SYMBOL(devfreq_suspend_device); |
| 1060 | |
| 1061 | /** |
| 1062 | * devfreq_resume_device() - Resume devfreq of a device. |
| 1063 | * @devfreq: the devfreq instance to be resumed |
MyungJoo Ham | de9c739 | 2013-02-05 18:40:17 +0900 | [diff] [blame] | 1064 | * |
| 1065 | * This function is intended to be called by the pm callbacks |
| 1066 | * (e.g., runtime_resume, resume) of the device driver that |
| 1067 | * holds the devfreq. |
Rajagopal Venkat | 206c30c | 2012-10-26 01:50:18 +0200 | [diff] [blame] | 1068 | */ |
| 1069 | int devfreq_resume_device(struct devfreq *devfreq) |
| 1070 | { |
Lukasz Luba | 83f8ca4 | 2018-12-05 12:05:53 +0100 | [diff] [blame] | 1071 | int ret; |
| 1072 | |
Rajagopal Venkat | 206c30c | 2012-10-26 01:50:18 +0200 | [diff] [blame] | 1073 | if (!devfreq) |
| 1074 | return -EINVAL; |
| 1075 | |
Lukasz Luba | 83f8ca4 | 2018-12-05 12:05:53 +0100 | [diff] [blame] | 1076 | if (atomic_dec_return(&devfreq->suspend_count) >= 1) |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 1077 | return 0; |
| 1078 | |
Lukasz Luba | 83f8ca4 | 2018-12-05 12:05:53 +0100 | [diff] [blame] | 1079 | if (devfreq->resume_freq) { |
Marek Szyprowski | e1e047a | 2019-11-12 11:47:34 +0100 | [diff] [blame] | 1080 | mutex_lock(&devfreq->lock); |
Lukasz Luba | 83f8ca4 | 2018-12-05 12:05:53 +0100 | [diff] [blame] | 1081 | ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0); |
Marek Szyprowski | e1e047a | 2019-11-12 11:47:34 +0100 | [diff] [blame] | 1082 | mutex_unlock(&devfreq->lock); |
Lukasz Luba | 83f8ca4 | 2018-12-05 12:05:53 +0100 | [diff] [blame] | 1083 | if (ret) |
| 1084 | return ret; |
| 1085 | } |
| 1086 | |
| 1087 | if (devfreq->governor) { |
| 1088 | ret = devfreq->governor->event_handler(devfreq, |
| 1089 | DEVFREQ_GOV_RESUME, NULL); |
| 1090 | if (ret) |
| 1091 | return ret; |
| 1092 | } |
| 1093 | |
| 1094 | return 0; |
Rajagopal Venkat | 206c30c | 2012-10-26 01:50:18 +0200 | [diff] [blame] | 1095 | } |
| 1096 | EXPORT_SYMBOL(devfreq_resume_device); |
| 1097 | |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 1098 | /** |
Lukasz Luba | 5903195 | 2018-12-05 12:05:54 +0100 | [diff] [blame] | 1099 | * devfreq_suspend() - Suspend devfreq governors and devices |
| 1100 | * |
| 1101 | * Called during system wide Suspend/Hibernate cycles for suspending governors |
| 1102 | * and devices preserving the state for resume. On some platforms the devfreq |
| 1103 | * device must have precise state (frequency) after resume in order to provide |
| 1104 | * fully operating setup. |
| 1105 | */ |
| 1106 | void devfreq_suspend(void) |
| 1107 | { |
| 1108 | struct devfreq *devfreq; |
| 1109 | int ret; |
| 1110 | |
| 1111 | mutex_lock(&devfreq_list_lock); |
| 1112 | list_for_each_entry(devfreq, &devfreq_list, node) { |
| 1113 | ret = devfreq_suspend_device(devfreq); |
| 1114 | if (ret) |
| 1115 | dev_err(&devfreq->dev, |
| 1116 | "failed to suspend devfreq device\n"); |
| 1117 | } |
| 1118 | mutex_unlock(&devfreq_list_lock); |
| 1119 | } |
| 1120 | |
| 1121 | /** |
| 1122 | * devfreq_resume() - Resume devfreq governors and devices |
| 1123 | * |
| 1124 | * Called during system wide Suspend/Hibernate cycle for resuming governors and |
| 1125 | * devices that are suspended with devfreq_suspend(). |
| 1126 | */ |
| 1127 | void devfreq_resume(void) |
| 1128 | { |
| 1129 | struct devfreq *devfreq; |
| 1130 | int ret; |
| 1131 | |
| 1132 | mutex_lock(&devfreq_list_lock); |
| 1133 | list_for_each_entry(devfreq, &devfreq_list, node) { |
| 1134 | ret = devfreq_resume_device(devfreq); |
| 1135 | if (ret) |
| 1136 | dev_warn(&devfreq->dev, |
| 1137 | "failed to resume devfreq device\n"); |
| 1138 | } |
| 1139 | mutex_unlock(&devfreq_list_lock); |
| 1140 | } |
| 1141 | |
| 1142 | /** |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 1143 | * devfreq_add_governor() - Add devfreq governor |
| 1144 | * @governor: the devfreq governor to be added |
| 1145 | */ |
| 1146 | int devfreq_add_governor(struct devfreq_governor *governor) |
| 1147 | { |
| 1148 | struct devfreq_governor *g; |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 1149 | struct devfreq *devfreq; |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 1150 | int err = 0; |
| 1151 | |
| 1152 | if (!governor) { |
| 1153 | pr_err("%s: Invalid parameters.\n", __func__); |
| 1154 | return -EINVAL; |
| 1155 | } |
| 1156 | |
| 1157 | mutex_lock(&devfreq_list_lock); |
| 1158 | g = find_devfreq_governor(governor->name); |
| 1159 | if (!IS_ERR(g)) { |
| 1160 | pr_err("%s: governor %s already registered\n", __func__, |
| 1161 | g->name); |
| 1162 | err = -EINVAL; |
| 1163 | goto err_out; |
| 1164 | } |
| 1165 | |
| 1166 | list_add(&governor->node, &devfreq_governor_list); |
| 1167 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 1168 | list_for_each_entry(devfreq, &devfreq_list, node) { |
| 1169 | int ret = 0; |
| 1170 | struct device *dev = devfreq->dev.parent; |
| 1171 | |
| 1172 | if (!strncmp(devfreq->governor_name, governor->name, |
| 1173 | DEVFREQ_NAME_LEN)) { |
| 1174 | /* The following should never occur */ |
| 1175 | if (devfreq->governor) { |
| 1176 | dev_warn(dev, |
| 1177 | "%s: Governor %s already present\n", |
| 1178 | __func__, devfreq->governor->name); |
| 1179 | ret = devfreq->governor->event_handler(devfreq, |
| 1180 | DEVFREQ_GOV_STOP, NULL); |
| 1181 | if (ret) { |
| 1182 | dev_warn(dev, |
| 1183 | "%s: Governor %s stop = %d\n", |
| 1184 | __func__, |
| 1185 | devfreq->governor->name, ret); |
| 1186 | } |
| 1187 | /* Fall through */ |
| 1188 | } |
| 1189 | devfreq->governor = governor; |
| 1190 | ret = devfreq->governor->event_handler(devfreq, |
| 1191 | DEVFREQ_GOV_START, NULL); |
| 1192 | if (ret) { |
| 1193 | dev_warn(dev, "%s: Governor %s start=%d\n", |
| 1194 | __func__, devfreq->governor->name, |
| 1195 | ret); |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 1200 | err_out: |
| 1201 | mutex_unlock(&devfreq_list_lock); |
| 1202 | |
| 1203 | return err; |
| 1204 | } |
| 1205 | EXPORT_SYMBOL(devfreq_add_governor); |
| 1206 | |
| 1207 | /** |
MyungJoo Ham | bafeb42 | 2016-11-09 10:29:14 +0900 | [diff] [blame] | 1208 | * devfreq_remove_governor() - Remove devfreq feature from a device. |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 1209 | * @governor: the devfreq governor to be removed |
| 1210 | */ |
| 1211 | int devfreq_remove_governor(struct devfreq_governor *governor) |
| 1212 | { |
| 1213 | struct devfreq_governor *g; |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 1214 | struct devfreq *devfreq; |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 1215 | int err = 0; |
| 1216 | |
| 1217 | if (!governor) { |
| 1218 | pr_err("%s: Invalid parameters.\n", __func__); |
| 1219 | return -EINVAL; |
| 1220 | } |
| 1221 | |
| 1222 | mutex_lock(&devfreq_list_lock); |
| 1223 | g = find_devfreq_governor(governor->name); |
| 1224 | if (IS_ERR(g)) { |
| 1225 | pr_err("%s: governor %s not registered\n", __func__, |
Sachin Kamat | b9e1c8e | 2012-11-21 10:36:13 +0530 | [diff] [blame] | 1226 | governor->name); |
Sachin Kamat | f9c08e2 | 2012-11-21 10:36:14 +0530 | [diff] [blame] | 1227 | err = PTR_ERR(g); |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 1228 | goto err_out; |
| 1229 | } |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 1230 | list_for_each_entry(devfreq, &devfreq_list, node) { |
| 1231 | int ret; |
| 1232 | struct device *dev = devfreq->dev.parent; |
| 1233 | |
| 1234 | if (!strncmp(devfreq->governor_name, governor->name, |
| 1235 | DEVFREQ_NAME_LEN)) { |
| 1236 | /* we should have a devfreq governor! */ |
| 1237 | if (!devfreq->governor) { |
| 1238 | dev_warn(dev, "%s: Governor %s NOT present\n", |
| 1239 | __func__, governor->name); |
| 1240 | continue; |
| 1241 | /* Fall through */ |
| 1242 | } |
| 1243 | ret = devfreq->governor->event_handler(devfreq, |
| 1244 | DEVFREQ_GOV_STOP, NULL); |
| 1245 | if (ret) { |
| 1246 | dev_warn(dev, "%s: Governor %s stop=%d\n", |
| 1247 | __func__, devfreq->governor->name, |
| 1248 | ret); |
| 1249 | } |
| 1250 | devfreq->governor = NULL; |
| 1251 | } |
| 1252 | } |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 1253 | |
| 1254 | list_del(&governor->node); |
| 1255 | err_out: |
| 1256 | mutex_unlock(&devfreq_list_lock); |
| 1257 | |
| 1258 | return err; |
| 1259 | } |
| 1260 | EXPORT_SYMBOL(devfreq_remove_governor); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1261 | |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1262 | static ssize_t governor_show(struct device *dev, |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1263 | struct device_attribute *attr, char *buf) |
| 1264 | { |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 1265 | if (!to_devfreq(dev)->governor) |
| 1266 | return -EINVAL; |
| 1267 | |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1268 | return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name); |
| 1269 | } |
| 1270 | |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1271 | static ssize_t governor_store(struct device *dev, struct device_attribute *attr, |
Nishanth Menon | 0359d1a | 2012-10-29 15:01:47 -0500 | [diff] [blame] | 1272 | const char *buf, size_t count) |
| 1273 | { |
| 1274 | struct devfreq *df = to_devfreq(dev); |
| 1275 | int ret; |
| 1276 | char str_governor[DEVFREQ_NAME_LEN + 1]; |
Saravana Kannan | bc658be | 2019-03-11 15:36:30 +0530 | [diff] [blame] | 1277 | const struct devfreq_governor *governor, *prev_governor; |
Nishanth Menon | 0359d1a | 2012-10-29 15:01:47 -0500 | [diff] [blame] | 1278 | |
| 1279 | ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor); |
| 1280 | if (ret != 1) |
| 1281 | return -EINVAL; |
| 1282 | |
| 1283 | mutex_lock(&devfreq_list_lock); |
Enric Balletbo i Serra | 23c7b54 | 2018-07-04 10:45:50 +0200 | [diff] [blame] | 1284 | governor = try_then_request_governor(str_governor); |
Nishanth Menon | 0359d1a | 2012-10-29 15:01:47 -0500 | [diff] [blame] | 1285 | if (IS_ERR(governor)) { |
| 1286 | ret = PTR_ERR(governor); |
| 1287 | goto out; |
| 1288 | } |
Tobias Jakobi | 14a21e7 | 2015-09-21 20:23:52 +0200 | [diff] [blame] | 1289 | if (df->governor == governor) { |
| 1290 | ret = 0; |
Nishanth Menon | 0359d1a | 2012-10-29 15:01:47 -0500 | [diff] [blame] | 1291 | goto out; |
Gustavo A. R. Silva | 63f1e05 | 2017-12-06 14:20:15 -0600 | [diff] [blame] | 1292 | } else if ((df->governor && df->governor->immutable) || |
| 1293 | governor->immutable) { |
Chanwoo Choi | bcf23c7 | 2017-01-31 15:38:16 +0900 | [diff] [blame] | 1294 | ret = -EINVAL; |
| 1295 | goto out; |
Tobias Jakobi | 14a21e7 | 2015-09-21 20:23:52 +0200 | [diff] [blame] | 1296 | } |
Nishanth Menon | 0359d1a | 2012-10-29 15:01:47 -0500 | [diff] [blame] | 1297 | |
| 1298 | if (df->governor) { |
| 1299 | ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL); |
| 1300 | if (ret) { |
| 1301 | dev_warn(dev, "%s: Governor %s not stopped(%d)\n", |
| 1302 | __func__, df->governor->name, ret); |
| 1303 | goto out; |
| 1304 | } |
| 1305 | } |
Saravana Kannan | bc658be | 2019-03-11 15:36:30 +0530 | [diff] [blame] | 1306 | prev_governor = df->governor; |
Nishanth Menon | 0359d1a | 2012-10-29 15:01:47 -0500 | [diff] [blame] | 1307 | df->governor = governor; |
| 1308 | strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN); |
| 1309 | ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL); |
Saravana Kannan | bc658be | 2019-03-11 15:36:30 +0530 | [diff] [blame] | 1310 | if (ret) { |
Nishanth Menon | 0359d1a | 2012-10-29 15:01:47 -0500 | [diff] [blame] | 1311 | dev_warn(dev, "%s: Governor %s not started(%d)\n", |
| 1312 | __func__, df->governor->name, ret); |
Saravana Kannan | bc658be | 2019-03-11 15:36:30 +0530 | [diff] [blame] | 1313 | df->governor = prev_governor; |
| 1314 | strncpy(df->governor_name, prev_governor->name, |
| 1315 | DEVFREQ_NAME_LEN); |
| 1316 | ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL); |
| 1317 | if (ret) { |
| 1318 | dev_err(dev, |
| 1319 | "%s: reverting to Governor %s failed (%d)\n", |
| 1320 | __func__, df->governor_name, ret); |
| 1321 | df->governor = NULL; |
| 1322 | } |
| 1323 | } |
Nishanth Menon | 0359d1a | 2012-10-29 15:01:47 -0500 | [diff] [blame] | 1324 | out: |
| 1325 | mutex_unlock(&devfreq_list_lock); |
| 1326 | |
| 1327 | if (!ret) |
| 1328 | ret = count; |
| 1329 | return ret; |
| 1330 | } |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1331 | static DEVICE_ATTR_RW(governor); |
| 1332 | |
| 1333 | static ssize_t available_governors_show(struct device *d, |
| 1334 | struct device_attribute *attr, |
| 1335 | char *buf) |
Nishanth Menon | 50a5b33 | 2012-10-29 15:01:48 -0500 | [diff] [blame] | 1336 | { |
Chanwoo Choi | bcf23c7 | 2017-01-31 15:38:16 +0900 | [diff] [blame] | 1337 | struct devfreq *df = to_devfreq(d); |
Nishanth Menon | 50a5b33 | 2012-10-29 15:01:48 -0500 | [diff] [blame] | 1338 | ssize_t count = 0; |
| 1339 | |
| 1340 | mutex_lock(&devfreq_list_lock); |
Chanwoo Choi | bcf23c7 | 2017-01-31 15:38:16 +0900 | [diff] [blame] | 1341 | |
| 1342 | /* |
| 1343 | * The devfreq with immutable governor (e.g., passive) shows |
| 1344 | * only own governor. |
| 1345 | */ |
Leonard Crestez | d68adc8 | 2019-09-24 10:26:53 +0300 | [diff] [blame] | 1346 | if (df->governor && df->governor->immutable) { |
Chanwoo Choi | bcf23c7 | 2017-01-31 15:38:16 +0900 | [diff] [blame] | 1347 | count = scnprintf(&buf[count], DEVFREQ_NAME_LEN, |
MyungJoo Ham | 6d690f7 | 2019-01-21 11:11:07 +0900 | [diff] [blame] | 1348 | "%s ", df->governor_name); |
Chanwoo Choi | bcf23c7 | 2017-01-31 15:38:16 +0900 | [diff] [blame] | 1349 | /* |
| 1350 | * The devfreq device shows the registered governor except for |
| 1351 | * immutable governors such as passive governor . |
| 1352 | */ |
| 1353 | } else { |
| 1354 | struct devfreq_governor *governor; |
| 1355 | |
| 1356 | list_for_each_entry(governor, &devfreq_governor_list, node) { |
| 1357 | if (governor->immutable) |
| 1358 | continue; |
| 1359 | count += scnprintf(&buf[count], (PAGE_SIZE - count - 2), |
| 1360 | "%s ", governor->name); |
| 1361 | } |
| 1362 | } |
| 1363 | |
Nishanth Menon | 50a5b33 | 2012-10-29 15:01:48 -0500 | [diff] [blame] | 1364 | mutex_unlock(&devfreq_list_lock); |
| 1365 | |
| 1366 | /* Truncate the trailing space */ |
| 1367 | if (count) |
| 1368 | count--; |
| 1369 | |
| 1370 | count += sprintf(&buf[count], "\n"); |
| 1371 | |
| 1372 | return count; |
| 1373 | } |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1374 | static DEVICE_ATTR_RO(available_governors); |
Nishanth Menon | 0359d1a | 2012-10-29 15:01:47 -0500 | [diff] [blame] | 1375 | |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1376 | static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr, |
| 1377 | char *buf) |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1378 | { |
Rajagopal Venkat | 7f98a90 | 2012-10-26 01:50:26 +0200 | [diff] [blame] | 1379 | unsigned long freq; |
| 1380 | struct devfreq *devfreq = to_devfreq(dev); |
| 1381 | |
| 1382 | if (devfreq->profile->get_cur_freq && |
| 1383 | !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq)) |
Chanwoo Choi | 9d0109b | 2016-11-19 22:47:36 +0900 | [diff] [blame] | 1384 | return sprintf(buf, "%lu\n", freq); |
Rajagopal Venkat | 7f98a90 | 2012-10-26 01:50:26 +0200 | [diff] [blame] | 1385 | |
| 1386 | return sprintf(buf, "%lu\n", devfreq->previous_freq); |
| 1387 | } |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1388 | static DEVICE_ATTR_RO(cur_freq); |
Rajagopal Venkat | 7f98a90 | 2012-10-26 01:50:26 +0200 | [diff] [blame] | 1389 | |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1390 | static ssize_t target_freq_show(struct device *dev, |
| 1391 | struct device_attribute *attr, char *buf) |
Rajagopal Venkat | 7f98a90 | 2012-10-26 01:50:26 +0200 | [diff] [blame] | 1392 | { |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1393 | return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq); |
| 1394 | } |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1395 | static DEVICE_ATTR_RO(target_freq); |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1396 | |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1397 | static ssize_t polling_interval_show(struct device *dev, |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1398 | struct device_attribute *attr, char *buf) |
| 1399 | { |
| 1400 | return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms); |
| 1401 | } |
| 1402 | |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1403 | static ssize_t polling_interval_store(struct device *dev, |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1404 | struct device_attribute *attr, |
| 1405 | const char *buf, size_t count) |
| 1406 | { |
| 1407 | struct devfreq *df = to_devfreq(dev); |
| 1408 | unsigned int value; |
| 1409 | int ret; |
| 1410 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 1411 | if (!df->governor) |
| 1412 | return -EINVAL; |
| 1413 | |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1414 | ret = sscanf(buf, "%u", &value); |
| 1415 | if (ret != 1) |
Nishanth Menon | 12e2626 | 2012-10-26 01:50:43 +0200 | [diff] [blame] | 1416 | return -EINVAL; |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1417 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 1418 | df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value); |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1419 | ret = count; |
| 1420 | |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1421 | return ret; |
| 1422 | } |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1423 | static DEVICE_ATTR_RW(polling_interval); |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1424 | |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1425 | static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr, |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 1426 | const char *buf, size_t count) |
| 1427 | { |
| 1428 | struct devfreq *df = to_devfreq(dev); |
| 1429 | unsigned long value; |
| 1430 | int ret; |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 1431 | |
Leonard Crestez | 27dbc54 | 2019-12-05 12:05:07 +0200 | [diff] [blame^] | 1432 | /* |
| 1433 | * Protect against theoretical sysfs writes between |
| 1434 | * device_add and dev_pm_qos_add_request |
| 1435 | */ |
| 1436 | if (!dev_pm_qos_request_active(&df->user_min_freq_req)) |
| 1437 | return -EAGAIN; |
| 1438 | |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 1439 | ret = sscanf(buf, "%lu", &value); |
| 1440 | if (ret != 1) |
Nishanth Menon | 12e2626 | 2012-10-26 01:50:43 +0200 | [diff] [blame] | 1441 | return -EINVAL; |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 1442 | |
Leonard Crestez | 27dbc54 | 2019-12-05 12:05:07 +0200 | [diff] [blame^] | 1443 | /* Round down to kHz for PM QoS */ |
| 1444 | ret = dev_pm_qos_update_request(&df->user_min_freq_req, |
| 1445 | value / HZ_PER_KHZ); |
| 1446 | if (ret < 0) |
| 1447 | return ret; |
Leonard Crestez | 46cecc0 | 2019-10-31 23:34:26 +0200 | [diff] [blame] | 1448 | |
| 1449 | return count; |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 1450 | } |
| 1451 | |
Chanwoo Choi | 1051e2c | 2017-10-23 10:32:07 +0900 | [diff] [blame] | 1452 | static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr, |
| 1453 | char *buf) |
| 1454 | { |
Chanwoo Choi | f1d981e | 2017-10-23 10:32:08 +0900 | [diff] [blame] | 1455 | struct devfreq *df = to_devfreq(dev); |
Leonard Crestez | 46cecc0 | 2019-10-31 23:34:26 +0200 | [diff] [blame] | 1456 | unsigned long min_freq, max_freq; |
Chanwoo Choi | f1d981e | 2017-10-23 10:32:08 +0900 | [diff] [blame] | 1457 | |
Leonard Crestez | 46cecc0 | 2019-10-31 23:34:26 +0200 | [diff] [blame] | 1458 | mutex_lock(&df->lock); |
| 1459 | get_freq_range(df, &min_freq, &max_freq); |
| 1460 | mutex_unlock(&df->lock); |
| 1461 | |
| 1462 | return sprintf(buf, "%lu\n", min_freq); |
Chanwoo Choi | 1051e2c | 2017-10-23 10:32:07 +0900 | [diff] [blame] | 1463 | } |
| 1464 | |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1465 | static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr, |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 1466 | const char *buf, size_t count) |
| 1467 | { |
| 1468 | struct devfreq *df = to_devfreq(dev); |
| 1469 | unsigned long value; |
| 1470 | int ret; |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 1471 | |
Leonard Crestez | 27dbc54 | 2019-12-05 12:05:07 +0200 | [diff] [blame^] | 1472 | /* |
| 1473 | * Protect against theoretical sysfs writes between |
| 1474 | * device_add and dev_pm_qos_add_request |
| 1475 | */ |
| 1476 | if (!dev_pm_qos_request_active(&df->user_max_freq_req)) |
| 1477 | return -EINVAL; |
| 1478 | |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 1479 | ret = sscanf(buf, "%lu", &value); |
| 1480 | if (ret != 1) |
Nishanth Menon | 12e2626 | 2012-10-26 01:50:43 +0200 | [diff] [blame] | 1481 | return -EINVAL; |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 1482 | |
Leonard Crestez | 27dbc54 | 2019-12-05 12:05:07 +0200 | [diff] [blame^] | 1483 | /* |
| 1484 | * PM QoS frequencies are in kHz so we need to convert. Convert by |
| 1485 | * rounding upwards so that the acceptable interval never shrinks. |
| 1486 | * |
| 1487 | * For example if the user writes "666666666" to sysfs this value will |
| 1488 | * be converted to 666667 kHz and back to 666667000 Hz before an OPP |
| 1489 | * lookup, this ensures that an OPP of 666666666Hz is still accepted. |
| 1490 | * |
| 1491 | * A value of zero means "no limit". |
| 1492 | */ |
| 1493 | if (value) |
| 1494 | value = DIV_ROUND_UP(value, HZ_PER_KHZ); |
| 1495 | else |
| 1496 | value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE; |
Matthias Kaehlcke | df5cf4a | 2018-08-03 13:05:09 -0700 | [diff] [blame] | 1497 | |
Leonard Crestez | 27dbc54 | 2019-12-05 12:05:07 +0200 | [diff] [blame^] | 1498 | ret = dev_pm_qos_update_request(&df->user_max_freq_req, value); |
| 1499 | if (ret < 0) |
| 1500 | return ret; |
Leonard Crestez | 46cecc0 | 2019-10-31 23:34:26 +0200 | [diff] [blame] | 1501 | |
| 1502 | return count; |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 1503 | } |
Chanwoo Choi | 3104fa3 | 2015-11-13 19:25:28 +0900 | [diff] [blame] | 1504 | static DEVICE_ATTR_RW(min_freq); |
Chanwoo Choi | 1051e2c | 2017-10-23 10:32:07 +0900 | [diff] [blame] | 1505 | |
| 1506 | static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr, |
| 1507 | char *buf) |
| 1508 | { |
Chanwoo Choi | f1d981e | 2017-10-23 10:32:08 +0900 | [diff] [blame] | 1509 | struct devfreq *df = to_devfreq(dev); |
Leonard Crestez | 46cecc0 | 2019-10-31 23:34:26 +0200 | [diff] [blame] | 1510 | unsigned long min_freq, max_freq; |
Chanwoo Choi | f1d981e | 2017-10-23 10:32:08 +0900 | [diff] [blame] | 1511 | |
Leonard Crestez | 46cecc0 | 2019-10-31 23:34:26 +0200 | [diff] [blame] | 1512 | mutex_lock(&df->lock); |
| 1513 | get_freq_range(df, &min_freq, &max_freq); |
| 1514 | mutex_unlock(&df->lock); |
| 1515 | |
| 1516 | return sprintf(buf, "%lu\n", max_freq); |
Chanwoo Choi | 1051e2c | 2017-10-23 10:32:07 +0900 | [diff] [blame] | 1517 | } |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1518 | static DEVICE_ATTR_RW(max_freq); |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 1519 | |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1520 | static ssize_t available_frequencies_show(struct device *d, |
| 1521 | struct device_attribute *attr, |
| 1522 | char *buf) |
Nishanth Menon | d287de8 | 2012-10-25 19:48:59 -0500 | [diff] [blame] | 1523 | { |
| 1524 | struct devfreq *df = to_devfreq(d); |
Nishanth Menon | d287de8 | 2012-10-25 19:48:59 -0500 | [diff] [blame] | 1525 | ssize_t count = 0; |
Chanwoo Choi | 416b46a | 2017-10-23 10:32:10 +0900 | [diff] [blame] | 1526 | int i; |
Nishanth Menon | d287de8 | 2012-10-25 19:48:59 -0500 | [diff] [blame] | 1527 | |
Chanwoo Choi | 416b46a | 2017-10-23 10:32:10 +0900 | [diff] [blame] | 1528 | mutex_lock(&df->lock); |
Nishanth Menon | d287de8 | 2012-10-25 19:48:59 -0500 | [diff] [blame] | 1529 | |
Chanwoo Choi | 416b46a | 2017-10-23 10:32:10 +0900 | [diff] [blame] | 1530 | for (i = 0; i < df->profile->max_state; i++) |
Nishanth Menon | d287de8 | 2012-10-25 19:48:59 -0500 | [diff] [blame] | 1531 | count += scnprintf(&buf[count], (PAGE_SIZE - count - 2), |
Chanwoo Choi | 416b46a | 2017-10-23 10:32:10 +0900 | [diff] [blame] | 1532 | "%lu ", df->profile->freq_table[i]); |
Nishanth Menon | d287de8 | 2012-10-25 19:48:59 -0500 | [diff] [blame] | 1533 | |
Chanwoo Choi | 416b46a | 2017-10-23 10:32:10 +0900 | [diff] [blame] | 1534 | mutex_unlock(&df->lock); |
Nishanth Menon | d287de8 | 2012-10-25 19:48:59 -0500 | [diff] [blame] | 1535 | /* Truncate the trailing space */ |
| 1536 | if (count) |
| 1537 | count--; |
| 1538 | |
| 1539 | count += sprintf(&buf[count], "\n"); |
| 1540 | |
| 1541 | return count; |
| 1542 | } |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1543 | static DEVICE_ATTR_RO(available_frequencies); |
Nishanth Menon | d287de8 | 2012-10-25 19:48:59 -0500 | [diff] [blame] | 1544 | |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1545 | static ssize_t trans_stat_show(struct device *dev, |
| 1546 | struct device_attribute *attr, char *buf) |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 1547 | { |
| 1548 | struct devfreq *devfreq = to_devfreq(dev); |
| 1549 | ssize_t len; |
Rajagopal Venkat | 39688ce6 | 2013-01-08 11:20:39 +0530 | [diff] [blame] | 1550 | int i, j; |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 1551 | unsigned int max_state = devfreq->profile->max_state; |
| 1552 | |
MyungJoo Ham | 34bd322 | 2015-11-23 15:45:36 +0900 | [diff] [blame] | 1553 | if (max_state == 0) |
| 1554 | return sprintf(buf, "Not Supported.\n"); |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 1555 | |
Leonard Crestez | 2abb0d5 | 2019-09-24 10:52:23 +0300 | [diff] [blame] | 1556 | mutex_lock(&devfreq->lock); |
| 1557 | if (!devfreq->stop_polling && |
| 1558 | devfreq_update_status(devfreq, devfreq->previous_freq)) { |
| 1559 | mutex_unlock(&devfreq->lock); |
| 1560 | return 0; |
| 1561 | } |
| 1562 | mutex_unlock(&devfreq->lock); |
| 1563 | |
Chanwoo Choi | d7df1e4 | 2015-11-19 16:28:46 +0900 | [diff] [blame] | 1564 | len = sprintf(buf, " From : To\n"); |
| 1565 | len += sprintf(buf + len, " :"); |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 1566 | for (i = 0; i < max_state; i++) |
Chanwoo Choi | d7df1e4 | 2015-11-19 16:28:46 +0900 | [diff] [blame] | 1567 | len += sprintf(buf + len, "%10lu", |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 1568 | devfreq->profile->freq_table[i]); |
| 1569 | |
| 1570 | len += sprintf(buf + len, " time(ms)\n"); |
| 1571 | |
| 1572 | for (i = 0; i < max_state; i++) { |
| 1573 | if (devfreq->profile->freq_table[i] |
| 1574 | == devfreq->previous_freq) { |
| 1575 | len += sprintf(buf + len, "*"); |
| 1576 | } else { |
| 1577 | len += sprintf(buf + len, " "); |
| 1578 | } |
Chanwoo Choi | d7df1e4 | 2015-11-19 16:28:46 +0900 | [diff] [blame] | 1579 | len += sprintf(buf + len, "%10lu:", |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 1580 | devfreq->profile->freq_table[i]); |
| 1581 | for (j = 0; j < max_state; j++) |
Chanwoo Choi | d7df1e4 | 2015-11-19 16:28:46 +0900 | [diff] [blame] | 1582 | len += sprintf(buf + len, "%10u", |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 1583 | devfreq->trans_table[(i * max_state) + j]); |
| 1584 | len += sprintf(buf + len, "%10u\n", |
| 1585 | jiffies_to_msecs(devfreq->time_in_state[i])); |
| 1586 | } |
| 1587 | |
| 1588 | len += sprintf(buf + len, "Total transition : %u\n", |
| 1589 | devfreq->total_trans); |
| 1590 | return len; |
| 1591 | } |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1592 | static DEVICE_ATTR_RO(trans_stat); |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 1593 | |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1594 | static struct attribute *devfreq_attrs[] = { |
| 1595 | &dev_attr_governor.attr, |
| 1596 | &dev_attr_available_governors.attr, |
| 1597 | &dev_attr_cur_freq.attr, |
| 1598 | &dev_attr_available_frequencies.attr, |
| 1599 | &dev_attr_target_freq.attr, |
| 1600 | &dev_attr_polling_interval.attr, |
| 1601 | &dev_attr_min_freq.attr, |
| 1602 | &dev_attr_max_freq.attr, |
| 1603 | &dev_attr_trans_stat.attr, |
| 1604 | NULL, |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1605 | }; |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1606 | ATTRIBUTE_GROUPS(devfreq); |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 1607 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1608 | static int __init devfreq_init(void) |
| 1609 | { |
| 1610 | devfreq_class = class_create(THIS_MODULE, "devfreq"); |
| 1611 | if (IS_ERR(devfreq_class)) { |
| 1612 | pr_err("%s: couldn't create class\n", __FILE__); |
| 1613 | return PTR_ERR(devfreq_class); |
| 1614 | } |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 1615 | |
| 1616 | devfreq_wq = create_freezable_workqueue("devfreq_wq"); |
Dan Carpenter | ea7f454 | 2013-08-15 10:55:10 +0300 | [diff] [blame] | 1617 | if (!devfreq_wq) { |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 1618 | class_destroy(devfreq_class); |
| 1619 | pr_err("%s: couldn't create workqueue\n", __FILE__); |
Dan Carpenter | ea7f454 | 2013-08-15 10:55:10 +0300 | [diff] [blame] | 1620 | return -ENOMEM; |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 1621 | } |
Greg Kroah-Hartman | a93d6b0 | 2013-07-24 15:05:09 -0700 | [diff] [blame] | 1622 | devfreq_class->dev_groups = devfreq_groups; |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 1623 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1624 | return 0; |
| 1625 | } |
| 1626 | subsys_initcall(devfreq_init); |
| 1627 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1628 | /* |
Masahiro Yamada | 4091fb9 | 2017-02-27 14:29:56 -0800 | [diff] [blame] | 1629 | * The following are helper functions for devfreq user device drivers with |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1630 | * OPP framework. |
| 1631 | */ |
| 1632 | |
| 1633 | /** |
| 1634 | * devfreq_recommended_opp() - Helper function to get proper OPP for the |
| 1635 | * freq value given to target callback. |
Nishanth Menon | c5b4a1c1 | 2012-10-26 01:50:35 +0200 | [diff] [blame] | 1636 | * @dev: The devfreq user device. (parent of devfreq) |
| 1637 | * @freq: The frequency given to target function |
| 1638 | * @flags: Flags handed from devfreq framework. |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1639 | * |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 1640 | * The callers are required to call dev_pm_opp_put() for the returned OPP after |
| 1641 | * use. |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1642 | */ |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 1643 | struct dev_pm_opp *devfreq_recommended_opp(struct device *dev, |
| 1644 | unsigned long *freq, |
| 1645 | u32 flags) |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1646 | { |
Nishanth Menon | 47d43ba | 2013-09-19 16:03:51 -0500 | [diff] [blame] | 1647 | struct dev_pm_opp *opp; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1648 | |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 1649 | if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) { |
| 1650 | /* The freq is an upper bound. opp should be lower */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1651 | opp = dev_pm_opp_find_freq_floor(dev, freq); |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 1652 | |
| 1653 | /* If not available, use the closest opp */ |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 1654 | if (opp == ERR_PTR(-ERANGE)) |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1655 | opp = dev_pm_opp_find_freq_ceil(dev, freq); |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 1656 | } else { |
| 1657 | /* The freq is an lower bound. opp should be higher */ |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1658 | opp = dev_pm_opp_find_freq_ceil(dev, freq); |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 1659 | |
| 1660 | /* If not available, use the closest opp */ |
Nishanth Menon | 0779726 | 2012-10-24 22:00:12 +0200 | [diff] [blame] | 1661 | if (opp == ERR_PTR(-ERANGE)) |
Nishanth Menon | 5d4879c | 2013-09-19 16:03:50 -0500 | [diff] [blame] | 1662 | opp = dev_pm_opp_find_freq_floor(dev, freq); |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 1663 | } |
| 1664 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1665 | return opp; |
| 1666 | } |
Ãrjan Eide | bd7e927 | 2014-07-18 15:09:53 +0100 | [diff] [blame] | 1667 | EXPORT_SYMBOL(devfreq_recommended_opp); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1668 | |
| 1669 | /** |
| 1670 | * devfreq_register_opp_notifier() - Helper function to get devfreq notified |
MyungJoo Ham | 6d690f7 | 2019-01-21 11:11:07 +0900 | [diff] [blame] | 1671 | * for any changes in the OPP availability |
| 1672 | * changes |
Nishanth Menon | c5b4a1c1 | 2012-10-26 01:50:35 +0200 | [diff] [blame] | 1673 | * @dev: The devfreq user device. (parent of devfreq) |
| 1674 | * @devfreq: The devfreq object. |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1675 | */ |
| 1676 | int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq) |
| 1677 | { |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1678 | return dev_pm_opp_register_notifier(dev, &devfreq->nb); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1679 | } |
Ãrjan Eide | bd7e927 | 2014-07-18 15:09:53 +0100 | [diff] [blame] | 1680 | EXPORT_SYMBOL(devfreq_register_opp_notifier); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1681 | |
| 1682 | /** |
| 1683 | * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq |
MyungJoo Ham | 6d690f7 | 2019-01-21 11:11:07 +0900 | [diff] [blame] | 1684 | * notified for any changes in the OPP |
| 1685 | * availability changes anymore. |
Nishanth Menon | c5b4a1c1 | 2012-10-26 01:50:35 +0200 | [diff] [blame] | 1686 | * @dev: The devfreq user device. (parent of devfreq) |
| 1687 | * @devfreq: The devfreq object. |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1688 | * |
| 1689 | * At exit() callback of devfreq_dev_profile, this must be included if |
| 1690 | * devfreq_recommended_opp is used. |
| 1691 | */ |
| 1692 | int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq) |
| 1693 | { |
Viresh Kumar | dc2c9ad | 2017-01-02 14:41:03 +0530 | [diff] [blame] | 1694 | return dev_pm_opp_unregister_notifier(dev, &devfreq->nb); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1695 | } |
Ãrjan Eide | bd7e927 | 2014-07-18 15:09:53 +0100 | [diff] [blame] | 1696 | EXPORT_SYMBOL(devfreq_unregister_opp_notifier); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1697 | |
Chanwoo Choi | d5b040d | 2014-05-09 16:43:09 +0900 | [diff] [blame] | 1698 | static void devm_devfreq_opp_release(struct device *dev, void *res) |
| 1699 | { |
| 1700 | devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res); |
| 1701 | } |
| 1702 | |
| 1703 | /** |
MyungJoo Ham | 6d690f7 | 2019-01-21 11:11:07 +0900 | [diff] [blame] | 1704 | * devm_devfreq_register_opp_notifier() - Resource-managed |
| 1705 | * devfreq_register_opp_notifier() |
Chanwoo Choi | d5b040d | 2014-05-09 16:43:09 +0900 | [diff] [blame] | 1706 | * @dev: The devfreq user device. (parent of devfreq) |
| 1707 | * @devfreq: The devfreq object. |
| 1708 | */ |
| 1709 | int devm_devfreq_register_opp_notifier(struct device *dev, |
| 1710 | struct devfreq *devfreq) |
| 1711 | { |
| 1712 | struct devfreq **ptr; |
| 1713 | int ret; |
| 1714 | |
| 1715 | ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL); |
| 1716 | if (!ptr) |
| 1717 | return -ENOMEM; |
| 1718 | |
| 1719 | ret = devfreq_register_opp_notifier(dev, devfreq); |
| 1720 | if (ret) { |
| 1721 | devres_free(ptr); |
| 1722 | return ret; |
| 1723 | } |
| 1724 | |
| 1725 | *ptr = devfreq; |
| 1726 | devres_add(dev, ptr); |
| 1727 | |
| 1728 | return 0; |
| 1729 | } |
| 1730 | EXPORT_SYMBOL(devm_devfreq_register_opp_notifier); |
| 1731 | |
| 1732 | /** |
MyungJoo Ham | 6d690f7 | 2019-01-21 11:11:07 +0900 | [diff] [blame] | 1733 | * devm_devfreq_unregister_opp_notifier() - Resource-managed |
| 1734 | * devfreq_unregister_opp_notifier() |
Chanwoo Choi | d5b040d | 2014-05-09 16:43:09 +0900 | [diff] [blame] | 1735 | * @dev: The devfreq user device. (parent of devfreq) |
| 1736 | * @devfreq: The devfreq object. |
| 1737 | */ |
| 1738 | void devm_devfreq_unregister_opp_notifier(struct device *dev, |
| 1739 | struct devfreq *devfreq) |
| 1740 | { |
| 1741 | WARN_ON(devres_release(dev, devm_devfreq_opp_release, |
| 1742 | devm_devfreq_dev_match, devfreq)); |
| 1743 | } |
| 1744 | EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier); |
| 1745 | |
Chanwoo Choi | 0fe3a66 | 2016-01-26 13:21:26 +0900 | [diff] [blame] | 1746 | /** |
| 1747 | * devfreq_register_notifier() - Register a driver with devfreq |
| 1748 | * @devfreq: The devfreq object. |
| 1749 | * @nb: The notifier block to register. |
| 1750 | * @list: DEVFREQ_TRANSITION_NOTIFIER. |
| 1751 | */ |
| 1752 | int devfreq_register_notifier(struct devfreq *devfreq, |
MyungJoo Ham | 6d690f7 | 2019-01-21 11:11:07 +0900 | [diff] [blame] | 1753 | struct notifier_block *nb, |
| 1754 | unsigned int list) |
Chanwoo Choi | 0fe3a66 | 2016-01-26 13:21:26 +0900 | [diff] [blame] | 1755 | { |
| 1756 | int ret = 0; |
| 1757 | |
| 1758 | if (!devfreq) |
| 1759 | return -EINVAL; |
| 1760 | |
| 1761 | switch (list) { |
| 1762 | case DEVFREQ_TRANSITION_NOTIFIER: |
| 1763 | ret = srcu_notifier_chain_register( |
| 1764 | &devfreq->transition_notifier_list, nb); |
| 1765 | break; |
| 1766 | default: |
| 1767 | ret = -EINVAL; |
| 1768 | } |
| 1769 | |
| 1770 | return ret; |
| 1771 | } |
| 1772 | EXPORT_SYMBOL(devfreq_register_notifier); |
| 1773 | |
| 1774 | /* |
| 1775 | * devfreq_unregister_notifier() - Unregister a driver with devfreq |
| 1776 | * @devfreq: The devfreq object. |
| 1777 | * @nb: The notifier block to be unregistered. |
| 1778 | * @list: DEVFREQ_TRANSITION_NOTIFIER. |
| 1779 | */ |
| 1780 | int devfreq_unregister_notifier(struct devfreq *devfreq, |
| 1781 | struct notifier_block *nb, |
| 1782 | unsigned int list) |
| 1783 | { |
| 1784 | int ret = 0; |
| 1785 | |
| 1786 | if (!devfreq) |
| 1787 | return -EINVAL; |
| 1788 | |
| 1789 | switch (list) { |
| 1790 | case DEVFREQ_TRANSITION_NOTIFIER: |
| 1791 | ret = srcu_notifier_chain_unregister( |
| 1792 | &devfreq->transition_notifier_list, nb); |
| 1793 | break; |
| 1794 | default: |
| 1795 | ret = -EINVAL; |
| 1796 | } |
| 1797 | |
| 1798 | return ret; |
| 1799 | } |
| 1800 | EXPORT_SYMBOL(devfreq_unregister_notifier); |
| 1801 | |
| 1802 | struct devfreq_notifier_devres { |
| 1803 | struct devfreq *devfreq; |
| 1804 | struct notifier_block *nb; |
| 1805 | unsigned int list; |
| 1806 | }; |
| 1807 | |
| 1808 | static void devm_devfreq_notifier_release(struct device *dev, void *res) |
| 1809 | { |
| 1810 | struct devfreq_notifier_devres *this = res; |
| 1811 | |
| 1812 | devfreq_unregister_notifier(this->devfreq, this->nb, this->list); |
| 1813 | } |
| 1814 | |
| 1815 | /** |
| 1816 | * devm_devfreq_register_notifier() |
| 1817 | - Resource-managed devfreq_register_notifier() |
| 1818 | * @dev: The devfreq user device. (parent of devfreq) |
| 1819 | * @devfreq: The devfreq object. |
| 1820 | * @nb: The notifier block to be unregistered. |
| 1821 | * @list: DEVFREQ_TRANSITION_NOTIFIER. |
| 1822 | */ |
| 1823 | int devm_devfreq_register_notifier(struct device *dev, |
| 1824 | struct devfreq *devfreq, |
| 1825 | struct notifier_block *nb, |
| 1826 | unsigned int list) |
| 1827 | { |
| 1828 | struct devfreq_notifier_devres *ptr; |
| 1829 | int ret; |
| 1830 | |
| 1831 | ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr), |
| 1832 | GFP_KERNEL); |
| 1833 | if (!ptr) |
| 1834 | return -ENOMEM; |
| 1835 | |
| 1836 | ret = devfreq_register_notifier(devfreq, nb, list); |
| 1837 | if (ret) { |
| 1838 | devres_free(ptr); |
| 1839 | return ret; |
| 1840 | } |
| 1841 | |
| 1842 | ptr->devfreq = devfreq; |
| 1843 | ptr->nb = nb; |
| 1844 | ptr->list = list; |
| 1845 | devres_add(dev, ptr); |
| 1846 | |
| 1847 | return 0; |
| 1848 | } |
| 1849 | EXPORT_SYMBOL(devm_devfreq_register_notifier); |
| 1850 | |
| 1851 | /** |
| 1852 | * devm_devfreq_unregister_notifier() |
| 1853 | - Resource-managed devfreq_unregister_notifier() |
| 1854 | * @dev: The devfreq user device. (parent of devfreq) |
| 1855 | * @devfreq: The devfreq object. |
| 1856 | * @nb: The notifier block to be unregistered. |
| 1857 | * @list: DEVFREQ_TRANSITION_NOTIFIER. |
| 1858 | */ |
| 1859 | void devm_devfreq_unregister_notifier(struct device *dev, |
MyungJoo Ham | 6d690f7 | 2019-01-21 11:11:07 +0900 | [diff] [blame] | 1860 | struct devfreq *devfreq, |
| 1861 | struct notifier_block *nb, |
| 1862 | unsigned int list) |
Chanwoo Choi | 0fe3a66 | 2016-01-26 13:21:26 +0900 | [diff] [blame] | 1863 | { |
| 1864 | WARN_ON(devres_release(dev, devm_devfreq_notifier_release, |
| 1865 | devm_devfreq_dev_match, devfreq)); |
| 1866 | } |
| 1867 | EXPORT_SYMBOL(devm_devfreq_unregister_notifier); |