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