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