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