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