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