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