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