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