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