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