1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * devfreq: Generic Dynamic Voltage and Frequency Scaling (DVFS) Framework 4 * for Non-CPU Devices. 5 * 6 * Copyright (C) 2011 Samsung Electronics 7 * MyungJoo Ham <[email protected]> 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/kmod.h> 12 #include <linux/sched.h> 13 #include <linux/debugfs.h> 14 #include <linux/devfreq_cooling.h> 15 #include <linux/errno.h> 16 #include <linux/err.h> 17 #include <linux/init.h> 18 #include <linux/export.h> 19 #include <linux/slab.h> 20 #include <linux/stat.h> 21 #include <linux/pm_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 <linux/of.h> 29 #include <linux/pm_qos.h> 30 #include <linux/units.h> 31 #include "governor.h" 32 33 #define CREATE_TRACE_POINTS 34 #include <trace/events/devfreq.h> 35 36 #define IS_SUPPORTED_FLAG(f, name) ((f & DEVFREQ_GOV_FLAG_##name) ? true : false) 37 #define IS_SUPPORTED_ATTR(f, name) ((f & DEVFREQ_GOV_ATTR_##name) ? true : false) 38 39 static struct class *devfreq_class; 40 static struct dentry *devfreq_debugfs; 41 42 /* 43 * devfreq core provides delayed work based load monitoring helper 44 * functions. Governors can use these or can implement their own 45 * monitoring mechanism. 46 */ 47 static struct workqueue_struct *devfreq_wq; 48 49 /* The list of all device-devfreq governors */ 50 static LIST_HEAD(devfreq_governor_list); 51 /* The list of all device-devfreq */ 52 static LIST_HEAD(devfreq_list); 53 static DEFINE_MUTEX(devfreq_list_lock); 54 55 static const char timer_name[][DEVFREQ_NAME_LEN] = { 56 [DEVFREQ_TIMER_DEFERRABLE] = { "deferrable" }, 57 [DEVFREQ_TIMER_DELAYED] = { "delayed" }, 58 }; 59 60 /** 61 * find_device_devfreq() - find devfreq struct using device pointer 62 * @dev: device pointer used to lookup device devfreq. 63 * 64 * Search the list of device devfreqs and return the matched device's 65 * devfreq info. devfreq_list_lock should be held by the caller. 66 */ 67 static struct devfreq *find_device_devfreq(struct device *dev) 68 { 69 struct devfreq *tmp_devfreq; 70 71 lockdep_assert_held(&devfreq_list_lock); 72 73 if (IS_ERR_OR_NULL(dev)) { 74 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__); 75 return ERR_PTR(-EINVAL); 76 } 77 78 list_for_each_entry(tmp_devfreq, &devfreq_list, node) { 79 if (tmp_devfreq->dev.parent == dev) 80 return tmp_devfreq; 81 } 82 83 return ERR_PTR(-ENODEV); 84 } 85 86 static unsigned long find_available_min_freq(struct devfreq *devfreq) 87 { 88 struct dev_pm_opp *opp; 89 unsigned long min_freq = 0; 90 91 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &min_freq); 92 if (IS_ERR(opp)) 93 min_freq = 0; 94 else 95 dev_pm_opp_put(opp); 96 97 return min_freq; 98 } 99 100 static unsigned long find_available_max_freq(struct devfreq *devfreq) 101 { 102 struct dev_pm_opp *opp; 103 unsigned long max_freq = ULONG_MAX; 104 105 opp = dev_pm_opp_find_freq_floor(devfreq->dev.parent, &max_freq); 106 if (IS_ERR(opp)) 107 max_freq = 0; 108 else 109 dev_pm_opp_put(opp); 110 111 return max_freq; 112 } 113 114 /** 115 * devfreq_get_freq_range() - Get the current freq range 116 * @devfreq: the devfreq instance 117 * @min_freq: the min frequency 118 * @max_freq: the max frequency 119 * 120 * This takes into consideration all constraints. 121 */ 122 void devfreq_get_freq_range(struct devfreq *devfreq, 123 unsigned long *min_freq, 124 unsigned long *max_freq) 125 { 126 unsigned long *freq_table = devfreq->freq_table; 127 s32 qos_min_freq, qos_max_freq; 128 129 lockdep_assert_held(&devfreq->lock); 130 131 /* 132 * Initialize minimum/maximum frequency from freq table. 133 * The devfreq drivers can initialize this in either ascending or 134 * descending order and devfreq core supports both. 135 */ 136 if (freq_table[0] < freq_table[devfreq->max_state - 1]) { 137 *min_freq = freq_table[0]; 138 *max_freq = freq_table[devfreq->max_state - 1]; 139 } else { 140 *min_freq = freq_table[devfreq->max_state - 1]; 141 *max_freq = freq_table[0]; 142 } 143 144 /* Apply constraints from PM QoS */ 145 qos_min_freq = dev_pm_qos_read_value(devfreq->dev.parent, 146 DEV_PM_QOS_MIN_FREQUENCY); 147 qos_max_freq = dev_pm_qos_read_value(devfreq->dev.parent, 148 DEV_PM_QOS_MAX_FREQUENCY); 149 *min_freq = max(*min_freq, (unsigned long)HZ_PER_KHZ * qos_min_freq); 150 if (qos_max_freq != PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE) 151 *max_freq = min(*max_freq, 152 (unsigned long)HZ_PER_KHZ * qos_max_freq); 153 154 /* Apply constraints from OPP interface */ 155 *min_freq = max(*min_freq, devfreq->scaling_min_freq); 156 *max_freq = min(*max_freq, devfreq->scaling_max_freq); 157 158 if (*min_freq > *max_freq) 159 *min_freq = *max_freq; 160 } 161 EXPORT_SYMBOL(devfreq_get_freq_range); 162 163 /** 164 * devfreq_get_freq_level() - Lookup freq_table for the frequency 165 * @devfreq: the devfreq instance 166 * @freq: the target frequency 167 */ 168 static int devfreq_get_freq_level(struct devfreq *devfreq, unsigned long freq) 169 { 170 int lev; 171 172 for (lev = 0; lev < devfreq->max_state; lev++) 173 if (freq == devfreq->freq_table[lev]) 174 return lev; 175 176 return -EINVAL; 177 } 178 179 static int set_freq_table(struct devfreq *devfreq) 180 { 181 struct dev_pm_opp *opp; 182 unsigned long freq; 183 int i, count; 184 185 /* Initialize the freq_table from OPP table */ 186 count = dev_pm_opp_get_opp_count(devfreq->dev.parent); 187 if (count <= 0) 188 return -EINVAL; 189 190 devfreq->max_state = count; 191 devfreq->freq_table = devm_kcalloc(devfreq->dev.parent, 192 devfreq->max_state, 193 sizeof(*devfreq->freq_table), 194 GFP_KERNEL); 195 if (!devfreq->freq_table) 196 return -ENOMEM; 197 198 for (i = 0, freq = 0; i < devfreq->max_state; i++, freq++) { 199 opp = dev_pm_opp_find_freq_ceil(devfreq->dev.parent, &freq); 200 if (IS_ERR(opp)) { 201 devm_kfree(devfreq->dev.parent, devfreq->freq_table); 202 return PTR_ERR(opp); 203 } 204 dev_pm_opp_put(opp); 205 devfreq->freq_table[i] = freq; 206 } 207 208 return 0; 209 } 210 211 /** 212 * devfreq_update_status() - Update statistics of devfreq behavior 213 * @devfreq: the devfreq instance 214 * @freq: the update target frequency 215 */ 216 int devfreq_update_status(struct devfreq *devfreq, unsigned long freq) 217 { 218 int lev, prev_lev, ret = 0; 219 u64 cur_time; 220 221 lockdep_assert_held(&devfreq->lock); 222 cur_time = get_jiffies_64(); 223 224 /* Immediately exit if previous_freq is not initialized yet. */ 225 if (!devfreq->previous_freq) 226 goto out; 227 228 prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq); 229 if (prev_lev < 0) { 230 ret = prev_lev; 231 goto out; 232 } 233 234 devfreq->stats.time_in_state[prev_lev] += 235 cur_time - devfreq->stats.last_update; 236 237 lev = devfreq_get_freq_level(devfreq, freq); 238 if (lev < 0) { 239 ret = lev; 240 goto out; 241 } 242 243 if (lev != prev_lev) { 244 devfreq->stats.trans_table[ 245 (prev_lev * devfreq->max_state) + lev]++; 246 devfreq->stats.total_trans++; 247 } 248 249 out: 250 devfreq->stats.last_update = cur_time; 251 return ret; 252 } 253 EXPORT_SYMBOL(devfreq_update_status); 254 255 /** 256 * find_devfreq_governor() - find devfreq governor from name 257 * @name: name of the governor 258 * 259 * Search the list of devfreq governors and return the matched 260 * governor's pointer. devfreq_list_lock should be held by the caller. 261 */ 262 static struct devfreq_governor *find_devfreq_governor(const char *name) 263 { 264 struct devfreq_governor *tmp_governor; 265 266 lockdep_assert_held(&devfreq_list_lock); 267 268 if (IS_ERR_OR_NULL(name)) { 269 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__); 270 return ERR_PTR(-EINVAL); 271 } 272 273 list_for_each_entry(tmp_governor, &devfreq_governor_list, node) { 274 if (!strncmp(tmp_governor->name, name, DEVFREQ_NAME_LEN)) 275 return tmp_governor; 276 } 277 278 return ERR_PTR(-ENODEV); 279 } 280 281 /** 282 * try_then_request_governor() - Try to find the governor and request the 283 * module if is not found. 284 * @name: name of the governor 285 * 286 * Search the list of devfreq governors and request the module and try again 287 * if is not found. This can happen when both drivers (the governor driver 288 * and the driver that call devfreq_add_device) are built as modules. 289 * devfreq_list_lock should be held by the caller. Returns the matched 290 * governor's pointer or an error pointer. 291 */ 292 static struct devfreq_governor *try_then_request_governor(const char *name) 293 { 294 struct devfreq_governor *governor; 295 int err = 0; 296 297 lockdep_assert_held(&devfreq_list_lock); 298 299 if (IS_ERR_OR_NULL(name)) { 300 pr_err("DEVFREQ: %s: Invalid parameters\n", __func__); 301 return ERR_PTR(-EINVAL); 302 } 303 304 governor = find_devfreq_governor(name); 305 if (IS_ERR(governor)) { 306 mutex_unlock(&devfreq_list_lock); 307 308 if (!strncmp(name, DEVFREQ_GOV_SIMPLE_ONDEMAND, 309 DEVFREQ_NAME_LEN)) 310 err = request_module("governor_%s", "simpleondemand"); 311 else 312 err = request_module("governor_%s", name); 313 /* Restore previous state before return */ 314 mutex_lock(&devfreq_list_lock); 315 if (err) 316 return (err < 0) ? ERR_PTR(err) : ERR_PTR(-EINVAL); 317 318 governor = find_devfreq_governor(name); 319 } 320 321 return governor; 322 } 323 324 static int devfreq_notify_transition(struct devfreq *devfreq, 325 struct devfreq_freqs *freqs, unsigned int state) 326 { 327 if (!devfreq) 328 return -EINVAL; 329 330 switch (state) { 331 case DEVFREQ_PRECHANGE: 332 srcu_notifier_call_chain(&devfreq->transition_notifier_list, 333 DEVFREQ_PRECHANGE, freqs); 334 break; 335 336 case DEVFREQ_POSTCHANGE: 337 srcu_notifier_call_chain(&devfreq->transition_notifier_list, 338 DEVFREQ_POSTCHANGE, freqs); 339 break; 340 default: 341 return -EINVAL; 342 } 343 344 return 0; 345 } 346 347 static int devfreq_set_target(struct devfreq *devfreq, unsigned long new_freq, 348 u32 flags) 349 { 350 struct devfreq_freqs freqs; 351 unsigned long cur_freq; 352 int err = 0; 353 354 if (devfreq->profile->get_cur_freq) 355 devfreq->profile->get_cur_freq(devfreq->dev.parent, &cur_freq); 356 else 357 cur_freq = devfreq->previous_freq; 358 359 freqs.old = cur_freq; 360 freqs.new = new_freq; 361 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_PRECHANGE); 362 363 err = devfreq->profile->target(devfreq->dev.parent, &new_freq, flags); 364 if (err) { 365 freqs.new = cur_freq; 366 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE); 367 return err; 368 } 369 370 /* 371 * Print devfreq_frequency trace information between DEVFREQ_PRECHANGE 372 * and DEVFREQ_POSTCHANGE because for showing the correct frequency 373 * change order of between devfreq device and passive devfreq device. 374 */ 375 if (trace_devfreq_frequency_enabled() && new_freq != cur_freq) 376 trace_devfreq_frequency(devfreq, new_freq, cur_freq); 377 378 freqs.new = new_freq; 379 devfreq_notify_transition(devfreq, &freqs, DEVFREQ_POSTCHANGE); 380 381 if (devfreq_update_status(devfreq, new_freq)) 382 dev_warn(&devfreq->dev, 383 "Couldn't update frequency transition information.\n"); 384 385 devfreq->previous_freq = new_freq; 386 387 if (devfreq->suspend_freq) 388 devfreq->resume_freq = new_freq; 389 390 return err; 391 } 392 393 /** 394 * devfreq_update_target() - Reevaluate the device and configure frequency 395 * on the final stage. 396 * @devfreq: the devfreq instance. 397 * @freq: the new frequency of parent device. This argument 398 * is only used for devfreq device using passive governor. 399 * 400 * Note: Lock devfreq->lock before calling devfreq_update_target. This function 401 * should be only used by both update_devfreq() and devfreq governors. 402 */ 403 int devfreq_update_target(struct devfreq *devfreq, unsigned long freq) 404 { 405 unsigned long min_freq, max_freq; 406 int err = 0; 407 u32 flags = 0; 408 409 lockdep_assert_held(&devfreq->lock); 410 411 if (!devfreq->governor) 412 return -EINVAL; 413 414 /* Reevaluate the proper frequency */ 415 err = devfreq->governor->get_target_freq(devfreq, &freq); 416 if (err) 417 return err; 418 devfreq_get_freq_range(devfreq, &min_freq, &max_freq); 419 420 if (freq < min_freq) { 421 freq = min_freq; 422 flags &= ~DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use GLB */ 423 } 424 if (freq > max_freq) { 425 freq = max_freq; 426 flags |= DEVFREQ_FLAG_LEAST_UPPER_BOUND; /* Use LUB */ 427 } 428 429 return devfreq_set_target(devfreq, freq, flags); 430 } 431 EXPORT_SYMBOL(devfreq_update_target); 432 433 /* Load monitoring helper functions for governors use */ 434 435 /** 436 * update_devfreq() - Reevaluate the device and configure frequency. 437 * @devfreq: the devfreq instance. 438 * 439 * Note: Lock devfreq->lock before calling update_devfreq 440 * This function is exported for governors. 441 */ 442 int update_devfreq(struct devfreq *devfreq) 443 { 444 return devfreq_update_target(devfreq, 0L); 445 } 446 EXPORT_SYMBOL(update_devfreq); 447 448 /** 449 * devfreq_monitor() - Periodically poll devfreq objects. 450 * @work: the work struct used to run devfreq_monitor periodically. 451 * 452 */ 453 static void devfreq_monitor(struct work_struct *work) 454 { 455 int err; 456 struct devfreq *devfreq = container_of(work, 457 struct devfreq, work.work); 458 459 mutex_lock(&devfreq->lock); 460 err = update_devfreq(devfreq); 461 if (err) 462 dev_err(&devfreq->dev, "dvfs failed with (%d) error\n", err); 463 464 queue_delayed_work(devfreq_wq, &devfreq->work, 465 msecs_to_jiffies(devfreq->profile->polling_ms)); 466 mutex_unlock(&devfreq->lock); 467 468 trace_devfreq_monitor(devfreq); 469 } 470 471 /** 472 * devfreq_monitor_start() - Start load monitoring of devfreq instance 473 * @devfreq: the devfreq instance. 474 * 475 * Helper function for starting devfreq device load monitoring. By default, 476 * deferrable timer is used for load monitoring. But the users can change this 477 * behavior using the "timer" type in devfreq_dev_profile. This function will be 478 * called by devfreq governor in response to the DEVFREQ_GOV_START event 479 * generated while adding a device to the devfreq framework. 480 */ 481 void devfreq_monitor_start(struct devfreq *devfreq) 482 { 483 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN)) 484 return; 485 486 switch (devfreq->profile->timer) { 487 case DEVFREQ_TIMER_DEFERRABLE: 488 INIT_DEFERRABLE_WORK(&devfreq->work, devfreq_monitor); 489 break; 490 case DEVFREQ_TIMER_DELAYED: 491 INIT_DELAYED_WORK(&devfreq->work, devfreq_monitor); 492 break; 493 default: 494 return; 495 } 496 497 if (devfreq->profile->polling_ms) 498 queue_delayed_work(devfreq_wq, &devfreq->work, 499 msecs_to_jiffies(devfreq->profile->polling_ms)); 500 } 501 EXPORT_SYMBOL(devfreq_monitor_start); 502 503 /** 504 * devfreq_monitor_stop() - Stop load monitoring of a devfreq instance 505 * @devfreq: the devfreq instance. 506 * 507 * Helper function to stop devfreq device load monitoring. Function 508 * to be called from governor in response to DEVFREQ_GOV_STOP 509 * event when device is removed from devfreq framework. 510 */ 511 void devfreq_monitor_stop(struct devfreq *devfreq) 512 { 513 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN)) 514 return; 515 516 cancel_delayed_work_sync(&devfreq->work); 517 } 518 EXPORT_SYMBOL(devfreq_monitor_stop); 519 520 /** 521 * devfreq_monitor_suspend() - Suspend load monitoring of a devfreq instance 522 * @devfreq: the devfreq instance. 523 * 524 * Helper function to suspend devfreq device load monitoring. Function 525 * to be called from governor in response to DEVFREQ_GOV_SUSPEND 526 * event or when polling interval is set to zero. 527 * 528 * Note: Though this function is same as devfreq_monitor_stop(), 529 * intentionally kept separate to provide hooks for collecting 530 * transition statistics. 531 */ 532 void devfreq_monitor_suspend(struct devfreq *devfreq) 533 { 534 mutex_lock(&devfreq->lock); 535 if (devfreq->stop_polling) { 536 mutex_unlock(&devfreq->lock); 537 return; 538 } 539 540 devfreq_update_status(devfreq, devfreq->previous_freq); 541 devfreq->stop_polling = true; 542 mutex_unlock(&devfreq->lock); 543 544 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN)) 545 return; 546 547 cancel_delayed_work_sync(&devfreq->work); 548 } 549 EXPORT_SYMBOL(devfreq_monitor_suspend); 550 551 /** 552 * devfreq_monitor_resume() - Resume load monitoring of a devfreq instance 553 * @devfreq: the devfreq instance. 554 * 555 * Helper function to resume devfreq device load monitoring. Function 556 * to be called from governor in response to DEVFREQ_GOV_RESUME 557 * event or when polling interval is set to non-zero. 558 */ 559 void devfreq_monitor_resume(struct devfreq *devfreq) 560 { 561 unsigned long freq; 562 563 mutex_lock(&devfreq->lock); 564 565 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN)) 566 goto out_update; 567 568 if (!devfreq->stop_polling) 569 goto out; 570 571 if (!delayed_work_pending(&devfreq->work) && 572 devfreq->profile->polling_ms) 573 queue_delayed_work(devfreq_wq, &devfreq->work, 574 msecs_to_jiffies(devfreq->profile->polling_ms)); 575 576 out_update: 577 devfreq->stats.last_update = get_jiffies_64(); 578 devfreq->stop_polling = false; 579 580 if (devfreq->profile->get_cur_freq && 581 !devfreq->profile->get_cur_freq(devfreq->dev.parent, &freq)) 582 devfreq->previous_freq = freq; 583 584 out: 585 mutex_unlock(&devfreq->lock); 586 } 587 EXPORT_SYMBOL(devfreq_monitor_resume); 588 589 /** 590 * devfreq_update_interval() - Update device devfreq monitoring interval 591 * @devfreq: the devfreq instance. 592 * @delay: new polling interval to be set. 593 * 594 * Helper function to set new load monitoring polling interval. Function 595 * to be called from governor in response to DEVFREQ_GOV_UPDATE_INTERVAL event. 596 */ 597 void devfreq_update_interval(struct devfreq *devfreq, unsigned int *delay) 598 { 599 unsigned int cur_delay = devfreq->profile->polling_ms; 600 unsigned int new_delay = *delay; 601 602 mutex_lock(&devfreq->lock); 603 devfreq->profile->polling_ms = new_delay; 604 605 if (IS_SUPPORTED_FLAG(devfreq->governor->flags, IRQ_DRIVEN)) 606 goto out; 607 608 if (devfreq->stop_polling) 609 goto out; 610 611 /* if new delay is zero, stop polling */ 612 if (!new_delay) { 613 mutex_unlock(&devfreq->lock); 614 cancel_delayed_work_sync(&devfreq->work); 615 return; 616 } 617 618 /* if current delay is zero, start polling with new delay */ 619 if (!cur_delay) { 620 queue_delayed_work(devfreq_wq, &devfreq->work, 621 msecs_to_jiffies(devfreq->profile->polling_ms)); 622 goto out; 623 } 624 625 /* if current delay is greater than new delay, restart polling */ 626 if (cur_delay > new_delay) { 627 mutex_unlock(&devfreq->lock); 628 cancel_delayed_work_sync(&devfreq->work); 629 mutex_lock(&devfreq->lock); 630 if (!devfreq->stop_polling) 631 queue_delayed_work(devfreq_wq, &devfreq->work, 632 msecs_to_jiffies(devfreq->profile->polling_ms)); 633 } 634 out: 635 mutex_unlock(&devfreq->lock); 636 } 637 EXPORT_SYMBOL(devfreq_update_interval); 638 639 /** 640 * devfreq_notifier_call() - Notify that the device frequency requirements 641 * has been changed out of devfreq framework. 642 * @nb: the notifier_block (supposed to be devfreq->nb) 643 * @type: not used 644 * @devp: not used 645 * 646 * Called by a notifier that uses devfreq->nb. 647 */ 648 static int devfreq_notifier_call(struct notifier_block *nb, unsigned long type, 649 void *devp) 650 { 651 struct devfreq *devfreq = container_of(nb, struct devfreq, nb); 652 int err = -EINVAL; 653 654 mutex_lock(&devfreq->lock); 655 656 devfreq->scaling_min_freq = find_available_min_freq(devfreq); 657 if (!devfreq->scaling_min_freq) 658 goto out; 659 660 devfreq->scaling_max_freq = find_available_max_freq(devfreq); 661 if (!devfreq->scaling_max_freq) { 662 devfreq->scaling_max_freq = ULONG_MAX; 663 goto out; 664 } 665 666 err = update_devfreq(devfreq); 667 668 out: 669 mutex_unlock(&devfreq->lock); 670 if (err) 671 dev_err(devfreq->dev.parent, 672 "failed to update frequency from OPP notifier (%d)\n", 673 err); 674 675 return NOTIFY_OK; 676 } 677 678 /** 679 * qos_notifier_call() - Common handler for QoS constraints. 680 * @devfreq: the devfreq instance. 681 */ 682 static int qos_notifier_call(struct devfreq *devfreq) 683 { 684 int err; 685 686 mutex_lock(&devfreq->lock); 687 err = update_devfreq(devfreq); 688 mutex_unlock(&devfreq->lock); 689 if (err) 690 dev_err(devfreq->dev.parent, 691 "failed to update frequency from PM QoS (%d)\n", 692 err); 693 694 return NOTIFY_OK; 695 } 696 697 /** 698 * qos_min_notifier_call() - Callback for QoS min_freq changes. 699 * @nb: Should be devfreq->nb_min 700 * @val: not used 701 * @ptr: not used 702 */ 703 static int qos_min_notifier_call(struct notifier_block *nb, 704 unsigned long val, void *ptr) 705 { 706 return qos_notifier_call(container_of(nb, struct devfreq, nb_min)); 707 } 708 709 /** 710 * qos_max_notifier_call() - Callback for QoS max_freq changes. 711 * @nb: Should be devfreq->nb_max 712 * @val: not used 713 * @ptr: not used 714 */ 715 static int qos_max_notifier_call(struct notifier_block *nb, 716 unsigned long val, void *ptr) 717 { 718 return qos_notifier_call(container_of(nb, struct devfreq, nb_max)); 719 } 720 721 /** 722 * devfreq_dev_release() - Callback for struct device to release the device. 723 * @dev: the devfreq device 724 * 725 * Remove devfreq from the list and release its resources. 726 */ 727 static void devfreq_dev_release(struct device *dev) 728 { 729 struct devfreq *devfreq = to_devfreq(dev); 730 int err; 731 732 mutex_lock(&devfreq_list_lock); 733 list_del(&devfreq->node); 734 mutex_unlock(&devfreq_list_lock); 735 736 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_max, 737 DEV_PM_QOS_MAX_FREQUENCY); 738 if (err && err != -ENOENT) 739 dev_warn(dev->parent, 740 "Failed to remove max_freq notifier: %d\n", err); 741 err = dev_pm_qos_remove_notifier(devfreq->dev.parent, &devfreq->nb_min, 742 DEV_PM_QOS_MIN_FREQUENCY); 743 if (err && err != -ENOENT) 744 dev_warn(dev->parent, 745 "Failed to remove min_freq notifier: %d\n", err); 746 747 if (dev_pm_qos_request_active(&devfreq->user_max_freq_req)) { 748 err = dev_pm_qos_remove_request(&devfreq->user_max_freq_req); 749 if (err < 0) 750 dev_warn(dev->parent, 751 "Failed to remove max_freq request: %d\n", err); 752 } 753 if (dev_pm_qos_request_active(&devfreq->user_min_freq_req)) { 754 err = dev_pm_qos_remove_request(&devfreq->user_min_freq_req); 755 if (err < 0) 756 dev_warn(dev->parent, 757 "Failed to remove min_freq request: %d\n", err); 758 } 759 760 if (devfreq->profile->exit) 761 devfreq->profile->exit(devfreq->dev.parent); 762 763 if (devfreq->opp_table) 764 dev_pm_opp_put_opp_table(devfreq->opp_table); 765 766 mutex_destroy(&devfreq->lock); 767 kfree(devfreq); 768 } 769 770 static void create_sysfs_files(struct devfreq *devfreq, 771 const struct devfreq_governor *gov); 772 static void remove_sysfs_files(struct devfreq *devfreq, 773 const struct devfreq_governor *gov); 774 775 /** 776 * devfreq_add_device() - Add devfreq feature to the device 777 * @dev: the device to add devfreq feature. 778 * @profile: device-specific profile to run devfreq. 779 * @governor_name: name of the policy to choose frequency. 780 * @data: devfreq driver pass to governors, governor should not change it. 781 */ 782 struct devfreq *devfreq_add_device(struct device *dev, 783 struct devfreq_dev_profile *profile, 784 const char *governor_name, 785 void *data) 786 { 787 struct devfreq *devfreq; 788 struct devfreq_governor *governor; 789 unsigned long min_freq, max_freq; 790 int err = 0; 791 792 if (!dev || !profile || !governor_name) { 793 dev_err(dev, "%s: Invalid parameters.\n", __func__); 794 return ERR_PTR(-EINVAL); 795 } 796 797 mutex_lock(&devfreq_list_lock); 798 devfreq = find_device_devfreq(dev); 799 mutex_unlock(&devfreq_list_lock); 800 if (!IS_ERR(devfreq)) { 801 dev_err(dev, "%s: devfreq device already exists!\n", 802 __func__); 803 err = -EINVAL; 804 goto err_out; 805 } 806 807 devfreq = kzalloc(sizeof(struct devfreq), GFP_KERNEL); 808 if (!devfreq) { 809 err = -ENOMEM; 810 goto err_out; 811 } 812 813 mutex_init(&devfreq->lock); 814 mutex_lock(&devfreq->lock); 815 devfreq->dev.parent = dev; 816 devfreq->dev.class = devfreq_class; 817 devfreq->dev.release = devfreq_dev_release; 818 INIT_LIST_HEAD(&devfreq->node); 819 devfreq->profile = profile; 820 devfreq->previous_freq = profile->initial_freq; 821 devfreq->last_status.current_frequency = profile->initial_freq; 822 devfreq->data = data; 823 devfreq->nb.notifier_call = devfreq_notifier_call; 824 825 if (devfreq->profile->timer < 0 826 || devfreq->profile->timer >= DEVFREQ_TIMER_NUM) { 827 mutex_unlock(&devfreq->lock); 828 err = -EINVAL; 829 goto err_dev; 830 } 831 832 if (!devfreq->profile->max_state || !devfreq->profile->freq_table) { 833 mutex_unlock(&devfreq->lock); 834 err = set_freq_table(devfreq); 835 if (err < 0) 836 goto err_dev; 837 mutex_lock(&devfreq->lock); 838 } else { 839 devfreq->freq_table = devfreq->profile->freq_table; 840 devfreq->max_state = devfreq->profile->max_state; 841 } 842 843 devfreq->scaling_min_freq = find_available_min_freq(devfreq); 844 if (!devfreq->scaling_min_freq) { 845 mutex_unlock(&devfreq->lock); 846 err = -EINVAL; 847 goto err_dev; 848 } 849 850 devfreq->scaling_max_freq = find_available_max_freq(devfreq); 851 if (!devfreq->scaling_max_freq) { 852 mutex_unlock(&devfreq->lock); 853 err = -EINVAL; 854 goto err_dev; 855 } 856 857 devfreq_get_freq_range(devfreq, &min_freq, &max_freq); 858 859 devfreq->suspend_freq = dev_pm_opp_get_suspend_opp_freq(dev); 860 devfreq->opp_table = dev_pm_opp_get_opp_table(dev); 861 if (IS_ERR(devfreq->opp_table)) 862 devfreq->opp_table = NULL; 863 864 atomic_set(&devfreq->suspend_count, 0); 865 866 dev_set_name(&devfreq->dev, "%s", dev_name(dev)); 867 err = device_register(&devfreq->dev); 868 if (err) { 869 mutex_unlock(&devfreq->lock); 870 put_device(&devfreq->dev); 871 goto err_out; 872 } 873 874 devfreq->stats.trans_table = devm_kzalloc(&devfreq->dev, 875 array3_size(sizeof(unsigned int), 876 devfreq->max_state, 877 devfreq->max_state), 878 GFP_KERNEL); 879 if (!devfreq->stats.trans_table) { 880 mutex_unlock(&devfreq->lock); 881 err = -ENOMEM; 882 goto err_devfreq; 883 } 884 885 devfreq->stats.time_in_state = devm_kcalloc(&devfreq->dev, 886 devfreq->max_state, 887 sizeof(*devfreq->stats.time_in_state), 888 GFP_KERNEL); 889 if (!devfreq->stats.time_in_state) { 890 mutex_unlock(&devfreq->lock); 891 err = -ENOMEM; 892 goto err_devfreq; 893 } 894 895 devfreq->stats.total_trans = 0; 896 devfreq->stats.last_update = get_jiffies_64(); 897 898 srcu_init_notifier_head(&devfreq->transition_notifier_list); 899 900 mutex_unlock(&devfreq->lock); 901 902 err = dev_pm_qos_add_request(dev, &devfreq->user_min_freq_req, 903 DEV_PM_QOS_MIN_FREQUENCY, 0); 904 if (err < 0) 905 goto err_devfreq; 906 err = dev_pm_qos_add_request(dev, &devfreq->user_max_freq_req, 907 DEV_PM_QOS_MAX_FREQUENCY, 908 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE); 909 if (err < 0) 910 goto err_devfreq; 911 912 devfreq->nb_min.notifier_call = qos_min_notifier_call; 913 err = dev_pm_qos_add_notifier(dev, &devfreq->nb_min, 914 DEV_PM_QOS_MIN_FREQUENCY); 915 if (err) 916 goto err_devfreq; 917 918 devfreq->nb_max.notifier_call = qos_max_notifier_call; 919 err = dev_pm_qos_add_notifier(dev, &devfreq->nb_max, 920 DEV_PM_QOS_MAX_FREQUENCY); 921 if (err) 922 goto err_devfreq; 923 924 mutex_lock(&devfreq_list_lock); 925 926 governor = try_then_request_governor(governor_name); 927 if (IS_ERR(governor)) { 928 dev_err(dev, "%s: Unable to find governor for the device\n", 929 __func__); 930 err = PTR_ERR(governor); 931 goto err_init; 932 } 933 934 devfreq->governor = governor; 935 err = devfreq->governor->event_handler(devfreq, DEVFREQ_GOV_START, 936 NULL); 937 if (err) { 938 dev_err_probe(dev, err, 939 "%s: Unable to start governor for the device\n", 940 __func__); 941 goto err_init; 942 } 943 create_sysfs_files(devfreq, devfreq->governor); 944 945 list_add(&devfreq->node, &devfreq_list); 946 947 mutex_unlock(&devfreq_list_lock); 948 949 if (devfreq->profile->is_cooling_device) { 950 devfreq->cdev = devfreq_cooling_em_register(devfreq, NULL); 951 if (IS_ERR(devfreq->cdev)) 952 devfreq->cdev = NULL; 953 } 954 955 return devfreq; 956 957 err_init: 958 mutex_unlock(&devfreq_list_lock); 959 err_devfreq: 960 devfreq_remove_device(devfreq); 961 devfreq = NULL; 962 err_dev: 963 kfree(devfreq); 964 err_out: 965 return ERR_PTR(err); 966 } 967 EXPORT_SYMBOL(devfreq_add_device); 968 969 /** 970 * devfreq_remove_device() - Remove devfreq feature from a device. 971 * @devfreq: the devfreq instance to be removed 972 * 973 * The opposite of devfreq_add_device(). 974 */ 975 int devfreq_remove_device(struct devfreq *devfreq) 976 { 977 if (!devfreq) 978 return -EINVAL; 979 980 devfreq_cooling_unregister(devfreq->cdev); 981 982 if (devfreq->governor) { 983 devfreq->governor->event_handler(devfreq, 984 DEVFREQ_GOV_STOP, NULL); 985 remove_sysfs_files(devfreq, devfreq->governor); 986 } 987 988 device_unregister(&devfreq->dev); 989 990 return 0; 991 } 992 EXPORT_SYMBOL(devfreq_remove_device); 993 994 static int devm_devfreq_dev_match(struct device *dev, void *res, void *data) 995 { 996 struct devfreq **r = res; 997 998 if (WARN_ON(!r || !*r)) 999 return 0; 1000 1001 return *r == data; 1002 } 1003 1004 static void devm_devfreq_dev_release(struct device *dev, void *res) 1005 { 1006 devfreq_remove_device(*(struct devfreq **)res); 1007 } 1008 1009 /** 1010 * devm_devfreq_add_device() - Resource-managed devfreq_add_device() 1011 * @dev: the device to add devfreq feature. 1012 * @profile: device-specific profile to run devfreq. 1013 * @governor_name: name of the policy to choose frequency. 1014 * @data: devfreq driver pass to governors, governor should not change it. 1015 * 1016 * This function manages automatically the memory of devfreq device using device 1017 * resource management and simplify the free operation for memory of devfreq 1018 * device. 1019 */ 1020 struct devfreq *devm_devfreq_add_device(struct device *dev, 1021 struct devfreq_dev_profile *profile, 1022 const char *governor_name, 1023 void *data) 1024 { 1025 struct devfreq **ptr, *devfreq; 1026 1027 ptr = devres_alloc(devm_devfreq_dev_release, sizeof(*ptr), GFP_KERNEL); 1028 if (!ptr) 1029 return ERR_PTR(-ENOMEM); 1030 1031 devfreq = devfreq_add_device(dev, profile, governor_name, data); 1032 if (IS_ERR(devfreq)) { 1033 devres_free(ptr); 1034 return devfreq; 1035 } 1036 1037 *ptr = devfreq; 1038 devres_add(dev, ptr); 1039 1040 return devfreq; 1041 } 1042 EXPORT_SYMBOL(devm_devfreq_add_device); 1043 1044 #ifdef CONFIG_OF 1045 /* 1046 * devfreq_get_devfreq_by_node - Get the devfreq device from devicetree 1047 * @node - pointer to device_node 1048 * 1049 * return the instance of devfreq device 1050 */ 1051 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node) 1052 { 1053 struct devfreq *devfreq; 1054 1055 if (!node) 1056 return ERR_PTR(-EINVAL); 1057 1058 mutex_lock(&devfreq_list_lock); 1059 list_for_each_entry(devfreq, &devfreq_list, node) { 1060 if (devfreq->dev.parent 1061 && device_match_of_node(devfreq->dev.parent, node)) { 1062 mutex_unlock(&devfreq_list_lock); 1063 return devfreq; 1064 } 1065 } 1066 mutex_unlock(&devfreq_list_lock); 1067 1068 return ERR_PTR(-ENODEV); 1069 } 1070 1071 /* 1072 * devfreq_get_devfreq_by_phandle - Get the devfreq device from devicetree 1073 * @dev - instance to the given device 1074 * @phandle_name - name of property holding a phandle value 1075 * @index - index into list of devfreq 1076 * 1077 * return the instance of devfreq device 1078 */ 1079 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, 1080 const char *phandle_name, int index) 1081 { 1082 struct device_node *node; 1083 struct devfreq *devfreq; 1084 1085 if (!dev || !phandle_name) 1086 return ERR_PTR(-EINVAL); 1087 1088 if (!dev->of_node) 1089 return ERR_PTR(-EINVAL); 1090 1091 node = of_parse_phandle(dev->of_node, phandle_name, index); 1092 if (!node) 1093 return ERR_PTR(-ENODEV); 1094 1095 devfreq = devfreq_get_devfreq_by_node(node); 1096 of_node_put(node); 1097 1098 return devfreq; 1099 } 1100 1101 #else 1102 struct devfreq *devfreq_get_devfreq_by_node(struct device_node *node) 1103 { 1104 return ERR_PTR(-ENODEV); 1105 } 1106 1107 struct devfreq *devfreq_get_devfreq_by_phandle(struct device *dev, 1108 const char *phandle_name, int index) 1109 { 1110 return ERR_PTR(-ENODEV); 1111 } 1112 #endif /* CONFIG_OF */ 1113 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_node); 1114 EXPORT_SYMBOL_GPL(devfreq_get_devfreq_by_phandle); 1115 1116 /** 1117 * devm_devfreq_remove_device() - Resource-managed devfreq_remove_device() 1118 * @dev: the device from which to remove devfreq feature. 1119 * @devfreq: the devfreq instance to be removed 1120 */ 1121 void devm_devfreq_remove_device(struct device *dev, struct devfreq *devfreq) 1122 { 1123 WARN_ON(devres_release(dev, devm_devfreq_dev_release, 1124 devm_devfreq_dev_match, devfreq)); 1125 } 1126 EXPORT_SYMBOL(devm_devfreq_remove_device); 1127 1128 /** 1129 * devfreq_suspend_device() - Suspend devfreq of a device. 1130 * @devfreq: the devfreq instance to be suspended 1131 * 1132 * This function is intended to be called by the pm callbacks 1133 * (e.g., runtime_suspend, suspend) of the device driver that 1134 * holds the devfreq. 1135 */ 1136 int devfreq_suspend_device(struct devfreq *devfreq) 1137 { 1138 int ret; 1139 1140 if (!devfreq) 1141 return -EINVAL; 1142 1143 if (atomic_inc_return(&devfreq->suspend_count) > 1) 1144 return 0; 1145 1146 if (devfreq->governor) { 1147 ret = devfreq->governor->event_handler(devfreq, 1148 DEVFREQ_GOV_SUSPEND, NULL); 1149 if (ret) 1150 return ret; 1151 } 1152 1153 if (devfreq->suspend_freq) { 1154 mutex_lock(&devfreq->lock); 1155 ret = devfreq_set_target(devfreq, devfreq->suspend_freq, 0); 1156 mutex_unlock(&devfreq->lock); 1157 if (ret) 1158 return ret; 1159 } 1160 1161 return 0; 1162 } 1163 EXPORT_SYMBOL(devfreq_suspend_device); 1164 1165 /** 1166 * devfreq_resume_device() - Resume devfreq of a device. 1167 * @devfreq: the devfreq instance to be resumed 1168 * 1169 * This function is intended to be called by the pm callbacks 1170 * (e.g., runtime_resume, resume) of the device driver that 1171 * holds the devfreq. 1172 */ 1173 int devfreq_resume_device(struct devfreq *devfreq) 1174 { 1175 int ret; 1176 1177 if (!devfreq) 1178 return -EINVAL; 1179 1180 if (atomic_dec_return(&devfreq->suspend_count) >= 1) 1181 return 0; 1182 1183 if (devfreq->resume_freq) { 1184 mutex_lock(&devfreq->lock); 1185 ret = devfreq_set_target(devfreq, devfreq->resume_freq, 0); 1186 mutex_unlock(&devfreq->lock); 1187 if (ret) 1188 return ret; 1189 } 1190 1191 if (devfreq->governor) { 1192 ret = devfreq->governor->event_handler(devfreq, 1193 DEVFREQ_GOV_RESUME, NULL); 1194 if (ret) 1195 return ret; 1196 } 1197 1198 return 0; 1199 } 1200 EXPORT_SYMBOL(devfreq_resume_device); 1201 1202 /** 1203 * devfreq_suspend() - Suspend devfreq governors and devices 1204 * 1205 * Called during system wide Suspend/Hibernate cycles for suspending governors 1206 * and devices preserving the state for resume. On some platforms the devfreq 1207 * device must have precise state (frequency) after resume in order to provide 1208 * fully operating setup. 1209 */ 1210 void devfreq_suspend(void) 1211 { 1212 struct devfreq *devfreq; 1213 int ret; 1214 1215 mutex_lock(&devfreq_list_lock); 1216 list_for_each_entry(devfreq, &devfreq_list, node) { 1217 ret = devfreq_suspend_device(devfreq); 1218 if (ret) 1219 dev_err(&devfreq->dev, 1220 "failed to suspend devfreq device\n"); 1221 } 1222 mutex_unlock(&devfreq_list_lock); 1223 } 1224 1225 /** 1226 * devfreq_resume() - Resume devfreq governors and devices 1227 * 1228 * Called during system wide Suspend/Hibernate cycle for resuming governors and 1229 * devices that are suspended with devfreq_suspend(). 1230 */ 1231 void devfreq_resume(void) 1232 { 1233 struct devfreq *devfreq; 1234 int ret; 1235 1236 mutex_lock(&devfreq_list_lock); 1237 list_for_each_entry(devfreq, &devfreq_list, node) { 1238 ret = devfreq_resume_device(devfreq); 1239 if (ret) 1240 dev_warn(&devfreq->dev, 1241 "failed to resume devfreq device\n"); 1242 } 1243 mutex_unlock(&devfreq_list_lock); 1244 } 1245 1246 /** 1247 * devfreq_add_governor() - Add devfreq governor 1248 * @governor: the devfreq governor to be added 1249 */ 1250 int devfreq_add_governor(struct devfreq_governor *governor) 1251 { 1252 struct devfreq_governor *g; 1253 struct devfreq *devfreq; 1254 int err = 0; 1255 1256 if (!governor) { 1257 pr_err("%s: Invalid parameters.\n", __func__); 1258 return -EINVAL; 1259 } 1260 1261 mutex_lock(&devfreq_list_lock); 1262 g = find_devfreq_governor(governor->name); 1263 if (!IS_ERR(g)) { 1264 pr_err("%s: governor %s already registered\n", __func__, 1265 g->name); 1266 err = -EINVAL; 1267 goto err_out; 1268 } 1269 1270 list_add(&governor->node, &devfreq_governor_list); 1271 1272 list_for_each_entry(devfreq, &devfreq_list, node) { 1273 int ret = 0; 1274 struct device *dev = devfreq->dev.parent; 1275 1276 if (!strncmp(devfreq->governor->name, governor->name, 1277 DEVFREQ_NAME_LEN)) { 1278 /* The following should never occur */ 1279 if (devfreq->governor) { 1280 dev_warn(dev, 1281 "%s: Governor %s already present\n", 1282 __func__, devfreq->governor->name); 1283 ret = devfreq->governor->event_handler(devfreq, 1284 DEVFREQ_GOV_STOP, NULL); 1285 if (ret) { 1286 dev_warn(dev, 1287 "%s: Governor %s stop = %d\n", 1288 __func__, 1289 devfreq->governor->name, ret); 1290 } 1291 /* Fall through */ 1292 } 1293 devfreq->governor = governor; 1294 ret = devfreq->governor->event_handler(devfreq, 1295 DEVFREQ_GOV_START, NULL); 1296 if (ret) { 1297 dev_warn(dev, "%s: Governor %s start=%d\n", 1298 __func__, devfreq->governor->name, 1299 ret); 1300 } 1301 } 1302 } 1303 1304 err_out: 1305 mutex_unlock(&devfreq_list_lock); 1306 1307 return err; 1308 } 1309 EXPORT_SYMBOL(devfreq_add_governor); 1310 1311 static void devm_devfreq_remove_governor(void *governor) 1312 { 1313 WARN_ON(devfreq_remove_governor(governor)); 1314 } 1315 1316 /** 1317 * devm_devfreq_add_governor() - Add devfreq governor 1318 * @dev: device which adds devfreq governor 1319 * @governor: the devfreq governor to be added 1320 * 1321 * This is a resource-managed variant of devfreq_add_governor(). 1322 */ 1323 int devm_devfreq_add_governor(struct device *dev, 1324 struct devfreq_governor *governor) 1325 { 1326 int err; 1327 1328 err = devfreq_add_governor(governor); 1329 if (err) 1330 return err; 1331 1332 return devm_add_action_or_reset(dev, devm_devfreq_remove_governor, 1333 governor); 1334 } 1335 EXPORT_SYMBOL(devm_devfreq_add_governor); 1336 1337 /** 1338 * devfreq_remove_governor() - Remove devfreq feature from a device. 1339 * @governor: the devfreq governor to be removed 1340 */ 1341 int devfreq_remove_governor(struct devfreq_governor *governor) 1342 { 1343 struct devfreq_governor *g; 1344 struct devfreq *devfreq; 1345 int err = 0; 1346 1347 if (!governor) { 1348 pr_err("%s: Invalid parameters.\n", __func__); 1349 return -EINVAL; 1350 } 1351 1352 mutex_lock(&devfreq_list_lock); 1353 g = find_devfreq_governor(governor->name); 1354 if (IS_ERR(g)) { 1355 pr_err("%s: governor %s not registered\n", __func__, 1356 governor->name); 1357 err = PTR_ERR(g); 1358 goto err_out; 1359 } 1360 list_for_each_entry(devfreq, &devfreq_list, node) { 1361 int ret; 1362 struct device *dev = devfreq->dev.parent; 1363 1364 if (!strncmp(devfreq->governor->name, governor->name, 1365 DEVFREQ_NAME_LEN)) { 1366 /* we should have a devfreq governor! */ 1367 if (!devfreq->governor) { 1368 dev_warn(dev, "%s: Governor %s NOT present\n", 1369 __func__, governor->name); 1370 continue; 1371 /* Fall through */ 1372 } 1373 ret = devfreq->governor->event_handler(devfreq, 1374 DEVFREQ_GOV_STOP, NULL); 1375 if (ret) { 1376 dev_warn(dev, "%s: Governor %s stop=%d\n", 1377 __func__, devfreq->governor->name, 1378 ret); 1379 } 1380 devfreq->governor = NULL; 1381 } 1382 } 1383 1384 list_del(&governor->node); 1385 err_out: 1386 mutex_unlock(&devfreq_list_lock); 1387 1388 return err; 1389 } 1390 EXPORT_SYMBOL(devfreq_remove_governor); 1391 1392 static ssize_t name_show(struct device *dev, 1393 struct device_attribute *attr, char *buf) 1394 { 1395 struct devfreq *df = to_devfreq(dev); 1396 return sprintf(buf, "%s\n", dev_name(df->dev.parent)); 1397 } 1398 static DEVICE_ATTR_RO(name); 1399 1400 static ssize_t governor_show(struct device *dev, 1401 struct device_attribute *attr, char *buf) 1402 { 1403 struct devfreq *df = to_devfreq(dev); 1404 1405 if (!df->governor) 1406 return -EINVAL; 1407 1408 return sprintf(buf, "%s\n", df->governor->name); 1409 } 1410 1411 static ssize_t governor_store(struct device *dev, struct device_attribute *attr, 1412 const char *buf, size_t count) 1413 { 1414 struct devfreq *df = to_devfreq(dev); 1415 int ret; 1416 char str_governor[DEVFREQ_NAME_LEN + 1]; 1417 const struct devfreq_governor *governor, *prev_governor; 1418 1419 if (!df->governor) 1420 return -EINVAL; 1421 1422 ret = sscanf(buf, "%" __stringify(DEVFREQ_NAME_LEN) "s", str_governor); 1423 if (ret != 1) 1424 return -EINVAL; 1425 1426 mutex_lock(&devfreq_list_lock); 1427 governor = try_then_request_governor(str_governor); 1428 if (IS_ERR(governor)) { 1429 ret = PTR_ERR(governor); 1430 goto out; 1431 } 1432 if (df->governor == governor) { 1433 ret = 0; 1434 goto out; 1435 } else if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE) 1436 || IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE)) { 1437 ret = -EINVAL; 1438 goto out; 1439 } 1440 1441 /* 1442 * Stop the current governor and remove the specific sysfs files 1443 * which depend on current governor. 1444 */ 1445 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL); 1446 if (ret) { 1447 dev_warn(dev, "%s: Governor %s not stopped(%d)\n", 1448 __func__, df->governor->name, ret); 1449 goto out; 1450 } 1451 remove_sysfs_files(df, df->governor); 1452 1453 /* 1454 * Start the new governor and create the specific sysfs files 1455 * which depend on the new governor. 1456 */ 1457 prev_governor = df->governor; 1458 df->governor = governor; 1459 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL); 1460 if (ret) { 1461 dev_warn(dev, "%s: Governor %s not started(%d)\n", 1462 __func__, df->governor->name, ret); 1463 1464 /* Restore previous governor */ 1465 df->governor = prev_governor; 1466 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL); 1467 if (ret) { 1468 dev_err(dev, 1469 "%s: reverting to Governor %s failed (%d)\n", 1470 __func__, prev_governor->name, ret); 1471 df->governor = NULL; 1472 goto out; 1473 } 1474 } 1475 1476 /* 1477 * Create the sysfs files for the new governor. But if failed to start 1478 * the new governor, restore the sysfs files of previous governor. 1479 */ 1480 create_sysfs_files(df, df->governor); 1481 1482 out: 1483 mutex_unlock(&devfreq_list_lock); 1484 1485 if (!ret) 1486 ret = count; 1487 return ret; 1488 } 1489 static DEVICE_ATTR_RW(governor); 1490 1491 static ssize_t available_governors_show(struct device *d, 1492 struct device_attribute *attr, 1493 char *buf) 1494 { 1495 struct devfreq *df = to_devfreq(d); 1496 ssize_t count = 0; 1497 1498 if (!df->governor) 1499 return -EINVAL; 1500 1501 mutex_lock(&devfreq_list_lock); 1502 1503 /* 1504 * The devfreq with immutable governor (e.g., passive) shows 1505 * only own governor. 1506 */ 1507 if (IS_SUPPORTED_FLAG(df->governor->flags, IMMUTABLE)) { 1508 count = scnprintf(&buf[count], DEVFREQ_NAME_LEN, 1509 "%s ", df->governor->name); 1510 /* 1511 * The devfreq device shows the registered governor except for 1512 * immutable governors such as passive governor . 1513 */ 1514 } else { 1515 struct devfreq_governor *governor; 1516 1517 list_for_each_entry(governor, &devfreq_governor_list, node) { 1518 if (IS_SUPPORTED_FLAG(governor->flags, IMMUTABLE)) 1519 continue; 1520 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2), 1521 "%s ", governor->name); 1522 } 1523 } 1524 1525 mutex_unlock(&devfreq_list_lock); 1526 1527 /* Truncate the trailing space */ 1528 if (count) 1529 count--; 1530 1531 count += sprintf(&buf[count], "\n"); 1532 1533 return count; 1534 } 1535 static DEVICE_ATTR_RO(available_governors); 1536 1537 static ssize_t cur_freq_show(struct device *dev, struct device_attribute *attr, 1538 char *buf) 1539 { 1540 unsigned long freq; 1541 struct devfreq *df = to_devfreq(dev); 1542 1543 if (!df->profile) 1544 return -EINVAL; 1545 1546 if (df->profile->get_cur_freq && 1547 !df->profile->get_cur_freq(df->dev.parent, &freq)) 1548 return sprintf(buf, "%lu\n", freq); 1549 1550 return sprintf(buf, "%lu\n", df->previous_freq); 1551 } 1552 static DEVICE_ATTR_RO(cur_freq); 1553 1554 static ssize_t target_freq_show(struct device *dev, 1555 struct device_attribute *attr, char *buf) 1556 { 1557 struct devfreq *df = to_devfreq(dev); 1558 1559 return sprintf(buf, "%lu\n", df->previous_freq); 1560 } 1561 static DEVICE_ATTR_RO(target_freq); 1562 1563 static ssize_t min_freq_store(struct device *dev, struct device_attribute *attr, 1564 const char *buf, size_t count) 1565 { 1566 struct devfreq *df = to_devfreq(dev); 1567 unsigned long value; 1568 int ret; 1569 1570 /* 1571 * Protect against theoretical sysfs writes between 1572 * device_add and dev_pm_qos_add_request 1573 */ 1574 if (!dev_pm_qos_request_active(&df->user_min_freq_req)) 1575 return -EAGAIN; 1576 1577 ret = sscanf(buf, "%lu", &value); 1578 if (ret != 1) 1579 return -EINVAL; 1580 1581 /* Round down to kHz for PM QoS */ 1582 ret = dev_pm_qos_update_request(&df->user_min_freq_req, 1583 value / HZ_PER_KHZ); 1584 if (ret < 0) 1585 return ret; 1586 1587 return count; 1588 } 1589 1590 static ssize_t min_freq_show(struct device *dev, struct device_attribute *attr, 1591 char *buf) 1592 { 1593 struct devfreq *df = to_devfreq(dev); 1594 unsigned long min_freq, max_freq; 1595 1596 mutex_lock(&df->lock); 1597 devfreq_get_freq_range(df, &min_freq, &max_freq); 1598 mutex_unlock(&df->lock); 1599 1600 return sprintf(buf, "%lu\n", min_freq); 1601 } 1602 static DEVICE_ATTR_RW(min_freq); 1603 1604 static ssize_t max_freq_store(struct device *dev, struct device_attribute *attr, 1605 const char *buf, size_t count) 1606 { 1607 struct devfreq *df = to_devfreq(dev); 1608 unsigned long value; 1609 int ret; 1610 1611 /* 1612 * Protect against theoretical sysfs writes between 1613 * device_add and dev_pm_qos_add_request 1614 */ 1615 if (!dev_pm_qos_request_active(&df->user_max_freq_req)) 1616 return -EINVAL; 1617 1618 ret = sscanf(buf, "%lu", &value); 1619 if (ret != 1) 1620 return -EINVAL; 1621 1622 /* 1623 * PM QoS frequencies are in kHz so we need to convert. Convert by 1624 * rounding upwards so that the acceptable interval never shrinks. 1625 * 1626 * For example if the user writes "666666666" to sysfs this value will 1627 * be converted to 666667 kHz and back to 666667000 Hz before an OPP 1628 * lookup, this ensures that an OPP of 666666666Hz is still accepted. 1629 * 1630 * A value of zero means "no limit". 1631 */ 1632 if (value) 1633 value = DIV_ROUND_UP(value, HZ_PER_KHZ); 1634 else 1635 value = PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE; 1636 1637 ret = dev_pm_qos_update_request(&df->user_max_freq_req, value); 1638 if (ret < 0) 1639 return ret; 1640 1641 return count; 1642 } 1643 1644 static ssize_t max_freq_show(struct device *dev, struct device_attribute *attr, 1645 char *buf) 1646 { 1647 struct devfreq *df = to_devfreq(dev); 1648 unsigned long min_freq, max_freq; 1649 1650 mutex_lock(&df->lock); 1651 devfreq_get_freq_range(df, &min_freq, &max_freq); 1652 mutex_unlock(&df->lock); 1653 1654 return sprintf(buf, "%lu\n", max_freq); 1655 } 1656 static DEVICE_ATTR_RW(max_freq); 1657 1658 static ssize_t available_frequencies_show(struct device *d, 1659 struct device_attribute *attr, 1660 char *buf) 1661 { 1662 struct devfreq *df = to_devfreq(d); 1663 ssize_t count = 0; 1664 int i; 1665 1666 if (!df->profile) 1667 return -EINVAL; 1668 1669 mutex_lock(&df->lock); 1670 1671 for (i = 0; i < df->max_state; i++) 1672 count += scnprintf(&buf[count], (PAGE_SIZE - count - 2), 1673 "%lu ", df->freq_table[i]); 1674 1675 mutex_unlock(&df->lock); 1676 /* Truncate the trailing space */ 1677 if (count) 1678 count--; 1679 1680 count += sprintf(&buf[count], "\n"); 1681 1682 return count; 1683 } 1684 static DEVICE_ATTR_RO(available_frequencies); 1685 1686 static ssize_t trans_stat_show(struct device *dev, 1687 struct device_attribute *attr, char *buf) 1688 { 1689 struct devfreq *df = to_devfreq(dev); 1690 ssize_t len; 1691 int i, j; 1692 unsigned int max_state; 1693 1694 if (!df->profile) 1695 return -EINVAL; 1696 max_state = df->max_state; 1697 1698 if (max_state == 0) 1699 return sprintf(buf, "Not Supported.\n"); 1700 1701 mutex_lock(&df->lock); 1702 if (!df->stop_polling && 1703 devfreq_update_status(df, df->previous_freq)) { 1704 mutex_unlock(&df->lock); 1705 return 0; 1706 } 1707 mutex_unlock(&df->lock); 1708 1709 len = sprintf(buf, " From : To\n"); 1710 len += sprintf(buf + len, " :"); 1711 for (i = 0; i < max_state; i++) 1712 len += sprintf(buf + len, "%10lu", 1713 df->freq_table[i]); 1714 1715 len += sprintf(buf + len, " time(ms)\n"); 1716 1717 for (i = 0; i < max_state; i++) { 1718 if (df->freq_table[i] == df->previous_freq) 1719 len += sprintf(buf + len, "*"); 1720 else 1721 len += sprintf(buf + len, " "); 1722 1723 len += sprintf(buf + len, "%10lu:", df->freq_table[i]); 1724 for (j = 0; j < max_state; j++) 1725 len += sprintf(buf + len, "%10u", 1726 df->stats.trans_table[(i * max_state) + j]); 1727 1728 len += sprintf(buf + len, "%10llu\n", (u64) 1729 jiffies64_to_msecs(df->stats.time_in_state[i])); 1730 } 1731 1732 len += sprintf(buf + len, "Total transition : %u\n", 1733 df->stats.total_trans); 1734 return len; 1735 } 1736 1737 static ssize_t trans_stat_store(struct device *dev, 1738 struct device_attribute *attr, 1739 const char *buf, size_t count) 1740 { 1741 struct devfreq *df = to_devfreq(dev); 1742 int err, value; 1743 1744 if (!df->profile) 1745 return -EINVAL; 1746 1747 if (df->max_state == 0) 1748 return count; 1749 1750 err = kstrtoint(buf, 10, &value); 1751 if (err || value != 0) 1752 return -EINVAL; 1753 1754 mutex_lock(&df->lock); 1755 memset(df->stats.time_in_state, 0, (df->max_state * 1756 sizeof(*df->stats.time_in_state))); 1757 memset(df->stats.trans_table, 0, array3_size(sizeof(unsigned int), 1758 df->max_state, 1759 df->max_state)); 1760 df->stats.total_trans = 0; 1761 df->stats.last_update = get_jiffies_64(); 1762 mutex_unlock(&df->lock); 1763 1764 return count; 1765 } 1766 static DEVICE_ATTR_RW(trans_stat); 1767 1768 static struct attribute *devfreq_attrs[] = { 1769 &dev_attr_name.attr, 1770 &dev_attr_governor.attr, 1771 &dev_attr_available_governors.attr, 1772 &dev_attr_cur_freq.attr, 1773 &dev_attr_available_frequencies.attr, 1774 &dev_attr_target_freq.attr, 1775 &dev_attr_min_freq.attr, 1776 &dev_attr_max_freq.attr, 1777 &dev_attr_trans_stat.attr, 1778 NULL, 1779 }; 1780 ATTRIBUTE_GROUPS(devfreq); 1781 1782 static ssize_t polling_interval_show(struct device *dev, 1783 struct device_attribute *attr, char *buf) 1784 { 1785 struct devfreq *df = to_devfreq(dev); 1786 1787 if (!df->profile) 1788 return -EINVAL; 1789 1790 return sprintf(buf, "%d\n", df->profile->polling_ms); 1791 } 1792 1793 static ssize_t polling_interval_store(struct device *dev, 1794 struct device_attribute *attr, 1795 const char *buf, size_t count) 1796 { 1797 struct devfreq *df = to_devfreq(dev); 1798 unsigned int value; 1799 int ret; 1800 1801 if (!df->governor) 1802 return -EINVAL; 1803 1804 ret = sscanf(buf, "%u", &value); 1805 if (ret != 1) 1806 return -EINVAL; 1807 1808 df->governor->event_handler(df, DEVFREQ_GOV_UPDATE_INTERVAL, &value); 1809 ret = count; 1810 1811 return ret; 1812 } 1813 static DEVICE_ATTR_RW(polling_interval); 1814 1815 static ssize_t timer_show(struct device *dev, 1816 struct device_attribute *attr, char *buf) 1817 { 1818 struct devfreq *df = to_devfreq(dev); 1819 1820 if (!df->profile) 1821 return -EINVAL; 1822 1823 return sprintf(buf, "%s\n", timer_name[df->profile->timer]); 1824 } 1825 1826 static ssize_t timer_store(struct device *dev, struct device_attribute *attr, 1827 const char *buf, size_t count) 1828 { 1829 struct devfreq *df = to_devfreq(dev); 1830 char str_timer[DEVFREQ_NAME_LEN + 1]; 1831 int timer = -1; 1832 int ret = 0, i; 1833 1834 if (!df->governor || !df->profile) 1835 return -EINVAL; 1836 1837 ret = sscanf(buf, "%16s", str_timer); 1838 if (ret != 1) 1839 return -EINVAL; 1840 1841 for (i = 0; i < DEVFREQ_TIMER_NUM; i++) { 1842 if (!strncmp(timer_name[i], str_timer, DEVFREQ_NAME_LEN)) { 1843 timer = i; 1844 break; 1845 } 1846 } 1847 1848 if (timer < 0) { 1849 ret = -EINVAL; 1850 goto out; 1851 } 1852 1853 if (df->profile->timer == timer) { 1854 ret = 0; 1855 goto out; 1856 } 1857 1858 mutex_lock(&df->lock); 1859 df->profile->timer = timer; 1860 mutex_unlock(&df->lock); 1861 1862 ret = df->governor->event_handler(df, DEVFREQ_GOV_STOP, NULL); 1863 if (ret) { 1864 dev_warn(dev, "%s: Governor %s not stopped(%d)\n", 1865 __func__, df->governor->name, ret); 1866 goto out; 1867 } 1868 1869 ret = df->governor->event_handler(df, DEVFREQ_GOV_START, NULL); 1870 if (ret) 1871 dev_warn(dev, "%s: Governor %s not started(%d)\n", 1872 __func__, df->governor->name, ret); 1873 out: 1874 return ret ? ret : count; 1875 } 1876 static DEVICE_ATTR_RW(timer); 1877 1878 #define CREATE_SYSFS_FILE(df, name) \ 1879 { \ 1880 int ret; \ 1881 ret = sysfs_create_file(&df->dev.kobj, &dev_attr_##name.attr); \ 1882 if (ret < 0) { \ 1883 dev_warn(&df->dev, \ 1884 "Unable to create attr(%s)\n", "##name"); \ 1885 } \ 1886 } \ 1887 1888 /* Create the specific sysfs files which depend on each governor. */ 1889 static void create_sysfs_files(struct devfreq *devfreq, 1890 const struct devfreq_governor *gov) 1891 { 1892 if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL)) 1893 CREATE_SYSFS_FILE(devfreq, polling_interval); 1894 if (IS_SUPPORTED_ATTR(gov->attrs, TIMER)) 1895 CREATE_SYSFS_FILE(devfreq, timer); 1896 } 1897 1898 /* Remove the specific sysfs files which depend on each governor. */ 1899 static void remove_sysfs_files(struct devfreq *devfreq, 1900 const struct devfreq_governor *gov) 1901 { 1902 if (IS_SUPPORTED_ATTR(gov->attrs, POLLING_INTERVAL)) 1903 sysfs_remove_file(&devfreq->dev.kobj, 1904 &dev_attr_polling_interval.attr); 1905 if (IS_SUPPORTED_ATTR(gov->attrs, TIMER)) 1906 sysfs_remove_file(&devfreq->dev.kobj, &dev_attr_timer.attr); 1907 } 1908 1909 /** 1910 * devfreq_summary_show() - Show the summary of the devfreq devices 1911 * @s: seq_file instance to show the summary of devfreq devices 1912 * @data: not used 1913 * 1914 * Show the summary of the devfreq devices via 'devfreq_summary' debugfs file. 1915 * It helps that user can know the detailed information of the devfreq devices. 1916 * 1917 * Return 0 always because it shows the information without any data change. 1918 */ 1919 static int devfreq_summary_show(struct seq_file *s, void *data) 1920 { 1921 struct devfreq *devfreq; 1922 struct devfreq *p_devfreq = NULL; 1923 unsigned long cur_freq, min_freq, max_freq; 1924 unsigned int polling_ms; 1925 unsigned int timer; 1926 1927 seq_printf(s, "%-30s %-30s %-15s %-10s %10s %12s %12s %12s\n", 1928 "dev", 1929 "parent_dev", 1930 "governor", 1931 "timer", 1932 "polling_ms", 1933 "cur_freq_Hz", 1934 "min_freq_Hz", 1935 "max_freq_Hz"); 1936 seq_printf(s, "%30s %30s %15s %10s %10s %12s %12s %12s\n", 1937 "------------------------------", 1938 "------------------------------", 1939 "---------------", 1940 "----------", 1941 "----------", 1942 "------------", 1943 "------------", 1944 "------------"); 1945 1946 mutex_lock(&devfreq_list_lock); 1947 1948 list_for_each_entry_reverse(devfreq, &devfreq_list, node) { 1949 #if IS_ENABLED(CONFIG_DEVFREQ_GOV_PASSIVE) 1950 if (!strncmp(devfreq->governor->name, DEVFREQ_GOV_PASSIVE, 1951 DEVFREQ_NAME_LEN)) { 1952 struct devfreq_passive_data *data = devfreq->data; 1953 1954 if (data) 1955 p_devfreq = data->parent; 1956 } else { 1957 p_devfreq = NULL; 1958 } 1959 #endif 1960 1961 mutex_lock(&devfreq->lock); 1962 cur_freq = devfreq->previous_freq; 1963 devfreq_get_freq_range(devfreq, &min_freq, &max_freq); 1964 timer = devfreq->profile->timer; 1965 1966 if (IS_SUPPORTED_ATTR(devfreq->governor->attrs, POLLING_INTERVAL)) 1967 polling_ms = devfreq->profile->polling_ms; 1968 else 1969 polling_ms = 0; 1970 mutex_unlock(&devfreq->lock); 1971 1972 seq_printf(s, 1973 "%-30s %-30s %-15s %-10s %10d %12ld %12ld %12ld\n", 1974 dev_name(&devfreq->dev), 1975 p_devfreq ? dev_name(&p_devfreq->dev) : "null", 1976 devfreq->governor->name, 1977 polling_ms ? timer_name[timer] : "null", 1978 polling_ms, 1979 cur_freq, 1980 min_freq, 1981 max_freq); 1982 } 1983 1984 mutex_unlock(&devfreq_list_lock); 1985 1986 return 0; 1987 } 1988 DEFINE_SHOW_ATTRIBUTE(devfreq_summary); 1989 1990 static int __init devfreq_init(void) 1991 { 1992 devfreq_class = class_create("devfreq"); 1993 if (IS_ERR(devfreq_class)) { 1994 pr_err("%s: couldn't create class\n", __FILE__); 1995 return PTR_ERR(devfreq_class); 1996 } 1997 1998 devfreq_wq = create_freezable_workqueue("devfreq_wq"); 1999 if (!devfreq_wq) { 2000 class_destroy(devfreq_class); 2001 pr_err("%s: couldn't create workqueue\n", __FILE__); 2002 return -ENOMEM; 2003 } 2004 devfreq_class->dev_groups = devfreq_groups; 2005 2006 devfreq_debugfs = debugfs_create_dir("devfreq", NULL); 2007 debugfs_create_file("devfreq_summary", 0444, 2008 devfreq_debugfs, NULL, 2009 &devfreq_summary_fops); 2010 2011 return 0; 2012 } 2013 subsys_initcall(devfreq_init); 2014 2015 /* 2016 * The following are helper functions for devfreq user device drivers with 2017 * OPP framework. 2018 */ 2019 2020 /** 2021 * devfreq_recommended_opp() - Helper function to get proper OPP for the 2022 * freq value given to target callback. 2023 * @dev: The devfreq user device. (parent of devfreq) 2024 * @freq: The frequency given to target function 2025 * @flags: Flags handed from devfreq framework. 2026 * 2027 * The callers are required to call dev_pm_opp_put() for the returned OPP after 2028 * use. 2029 */ 2030 struct dev_pm_opp *devfreq_recommended_opp(struct device *dev, 2031 unsigned long *freq, 2032 u32 flags) 2033 { 2034 struct dev_pm_opp *opp; 2035 2036 if (flags & DEVFREQ_FLAG_LEAST_UPPER_BOUND) { 2037 /* The freq is an upper bound. opp should be lower */ 2038 opp = dev_pm_opp_find_freq_floor(dev, freq); 2039 2040 /* If not available, use the closest opp */ 2041 if (opp == ERR_PTR(-ERANGE)) 2042 opp = dev_pm_opp_find_freq_ceil(dev, freq); 2043 } else { 2044 /* The freq is an lower bound. opp should be higher */ 2045 opp = dev_pm_opp_find_freq_ceil(dev, freq); 2046 2047 /* If not available, use the closest opp */ 2048 if (opp == ERR_PTR(-ERANGE)) 2049 opp = dev_pm_opp_find_freq_floor(dev, freq); 2050 } 2051 2052 return opp; 2053 } 2054 EXPORT_SYMBOL(devfreq_recommended_opp); 2055 2056 /** 2057 * devfreq_register_opp_notifier() - Helper function to get devfreq notified 2058 * for any changes in the OPP availability 2059 * changes 2060 * @dev: The devfreq user device. (parent of devfreq) 2061 * @devfreq: The devfreq object. 2062 */ 2063 int devfreq_register_opp_notifier(struct device *dev, struct devfreq *devfreq) 2064 { 2065 return dev_pm_opp_register_notifier(dev, &devfreq->nb); 2066 } 2067 EXPORT_SYMBOL(devfreq_register_opp_notifier); 2068 2069 /** 2070 * devfreq_unregister_opp_notifier() - Helper function to stop getting devfreq 2071 * notified for any changes in the OPP 2072 * availability changes anymore. 2073 * @dev: The devfreq user device. (parent of devfreq) 2074 * @devfreq: The devfreq object. 2075 * 2076 * At exit() callback of devfreq_dev_profile, this must be included if 2077 * devfreq_recommended_opp is used. 2078 */ 2079 int devfreq_unregister_opp_notifier(struct device *dev, struct devfreq *devfreq) 2080 { 2081 return dev_pm_opp_unregister_notifier(dev, &devfreq->nb); 2082 } 2083 EXPORT_SYMBOL(devfreq_unregister_opp_notifier); 2084 2085 static void devm_devfreq_opp_release(struct device *dev, void *res) 2086 { 2087 devfreq_unregister_opp_notifier(dev, *(struct devfreq **)res); 2088 } 2089 2090 /** 2091 * devm_devfreq_register_opp_notifier() - Resource-managed 2092 * devfreq_register_opp_notifier() 2093 * @dev: The devfreq user device. (parent of devfreq) 2094 * @devfreq: The devfreq object. 2095 */ 2096 int devm_devfreq_register_opp_notifier(struct device *dev, 2097 struct devfreq *devfreq) 2098 { 2099 struct devfreq **ptr; 2100 int ret; 2101 2102 ptr = devres_alloc(devm_devfreq_opp_release, sizeof(*ptr), GFP_KERNEL); 2103 if (!ptr) 2104 return -ENOMEM; 2105 2106 ret = devfreq_register_opp_notifier(dev, devfreq); 2107 if (ret) { 2108 devres_free(ptr); 2109 return ret; 2110 } 2111 2112 *ptr = devfreq; 2113 devres_add(dev, ptr); 2114 2115 return 0; 2116 } 2117 EXPORT_SYMBOL(devm_devfreq_register_opp_notifier); 2118 2119 /** 2120 * devm_devfreq_unregister_opp_notifier() - Resource-managed 2121 * devfreq_unregister_opp_notifier() 2122 * @dev: The devfreq user device. (parent of devfreq) 2123 * @devfreq: The devfreq object. 2124 */ 2125 void devm_devfreq_unregister_opp_notifier(struct device *dev, 2126 struct devfreq *devfreq) 2127 { 2128 WARN_ON(devres_release(dev, devm_devfreq_opp_release, 2129 devm_devfreq_dev_match, devfreq)); 2130 } 2131 EXPORT_SYMBOL(devm_devfreq_unregister_opp_notifier); 2132 2133 /** 2134 * devfreq_register_notifier() - Register a driver with devfreq 2135 * @devfreq: The devfreq object. 2136 * @nb: The notifier block to register. 2137 * @list: DEVFREQ_TRANSITION_NOTIFIER. 2138 */ 2139 int devfreq_register_notifier(struct devfreq *devfreq, 2140 struct notifier_block *nb, 2141 unsigned int list) 2142 { 2143 int ret = 0; 2144 2145 if (!devfreq) 2146 return -EINVAL; 2147 2148 switch (list) { 2149 case DEVFREQ_TRANSITION_NOTIFIER: 2150 ret = srcu_notifier_chain_register( 2151 &devfreq->transition_notifier_list, nb); 2152 break; 2153 default: 2154 ret = -EINVAL; 2155 } 2156 2157 return ret; 2158 } 2159 EXPORT_SYMBOL(devfreq_register_notifier); 2160 2161 /* 2162 * devfreq_unregister_notifier() - Unregister a driver with devfreq 2163 * @devfreq: The devfreq object. 2164 * @nb: The notifier block to be unregistered. 2165 * @list: DEVFREQ_TRANSITION_NOTIFIER. 2166 */ 2167 int devfreq_unregister_notifier(struct devfreq *devfreq, 2168 struct notifier_block *nb, 2169 unsigned int list) 2170 { 2171 int ret = 0; 2172 2173 if (!devfreq) 2174 return -EINVAL; 2175 2176 switch (list) { 2177 case DEVFREQ_TRANSITION_NOTIFIER: 2178 ret = srcu_notifier_chain_unregister( 2179 &devfreq->transition_notifier_list, nb); 2180 break; 2181 default: 2182 ret = -EINVAL; 2183 } 2184 2185 return ret; 2186 } 2187 EXPORT_SYMBOL(devfreq_unregister_notifier); 2188 2189 struct devfreq_notifier_devres { 2190 struct devfreq *devfreq; 2191 struct notifier_block *nb; 2192 unsigned int list; 2193 }; 2194 2195 static void devm_devfreq_notifier_release(struct device *dev, void *res) 2196 { 2197 struct devfreq_notifier_devres *this = res; 2198 2199 devfreq_unregister_notifier(this->devfreq, this->nb, this->list); 2200 } 2201 2202 /** 2203 * devm_devfreq_register_notifier() 2204 * - Resource-managed devfreq_register_notifier() 2205 * @dev: The devfreq user device. (parent of devfreq) 2206 * @devfreq: The devfreq object. 2207 * @nb: The notifier block to be unregistered. 2208 * @list: DEVFREQ_TRANSITION_NOTIFIER. 2209 */ 2210 int devm_devfreq_register_notifier(struct device *dev, 2211 struct devfreq *devfreq, 2212 struct notifier_block *nb, 2213 unsigned int list) 2214 { 2215 struct devfreq_notifier_devres *ptr; 2216 int ret; 2217 2218 ptr = devres_alloc(devm_devfreq_notifier_release, sizeof(*ptr), 2219 GFP_KERNEL); 2220 if (!ptr) 2221 return -ENOMEM; 2222 2223 ret = devfreq_register_notifier(devfreq, nb, list); 2224 if (ret) { 2225 devres_free(ptr); 2226 return ret; 2227 } 2228 2229 ptr->devfreq = devfreq; 2230 ptr->nb = nb; 2231 ptr->list = list; 2232 devres_add(dev, ptr); 2233 2234 return 0; 2235 } 2236 EXPORT_SYMBOL(devm_devfreq_register_notifier); 2237 2238 /** 2239 * devm_devfreq_unregister_notifier() 2240 * - Resource-managed devfreq_unregister_notifier() 2241 * @dev: The devfreq user device. (parent of devfreq) 2242 * @devfreq: The devfreq object. 2243 * @nb: The notifier block to be unregistered. 2244 * @list: DEVFREQ_TRANSITION_NOTIFIER. 2245 */ 2246 void devm_devfreq_unregister_notifier(struct device *dev, 2247 struct devfreq *devfreq, 2248 struct notifier_block *nb, 2249 unsigned int list) 2250 { 2251 WARN_ON(devres_release(dev, devm_devfreq_notifier_release, 2252 devm_devfreq_dev_match, devfreq)); 2253 } 2254 EXPORT_SYMBOL(devm_devfreq_unregister_notifier); 2255