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