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> |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 21 | #include <linux/opp.h> |
| 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> |
| 28 | #include "governor.h" |
| 29 | |
Nishanth Menon | 1a1357e | 2012-10-26 01:50:53 +0200 | [diff] [blame] | 30 | static struct class *devfreq_class; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 31 | |
| 32 | /* |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 33 | * devfreq core provides delayed work based load monitoring helper |
| 34 | * functions. Governors can use these or can implement their own |
| 35 | * monitoring mechanism. |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 36 | */ |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 37 | static struct workqueue_struct *devfreq_wq; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 38 | |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 39 | /* The list of all device-devfreq governors */ |
| 40 | static LIST_HEAD(devfreq_governor_list); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 41 | /* The list of all device-devfreq */ |
| 42 | static LIST_HEAD(devfreq_list); |
| 43 | static DEFINE_MUTEX(devfreq_list_lock); |
| 44 | |
| 45 | /** |
| 46 | * find_device_devfreq() - find devfreq struct using device pointer |
| 47 | * @dev: device pointer used to lookup device devfreq. |
| 48 | * |
| 49 | * Search the list of device devfreqs and return the matched device's |
| 50 | * devfreq info. devfreq_list_lock should be held by the caller. |
| 51 | */ |
| 52 | static struct devfreq *find_device_devfreq(struct device *dev) |
| 53 | { |
| 54 | struct devfreq *tmp_devfreq; |
| 55 | |
| 56 | if (unlikely(IS_ERR_OR_NULL(dev))) { |
| 57 | pr_err("DEVFREQ: %s: Invalid parameters\n", __func__); |
| 58 | return ERR_PTR(-EINVAL); |
| 59 | } |
| 60 | WARN(!mutex_is_locked(&devfreq_list_lock), |
| 61 | "devfreq_list_lock must be locked."); |
| 62 | |
| 63 | list_for_each_entry(tmp_devfreq, &devfreq_list, node) { |
| 64 | if (tmp_devfreq->dev.parent == dev) |
| 65 | return tmp_devfreq; |
| 66 | } |
| 67 | |
| 68 | return ERR_PTR(-ENODEV); |
| 69 | } |
| 70 | |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 71 | /** |
| 72 | * devfreq_get_freq_level() - Lookup freq_table for the frequency |
| 73 | * @devfreq: the devfreq instance |
| 74 | * @freq: the target frequency |
| 75 | */ |
| 76 | static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq) |
| 77 | { |
| 78 | int lev; |
| 79 | |
| 80 | for (lev = 0; lev < devfreq->profile->max_state; lev++) |
| 81 | if (freq == devfreq->profile->freq_table[lev]) |
| 82 | return lev; |
| 83 | |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * devfreq_update_status() - Update statistics of devfreq behavior |
| 89 | * @devfreq: the devfreq instance |
| 90 | * @freq: the update target frequency |
| 91 | */ |
| 92 | static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq) |
| 93 | { |
| 94 | int lev, prev_lev; |
| 95 | unsigned long cur_time; |
| 96 | |
| 97 | lev = devfreq_get_freq_level(devfreq, freq); |
| 98 | if (lev < 0) |
| 99 | return lev; |
| 100 | |
| 101 | cur_time = jiffies; |
| 102 | devfreq->time_in_state[lev] += |
| 103 | cur_time - devfreq->last_stat_updated; |
| 104 | if (freq != devfreq->previous_freq) { |
| 105 | prev_lev = devfreq_get_freq_level(devfreq, |
| 106 | devfreq->previous_freq); |
| 107 | devfreq->trans_table[(prev_lev * |
| 108 | devfreq->profile->max_state) + lev]++; |
| 109 | devfreq->total_trans++; |
| 110 | } |
| 111 | devfreq->last_stat_updated = cur_time; |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 116 | /** |
| 117 | * find_devfreq_governor() - find devfreq governor from name |
| 118 | * @name: name of the governor |
| 119 | * |
| 120 | * Search the list of devfreq governors and return the matched |
| 121 | * governor's pointer. devfreq_list_lock should be held by the caller. |
| 122 | */ |
| 123 | static struct devfreq_governor *find_devfreq_governor(const char *name) |
| 124 | { |
| 125 | struct devfreq_governor *tmp_governor; |
| 126 | |
| 127 | if (unlikely(IS_ERR_OR_NULL(name))) { |
| 128 | pr_err("DEVFREQ: %s: Invalid parameters\n", __func__); |
| 129 | return ERR_PTR(-EINVAL); |
| 130 | } |
| 131 | WARN(!mutex_is_locked(&devfreq_list_lock), |
| 132 | "devfreq_list_lock must be locked."); |
| 133 | |
| 134 | list_for_each_entry(tmp_governor, &devfreq_governor_list, node) { |
| 135 | if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN)) |
| 136 | return tmp_governor; |
| 137 | } |
| 138 | |
| 139 | return ERR_PTR(-ENODEV); |
| 140 | } |
| 141 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 142 | /* Load monitoring helper functions for governors use */ |
| 143 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 144 | /** |
| 145 | * update_devfreq() - Reevaluate the device and configure frequency. |
| 146 | * @devfreq: the devfreq instance. |
| 147 | * |
| 148 | * Note: Lock devfreq->lock before calling update_devfreq |
| 149 | * This function is exported for governors. |
| 150 | */ |
| 151 | int update_devfreq(struct devfreq *devfreq) |
| 152 | { |
| 153 | unsigned long freq; |
| 154 | int err = 0; |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 155 | u32 flags = 0; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 156 | |
| 157 | if (!mutex_is_locked(&devfreq->lock)) { |
| 158 | WARN(true, "devfreq->lock must be locked by the caller.\n"); |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 162 | if (!devfreq->governor) |
| 163 | return -EINVAL; |
| 164 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 165 | /* Reevaluate the proper frequency */ |
| 166 | err = devfreq->governor->get_target_freq(devfreq, &freq); |
| 167 | if (err) |
| 168 | return err; |
| 169 | |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 170 | /* |
| 171 | * Adjust the freuqency with user freq and QoS. |
| 172 | * |
| 173 | * List from the highest proiority |
| 174 | * max_freq (probably called by thermal when it's too hot) |
| 175 | * min_freq |
| 176 | */ |
| 177 | |
| 178 | if (devfreq->min_freq && freq < devfreq->min_freq) { |
| 179 | freq = devfreq->min_freq; |
| 180 | flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */ |
| 181 | } |
| 182 | if (devfreq->max_freq && freq > devfreq->max_freq) { |
| 183 | freq = devfreq->max_freq; |
| 184 | flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */ |
| 185 | } |
| 186 | |
| 187 | err = devfreq->profile->target(devfreq->dev.parent, &freq, flags); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 188 | if (err) |
| 189 | return err; |
| 190 | |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 191 | if (devfreq->profile->freq_table) |
| 192 | if (devfreq_update_status(devfreq, freq)) |
| 193 | dev_err(&devfreq->dev, |
| 194 | "Couldn't update frequency transition information.\n"); |
| 195 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 196 | devfreq->previous_freq = freq; |
| 197 | return err; |
| 198 | } |
Nishanth Menon | 2df5021 | 2012-10-29 15:01:42 -0500 | [diff] [blame] | 199 | EXPORT_SYMBOL(update_devfreq); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 200 | |
| 201 | /** |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 202 | * devfreq_monitor() - Periodically poll devfreq objects. |
| 203 | * @work: the work struct used to run devfreq_monitor periodically. |
| 204 | * |
| 205 | */ |
| 206 | static void devfreq_monitor(struct work_struct *work) |
| 207 | { |
| 208 | int err; |
| 209 | struct devfreq *devfreq = container_of(work, |
| 210 | struct devfreq, work.work); |
| 211 | |
| 212 | mutex_lock(&devfreq->lock); |
| 213 | err = update_devfreq(devfreq); |
| 214 | if (err) |
| 215 | dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err); |
| 216 | |
| 217 | queue_delayed_work(devfreq_wq, &devfreq->work, |
| 218 | msecs_to_jiffies(devfreq->profile->polling_ms)); |
| 219 | mutex_unlock(&devfreq->lock); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * devfreq_monitor_start() - Start load monitoring of devfreq instance |
| 224 | * @devfreq: the devfreq instance. |
| 225 | * |
| 226 | * Helper function for starting devfreq device load monitoing. By |
| 227 | * default delayed work based monitoring is supported. Function |
| 228 | * to be called from governor in response to DEVFREQ_GOV_START |
| 229 | * event when device is added to devfreq framework. |
| 230 | */ |
| 231 | void devfreq_monitor_start(struct devfreq *devfreq) |
| 232 | { |
| 233 | INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor); |
| 234 | if (devfreq->profile->polling_ms) |
| 235 | queue_delayed_work(devfreq_wq, &devfreq->work, |
| 236 | msecs_to_jiffies(devfreq->profile->polling_ms)); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance |
| 241 | * @devfreq: the devfreq instance. |
| 242 | * |
| 243 | * Helper function to stop devfreq device load monitoing. Function |
| 244 | * to be called from governor in response to DEVFREQ_GOV_STOP |
| 245 | * event when device is removed from devfreq framework. |
| 246 | */ |
| 247 | void devfreq_monitor_stop(struct devfreq *devfreq) |
| 248 | { |
| 249 | cancel_delayed_work_sync(&devfreq->work); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance |
| 254 | * @devfreq: the devfreq instance. |
| 255 | * |
| 256 | * Helper function to suspend devfreq device load monitoing. Function |
| 257 | * to be called from governor in response to DEVFREQ_GOV_SUSPEND |
| 258 | * event or when polling interval is set to zero. |
| 259 | * |
| 260 | * Note: Though this function is same as devfreq_monitor_stop(), |
| 261 | * intentionally kept separate to provide hooks for collecting |
| 262 | * transition statistics. |
| 263 | */ |
| 264 | void devfreq_monitor_suspend(struct devfreq *devfreq) |
| 265 | { |
| 266 | mutex_lock(&devfreq->lock); |
| 267 | if (devfreq->stop_polling) { |
| 268 | mutex_unlock(&devfreq->lock); |
| 269 | return; |
| 270 | } |
| 271 | |
| 272 | devfreq->stop_polling = true; |
| 273 | mutex_unlock(&devfreq->lock); |
| 274 | cancel_delayed_work_sync(&devfreq->work); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance |
| 279 | * @devfreq: the devfreq instance. |
| 280 | * |
| 281 | * Helper function to resume devfreq device load monitoing. Function |
| 282 | * to be called from governor in response to DEVFREQ_GOV_RESUME |
| 283 | * event or when polling interval is set to non-zero. |
| 284 | */ |
| 285 | void devfreq_monitor_resume(struct devfreq *devfreq) |
| 286 | { |
| 287 | mutex_lock(&devfreq->lock); |
| 288 | if (!devfreq->stop_polling) |
| 289 | goto out; |
| 290 | |
| 291 | if (!delayed_work_pending(&devfreq->work) && |
| 292 | devfreq->profile->polling_ms) |
| 293 | queue_delayed_work(devfreq_wq, &devfreq->work, |
| 294 | msecs_to_jiffies(devfreq->profile->polling_ms)); |
| 295 | devfreq->stop_polling = false; |
| 296 | |
| 297 | out: |
| 298 | mutex_unlock(&devfreq->lock); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * devfreq_interval_update() - Update device devfreq monitoring interval |
| 303 | * @devfreq: the devfreq instance. |
| 304 | * @delay: new polling interval to be set. |
| 305 | * |
| 306 | * Helper function to set new load monitoring polling interval. Function |
| 307 | * to be called from governor in response to DEVFREQ_GOV_INTERVAL event. |
| 308 | */ |
| 309 | void devfreq_interval_update(struct devfreq *devfreq, unsigned int *delay) |
| 310 | { |
| 311 | unsigned int cur_delay = devfreq->profile->polling_ms; |
| 312 | unsigned int new_delay = *delay; |
| 313 | |
| 314 | mutex_lock(&devfreq->lock); |
| 315 | devfreq->profile->polling_ms = new_delay; |
| 316 | |
| 317 | if (devfreq->stop_polling) |
| 318 | goto out; |
| 319 | |
| 320 | /* if new delay is zero, stop polling */ |
| 321 | if (!new_delay) { |
| 322 | mutex_unlock(&devfreq->lock); |
| 323 | cancel_delayed_work_sync(&devfreq->work); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | /* if current delay is zero, start polling with new delay */ |
| 328 | if (!cur_delay) { |
| 329 | queue_delayed_work(devfreq_wq, &devfreq->work, |
| 330 | msecs_to_jiffies(devfreq->profile->polling_ms)); |
| 331 | goto out; |
| 332 | } |
| 333 | |
| 334 | /* if current delay is greater than new delay, restart polling */ |
| 335 | if (cur_delay > new_delay) { |
| 336 | mutex_unlock(&devfreq->lock); |
| 337 | cancel_delayed_work_sync(&devfreq->work); |
| 338 | mutex_lock(&devfreq->lock); |
| 339 | if (!devfreq->stop_polling) |
| 340 | queue_delayed_work(devfreq_wq, &devfreq->work, |
| 341 | msecs_to_jiffies(devfreq->profile->polling_ms)); |
| 342 | } |
| 343 | out: |
| 344 | mutex_unlock(&devfreq->lock); |
| 345 | } |
| 346 | |
| 347 | /** |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 348 | * devfreq_notifier_call() - Notify that the device frequency requirements |
| 349 | * has been changed out of devfreq framework. |
Nishanth Menon | c5b4a1c1 | 2012-10-26 01:50:35 +0200 | [diff] [blame] | 350 | * @nb: the notifier_block (supposed to be devfreq->nb) |
| 351 | * @type: not used |
| 352 | * @devp: not used |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 353 | * |
| 354 | * Called by a notifier that uses devfreq->nb. |
| 355 | */ |
| 356 | static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type, |
| 357 | void *devp) |
| 358 | { |
| 359 | struct devfreq *devfreq = container_of(nb, struct devfreq, nb); |
| 360 | int ret; |
| 361 | |
| 362 | mutex_lock(&devfreq->lock); |
| 363 | ret = update_devfreq(devfreq); |
| 364 | mutex_unlock(&devfreq->lock); |
| 365 | |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | /** |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 370 | * _remove_devfreq() - Remove devfreq from the list and release its resources. |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 371 | * @devfreq: the devfreq struct |
| 372 | * @skip: skip calling device_unregister(). |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 373 | */ |
| 374 | static void _remove_devfreq(struct devfreq *devfreq, bool skip) |
| 375 | { |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 376 | mutex_lock(&devfreq_list_lock); |
| 377 | if (IS_ERR(find_device_devfreq(devfreq->dev.parent))) { |
| 378 | mutex_unlock(&devfreq_list_lock); |
| 379 | dev_warn(&devfreq->dev, "releasing devfreq which doesn't exist\n"); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 380 | return; |
| 381 | } |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 382 | list_del(&devfreq->node); |
| 383 | mutex_unlock(&devfreq_list_lock); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 384 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 385 | if (devfreq->governor) |
| 386 | devfreq->governor->event_handler(devfreq, |
| 387 | DEVFREQ_GOV_STOP, NULL); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 388 | |
| 389 | if (devfreq->profile->exit) |
| 390 | devfreq->profile->exit(devfreq->dev.parent); |
| 391 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 392 | if (!skip && get_device(&devfreq->dev)) { |
| 393 | device_unregister(&devfreq->dev); |
| 394 | put_device(&devfreq->dev); |
| 395 | } |
| 396 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 397 | mutex_destroy(&devfreq->lock); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 398 | kfree(devfreq); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * devfreq_dev_release() - Callback for struct device to release the device. |
| 403 | * @dev: the devfreq device |
| 404 | * |
| 405 | * This calls _remove_devfreq() if _remove_devfreq() is not called. |
| 406 | * Note that devfreq_dev_release() could be called by _remove_devfreq() as |
| 407 | * well as by others unregistering the device. |
| 408 | */ |
| 409 | static void devfreq_dev_release(struct device *dev) |
| 410 | { |
| 411 | struct devfreq *devfreq = to_devfreq(dev); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 412 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 413 | _remove_devfreq(devfreq, true); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /** |
| 417 | * devfreq_add_device() - Add devfreq feature to the device |
| 418 | * @dev: the device to add devfreq feature. |
| 419 | * @profile: device-specific profile to run devfreq. |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 420 | * @governor_name: name of the policy to choose frequency. |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 421 | * @data: private data for the governor. The devfreq framework does not |
| 422 | * touch this value. |
| 423 | */ |
| 424 | struct devfreq *devfreq_add_device(struct device *dev, |
| 425 | struct devfreq_dev_profile *profile, |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 426 | const char *governor_name, |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 427 | void *data) |
| 428 | { |
| 429 | struct devfreq *devfreq; |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 430 | struct devfreq_governor *governor; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 431 | int err = 0; |
| 432 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 433 | if (!dev || !profile || !governor_name) { |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 434 | dev_err(dev, "%s: Invalid parameters.\n", __func__); |
| 435 | return ERR_PTR(-EINVAL); |
| 436 | } |
| 437 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 438 | mutex_lock(&devfreq_list_lock); |
| 439 | devfreq = find_device_devfreq(dev); |
| 440 | mutex_unlock(&devfreq_list_lock); |
| 441 | if (!IS_ERR(devfreq)) { |
| 442 | dev_err(dev, "%s: Unable to create devfreq for the device. It already has one.\n", __func__); |
| 443 | err = -EINVAL; |
| 444 | goto err_out; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL); |
| 448 | if (!devfreq) { |
| 449 | dev_err(dev, "%s: Unable to create devfreq for the device\n", |
| 450 | __func__); |
| 451 | err = -ENOMEM; |
Axel Lin | 3f19f08 | 2011-11-15 21:59:09 +0100 | [diff] [blame] | 452 | goto err_out; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | mutex_init(&devfreq->lock); |
| 456 | mutex_lock(&devfreq->lock); |
| 457 | devfreq->dev.parent = dev; |
| 458 | devfreq->dev.class = devfreq_class; |
| 459 | devfreq->dev.release = devfreq_dev_release; |
| 460 | devfreq->profile = profile; |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 461 | strncpy(devfreq->governor_name, governor_name, DEVFREQ_NAME_LEN); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 462 | devfreq->previous_freq = profile->initial_freq; |
| 463 | devfreq->data = data; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 464 | devfreq->nb.notifier_call = devfreq_notifier_call; |
| 465 | |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 466 | devfreq->trans_table = devm_kzalloc(dev, sizeof(unsigned int) * |
| 467 | devfreq->profile->max_state * |
| 468 | devfreq->profile->max_state, |
| 469 | GFP_KERNEL); |
| 470 | devfreq->time_in_state = devm_kzalloc(dev, sizeof(unsigned int) * |
| 471 | devfreq->profile->max_state, |
| 472 | GFP_KERNEL); |
| 473 | devfreq->last_stat_updated = jiffies; |
| 474 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 475 | dev_set_name(&devfreq->dev, dev_name(dev)); |
| 476 | err = device_register(&devfreq->dev); |
| 477 | if (err) { |
| 478 | put_device(&devfreq->dev); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 479 | mutex_unlock(&devfreq->lock); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 480 | goto err_dev; |
| 481 | } |
| 482 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 483 | mutex_unlock(&devfreq->lock); |
| 484 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 485 | mutex_lock(&devfreq_list_lock); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 486 | list_add(&devfreq->node, &devfreq_list); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 487 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 488 | governor = find_devfreq_governor(devfreq->governor_name); |
| 489 | if (!IS_ERR(governor)) |
| 490 | devfreq->governor = governor; |
| 491 | if (devfreq->governor) |
| 492 | err = devfreq->governor->event_handler(devfreq, |
| 493 | DEVFREQ_GOV_START, NULL); |
| 494 | mutex_unlock(&devfreq_list_lock); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 495 | if (err) { |
| 496 | dev_err(dev, "%s: Unable to start governor for the device\n", |
| 497 | __func__); |
| 498 | goto err_init; |
| 499 | } |
| 500 | |
Axel Lin | 3f19f08 | 2011-11-15 21:59:09 +0100 | [diff] [blame] | 501 | return devfreq; |
| 502 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 503 | err_init: |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 504 | list_del(&devfreq->node); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 505 | device_unregister(&devfreq->dev); |
| 506 | err_dev: |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 507 | kfree(devfreq); |
Axel Lin | 3f19f08 | 2011-11-15 21:59:09 +0100 | [diff] [blame] | 508 | err_out: |
| 509 | return ERR_PTR(err); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 510 | } |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 511 | EXPORT_SYMBOL(devfreq_add_device); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 512 | |
| 513 | /** |
| 514 | * devfreq_remove_device() - Remove devfreq feature from a device. |
Nishanth Menon | c5b4a1c1 | 2012-10-26 01:50:35 +0200 | [diff] [blame] | 515 | * @devfreq: the devfreq instance to be removed |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 516 | */ |
| 517 | int devfreq_remove_device(struct devfreq *devfreq) |
| 518 | { |
| 519 | if (!devfreq) |
| 520 | return -EINVAL; |
| 521 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 522 | _remove_devfreq(devfreq, false); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 523 | |
| 524 | return 0; |
| 525 | } |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 526 | EXPORT_SYMBOL(devfreq_remove_device); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 527 | |
Rajagopal Venkat | 206c30c | 2012-10-26 01:50:18 +0200 | [diff] [blame] | 528 | /** |
| 529 | * devfreq_suspend_device() - Suspend devfreq of a device. |
| 530 | * @devfreq: the devfreq instance to be suspended |
| 531 | */ |
| 532 | int devfreq_suspend_device(struct devfreq *devfreq) |
| 533 | { |
| 534 | if (!devfreq) |
| 535 | return -EINVAL; |
| 536 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 537 | if (!devfreq->governor) |
| 538 | return 0; |
| 539 | |
Rajagopal Venkat | 206c30c | 2012-10-26 01:50:18 +0200 | [diff] [blame] | 540 | return devfreq->governor->event_handler(devfreq, |
| 541 | DEVFREQ_GOV_SUSPEND, NULL); |
| 542 | } |
| 543 | EXPORT_SYMBOL(devfreq_suspend_device); |
| 544 | |
| 545 | /** |
| 546 | * devfreq_resume_device() - Resume devfreq of a device. |
| 547 | * @devfreq: the devfreq instance to be resumed |
| 548 | */ |
| 549 | int devfreq_resume_device(struct devfreq *devfreq) |
| 550 | { |
| 551 | if (!devfreq) |
| 552 | return -EINVAL; |
| 553 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 554 | if (!devfreq->governor) |
| 555 | return 0; |
| 556 | |
Rajagopal Venkat | 206c30c | 2012-10-26 01:50:18 +0200 | [diff] [blame] | 557 | return devfreq->governor->event_handler(devfreq, |
| 558 | DEVFREQ_GOV_RESUME, NULL); |
| 559 | } |
| 560 | EXPORT_SYMBOL(devfreq_resume_device); |
| 561 | |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 562 | /** |
| 563 | * devfreq_add_governor() - Add devfreq governor |
| 564 | * @governor: the devfreq governor to be added |
| 565 | */ |
| 566 | int devfreq_add_governor(struct devfreq_governor *governor) |
| 567 | { |
| 568 | struct devfreq_governor *g; |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 569 | struct devfreq *devfreq; |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 570 | int err = 0; |
| 571 | |
| 572 | if (!governor) { |
| 573 | pr_err("%s: Invalid parameters.\n", __func__); |
| 574 | return -EINVAL; |
| 575 | } |
| 576 | |
| 577 | mutex_lock(&devfreq_list_lock); |
| 578 | g = find_devfreq_governor(governor->name); |
| 579 | if (!IS_ERR(g)) { |
| 580 | pr_err("%s: governor %s already registered\n", __func__, |
| 581 | g->name); |
| 582 | err = -EINVAL; |
| 583 | goto err_out; |
| 584 | } |
| 585 | |
| 586 | list_add(&governor->node, &devfreq_governor_list); |
| 587 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 588 | list_for_each_entry(devfreq, &devfreq_list, node) { |
| 589 | int ret = 0; |
| 590 | struct device *dev = devfreq->dev.parent; |
| 591 | |
| 592 | if (!strncmp(devfreq->governor_name, governor->name, |
| 593 | DEVFREQ_NAME_LEN)) { |
| 594 | /* The following should never occur */ |
| 595 | if (devfreq->governor) { |
| 596 | dev_warn(dev, |
| 597 | "%s: Governor %s already present\n", |
| 598 | __func__, devfreq->governor->name); |
| 599 | ret = devfreq->governor->event_handler(devfreq, |
| 600 | DEVFREQ_GOV_STOP, NULL); |
| 601 | if (ret) { |
| 602 | dev_warn(dev, |
| 603 | "%s: Governor %s stop = %d\n", |
| 604 | __func__, |
| 605 | devfreq->governor->name, ret); |
| 606 | } |
| 607 | /* Fall through */ |
| 608 | } |
| 609 | devfreq->governor = governor; |
| 610 | ret = devfreq->governor->event_handler(devfreq, |
| 611 | DEVFREQ_GOV_START, NULL); |
| 612 | if (ret) { |
| 613 | dev_warn(dev, "%s: Governor %s start=%d\n", |
| 614 | __func__, devfreq->governor->name, |
| 615 | ret); |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 620 | err_out: |
| 621 | mutex_unlock(&devfreq_list_lock); |
| 622 | |
| 623 | return err; |
| 624 | } |
| 625 | EXPORT_SYMBOL(devfreq_add_governor); |
| 626 | |
| 627 | /** |
| 628 | * devfreq_remove_device() - Remove devfreq feature from a device. |
| 629 | * @governor: the devfreq governor to be removed |
| 630 | */ |
| 631 | int devfreq_remove_governor(struct devfreq_governor *governor) |
| 632 | { |
| 633 | struct devfreq_governor *g; |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 634 | struct devfreq *devfreq; |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 635 | int err = 0; |
| 636 | |
| 637 | if (!governor) { |
| 638 | pr_err("%s: Invalid parameters.\n", __func__); |
| 639 | return -EINVAL; |
| 640 | } |
| 641 | |
| 642 | mutex_lock(&devfreq_list_lock); |
| 643 | g = find_devfreq_governor(governor->name); |
| 644 | if (IS_ERR(g)) { |
| 645 | pr_err("%s: governor %s not registered\n", __func__, |
| 646 | g->name); |
| 647 | err = -EINVAL; |
| 648 | goto err_out; |
| 649 | } |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 650 | list_for_each_entry(devfreq, &devfreq_list, node) { |
| 651 | int ret; |
| 652 | struct device *dev = devfreq->dev.parent; |
| 653 | |
| 654 | if (!strncmp(devfreq->governor_name, governor->name, |
| 655 | DEVFREQ_NAME_LEN)) { |
| 656 | /* we should have a devfreq governor! */ |
| 657 | if (!devfreq->governor) { |
| 658 | dev_warn(dev, "%s: Governor %s NOT present\n", |
| 659 | __func__, governor->name); |
| 660 | continue; |
| 661 | /* Fall through */ |
| 662 | } |
| 663 | ret = devfreq->governor->event_handler(devfreq, |
| 664 | DEVFREQ_GOV_STOP, NULL); |
| 665 | if (ret) { |
| 666 | dev_warn(dev, "%s: Governor %s stop=%d\n", |
| 667 | __func__, devfreq->governor->name, |
| 668 | ret); |
| 669 | } |
| 670 | devfreq->governor = NULL; |
| 671 | } |
| 672 | } |
Nishanth Menon | 3aa173b | 2012-10-29 15:01:43 -0500 | [diff] [blame] | 673 | |
| 674 | list_del(&governor->node); |
| 675 | err_out: |
| 676 | mutex_unlock(&devfreq_list_lock); |
| 677 | |
| 678 | return err; |
| 679 | } |
| 680 | EXPORT_SYMBOL(devfreq_remove_governor); |
| 681 | |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 682 | static ssize_t show_governor(struct device *dev, |
| 683 | struct device_attribute *attr, char *buf) |
| 684 | { |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 685 | if (!to_devfreq(dev)->governor) |
| 686 | return -EINVAL; |
| 687 | |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 688 | return sprintf(buf, "%s\n", to_devfreq(dev)->governor->name); |
| 689 | } |
| 690 | |
Nishanth Menon | 0359d1a | 2012-10-29 15:01:47 -0500 | [diff] [blame] | 691 | static ssize_t store_governor(struct device *dev, struct device_attribute *attr, |
| 692 | const char *buf, size_t count) |
| 693 | { |
| 694 | struct devfreq *df = to_devfreq(dev); |
| 695 | int ret; |
| 696 | char str_governor[DEVFREQ_NAME_LEN + 1]; |
| 697 | struct devfreq_governor *governor; |
| 698 | |
| 699 | ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor); |
| 700 | if (ret != 1) |
| 701 | return -EINVAL; |
| 702 | |
| 703 | mutex_lock(&devfreq_list_lock); |
| 704 | governor = find_devfreq_governor(str_governor); |
| 705 | if (IS_ERR(governor)) { |
| 706 | ret = PTR_ERR(governor); |
| 707 | goto out; |
| 708 | } |
| 709 | if (df->governor == governor) |
| 710 | goto out; |
| 711 | |
| 712 | if (df->governor) { |
| 713 | ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL); |
| 714 | if (ret) { |
| 715 | dev_warn(dev, "%s: Governor %s not stopped(%d)\n", |
| 716 | __func__, df->governor->name, ret); |
| 717 | goto out; |
| 718 | } |
| 719 | } |
| 720 | df->governor = governor; |
| 721 | strncpy(df->governor_name, governor->name, DEVFREQ_NAME_LEN); |
| 722 | ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL); |
| 723 | if (ret) |
| 724 | dev_warn(dev, "%s: Governor %s not started(%d)\n", |
| 725 | __func__, df->governor->name, ret); |
| 726 | out: |
| 727 | mutex_unlock(&devfreq_list_lock); |
| 728 | |
| 729 | if (!ret) |
| 730 | ret = count; |
| 731 | return ret; |
| 732 | } |
Nishanth Menon | 50a5b33 | 2012-10-29 15:01:48 -0500 | [diff] [blame] | 733 | static ssize_t show_available_governors(struct device *d, |
| 734 | struct device_attribute *attr, |
| 735 | char *buf) |
| 736 | { |
| 737 | struct devfreq_governor *tmp_governor; |
| 738 | ssize_t count = 0; |
| 739 | |
| 740 | mutex_lock(&devfreq_list_lock); |
| 741 | list_for_each_entry(tmp_governor, &devfreq_governor_list, node) |
| 742 | count += scnprintf(&buf[count], (PAGE_SIZE - count - 2), |
| 743 | "%s ", tmp_governor->name); |
| 744 | mutex_unlock(&devfreq_list_lock); |
| 745 | |
| 746 | /* Truncate the trailing space */ |
| 747 | if (count) |
| 748 | count--; |
| 749 | |
| 750 | count += sprintf(&buf[count], "\n"); |
| 751 | |
| 752 | return count; |
| 753 | } |
Nishanth Menon | 0359d1a | 2012-10-29 15:01:47 -0500 | [diff] [blame] | 754 | |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 755 | static ssize_t show_freq(struct device *dev, |
| 756 | struct device_attribute *attr, char *buf) |
| 757 | { |
Rajagopal Venkat | 7f98a90 | 2012-10-26 01:50:26 +0200 | [diff] [blame] | 758 | unsigned long freq; |
| 759 | struct devfreq *devfreq = to_devfreq(dev); |
| 760 | |
| 761 | if (devfreq->profile->get_cur_freq && |
| 762 | !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq)) |
| 763 | return sprintf(buf, "%lu\n", freq); |
| 764 | |
| 765 | return sprintf(buf, "%lu\n", devfreq->previous_freq); |
| 766 | } |
| 767 | |
| 768 | static ssize_t show_target_freq(struct device *dev, |
| 769 | struct device_attribute *attr, char *buf) |
| 770 | { |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 771 | return sprintf(buf, "%lu\n", to_devfreq(dev)->previous_freq); |
| 772 | } |
| 773 | |
| 774 | static ssize_t show_polling_interval(struct device *dev, |
| 775 | struct device_attribute *attr, char *buf) |
| 776 | { |
| 777 | return sprintf(buf, "%d\n", to_devfreq(dev)->profile->polling_ms); |
| 778 | } |
| 779 | |
| 780 | static ssize_t store_polling_interval(struct device *dev, |
| 781 | struct device_attribute *attr, |
| 782 | const char *buf, size_t count) |
| 783 | { |
| 784 | struct devfreq *df = to_devfreq(dev); |
| 785 | unsigned int value; |
| 786 | int ret; |
| 787 | |
Nishanth Menon | 1b5c1be | 2012-10-29 15:01:45 -0500 | [diff] [blame] | 788 | if (!df->governor) |
| 789 | return -EINVAL; |
| 790 | |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 791 | ret = sscanf(buf, "%u", &value); |
| 792 | if (ret != 1) |
Nishanth Menon | 12e2626 | 2012-10-26 01:50:43 +0200 | [diff] [blame] | 793 | return -EINVAL; |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 794 | |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 795 | df->governor->event_handler(df, DEVFREQ_GOV_INTERVAL, &value); |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 796 | ret = count; |
| 797 | |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 798 | return ret; |
| 799 | } |
| 800 | |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 801 | static ssize_t store_min_freq(struct device *dev, struct device_attribute *attr, |
| 802 | const char *buf, size_t count) |
| 803 | { |
| 804 | struct devfreq *df = to_devfreq(dev); |
| 805 | unsigned long value; |
| 806 | int ret; |
| 807 | unsigned long max; |
| 808 | |
| 809 | ret = sscanf(buf, "%lu", &value); |
| 810 | if (ret != 1) |
Nishanth Menon | 12e2626 | 2012-10-26 01:50:43 +0200 | [diff] [blame] | 811 | return -EINVAL; |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 812 | |
| 813 | mutex_lock(&df->lock); |
| 814 | max = df->max_freq; |
| 815 | if (value && max && value > max) { |
| 816 | ret = -EINVAL; |
| 817 | goto unlock; |
| 818 | } |
| 819 | |
| 820 | df->min_freq = value; |
| 821 | update_devfreq(df); |
| 822 | ret = count; |
| 823 | unlock: |
| 824 | mutex_unlock(&df->lock); |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 825 | return ret; |
| 826 | } |
| 827 | |
| 828 | static ssize_t show_min_freq(struct device *dev, struct device_attribute *attr, |
| 829 | char *buf) |
| 830 | { |
| 831 | return sprintf(buf, "%lu\n", to_devfreq(dev)->min_freq); |
| 832 | } |
| 833 | |
| 834 | static ssize_t store_max_freq(struct device *dev, struct device_attribute *attr, |
| 835 | const char *buf, size_t count) |
| 836 | { |
| 837 | struct devfreq *df = to_devfreq(dev); |
| 838 | unsigned long value; |
| 839 | int ret; |
| 840 | unsigned long min; |
| 841 | |
| 842 | ret = sscanf(buf, "%lu", &value); |
| 843 | if (ret != 1) |
Nishanth Menon | 12e2626 | 2012-10-26 01:50:43 +0200 | [diff] [blame] | 844 | return -EINVAL; |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 845 | |
| 846 | mutex_lock(&df->lock); |
| 847 | min = df->min_freq; |
| 848 | if (value && min && value < min) { |
| 849 | ret = -EINVAL; |
| 850 | goto unlock; |
| 851 | } |
| 852 | |
| 853 | df->max_freq = value; |
| 854 | update_devfreq(df); |
| 855 | ret = count; |
| 856 | unlock: |
| 857 | mutex_unlock(&df->lock); |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 858 | return ret; |
| 859 | } |
| 860 | |
| 861 | static ssize_t show_max_freq(struct device *dev, struct device_attribute *attr, |
| 862 | char *buf) |
| 863 | { |
| 864 | return sprintf(buf, "%lu\n", to_devfreq(dev)->max_freq); |
| 865 | } |
| 866 | |
Nishanth Menon | d287de8 | 2012-10-25 19:48:59 -0500 | [diff] [blame] | 867 | static ssize_t show_available_freqs(struct device *d, |
| 868 | struct device_attribute *attr, |
| 869 | char *buf) |
| 870 | { |
| 871 | struct devfreq *df = to_devfreq(d); |
| 872 | struct device *dev = df->dev.parent; |
| 873 | struct opp *opp; |
| 874 | ssize_t count = 0; |
| 875 | unsigned long freq = 0; |
| 876 | |
| 877 | rcu_read_lock(); |
| 878 | do { |
| 879 | opp = opp_find_freq_ceil(dev, &freq); |
| 880 | if (IS_ERR(opp)) |
| 881 | break; |
| 882 | |
| 883 | count += scnprintf(&buf[count], (PAGE_SIZE - count - 2), |
| 884 | "%lu ", freq); |
| 885 | freq++; |
| 886 | } while (1); |
| 887 | rcu_read_unlock(); |
| 888 | |
| 889 | /* Truncate the trailing space */ |
| 890 | if (count) |
| 891 | count--; |
| 892 | |
| 893 | count += sprintf(&buf[count], "\n"); |
| 894 | |
| 895 | return count; |
| 896 | } |
| 897 | |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 898 | static ssize_t show_trans_table(struct device *dev, struct device_attribute *attr, |
| 899 | char *buf) |
| 900 | { |
| 901 | struct devfreq *devfreq = to_devfreq(dev); |
| 902 | ssize_t len; |
| 903 | int i, j, err; |
| 904 | unsigned int max_state = devfreq->profile->max_state; |
| 905 | |
| 906 | err = devfreq_update_status(devfreq, devfreq->previous_freq); |
| 907 | if (err) |
| 908 | return 0; |
| 909 | |
| 910 | len = sprintf(buf, " From : To\n"); |
| 911 | len += sprintf(buf + len, " :"); |
| 912 | for (i = 0; i < max_state; i++) |
| 913 | len += sprintf(buf + len, "%8u", |
| 914 | devfreq->profile->freq_table[i]); |
| 915 | |
| 916 | len += sprintf(buf + len, " time(ms)\n"); |
| 917 | |
| 918 | for (i = 0; i < max_state; i++) { |
| 919 | if (devfreq->profile->freq_table[i] |
| 920 | == devfreq->previous_freq) { |
| 921 | len += sprintf(buf + len, "*"); |
| 922 | } else { |
| 923 | len += sprintf(buf + len, " "); |
| 924 | } |
| 925 | len += sprintf(buf + len, "%8u:", |
| 926 | devfreq->profile->freq_table[i]); |
| 927 | for (j = 0; j < max_state; j++) |
| 928 | len += sprintf(buf + len, "%8u", |
| 929 | devfreq->trans_table[(i * max_state) + j]); |
| 930 | len += sprintf(buf + len, "%10u\n", |
| 931 | jiffies_to_msecs(devfreq->time_in_state[i])); |
| 932 | } |
| 933 | |
| 934 | len += sprintf(buf + len, "Total transition : %u\n", |
| 935 | devfreq->total_trans); |
| 936 | return len; |
| 937 | } |
| 938 | |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 939 | static struct device_attribute devfreq_attrs[] = { |
Nishanth Menon | 0359d1a | 2012-10-29 15:01:47 -0500 | [diff] [blame] | 940 | __ATTR(governor, S_IRUGO | S_IWUSR, show_governor, store_governor), |
Nishanth Menon | 50a5b33 | 2012-10-29 15:01:48 -0500 | [diff] [blame] | 941 | __ATTR(available_governors, S_IRUGO, show_available_governors, NULL), |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 942 | __ATTR(cur_freq, S_IRUGO, show_freq, NULL), |
Nishanth Menon | d287de8 | 2012-10-25 19:48:59 -0500 | [diff] [blame] | 943 | __ATTR(available_frequencies, S_IRUGO, show_available_freqs, NULL), |
Rajagopal Venkat | 7f98a90 | 2012-10-26 01:50:26 +0200 | [diff] [blame] | 944 | __ATTR(target_freq, S_IRUGO, show_target_freq, NULL), |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 945 | __ATTR(polling_interval, S_IRUGO | S_IWUSR, show_polling_interval, |
| 946 | store_polling_interval), |
MyungJoo Ham | 6530b9de | 2011-12-09 16:42:19 +0900 | [diff] [blame] | 947 | __ATTR(min_freq, S_IRUGO | S_IWUSR, show_min_freq, store_min_freq), |
| 948 | __ATTR(max_freq, S_IRUGO | S_IWUSR, show_max_freq, store_max_freq), |
Jonghwa Lee | e552bba | 2012-08-23 20:00:46 +0900 | [diff] [blame] | 949 | __ATTR(trans_stat, S_IRUGO, show_trans_table, NULL), |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 950 | { }, |
| 951 | }; |
| 952 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 953 | static int __init devfreq_init(void) |
| 954 | { |
| 955 | devfreq_class = class_create(THIS_MODULE, "devfreq"); |
| 956 | if (IS_ERR(devfreq_class)) { |
| 957 | pr_err("%s: couldn't create class\n", __FILE__); |
| 958 | return PTR_ERR(devfreq_class); |
| 959 | } |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 960 | |
| 961 | devfreq_wq = create_freezable_workqueue("devfreq_wq"); |
| 962 | if (IS_ERR(devfreq_wq)) { |
| 963 | class_destroy(devfreq_class); |
| 964 | pr_err("%s: couldn't create workqueue\n", __FILE__); |
| 965 | return PTR_ERR(devfreq_wq); |
| 966 | } |
MyungJoo Ham | 9005b65 | 2011-10-02 00:19:28 +0200 | [diff] [blame] | 967 | devfreq_class->dev_attrs = devfreq_attrs; |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 968 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 969 | return 0; |
| 970 | } |
| 971 | subsys_initcall(devfreq_init); |
| 972 | |
| 973 | static void __exit devfreq_exit(void) |
| 974 | { |
| 975 | class_destroy(devfreq_class); |
Rajagopal Venkat | 7e6fdd4 | 2012-10-26 01:50:09 +0200 | [diff] [blame] | 976 | destroy_workqueue(devfreq_wq); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 977 | } |
| 978 | module_exit(devfreq_exit); |
| 979 | |
| 980 | /* |
| 981 | * The followings are helper functions for devfreq user device drivers with |
| 982 | * OPP framework. |
| 983 | */ |
| 984 | |
| 985 | /** |
| 986 | * devfreq_recommended_opp() - Helper function to get proper OPP for the |
| 987 | * freq value given to target callback. |
Nishanth Menon | c5b4a1c1 | 2012-10-26 01:50:35 +0200 | [diff] [blame] | 988 | * @dev: The devfreq user device. (parent of devfreq) |
| 989 | * @freq: The frequency given to target function |
| 990 | * @flags: Flags handed from devfreq framework. |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 991 | * |
| 992 | */ |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 993 | struct opp *devfreq_recommended_opp(struct device *dev, unsigned long *freq, |
| 994 | u32 flags) |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 995 | { |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 996 | struct opp *opp; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 997 | |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 998 | if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) { |
| 999 | /* The freq is an upper bound. opp should be lower */ |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1000 | opp = opp_find_freq_floor(dev, freq); |
MyungJoo Ham | ab5f299 | 2012-03-16 21:54:53 +0100 | [diff] [blame] | 1001 | |
| 1002 | /* If not available, use the closest opp */ |
| 1003 | if (opp == ERR_PTR(-ENODEV)) |
| 1004 | opp = opp_find_freq_ceil(dev, freq); |
| 1005 | } else { |
| 1006 | /* The freq is an lower bound. opp should be higher */ |
| 1007 | opp = opp_find_freq_ceil(dev, freq); |
| 1008 | |
| 1009 | /* If not available, use the closest opp */ |
| 1010 | if (opp == ERR_PTR(-ENODEV)) |
| 1011 | opp = opp_find_freq_floor(dev, freq); |
| 1012 | } |
| 1013 | |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1014 | return opp; |
| 1015 | } |
| 1016 | |
| 1017 | /** |
| 1018 | * devfreq_register_opp_notifier() - Helper function to get devfreq notified |
| 1019 | * for any changes in the OPP availability |
| 1020 | * changes |
Nishanth Menon | c5b4a1c1 | 2012-10-26 01:50:35 +0200 | [diff] [blame] | 1021 | * @dev: The devfreq user device. (parent of devfreq) |
| 1022 | * @devfreq: The devfreq object. |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1023 | */ |
| 1024 | int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq) |
| 1025 | { |
MyungJoo Ham | 389baed | 2012-11-21 19:04:51 +0900 | [diff] [blame^] | 1026 | struct srcu_notifier_head *nh; |
| 1027 | int ret = 0; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1028 | |
MyungJoo Ham | 389baed | 2012-11-21 19:04:51 +0900 | [diff] [blame^] | 1029 | rcu_read_lock(); |
| 1030 | nh = opp_get_notifier(dev); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1031 | if (IS_ERR(nh)) |
MyungJoo Ham | 389baed | 2012-11-21 19:04:51 +0900 | [diff] [blame^] | 1032 | ret = PTR_ERR(nh); |
| 1033 | rcu_read_unlock(); |
| 1034 | if (!ret) |
| 1035 | ret = srcu_notifier_chain_register(nh, &devfreq->nb); |
| 1036 | |
| 1037 | return ret; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq |
| 1042 | * notified for any changes in the OPP |
| 1043 | * availability changes anymore. |
Nishanth Menon | c5b4a1c1 | 2012-10-26 01:50:35 +0200 | [diff] [blame] | 1044 | * @dev: The devfreq user device. (parent of devfreq) |
| 1045 | * @devfreq: The devfreq object. |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1046 | * |
| 1047 | * At exit() callback of devfreq_dev_profile, this must be included if |
| 1048 | * devfreq_recommended_opp is used. |
| 1049 | */ |
| 1050 | int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq) |
| 1051 | { |
MyungJoo Ham | 389baed | 2012-11-21 19:04:51 +0900 | [diff] [blame^] | 1052 | struct srcu_notifier_head *nh; |
| 1053 | int ret = 0; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1054 | |
MyungJoo Ham | 389baed | 2012-11-21 19:04:51 +0900 | [diff] [blame^] | 1055 | rcu_read_lock(); |
| 1056 | nh = opp_get_notifier(dev); |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1057 | if (IS_ERR(nh)) |
MyungJoo Ham | 389baed | 2012-11-21 19:04:51 +0900 | [diff] [blame^] | 1058 | ret = PTR_ERR(nh); |
| 1059 | rcu_read_unlock(); |
| 1060 | if (!ret) |
| 1061 | ret = srcu_notifier_chain_unregister(nh, &devfreq->nb); |
| 1062 | |
| 1063 | return ret; |
MyungJoo Ham | a3c98b8 | 2011-10-02 00:19:15 +0200 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>"); |
| 1067 | MODULE_DESCRIPTION("devfreq class support"); |
| 1068 | MODULE_LICENSE("GPL"); |