| .. | .. |
|---|
| 4 | 4 | * Author: Finley Xiao <finley.xiao@rock-chips.com> |
|---|
| 5 | 5 | */ |
|---|
| 6 | 6 | |
|---|
| 7 | | -#include <dt-bindings/soc/rockchip-system-status.h> |
|---|
| 7 | +#include <linux/clk-provider.h> |
|---|
| 8 | 8 | #include <linux/cpu.h> |
|---|
| 9 | 9 | #include <linux/cpufreq.h> |
|---|
| 10 | +#include <linux/devfreq.h> |
|---|
| 10 | 11 | #include <linux/device.h> |
|---|
| 11 | 12 | #include <linux/fb.h> |
|---|
| 12 | 13 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 15 | 16 | #include <linux/of.h> |
|---|
| 16 | 17 | #include <linux/platform_device.h> |
|---|
| 17 | 18 | #include <linux/pm_opp.h> |
|---|
| 18 | | -#include <linux/uaccess.h> |
|---|
| 19 | +#include <linux/pm_qos.h> |
|---|
| 20 | +#include <linux/pm_runtime.h> |
|---|
| 21 | +#include <linux/regulator/consumer.h> |
|---|
| 22 | +#include <linux/regulator/coupler.h> |
|---|
| 23 | +#include <linux/regulator/driver.h> |
|---|
| 24 | +#include <linux/regulator/machine.h> |
|---|
| 19 | 25 | #include <linux/reboot.h> |
|---|
| 26 | +#include <linux/rockchip/rockchip_sip.h> |
|---|
| 20 | 27 | #include <linux/slab.h> |
|---|
| 21 | 28 | #include <linux/suspend.h> |
|---|
| 22 | 29 | #include <linux/thermal.h> |
|---|
| 30 | +#include <linux/uaccess.h> |
|---|
| 23 | 31 | #include <linux/version.h> |
|---|
| 32 | +#include <linux/delay.h> |
|---|
| 33 | +#include <soc/rockchip/rockchip_opp_select.h> |
|---|
| 24 | 34 | #include <soc/rockchip/rockchip_system_monitor.h> |
|---|
| 25 | 35 | #include <soc/rockchip/rockchip-system-status.h> |
|---|
| 26 | 36 | |
|---|
| 27 | | -#include "../../opp/opp.h" |
|---|
| 28 | | -#include "../../devfreq/governor.h" |
|---|
| 29 | | - |
|---|
| 30 | 37 | #include "../../gpu/drm/rockchip/ebc-dev/ebc_dev.h" |
|---|
| 38 | +#include "../../opp/opp.h" |
|---|
| 39 | +#include "../../regulator/internal.h" |
|---|
| 40 | +#include "../../thermal/thermal_core.h" |
|---|
| 31 | 41 | |
|---|
| 32 | 42 | #define CPU_REBOOT_FREQ 816000 /* kHz */ |
|---|
| 33 | 43 | #define VIDEO_1080P_SIZE (1920 * 1080) |
|---|
| 34 | 44 | #define THERMAL_POLLING_DELAY 200 /* milliseconds */ |
|---|
| 35 | | - |
|---|
| 36 | | -#define devfreq_nb_to_monitor(nb) container_of(nb, struct monitor_dev_info, \ |
|---|
| 37 | | - devfreq_nb) |
|---|
| 38 | 45 | |
|---|
| 39 | 46 | struct video_info { |
|---|
| 40 | 47 | unsigned int width; |
|---|
| .. | .. |
|---|
| 64 | 71 | |
|---|
| 65 | 72 | struct thermal_zone_device *tz; |
|---|
| 66 | 73 | struct delayed_work thermal_work; |
|---|
| 74 | + int last_temp; |
|---|
| 67 | 75 | int offline_cpus_temp; |
|---|
| 68 | 76 | int temp_hysteresis; |
|---|
| 69 | 77 | unsigned int delay; |
|---|
| .. | .. |
|---|
| 84 | 92 | static struct system_monitor *system_monitor; |
|---|
| 85 | 93 | static atomic_t monitor_in_suspend; |
|---|
| 86 | 94 | |
|---|
| 95 | +static BLOCKING_NOTIFIER_HEAD(system_monitor_notifier_list); |
|---|
| 87 | 96 | static BLOCKING_NOTIFIER_HEAD(system_status_notifier_list); |
|---|
| 88 | 97 | |
|---|
| 89 | 98 | int rockchip_register_system_status_notifier(struct notifier_block *nb) |
|---|
| .. | .. |
|---|
| 173 | 182 | return -EINVAL; |
|---|
| 174 | 183 | } |
|---|
| 175 | 184 | |
|---|
| 176 | | - return __compat_only_sysfs_link_entry_to_kobj(&dev->kobj, |
|---|
| 177 | | - system_monitor->kobj, |
|---|
| 178 | | - "system_status"); |
|---|
| 185 | + return compat_only_sysfs_link_entry_to_kobj(&dev->kobj, |
|---|
| 186 | + system_monitor->kobj, |
|---|
| 187 | + "system_status", NULL); |
|---|
| 179 | 188 | } |
|---|
| 180 | 189 | EXPORT_SYMBOL(rockchip_add_system_status_interface); |
|---|
| 181 | 190 | |
|---|
| .. | .. |
|---|
| 237 | 246 | return video_info; |
|---|
| 238 | 247 | } |
|---|
| 239 | 248 | |
|---|
| 240 | | -static struct video_info *rockchip_find_video_info(const char *buf) |
|---|
| 241 | | -{ |
|---|
| 242 | | - struct video_info *info, *video_info; |
|---|
| 243 | | - |
|---|
| 244 | | - video_info = rockchip_parse_video_info(buf); |
|---|
| 245 | | - |
|---|
| 246 | | - if (!video_info) |
|---|
| 247 | | - return NULL; |
|---|
| 248 | | - |
|---|
| 249 | | - mutex_lock(&video_info_mutex); |
|---|
| 250 | | - list_for_each_entry(info, &video_info_list, node) { |
|---|
| 251 | | - if (info->width == video_info->width && |
|---|
| 252 | | - info->height == video_info->height && |
|---|
| 253 | | - info->ishevc == video_info->ishevc && |
|---|
| 254 | | - info->videoFramerate == video_info->videoFramerate && |
|---|
| 255 | | - info->streamBitrate == video_info->streamBitrate) { |
|---|
| 256 | | - mutex_unlock(&video_info_mutex); |
|---|
| 257 | | - kfree(video_info); |
|---|
| 258 | | - return info; |
|---|
| 259 | | - } |
|---|
| 260 | | - } |
|---|
| 261 | | - |
|---|
| 262 | | - mutex_unlock(&video_info_mutex); |
|---|
| 263 | | - kfree(video_info); |
|---|
| 264 | | - |
|---|
| 265 | | - return NULL; |
|---|
| 266 | | -} |
|---|
| 267 | | - |
|---|
| 268 | 249 | static void rockchip_add_video_info(struct video_info *video_info) |
|---|
| 269 | 250 | { |
|---|
| 270 | 251 | if (video_info) { |
|---|
| .. | .. |
|---|
| 276 | 257 | |
|---|
| 277 | 258 | static void rockchip_del_video_info(struct video_info *video_info) |
|---|
| 278 | 259 | { |
|---|
| 279 | | - if (video_info) { |
|---|
| 280 | | - mutex_lock(&video_info_mutex); |
|---|
| 281 | | - list_del(&video_info->node); |
|---|
| 282 | | - mutex_unlock(&video_info_mutex); |
|---|
| 283 | | - kfree(video_info); |
|---|
| 260 | + struct video_info *info, *tmp; |
|---|
| 261 | + |
|---|
| 262 | + if (!video_info) |
|---|
| 263 | + return; |
|---|
| 264 | + |
|---|
| 265 | + mutex_lock(&video_info_mutex); |
|---|
| 266 | + list_for_each_entry_safe(info, tmp, &video_info_list, node) { |
|---|
| 267 | + if (info->width == video_info->width && |
|---|
| 268 | + info->height == video_info->height && |
|---|
| 269 | + info->ishevc == video_info->ishevc && |
|---|
| 270 | + info->videoFramerate == video_info->videoFramerate && |
|---|
| 271 | + info->streamBitrate == video_info->streamBitrate) { |
|---|
| 272 | + list_del(&info->node); |
|---|
| 273 | + kfree(info); |
|---|
| 274 | + break; |
|---|
| 275 | + } |
|---|
| 284 | 276 | } |
|---|
| 277 | + kfree(video_info); |
|---|
| 278 | + mutex_unlock(&video_info_mutex); |
|---|
| 285 | 279 | } |
|---|
| 286 | 280 | |
|---|
| 287 | 281 | static void rockchip_update_video_info(void) |
|---|
| 288 | 282 | { |
|---|
| 289 | 283 | struct video_info *video_info; |
|---|
| 290 | 284 | unsigned int max_res = 0, max_stream_bitrate = 0, res = 0; |
|---|
| 285 | + unsigned int max_video_framerate = 0; |
|---|
| 291 | 286 | |
|---|
| 292 | 287 | mutex_lock(&video_info_mutex); |
|---|
| 293 | 288 | if (list_empty(&video_info_list)) { |
|---|
| .. | .. |
|---|
| 302 | 297 | max_res = res; |
|---|
| 303 | 298 | if (video_info->streamBitrate > max_stream_bitrate) |
|---|
| 304 | 299 | max_stream_bitrate = video_info->streamBitrate; |
|---|
| 300 | + if (video_info->videoFramerate > max_video_framerate) |
|---|
| 301 | + max_video_framerate = video_info->videoFramerate; |
|---|
| 305 | 302 | } |
|---|
| 306 | 303 | mutex_unlock(&video_info_mutex); |
|---|
| 307 | 304 | |
|---|
| .. | .. |
|---|
| 310 | 307 | } else { |
|---|
| 311 | 308 | if (max_stream_bitrate == 10) |
|---|
| 312 | 309 | rockchip_set_system_status(SYS_STATUS_VIDEO_4K_10B); |
|---|
| 313 | | - else |
|---|
| 314 | | - rockchip_set_system_status(SYS_STATUS_VIDEO_4K); |
|---|
| 310 | + if (max_video_framerate == 60) |
|---|
| 311 | + rockchip_set_system_status(SYS_STATUS_VIDEO_4K_60P); |
|---|
| 312 | + rockchip_set_system_status(SYS_STATUS_VIDEO_4K); |
|---|
| 315 | 313 | } |
|---|
| 316 | 314 | } |
|---|
| 317 | 315 | |
|---|
| .. | .. |
|---|
| 325 | 323 | switch (buf[0]) { |
|---|
| 326 | 324 | case '0': |
|---|
| 327 | 325 | /* clear video flag */ |
|---|
| 328 | | - video_info = rockchip_find_video_info(buf); |
|---|
| 326 | + video_info = rockchip_parse_video_info(buf); |
|---|
| 329 | 327 | if (video_info) { |
|---|
| 330 | 328 | rockchip_del_video_info(video_info); |
|---|
| 331 | 329 | rockchip_update_video_info(); |
|---|
| .. | .. |
|---|
| 354 | 352 | case 'n': |
|---|
| 355 | 353 | /* clear performance flag */ |
|---|
| 356 | 354 | rockchip_clear_system_status(SYS_STATUS_PERFORMANCE); |
|---|
| 355 | + break; |
|---|
| 356 | + case 'S': |
|---|
| 357 | + /* set video svep flag */ |
|---|
| 358 | + rockchip_set_system_status(SYS_STATUS_VIDEO_SVEP); |
|---|
| 359 | + break; |
|---|
| 360 | + case 's': |
|---|
| 361 | + /* clear video svep flag */ |
|---|
| 362 | + rockchip_clear_system_status(SYS_STATUS_VIDEO_SVEP); |
|---|
| 357 | 363 | break; |
|---|
| 358 | 364 | default: |
|---|
| 359 | 365 | break; |
|---|
| .. | .. |
|---|
| 492 | 498 | int delta_volt = 0; |
|---|
| 493 | 499 | int i = 0, max_count; |
|---|
| 494 | 500 | unsigned long low_limit = 0, high_limit = 0; |
|---|
| 501 | + unsigned long low_limit_mem = 0, high_limit_mem = 0; |
|---|
| 495 | 502 | bool reach_max_volt = false; |
|---|
| 503 | + bool reach_max_mem_volt = false; |
|---|
| 496 | 504 | bool reach_high_temp_max_volt = false; |
|---|
| 505 | + bool reach_high_temp_max_mem_volt = false; |
|---|
| 497 | 506 | |
|---|
| 498 | 507 | max_count = dev_pm_opp_get_opp_count(dev); |
|---|
| 499 | 508 | if (max_count <= 0) |
|---|
| .. | .. |
|---|
| 511 | 520 | } |
|---|
| 512 | 521 | mutex_lock(&opp_table->lock); |
|---|
| 513 | 522 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
|---|
| 523 | + if (!opp->available) |
|---|
| 524 | + continue; |
|---|
| 514 | 525 | info->opp_table[i].rate = opp->rate; |
|---|
| 515 | 526 | info->opp_table[i].volt = opp->supplies[0].u_volt; |
|---|
| 516 | 527 | info->opp_table[i].max_volt = opp->supplies[0].u_volt_max; |
|---|
| .. | .. |
|---|
| 542 | 553 | info->low_limit = low_limit; |
|---|
| 543 | 554 | if (high_limit && high_limit != opp->rate) |
|---|
| 544 | 555 | info->high_limit = high_limit; |
|---|
| 545 | | - dev_dbg(dev, "rate=%lu, volt=%lu, low_temp_volt=%lu\n", |
|---|
| 556 | + |
|---|
| 557 | + if (opp_table->regulator_count > 1) { |
|---|
| 558 | + info->opp_table[i].mem_volt = opp->supplies[1].u_volt; |
|---|
| 559 | + info->opp_table[i].max_mem_volt = opp->supplies[1].u_volt_max; |
|---|
| 560 | + |
|---|
| 561 | + if (opp->supplies[1].u_volt <= info->high_temp_max_volt) { |
|---|
| 562 | + if (!reach_high_temp_max_mem_volt) |
|---|
| 563 | + high_limit_mem = opp->rate; |
|---|
| 564 | + if (opp->supplies[1].u_volt == info->high_temp_max_volt) |
|---|
| 565 | + reach_high_temp_max_mem_volt = true; |
|---|
| 566 | + } |
|---|
| 567 | + |
|---|
| 568 | + if ((opp->supplies[1].u_volt + delta_volt) <= info->max_volt) { |
|---|
| 569 | + info->opp_table[i].low_temp_mem_volt = |
|---|
| 570 | + opp->supplies[1].u_volt + delta_volt; |
|---|
| 571 | + if (info->opp_table[i].low_temp_mem_volt < |
|---|
| 572 | + info->low_temp_min_volt) |
|---|
| 573 | + info->opp_table[i].low_temp_mem_volt = |
|---|
| 574 | + info->low_temp_min_volt; |
|---|
| 575 | + if (!reach_max_mem_volt) |
|---|
| 576 | + low_limit_mem = opp->rate; |
|---|
| 577 | + if (info->opp_table[i].low_temp_mem_volt == info->max_volt) |
|---|
| 578 | + reach_max_mem_volt = true; |
|---|
| 579 | + } else { |
|---|
| 580 | + info->opp_table[i].low_temp_mem_volt = info->max_volt; |
|---|
| 581 | + } |
|---|
| 582 | + |
|---|
| 583 | + if (low_limit_mem && low_limit_mem != opp->rate) { |
|---|
| 584 | + if (info->low_limit > low_limit_mem) |
|---|
| 585 | + info->low_limit = low_limit_mem; |
|---|
| 586 | + } |
|---|
| 587 | + if (high_limit_mem && high_limit_mem != opp->rate) { |
|---|
| 588 | + if (info->high_limit > high_limit_mem) |
|---|
| 589 | + info->high_limit = high_limit_mem; |
|---|
| 590 | + } |
|---|
| 591 | + } |
|---|
| 592 | + |
|---|
| 593 | + dev_dbg(dev, "rate=%lu, volt=%lu %lu low_temp_volt=%lu %lu\n", |
|---|
| 546 | 594 | info->opp_table[i].rate, info->opp_table[i].volt, |
|---|
| 547 | | - info->opp_table[i].low_temp_volt); |
|---|
| 595 | + info->opp_table[i].mem_volt, |
|---|
| 596 | + info->opp_table[i].low_temp_volt, |
|---|
| 597 | + info->opp_table[i].low_temp_mem_volt); |
|---|
| 548 | 598 | i++; |
|---|
| 549 | 599 | } |
|---|
| 550 | 600 | mutex_unlock(&opp_table->lock); |
|---|
| .. | .. |
|---|
| 586 | 636 | else |
|---|
| 587 | 637 | info->high_temp_max_volt = value; |
|---|
| 588 | 638 | rockchip_init_temp_opp_table(info); |
|---|
| 589 | | - if (rockchip_get_temp_freq_table(np, "rockchip,high-temp-limit-table", |
|---|
| 590 | | - &info->high_limit_table)) { |
|---|
| 591 | | - info->high_limit_table = NULL; |
|---|
| 639 | + rockchip_get_temp_freq_table(np, "rockchip,temp-freq-table", |
|---|
| 640 | + &info->high_limit_table); |
|---|
| 641 | + if (!info->high_limit_table) |
|---|
| 642 | + rockchip_get_temp_freq_table(np, "rockchip,high-temp-limit-table", |
|---|
| 643 | + &info->high_limit_table); |
|---|
| 644 | + if (!info->high_limit_table) { |
|---|
| 592 | 645 | if (!of_property_read_u32(np, "rockchip,high-temp-max-freq", |
|---|
| 593 | 646 | &value)) { |
|---|
| 594 | 647 | high_temp_max_freq = value * 1000; |
|---|
| .. | .. |
|---|
| 640 | 693 | &info->video_4k_freq); |
|---|
| 641 | 694 | ret &= of_property_read_u32(np, "rockchip,reboot-freq", |
|---|
| 642 | 695 | &info->reboot_freq); |
|---|
| 643 | | - if (info->devp->type == MONITOR_TPYE_CPU) { |
|---|
| 696 | + if (info->devp->type == MONITOR_TYPE_CPU) { |
|---|
| 644 | 697 | if (!info->reboot_freq) { |
|---|
| 645 | 698 | info->reboot_freq = CPU_REBOOT_FREQ; |
|---|
| 646 | 699 | ret = 0; |
|---|
| .. | .. |
|---|
| 650 | 703 | return ret; |
|---|
| 651 | 704 | } |
|---|
| 652 | 705 | |
|---|
| 706 | +static int monitor_device_parse_early_min_volt(struct device_node *np, |
|---|
| 707 | + struct monitor_dev_info *info) |
|---|
| 708 | +{ |
|---|
| 709 | + return of_property_read_u32(np, "rockchip,early-min-microvolt", |
|---|
| 710 | + &info->early_min_volt); |
|---|
| 711 | +} |
|---|
| 712 | + |
|---|
| 713 | +static int monitor_device_parse_read_margin(struct device_node *np, |
|---|
| 714 | + struct monitor_dev_info *info) |
|---|
| 715 | +{ |
|---|
| 716 | + if (of_property_read_bool(np, "volt-mem-read-margin")) |
|---|
| 717 | + return 0; |
|---|
| 718 | + return -EINVAL; |
|---|
| 719 | +} |
|---|
| 720 | + |
|---|
| 721 | +static int monitor_device_parse_scmi_clk(struct device_node *np, |
|---|
| 722 | + struct monitor_dev_info *info) |
|---|
| 723 | +{ |
|---|
| 724 | + struct clk *clk; |
|---|
| 725 | + |
|---|
| 726 | + clk = clk_get(info->dev, NULL); |
|---|
| 727 | + if (strstr(__clk_get_name(clk), "scmi")) |
|---|
| 728 | + return 0; |
|---|
| 729 | + return -EINVAL; |
|---|
| 730 | +} |
|---|
| 731 | + |
|---|
| 653 | 732 | static int monitor_device_parse_dt(struct device *dev, |
|---|
| 654 | 733 | struct monitor_dev_info *info) |
|---|
| 655 | 734 | { |
|---|
| 656 | 735 | struct device_node *np; |
|---|
| 657 | | - int ret = 0; |
|---|
| 658 | | - bool is_wide_temp_en = false; |
|---|
| 659 | | - bool is_status_limit_en = false; |
|---|
| 660 | | - bool is_temp_freq_en = false; |
|---|
| 736 | + int ret; |
|---|
| 661 | 737 | |
|---|
| 662 | 738 | np = of_parse_phandle(dev->of_node, "operating-points-v2", 0); |
|---|
| 663 | 739 | if (!np) |
|---|
| 664 | 740 | return -EINVAL; |
|---|
| 665 | 741 | |
|---|
| 666 | | - if (!monitor_device_parse_wide_temp_config(np, info)) |
|---|
| 667 | | - is_wide_temp_en = true; |
|---|
| 668 | | - if (!monitor_device_parse_status_config(np, info)) |
|---|
| 669 | | - is_status_limit_en = true; |
|---|
| 670 | | - if (!rockchip_get_temp_freq_table(np, "rockchip,temp-freq-table", |
|---|
| 671 | | - &info->temp_freq_table)) |
|---|
| 672 | | - is_temp_freq_en = true; |
|---|
| 673 | | - if (is_wide_temp_en || is_status_limit_en || is_temp_freq_en) |
|---|
| 674 | | - ret = 0; |
|---|
| 675 | | - else |
|---|
| 676 | | - ret = -EINVAL; |
|---|
| 742 | + of_property_read_u32(np, "rockchip,init-freq", &info->init_freq); |
|---|
| 743 | + |
|---|
| 744 | + ret = monitor_device_parse_wide_temp_config(np, info); |
|---|
| 745 | + ret &= monitor_device_parse_status_config(np, info); |
|---|
| 746 | + ret &= monitor_device_parse_early_min_volt(np, info); |
|---|
| 747 | + ret &= monitor_device_parse_read_margin(np, info); |
|---|
| 748 | + ret &= monitor_device_parse_scmi_clk(np, info); |
|---|
| 677 | 749 | |
|---|
| 678 | 750 | of_node_put(np); |
|---|
| 679 | 751 | |
|---|
| 680 | 752 | return ret; |
|---|
| 681 | 753 | } |
|---|
| 682 | 754 | |
|---|
| 683 | | -int rockchip_monitor_opp_set_rate(struct monitor_dev_info *info, |
|---|
| 684 | | - unsigned long target_freq) |
|---|
| 685 | | -{ |
|---|
| 686 | | - int ret = 0; |
|---|
| 687 | | - |
|---|
| 688 | | - mutex_lock(&info->volt_adjust_mutex); |
|---|
| 689 | | - ret = dev_pm_opp_set_rate(info->dev, target_freq); |
|---|
| 690 | | - mutex_unlock(&info->volt_adjust_mutex); |
|---|
| 691 | | - |
|---|
| 692 | | - return ret; |
|---|
| 693 | | -} |
|---|
| 694 | | -EXPORT_SYMBOL(rockchip_monitor_opp_set_rate); |
|---|
| 695 | | - |
|---|
| 696 | 755 | int rockchip_monitor_cpu_low_temp_adjust(struct monitor_dev_info *info, |
|---|
| 697 | 756 | bool is_low) |
|---|
| 698 | 757 | { |
|---|
| 699 | | - struct device *dev = info->dev; |
|---|
| 700 | | - unsigned int cpu = cpumask_any(&info->devp->allowed_cpus); |
|---|
| 758 | + if (!info->low_limit) |
|---|
| 759 | + return 0; |
|---|
| 701 | 760 | |
|---|
| 702 | | - if (info->low_limit) { |
|---|
| 703 | | - if (is_low) |
|---|
| 704 | | - info->wide_temp_limit = info->low_limit; |
|---|
| 705 | | - else |
|---|
| 706 | | - info->wide_temp_limit = 0; |
|---|
| 707 | | - cpufreq_update_policy(cpu); |
|---|
| 708 | | - } |
|---|
| 761 | + if (!freq_qos_request_active(&info->max_temp_freq_req)) |
|---|
| 762 | + return 0; |
|---|
| 709 | 763 | |
|---|
| 710 | | - mutex_lock(&info->volt_adjust_mutex); |
|---|
| 711 | | - dev_pm_opp_check_rate_volt(dev, false); |
|---|
| 712 | | - mutex_unlock(&info->volt_adjust_mutex); |
|---|
| 764 | + if (is_low) |
|---|
| 765 | + freq_qos_update_request(&info->max_temp_freq_req, |
|---|
| 766 | + info->low_limit / 1000); |
|---|
| 767 | + else |
|---|
| 768 | + freq_qos_update_request(&info->max_temp_freq_req, |
|---|
| 769 | + FREQ_QOS_MAX_DEFAULT_VALUE); |
|---|
| 713 | 770 | |
|---|
| 714 | 771 | return 0; |
|---|
| 715 | 772 | } |
|---|
| .. | .. |
|---|
| 718 | 775 | int rockchip_monitor_cpu_high_temp_adjust(struct monitor_dev_info *info, |
|---|
| 719 | 776 | bool is_high) |
|---|
| 720 | 777 | { |
|---|
| 721 | | - unsigned int cpu = cpumask_any(&info->devp->allowed_cpus); |
|---|
| 778 | + if (!info->high_limit) |
|---|
| 779 | + return 0; |
|---|
| 780 | + |
|---|
| 781 | + if (!freq_qos_request_active(&info->max_temp_freq_req)) |
|---|
| 782 | + return 0; |
|---|
| 722 | 783 | |
|---|
| 723 | 784 | if (info->high_limit_table) { |
|---|
| 724 | | - info->wide_temp_limit = info->high_limit; |
|---|
| 725 | | - cpufreq_update_policy(cpu); |
|---|
| 726 | | - } else { |
|---|
| 727 | | - if (info->high_limit) { |
|---|
| 728 | | - if (is_high) |
|---|
| 729 | | - info->wide_temp_limit = info->high_limit; |
|---|
| 730 | | - else |
|---|
| 731 | | - info->wide_temp_limit = 0; |
|---|
| 732 | | - cpufreq_update_policy(cpu); |
|---|
| 733 | | - } |
|---|
| 785 | + freq_qos_update_request(&info->max_temp_freq_req, |
|---|
| 786 | + info->high_limit / 1000); |
|---|
| 787 | + return 0; |
|---|
| 734 | 788 | } |
|---|
| 789 | + |
|---|
| 790 | + if (is_high) |
|---|
| 791 | + freq_qos_update_request(&info->max_temp_freq_req, |
|---|
| 792 | + info->high_limit / 1000); |
|---|
| 793 | + else |
|---|
| 794 | + freq_qos_update_request(&info->max_temp_freq_req, |
|---|
| 795 | + FREQ_QOS_MAX_DEFAULT_VALUE); |
|---|
| 735 | 796 | |
|---|
| 736 | 797 | return 0; |
|---|
| 737 | 798 | } |
|---|
| 738 | 799 | EXPORT_SYMBOL(rockchip_monitor_cpu_high_temp_adjust); |
|---|
| 739 | 800 | |
|---|
| 740 | | -static int rockchip_monitor_update_devfreq(struct devfreq *df) |
|---|
| 741 | | -{ |
|---|
| 742 | | - int ret = 0; |
|---|
| 743 | | - |
|---|
| 744 | | -#ifdef CONFIG_PM_DEVFREQ |
|---|
| 745 | | - mutex_lock(&df->lock); |
|---|
| 746 | | - ret = update_devfreq(df); |
|---|
| 747 | | - mutex_unlock(&df->lock); |
|---|
| 748 | | -#endif |
|---|
| 749 | | - return ret; |
|---|
| 750 | | -}; |
|---|
| 751 | | - |
|---|
| 752 | 801 | int rockchip_monitor_dev_low_temp_adjust(struct monitor_dev_info *info, |
|---|
| 753 | 802 | bool is_low) |
|---|
| 754 | 803 | { |
|---|
| 755 | | - struct devfreq *df; |
|---|
| 804 | + if (!dev_pm_qos_request_active(&info->dev_max_freq_req)) |
|---|
| 805 | + return 0; |
|---|
| 756 | 806 | |
|---|
| 757 | | - if (info->low_limit) { |
|---|
| 758 | | - if (is_low) |
|---|
| 759 | | - info->wide_temp_limit = info->low_limit; |
|---|
| 760 | | - else |
|---|
| 761 | | - info->wide_temp_limit = 0; |
|---|
| 762 | | - } |
|---|
| 807 | + if (!info->low_limit) |
|---|
| 808 | + return 0; |
|---|
| 763 | 809 | |
|---|
| 764 | | - if (info->devp && info->devp->data) { |
|---|
| 765 | | - df = (struct devfreq *)info->devp->data; |
|---|
| 766 | | - rockchip_monitor_update_devfreq(df); |
|---|
| 767 | | - } |
|---|
| 810 | + if (is_low) |
|---|
| 811 | + dev_pm_qos_update_request(&info->dev_max_freq_req, |
|---|
| 812 | + info->low_limit / 1000); |
|---|
| 813 | + else |
|---|
| 814 | + dev_pm_qos_update_request(&info->dev_max_freq_req, |
|---|
| 815 | + PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE); |
|---|
| 768 | 816 | |
|---|
| 769 | 817 | return 0; |
|---|
| 770 | 818 | } |
|---|
| .. | .. |
|---|
| 773 | 821 | int rockchip_monitor_dev_high_temp_adjust(struct monitor_dev_info *info, |
|---|
| 774 | 822 | bool is_high) |
|---|
| 775 | 823 | { |
|---|
| 776 | | - struct devfreq *df; |
|---|
| 824 | + if (!dev_pm_qos_request_active(&info->dev_max_freq_req)) |
|---|
| 825 | + return 0; |
|---|
| 826 | + |
|---|
| 827 | + if (!info->high_limit) |
|---|
| 828 | + return 0; |
|---|
| 777 | 829 | |
|---|
| 778 | 830 | if (info->high_limit_table) { |
|---|
| 779 | | - info->wide_temp_limit = info->high_limit; |
|---|
| 780 | | - if (info->devp && info->devp->data) { |
|---|
| 781 | | - df = (struct devfreq *)info->devp->data; |
|---|
| 782 | | - rockchip_monitor_update_devfreq(df); |
|---|
| 783 | | - } |
|---|
| 784 | | - } else { |
|---|
| 785 | | - if (info->high_limit) { |
|---|
| 786 | | - if (is_high) |
|---|
| 787 | | - info->wide_temp_limit = info->high_limit; |
|---|
| 788 | | - else |
|---|
| 789 | | - info->wide_temp_limit = 0; |
|---|
| 790 | | - if (info->devp && info->devp->data) { |
|---|
| 791 | | - df = (struct devfreq *)info->devp->data; |
|---|
| 792 | | - rockchip_monitor_update_devfreq(df); |
|---|
| 793 | | - } |
|---|
| 794 | | - } |
|---|
| 831 | + dev_pm_qos_update_request(&info->dev_max_freq_req, |
|---|
| 832 | + info->high_limit / 1000); |
|---|
| 833 | + return 0; |
|---|
| 795 | 834 | } |
|---|
| 835 | + |
|---|
| 836 | + if (is_high) |
|---|
| 837 | + dev_pm_qos_update_request(&info->dev_max_freq_req, |
|---|
| 838 | + info->high_limit / 1000); |
|---|
| 839 | + else |
|---|
| 840 | + dev_pm_qos_update_request(&info->dev_max_freq_req, |
|---|
| 841 | + PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE); |
|---|
| 796 | 842 | |
|---|
| 797 | 843 | return 0; |
|---|
| 798 | 844 | } |
|---|
| .. | .. |
|---|
| 812 | 858 | |
|---|
| 813 | 859 | mutex_lock(&opp_table->lock); |
|---|
| 814 | 860 | list_for_each_entry(opp, &opp_table->opp_list, node) { |
|---|
| 861 | + if (!opp->available) |
|---|
| 862 | + continue; |
|---|
| 815 | 863 | if (is_low_temp) { |
|---|
| 816 | 864 | if (opp->supplies[0].u_volt_max < |
|---|
| 817 | 865 | info->opp_table[i].low_temp_volt) |
|---|
| .. | .. |
|---|
| 820 | 868 | opp->supplies[0].u_volt = |
|---|
| 821 | 869 | info->opp_table[i].low_temp_volt; |
|---|
| 822 | 870 | opp->supplies[0].u_volt_min = opp->supplies[0].u_volt; |
|---|
| 871 | + if (opp_table->regulator_count > 1) { |
|---|
| 872 | + if (opp->supplies[1].u_volt_max < |
|---|
| 873 | + info->opp_table[i].low_temp_mem_volt) |
|---|
| 874 | + opp->supplies[1].u_volt_max = |
|---|
| 875 | + info->opp_table[i].low_temp_mem_volt; |
|---|
| 876 | + opp->supplies[1].u_volt = |
|---|
| 877 | + info->opp_table[i].low_temp_mem_volt; |
|---|
| 878 | + opp->supplies[1].u_volt_min = |
|---|
| 879 | + opp->supplies[1].u_volt; |
|---|
| 880 | + } |
|---|
| 823 | 881 | } else { |
|---|
| 824 | 882 | opp->supplies[0].u_volt_min = info->opp_table[i].volt; |
|---|
| 825 | 883 | opp->supplies[0].u_volt = opp->supplies[0].u_volt_min; |
|---|
| 826 | 884 | opp->supplies[0].u_volt_max = |
|---|
| 827 | 885 | info->opp_table[i].max_volt; |
|---|
| 886 | + if (opp_table->regulator_count > 1) { |
|---|
| 887 | + opp->supplies[1].u_volt_min = |
|---|
| 888 | + info->opp_table[i].mem_volt; |
|---|
| 889 | + opp->supplies[1].u_volt = |
|---|
| 890 | + opp->supplies[1].u_volt_min; |
|---|
| 891 | + opp->supplies[1].u_volt_max = |
|---|
| 892 | + info->opp_table[i].max_mem_volt; |
|---|
| 893 | + } |
|---|
| 828 | 894 | } |
|---|
| 829 | 895 | i++; |
|---|
| 830 | 896 | } |
|---|
| .. | .. |
|---|
| 839 | 905 | bool is_low) |
|---|
| 840 | 906 | { |
|---|
| 841 | 907 | struct monitor_dev_profile *devp = info->devp; |
|---|
| 908 | + struct arm_smccc_res res; |
|---|
| 842 | 909 | int ret = 0; |
|---|
| 843 | 910 | |
|---|
| 844 | 911 | dev_dbg(info->dev, "low_temp %d\n", is_low); |
|---|
| .. | .. |
|---|
| 850 | 917 | ret = devp->low_temp_adjust(info, is_low); |
|---|
| 851 | 918 | if (!ret) |
|---|
| 852 | 919 | info->is_low_temp = is_low; |
|---|
| 920 | + |
|---|
| 921 | + if (devp->update_volt) |
|---|
| 922 | + devp->update_volt(info); |
|---|
| 923 | + |
|---|
| 924 | + if (devp->opp_info && devp->opp_info->pvtpll_low_temp) { |
|---|
| 925 | + res = sip_smc_pvtpll_config(PVTPLL_LOW_TEMP, |
|---|
| 926 | + devp->opp_info->pvtpll_clk_id, |
|---|
| 927 | + is_low, 0, 0, 0, 0); |
|---|
| 928 | + if (res.a0) |
|---|
| 929 | + dev_err(info->dev, |
|---|
| 930 | + "%s: error cfg id=%u low temp %d (%d)\n", |
|---|
| 931 | + __func__, devp->opp_info->pvtpll_clk_id, |
|---|
| 932 | + is_low, (int)res.a0); |
|---|
| 933 | + } |
|---|
| 853 | 934 | } |
|---|
| 854 | 935 | |
|---|
| 855 | 936 | static void rockchip_high_temp_adjust(struct monitor_dev_info *info, |
|---|
| .. | .. |
|---|
| 858 | 939 | struct monitor_dev_profile *devp = info->devp; |
|---|
| 859 | 940 | int ret = 0; |
|---|
| 860 | 941 | |
|---|
| 942 | + if (!devp->high_temp_adjust) |
|---|
| 943 | + return; |
|---|
| 944 | + |
|---|
| 861 | 945 | if (info->high_limit_table) { |
|---|
| 862 | 946 | devp->high_temp_adjust(info, is_high); |
|---|
| 863 | 947 | } else { |
|---|
| 864 | 948 | dev_dbg(info->dev, "high_temp %d\n", is_high); |
|---|
| 865 | | - if (devp->high_temp_adjust) |
|---|
| 866 | | - ret = devp->high_temp_adjust(info, is_high); |
|---|
| 949 | + ret = devp->high_temp_adjust(info, is_high); |
|---|
| 867 | 950 | if (!ret) |
|---|
| 868 | 951 | info->is_high_temp = is_high; |
|---|
| 869 | 952 | } |
|---|
| .. | .. |
|---|
| 874 | 957 | struct monitor_dev_info *info = NULL, *tmp; |
|---|
| 875 | 958 | |
|---|
| 876 | 959 | list_for_each_entry(tmp, &monitor_dev_list, node) { |
|---|
| 877 | | - if (tmp->devp->type != MONITOR_TPYE_CPU) |
|---|
| 960 | + if (tmp->devp->type != MONITOR_TYPE_CPU) |
|---|
| 878 | 961 | continue; |
|---|
| 879 | 962 | if (cpumask_test_cpu(cpu, &tmp->devp->allowed_cpus)) { |
|---|
| 880 | 963 | info = tmp; |
|---|
| .. | .. |
|---|
| 941 | 1024 | { |
|---|
| 942 | 1025 | int ret, temp; |
|---|
| 943 | 1026 | |
|---|
| 1027 | + if (!info->opp_table) |
|---|
| 1028 | + return; |
|---|
| 1029 | + if (!system_monitor->tz) |
|---|
| 1030 | + return; |
|---|
| 1031 | + |
|---|
| 944 | 1032 | /* |
|---|
| 945 | 1033 | * set the init state to low temperature that the voltage will be enough |
|---|
| 946 | 1034 | * when cpu up at low temperature. |
|---|
| .. | .. |
|---|
| 948 | 1036 | if (!info->is_low_temp) { |
|---|
| 949 | 1037 | if (info->opp_table) |
|---|
| 950 | 1038 | rockchip_adjust_low_temp_opp_volt(info, true); |
|---|
| 951 | | - info->wide_temp_limit = info->low_limit; |
|---|
| 952 | 1039 | info->is_low_temp = true; |
|---|
| 953 | 1040 | } |
|---|
| 954 | 1041 | |
|---|
| .. | .. |
|---|
| 963 | 1050 | if (info->opp_table) |
|---|
| 964 | 1051 | rockchip_adjust_low_temp_opp_volt(info, false); |
|---|
| 965 | 1052 | info->is_low_temp = false; |
|---|
| 966 | | - |
|---|
| 967 | | - info->wide_temp_limit = info->high_limit; |
|---|
| 968 | 1053 | info->is_high_temp = true; |
|---|
| 969 | 1054 | } else if (temp > (info->low_temp + info->temp_hysteresis)) { |
|---|
| 970 | 1055 | if (info->opp_table) |
|---|
| 971 | 1056 | rockchip_adjust_low_temp_opp_volt(info, false); |
|---|
| 972 | 1057 | info->is_low_temp = false; |
|---|
| 973 | | - info->wide_temp_limit = 0; |
|---|
| 974 | 1058 | } |
|---|
| 975 | 1059 | } |
|---|
| 976 | 1060 | |
|---|
| 977 | | -static int system_monitor_devfreq_notifier_call(struct notifier_block *nb, |
|---|
| 978 | | - unsigned long event, |
|---|
| 979 | | - void *data) |
|---|
| 1061 | +static const char *get_rdev_name(struct regulator_dev *rdev) |
|---|
| 980 | 1062 | { |
|---|
| 981 | | - struct monitor_dev_info *info = devfreq_nb_to_monitor(nb); |
|---|
| 982 | | - struct devfreq_policy *policy = data; |
|---|
| 983 | | - |
|---|
| 984 | | - if (event != DEVFREQ_ADJUST) |
|---|
| 985 | | - return NOTIFY_DONE; |
|---|
| 986 | | - |
|---|
| 987 | | - if (info->wide_temp_limit && info->wide_temp_limit < policy->max) |
|---|
| 988 | | - devfreq_verify_within_limits(policy, 0, info->wide_temp_limit); |
|---|
| 989 | | - |
|---|
| 990 | | - return NOTIFY_OK; |
|---|
| 1063 | + if (rdev->constraints && rdev->constraints->name) |
|---|
| 1064 | + return rdev->constraints->name; |
|---|
| 1065 | + else if (rdev->desc->name) |
|---|
| 1066 | + return rdev->desc->name; |
|---|
| 1067 | + else |
|---|
| 1068 | + return ""; |
|---|
| 991 | 1069 | } |
|---|
| 992 | 1070 | |
|---|
| 993 | | -static int monitor_set_freq_table(struct device *dev, |
|---|
| 994 | | - struct monitor_dev_info *info) |
|---|
| 1071 | +static void |
|---|
| 1072 | +rockchip_system_monitor_early_regulator_init(struct monitor_dev_info *info) |
|---|
| 995 | 1073 | { |
|---|
| 996 | | - struct opp_table *opp_table; |
|---|
| 997 | | - struct dev_pm_opp *opp; |
|---|
| 998 | | - unsigned long *freq_table; |
|---|
| 999 | | - int count; |
|---|
| 1074 | + struct regulator *reg; |
|---|
| 1075 | + struct regulator_dev *rdev; |
|---|
| 1000 | 1076 | |
|---|
| 1001 | | - opp_table = dev_pm_opp_get_opp_table(dev); |
|---|
| 1002 | | - if (!opp_table) |
|---|
| 1003 | | - return -ENOMEM; |
|---|
| 1077 | + if (!info->early_min_volt || !info->regulators) |
|---|
| 1078 | + return; |
|---|
| 1004 | 1079 | |
|---|
| 1005 | | - /* Initialize the freq_table from OPP table */ |
|---|
| 1006 | | - count = dev_pm_opp_get_opp_count(dev); |
|---|
| 1007 | | - if (count <= 0) |
|---|
| 1008 | | - return -EINVAL; |
|---|
| 1009 | | - |
|---|
| 1010 | | - info->max_state = count; |
|---|
| 1011 | | - freq_table = kcalloc(count, sizeof(*info->freq_table), GFP_KERNEL); |
|---|
| 1012 | | - if (!freq_table) { |
|---|
| 1013 | | - info->max_state = 0; |
|---|
| 1014 | | - return -ENOMEM; |
|---|
| 1080 | + rdev = info->regulators[0]->rdev; |
|---|
| 1081 | + reg = regulator_get(NULL, get_rdev_name(rdev)); |
|---|
| 1082 | + if (!IS_ERR_OR_NULL(reg)) { |
|---|
| 1083 | + info->early_reg = reg; |
|---|
| 1084 | + reg->voltage[PM_SUSPEND_ON].min_uV = info->early_min_volt; |
|---|
| 1085 | + reg->voltage[PM_SUSPEND_ON].max_uV = rdev->constraints->max_uV; |
|---|
| 1015 | 1086 | } |
|---|
| 1087 | +} |
|---|
| 1016 | 1088 | |
|---|
| 1017 | | - mutex_lock(&opp_table->lock); |
|---|
| 1018 | | - list_for_each_entry(opp, &opp_table->opp_list, node) { |
|---|
| 1019 | | - if (opp->available) { |
|---|
| 1020 | | - count--; |
|---|
| 1021 | | - freq_table[count] = opp->rate; |
|---|
| 1089 | +static int |
|---|
| 1090 | +rockchip_system_monitor_freq_qos_requset(struct monitor_dev_info *info) |
|---|
| 1091 | +{ |
|---|
| 1092 | + struct devfreq *devfreq; |
|---|
| 1093 | + struct cpufreq_policy *policy; |
|---|
| 1094 | + int max_default_value = FREQ_QOS_MAX_DEFAULT_VALUE; |
|---|
| 1095 | + int ret; |
|---|
| 1096 | + |
|---|
| 1097 | + if (!info->devp->data) |
|---|
| 1098 | + return 0; |
|---|
| 1099 | + |
|---|
| 1100 | + if (info->is_low_temp && info->low_limit) |
|---|
| 1101 | + max_default_value = info->low_limit / 1000; |
|---|
| 1102 | + else if (info->is_high_temp && info->high_limit) |
|---|
| 1103 | + max_default_value = info->high_limit / 1000; |
|---|
| 1104 | + |
|---|
| 1105 | + if (info->devp->type == MONITOR_TYPE_CPU) { |
|---|
| 1106 | + policy = (struct cpufreq_policy *)info->devp->data; |
|---|
| 1107 | + ret = freq_qos_add_request(&policy->constraints, |
|---|
| 1108 | + &info->max_temp_freq_req, |
|---|
| 1109 | + FREQ_QOS_MAX, |
|---|
| 1110 | + max_default_value); |
|---|
| 1111 | + if (ret < 0) { |
|---|
| 1112 | + dev_info(info->dev, |
|---|
| 1113 | + "failed to add temp freq constraint\n"); |
|---|
| 1114 | + return ret; |
|---|
| 1115 | + } |
|---|
| 1116 | + ret = freq_qos_add_request(&policy->constraints, |
|---|
| 1117 | + &info->min_sta_freq_req, |
|---|
| 1118 | + FREQ_QOS_MIN, |
|---|
| 1119 | + FREQ_QOS_MIN_DEFAULT_VALUE); |
|---|
| 1120 | + if (ret < 0) { |
|---|
| 1121 | + dev_info(info->dev, |
|---|
| 1122 | + "failed to add sta freq constraint\n"); |
|---|
| 1123 | + freq_qos_remove_request(&info->max_temp_freq_req); |
|---|
| 1124 | + return ret; |
|---|
| 1125 | + } |
|---|
| 1126 | + ret = freq_qos_add_request(&policy->constraints, |
|---|
| 1127 | + &info->max_sta_freq_req, |
|---|
| 1128 | + FREQ_QOS_MAX, |
|---|
| 1129 | + FREQ_QOS_MAX_DEFAULT_VALUE); |
|---|
| 1130 | + if (ret < 0) { |
|---|
| 1131 | + dev_info(info->dev, |
|---|
| 1132 | + "failed to add sta freq constraint\n"); |
|---|
| 1133 | + freq_qos_remove_request(&info->max_temp_freq_req); |
|---|
| 1134 | + freq_qos_remove_request(&info->min_sta_freq_req); |
|---|
| 1135 | + return ret; |
|---|
| 1136 | + } |
|---|
| 1137 | + } else if (info->devp->type == MONITOR_TYPE_DEV) { |
|---|
| 1138 | + devfreq = (struct devfreq *)info->devp->data; |
|---|
| 1139 | + ret = dev_pm_qos_add_request(devfreq->dev.parent, |
|---|
| 1140 | + &info->dev_max_freq_req, |
|---|
| 1141 | + DEV_PM_QOS_MAX_FREQUENCY, |
|---|
| 1142 | + max_default_value); |
|---|
| 1143 | + if (ret < 0) { |
|---|
| 1144 | + dev_info(info->dev, "failed to add freq constraint\n"); |
|---|
| 1145 | + return ret; |
|---|
| 1022 | 1146 | } |
|---|
| 1023 | 1147 | } |
|---|
| 1024 | | - mutex_unlock(&opp_table->lock); |
|---|
| 1148 | + |
|---|
| 1149 | + return 0; |
|---|
| 1150 | +} |
|---|
| 1151 | + |
|---|
| 1152 | +static int rockchip_system_monitor_parse_supplies(struct device *dev, |
|---|
| 1153 | + struct monitor_dev_info *info) |
|---|
| 1154 | +{ |
|---|
| 1155 | + struct opp_table *opp_table; |
|---|
| 1156 | + struct dev_pm_set_opp_data *data; |
|---|
| 1157 | + int len, count; |
|---|
| 1158 | + |
|---|
| 1159 | + opp_table = dev_pm_opp_get_opp_table(dev); |
|---|
| 1160 | + if (IS_ERR(opp_table)) |
|---|
| 1161 | + return PTR_ERR(opp_table); |
|---|
| 1162 | + |
|---|
| 1163 | + if (opp_table->clk) |
|---|
| 1164 | + info->clk = opp_table->clk; |
|---|
| 1165 | + if (opp_table->regulators) |
|---|
| 1166 | + info->regulators = opp_table->regulators; |
|---|
| 1167 | + info->regulator_count = opp_table->regulator_count; |
|---|
| 1168 | + |
|---|
| 1169 | + if (opp_table->regulators && info->devp->set_opp) { |
|---|
| 1170 | + count = opp_table->regulator_count; |
|---|
| 1171 | + /* space for set_opp_data */ |
|---|
| 1172 | + len = sizeof(*data); |
|---|
| 1173 | + /* space for old_opp.supplies and new_opp.supplies */ |
|---|
| 1174 | + len += 2 * sizeof(struct dev_pm_opp_supply) * count; |
|---|
| 1175 | + data = kzalloc(len, GFP_KERNEL); |
|---|
| 1176 | + if (!data) |
|---|
| 1177 | + return -ENOMEM; |
|---|
| 1178 | + data->old_opp.supplies = (void *)(data + 1); |
|---|
| 1179 | + data->new_opp.supplies = data->old_opp.supplies + count; |
|---|
| 1180 | + info->set_opp_data = data; |
|---|
| 1181 | + } |
|---|
| 1025 | 1182 | |
|---|
| 1026 | 1183 | dev_pm_opp_put_opp_table(opp_table); |
|---|
| 1027 | 1184 | |
|---|
| 1028 | | - info->freq_table = freq_table; |
|---|
| 1185 | + return 0; |
|---|
| 1186 | +} |
|---|
| 1187 | + |
|---|
| 1188 | +void rockchip_monitor_volt_adjust_lock(struct monitor_dev_info *info) |
|---|
| 1189 | +{ |
|---|
| 1190 | + if (info) |
|---|
| 1191 | + mutex_lock(&info->volt_adjust_mutex); |
|---|
| 1192 | +} |
|---|
| 1193 | +EXPORT_SYMBOL(rockchip_monitor_volt_adjust_lock); |
|---|
| 1194 | + |
|---|
| 1195 | +void rockchip_monitor_volt_adjust_unlock(struct monitor_dev_info *info) |
|---|
| 1196 | +{ |
|---|
| 1197 | + if (info) |
|---|
| 1198 | + mutex_unlock(&info->volt_adjust_mutex); |
|---|
| 1199 | +} |
|---|
| 1200 | +EXPORT_SYMBOL(rockchip_monitor_volt_adjust_unlock); |
|---|
| 1201 | + |
|---|
| 1202 | +static int rockchip_monitor_enable_opp_clk(struct device *dev, |
|---|
| 1203 | + struct rockchip_opp_info *opp_info) |
|---|
| 1204 | +{ |
|---|
| 1205 | + int ret = 0; |
|---|
| 1206 | + |
|---|
| 1207 | + if (!opp_info) |
|---|
| 1208 | + return 0; |
|---|
| 1209 | + |
|---|
| 1210 | + ret = clk_bulk_prepare_enable(opp_info->num_clks, opp_info->clks); |
|---|
| 1211 | + if (ret) { |
|---|
| 1212 | + dev_err(dev, "failed to enable opp clks\n"); |
|---|
| 1213 | + return ret; |
|---|
| 1214 | + } |
|---|
| 1029 | 1215 | |
|---|
| 1030 | 1216 | return 0; |
|---|
| 1031 | 1217 | } |
|---|
| 1032 | 1218 | |
|---|
| 1033 | | -static unsigned long monitor_freq_to_state(struct monitor_dev_info *info, |
|---|
| 1034 | | - unsigned long freq) |
|---|
| 1219 | +static void rockchip_monitor_disable_opp_clk(struct device *dev, |
|---|
| 1220 | + struct rockchip_opp_info *opp_info) |
|---|
| 1035 | 1221 | { |
|---|
| 1036 | | - unsigned long i = 0; |
|---|
| 1222 | + if (!opp_info) |
|---|
| 1223 | + return; |
|---|
| 1037 | 1224 | |
|---|
| 1038 | | - if (!info->freq_table) { |
|---|
| 1039 | | - dev_info(info->dev, "failed to get freq_table"); |
|---|
| 1040 | | - return 0; |
|---|
| 1041 | | - } |
|---|
| 1042 | | - |
|---|
| 1043 | | - for (i = 0; i < info->max_state; i++) { |
|---|
| 1044 | | - if (freq >= info->freq_table[i]) |
|---|
| 1045 | | - return i; |
|---|
| 1046 | | - } |
|---|
| 1047 | | - |
|---|
| 1048 | | - return info->max_state - 1; |
|---|
| 1225 | + clk_bulk_disable_unprepare(opp_info->num_clks, opp_info->clks); |
|---|
| 1049 | 1226 | } |
|---|
| 1050 | 1227 | |
|---|
| 1051 | | -static int monitor_temp_to_state(struct monitor_dev_info *info, |
|---|
| 1052 | | - int temp, unsigned long *state) |
|---|
| 1228 | +static int rockchip_monitor_set_opp(struct monitor_dev_info *info, |
|---|
| 1229 | + unsigned long old_freq, |
|---|
| 1230 | + unsigned long freq, |
|---|
| 1231 | + struct dev_pm_opp_supply *old_supply, |
|---|
| 1232 | + struct dev_pm_opp_supply *new_supply) |
|---|
| 1053 | 1233 | { |
|---|
| 1054 | | - unsigned long target_state, target_freq = 0; |
|---|
| 1055 | | - int i; |
|---|
| 1234 | + struct dev_pm_set_opp_data *data; |
|---|
| 1235 | + int size; |
|---|
| 1056 | 1236 | |
|---|
| 1057 | | - if (temp == THERMAL_TEMP_INVALID) |
|---|
| 1237 | + data = info->set_opp_data; |
|---|
| 1238 | + data->regulators = info->regulators; |
|---|
| 1239 | + data->regulator_count = info->regulator_count; |
|---|
| 1240 | + data->clk = info->clk; |
|---|
| 1241 | + data->dev = info->dev; |
|---|
| 1242 | + |
|---|
| 1243 | + data->old_opp.rate = old_freq; |
|---|
| 1244 | + size = sizeof(*old_supply) * info->regulator_count; |
|---|
| 1245 | + if (!old_supply) |
|---|
| 1246 | + memset(data->old_opp.supplies, 0, size); |
|---|
| 1247 | + else |
|---|
| 1248 | + memcpy(data->old_opp.supplies, old_supply, size); |
|---|
| 1249 | + |
|---|
| 1250 | + data->new_opp.rate = freq; |
|---|
| 1251 | + memcpy(data->new_opp.supplies, new_supply, size); |
|---|
| 1252 | + |
|---|
| 1253 | + return info->devp->set_opp(data); |
|---|
| 1254 | +} |
|---|
| 1255 | + |
|---|
| 1256 | +int rockchip_monitor_check_rate_volt(struct monitor_dev_info *info) |
|---|
| 1257 | +{ |
|---|
| 1258 | + struct device *dev = info->dev; |
|---|
| 1259 | + struct regulator *vdd_reg = NULL; |
|---|
| 1260 | + struct regulator *mem_reg = NULL; |
|---|
| 1261 | + struct rockchip_opp_info *opp_info = info->devp->opp_info; |
|---|
| 1262 | + struct dev_pm_opp *opp; |
|---|
| 1263 | + unsigned long old_rate, new_rate, new_volt, new_mem_volt; |
|---|
| 1264 | + int old_volt, old_mem_volt; |
|---|
| 1265 | + u32 target_rm = UINT_MAX; |
|---|
| 1266 | + bool is_set_clk = true; |
|---|
| 1267 | + bool is_set_rm = false; |
|---|
| 1268 | + int ret = 0; |
|---|
| 1269 | + |
|---|
| 1270 | + if (!info->regulators || !info->clk) |
|---|
| 1058 | 1271 | return 0; |
|---|
| 1059 | | - if (info->temp_freq_table) { |
|---|
| 1060 | | - for (i = 0; info->temp_freq_table[i].freq != UINT_MAX; i++) { |
|---|
| 1061 | | - if (temp > info->temp_freq_table[i].temp) |
|---|
| 1062 | | - target_freq = info->temp_freq_table[i].freq; |
|---|
| 1272 | + |
|---|
| 1273 | + mutex_lock(&info->volt_adjust_mutex); |
|---|
| 1274 | + |
|---|
| 1275 | + vdd_reg = info->regulators[0]; |
|---|
| 1276 | + old_rate = clk_get_rate(info->clk); |
|---|
| 1277 | + old_volt = regulator_get_voltage(vdd_reg); |
|---|
| 1278 | + if (info->regulator_count > 1) { |
|---|
| 1279 | + mem_reg = info->regulators[1]; |
|---|
| 1280 | + old_mem_volt = regulator_get_voltage(mem_reg); |
|---|
| 1281 | + } |
|---|
| 1282 | + |
|---|
| 1283 | + if (info->init_freq) { |
|---|
| 1284 | + new_rate = info->init_freq * 1000; |
|---|
| 1285 | + info->init_freq = 0; |
|---|
| 1286 | + } else { |
|---|
| 1287 | + new_rate = old_rate; |
|---|
| 1288 | + } |
|---|
| 1289 | + opp = dev_pm_opp_find_freq_ceil(dev, &new_rate); |
|---|
| 1290 | + if (IS_ERR(opp)) { |
|---|
| 1291 | + opp = dev_pm_opp_find_freq_floor(dev, &new_rate); |
|---|
| 1292 | + if (IS_ERR(opp)) { |
|---|
| 1293 | + ret = PTR_ERR(opp); |
|---|
| 1294 | + goto unlock; |
|---|
| 1063 | 1295 | } |
|---|
| 1064 | | - if (target_freq) |
|---|
| 1065 | | - *state = monitor_freq_to_state(info, |
|---|
| 1066 | | - target_freq * 1000); |
|---|
| 1067 | | - else |
|---|
| 1068 | | - *state = 0; |
|---|
| 1296 | + } |
|---|
| 1297 | + new_volt = opp->supplies[0].u_volt; |
|---|
| 1298 | + if (info->regulator_count > 1) |
|---|
| 1299 | + new_mem_volt = opp->supplies[1].u_volt; |
|---|
| 1300 | + dev_pm_opp_put(opp); |
|---|
| 1301 | + |
|---|
| 1302 | + if (old_rate == new_rate) { |
|---|
| 1303 | + if (info->regulator_count > 1) { |
|---|
| 1304 | + if (old_volt == new_volt && |
|---|
| 1305 | + new_mem_volt == old_mem_volt) |
|---|
| 1306 | + goto unlock; |
|---|
| 1307 | + } else if (old_volt == new_volt) { |
|---|
| 1308 | + goto unlock; |
|---|
| 1309 | + } |
|---|
| 1310 | + } |
|---|
| 1311 | + if (!new_volt || (info->regulator_count > 1 && !new_mem_volt)) |
|---|
| 1312 | + goto unlock; |
|---|
| 1313 | + |
|---|
| 1314 | + if (info->devp->set_opp) { |
|---|
| 1315 | + ret = rockchip_monitor_set_opp(info, old_rate, new_rate, |
|---|
| 1316 | + NULL, opp->supplies); |
|---|
| 1317 | + goto unlock; |
|---|
| 1069 | 1318 | } |
|---|
| 1070 | 1319 | |
|---|
| 1071 | | - if (info->status_min_limit) { |
|---|
| 1072 | | - target_freq = info->status_min_limit * 1000; |
|---|
| 1073 | | - target_state = monitor_freq_to_state(info, target_freq); |
|---|
| 1074 | | - if (*state > target_state) |
|---|
| 1075 | | - *state = target_state; |
|---|
| 1320 | + if (opp_info && opp_info->data && opp_info->data->set_read_margin) { |
|---|
| 1321 | + is_set_rm = true; |
|---|
| 1322 | + if (info->devp->type == MONITOR_TYPE_DEV) { |
|---|
| 1323 | + if (!pm_runtime_active(dev)) { |
|---|
| 1324 | + is_set_rm = false; |
|---|
| 1325 | + if (opp_info->scmi_clk) |
|---|
| 1326 | + is_set_clk = false; |
|---|
| 1327 | + } |
|---|
| 1328 | + } |
|---|
| 1076 | 1329 | } |
|---|
| 1330 | + rockchip_monitor_enable_opp_clk(dev, opp_info); |
|---|
| 1331 | + rockchip_get_read_margin(dev, opp_info, new_volt, &target_rm); |
|---|
| 1077 | 1332 | |
|---|
| 1078 | | - return 0; |
|---|
| 1333 | + dev_dbg(dev, "%s: %lu Hz --> %lu Hz\n", __func__, old_rate, new_rate); |
|---|
| 1334 | + if (new_rate >= old_rate) { |
|---|
| 1335 | + rockchip_set_intermediate_rate(dev, opp_info, info->clk, |
|---|
| 1336 | + old_rate, new_rate, |
|---|
| 1337 | + true, is_set_clk); |
|---|
| 1338 | + |
|---|
| 1339 | + if (old_volt > new_volt) { |
|---|
| 1340 | + ret = regulator_set_voltage(vdd_reg, new_volt, INT_MAX); |
|---|
| 1341 | + if (ret) { |
|---|
| 1342 | + dev_err(dev, "%s: failed to set volt: %lu\n", |
|---|
| 1343 | + __func__, new_volt); |
|---|
| 1344 | + goto restore_voltage; |
|---|
| 1345 | + } |
|---|
| 1346 | + } |
|---|
| 1347 | + if (info->regulator_count > 1) { |
|---|
| 1348 | + ret = regulator_set_voltage(mem_reg, new_mem_volt, |
|---|
| 1349 | + INT_MAX); |
|---|
| 1350 | + if (ret) { |
|---|
| 1351 | + dev_err(dev, "%s: failed to set volt: %lu\n", |
|---|
| 1352 | + __func__, new_mem_volt); |
|---|
| 1353 | + goto restore_voltage; |
|---|
| 1354 | + } |
|---|
| 1355 | + } |
|---|
| 1356 | + if (old_volt <= new_volt) { |
|---|
| 1357 | + ret = regulator_set_voltage(vdd_reg, new_volt, INT_MAX); |
|---|
| 1358 | + if (ret) { |
|---|
| 1359 | + dev_err(dev, "%s: failed to set volt: %lu\n", |
|---|
| 1360 | + __func__, new_volt); |
|---|
| 1361 | + goto restore_voltage; |
|---|
| 1362 | + } |
|---|
| 1363 | + } |
|---|
| 1364 | + rockchip_set_read_margin(dev, opp_info, target_rm, is_set_rm); |
|---|
| 1365 | + if (is_set_clk && clk_set_rate(info->clk, new_rate)) { |
|---|
| 1366 | + dev_err(dev, "%s: failed to set clock rate: %lu\n", |
|---|
| 1367 | + __func__, new_rate); |
|---|
| 1368 | + goto restore_rm; |
|---|
| 1369 | + } |
|---|
| 1370 | + } else { |
|---|
| 1371 | + rockchip_set_intermediate_rate(dev, opp_info, info->clk, |
|---|
| 1372 | + old_rate, new_rate, |
|---|
| 1373 | + false, is_set_clk); |
|---|
| 1374 | + rockchip_set_read_margin(dev, opp_info, target_rm, is_set_rm); |
|---|
| 1375 | + if (is_set_clk && clk_set_rate(info->clk, new_rate)) { |
|---|
| 1376 | + dev_err(dev, "%s: failed to set clock rate: %lu\n", |
|---|
| 1377 | + __func__, new_rate); |
|---|
| 1378 | + goto restore_rm; |
|---|
| 1379 | + } |
|---|
| 1380 | + ret = regulator_set_voltage(vdd_reg, new_volt, |
|---|
| 1381 | + INT_MAX); |
|---|
| 1382 | + if (ret) { |
|---|
| 1383 | + dev_err(dev, "%s: failed to set volt: %lu\n", |
|---|
| 1384 | + __func__, new_volt); |
|---|
| 1385 | + goto restore_freq; |
|---|
| 1386 | + } |
|---|
| 1387 | + if (info->regulator_count > 1) { |
|---|
| 1388 | + ret = regulator_set_voltage(mem_reg, new_mem_volt, |
|---|
| 1389 | + INT_MAX); |
|---|
| 1390 | + if (ret) { |
|---|
| 1391 | + dev_err(dev, "%s: failed to set volt: %lu\n", |
|---|
| 1392 | + __func__, new_mem_volt); |
|---|
| 1393 | + goto restore_freq; |
|---|
| 1394 | + } |
|---|
| 1395 | + } |
|---|
| 1396 | + } |
|---|
| 1397 | + goto disable_clk; |
|---|
| 1398 | + |
|---|
| 1399 | +restore_freq: |
|---|
| 1400 | + if (is_set_clk && clk_set_rate(info->clk, old_rate)) |
|---|
| 1401 | + dev_err(dev, "%s: failed to restore old-freq (%lu Hz)\n", |
|---|
| 1402 | + __func__, old_rate); |
|---|
| 1403 | +restore_rm: |
|---|
| 1404 | + rockchip_get_read_margin(dev, opp_info, old_volt, &target_rm); |
|---|
| 1405 | + rockchip_set_read_margin(dev, opp_info, target_rm, is_set_rm); |
|---|
| 1406 | +restore_voltage: |
|---|
| 1407 | + if (old_volt <= new_volt) |
|---|
| 1408 | + regulator_set_voltage(vdd_reg, old_volt, INT_MAX); |
|---|
| 1409 | + if (info->regulator_count > 1) |
|---|
| 1410 | + regulator_set_voltage(mem_reg, old_mem_volt, INT_MAX); |
|---|
| 1411 | + if (old_volt > new_volt) |
|---|
| 1412 | + regulator_set_voltage(vdd_reg, old_volt, INT_MAX); |
|---|
| 1413 | +disable_clk: |
|---|
| 1414 | + rockchip_monitor_disable_opp_clk(dev, opp_info); |
|---|
| 1415 | +unlock: |
|---|
| 1416 | + mutex_unlock(&info->volt_adjust_mutex); |
|---|
| 1417 | + |
|---|
| 1418 | + return ret; |
|---|
| 1079 | 1419 | } |
|---|
| 1080 | | - |
|---|
| 1081 | | -int |
|---|
| 1082 | | -rockchip_system_monitor_adjust_cdev_state(struct thermal_cooling_device *cdev, |
|---|
| 1083 | | - int temp, unsigned long *state) |
|---|
| 1084 | | -{ |
|---|
| 1085 | | - struct monitor_dev_info *info; |
|---|
| 1086 | | - |
|---|
| 1087 | | - down_read(&mdev_list_sem); |
|---|
| 1088 | | - list_for_each_entry(info, &monitor_dev_list, node) { |
|---|
| 1089 | | - if (cdev->np != info->dev->of_node) |
|---|
| 1090 | | - continue; |
|---|
| 1091 | | - monitor_temp_to_state(info, temp, state); |
|---|
| 1092 | | - break; |
|---|
| 1093 | | - } |
|---|
| 1094 | | - up_read(&mdev_list_sem); |
|---|
| 1095 | | - |
|---|
| 1096 | | - return 0; |
|---|
| 1097 | | -} |
|---|
| 1098 | | -EXPORT_SYMBOL(rockchip_system_monitor_adjust_cdev_state); |
|---|
| 1420 | +EXPORT_SYMBOL(rockchip_monitor_check_rate_volt); |
|---|
| 1099 | 1421 | |
|---|
| 1100 | 1422 | struct monitor_dev_info * |
|---|
| 1101 | 1423 | rockchip_system_monitor_register(struct device *dev, |
|---|
| 1102 | 1424 | struct monitor_dev_profile *devp) |
|---|
| 1103 | 1425 | { |
|---|
| 1104 | 1426 | struct monitor_dev_info *info; |
|---|
| 1105 | | - struct devfreq *devfreq; |
|---|
| 1106 | | - int ret; |
|---|
| 1107 | 1427 | |
|---|
| 1108 | 1428 | if (!system_monitor) |
|---|
| 1109 | 1429 | return ERR_PTR(-ENOMEM); |
|---|
| 1430 | + |
|---|
| 1431 | + if (!devp) |
|---|
| 1432 | + return ERR_PTR(-EINVAL); |
|---|
| 1110 | 1433 | |
|---|
| 1111 | 1434 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
|---|
| 1112 | 1435 | if (!info) |
|---|
| .. | .. |
|---|
| 1114 | 1437 | info->dev = dev; |
|---|
| 1115 | 1438 | info->devp = devp; |
|---|
| 1116 | 1439 | |
|---|
| 1117 | | - ret = monitor_device_parse_dt(dev, info); |
|---|
| 1118 | | - if (ret) |
|---|
| 1119 | | - goto free_info; |
|---|
| 1440 | + mutex_init(&info->volt_adjust_mutex); |
|---|
| 1120 | 1441 | |
|---|
| 1121 | | - monitor_set_freq_table(dev, info); |
|---|
| 1122 | | - |
|---|
| 1123 | | - if (info->devp->type == MONITOR_TPYE_DEV) { |
|---|
| 1124 | | - info->devfreq_nb.notifier_call = |
|---|
| 1125 | | - system_monitor_devfreq_notifier_call; |
|---|
| 1126 | | - devfreq = (struct devfreq *)info->devp->data; |
|---|
| 1127 | | - devm_devfreq_register_notifier(dev, devfreq, &info->devfreq_nb, |
|---|
| 1128 | | - DEVFREQ_POLICY_NOTIFIER); |
|---|
| 1442 | + rockchip_system_monitor_parse_supplies(dev, info); |
|---|
| 1443 | + if (monitor_device_parse_dt(dev, info)) { |
|---|
| 1444 | + rockchip_monitor_check_rate_volt(info); |
|---|
| 1445 | + devp->is_checked = true; |
|---|
| 1446 | + kfree(info->set_opp_data); |
|---|
| 1447 | + kfree(info); |
|---|
| 1448 | + return ERR_PTR(-EINVAL); |
|---|
| 1129 | 1449 | } |
|---|
| 1130 | 1450 | |
|---|
| 1451 | + rockchip_system_monitor_early_regulator_init(info); |
|---|
| 1131 | 1452 | rockchip_system_monitor_wide_temp_init(info); |
|---|
| 1132 | | - mutex_init(&info->volt_adjust_mutex); |
|---|
| 1453 | + rockchip_monitor_check_rate_volt(info); |
|---|
| 1454 | + devp->is_checked = true; |
|---|
| 1455 | + rockchip_system_monitor_freq_qos_requset(info); |
|---|
| 1133 | 1456 | |
|---|
| 1134 | 1457 | down_write(&mdev_list_sem); |
|---|
| 1135 | 1458 | list_add(&info->node, &monitor_dev_list); |
|---|
| 1136 | 1459 | up_write(&mdev_list_sem); |
|---|
| 1137 | 1460 | |
|---|
| 1138 | 1461 | return info; |
|---|
| 1139 | | - |
|---|
| 1140 | | -free_info: |
|---|
| 1141 | | - kfree(info); |
|---|
| 1142 | | - return ERR_PTR(-EINVAL); |
|---|
| 1143 | 1462 | } |
|---|
| 1144 | 1463 | EXPORT_SYMBOL(rockchip_system_monitor_register); |
|---|
| 1145 | 1464 | |
|---|
| 1146 | 1465 | void rockchip_system_monitor_unregister(struct monitor_dev_info *info) |
|---|
| 1147 | 1466 | { |
|---|
| 1148 | | - struct devfreq *devfreq; |
|---|
| 1149 | | - |
|---|
| 1150 | 1467 | if (!info) |
|---|
| 1151 | 1468 | return; |
|---|
| 1152 | 1469 | |
|---|
| .. | .. |
|---|
| 1154 | 1471 | list_del(&info->node); |
|---|
| 1155 | 1472 | up_write(&mdev_list_sem); |
|---|
| 1156 | 1473 | |
|---|
| 1157 | | - devfreq = (struct devfreq *)info->devp->data; |
|---|
| 1158 | | - if (info->devp->type == MONITOR_TPYE_DEV) |
|---|
| 1159 | | - devm_devfreq_unregister_notifier(info->dev, devfreq, |
|---|
| 1160 | | - &info->devfreq_nb, |
|---|
| 1161 | | - DEVFREQ_TRANSITION_NOTIFIER); |
|---|
| 1474 | + if (info->devp->type == MONITOR_TYPE_CPU) { |
|---|
| 1475 | + if (freq_qos_request_active(&info->max_temp_freq_req)) |
|---|
| 1476 | + freq_qos_remove_request(&info->max_temp_freq_req); |
|---|
| 1477 | + if (freq_qos_request_active(&info->min_sta_freq_req)) |
|---|
| 1478 | + freq_qos_remove_request(&info->min_sta_freq_req); |
|---|
| 1479 | + if (freq_qos_request_active(&info->max_sta_freq_req)) |
|---|
| 1480 | + freq_qos_remove_request(&info->max_sta_freq_req); |
|---|
| 1481 | + } else { |
|---|
| 1482 | + if (dev_pm_qos_request_active(&info->dev_max_freq_req)) |
|---|
| 1483 | + dev_pm_qos_remove_request(&info->dev_max_freq_req); |
|---|
| 1484 | + } |
|---|
| 1162 | 1485 | |
|---|
| 1163 | 1486 | kfree(info->low_temp_adjust_table); |
|---|
| 1164 | 1487 | kfree(info->opp_table); |
|---|
| 1165 | | - kfree(info->freq_table); |
|---|
| 1488 | + kfree(info->set_opp_data); |
|---|
| 1166 | 1489 | kfree(info); |
|---|
| 1167 | 1490 | } |
|---|
| 1168 | 1491 | EXPORT_SYMBOL(rockchip_system_monitor_unregister); |
|---|
| 1492 | + |
|---|
| 1493 | +int rockchip_system_monitor_register_notifier(struct notifier_block *nb) |
|---|
| 1494 | +{ |
|---|
| 1495 | + return blocking_notifier_chain_register(&system_monitor_notifier_list, nb); |
|---|
| 1496 | +} |
|---|
| 1497 | +EXPORT_SYMBOL(rockchip_system_monitor_register_notifier); |
|---|
| 1498 | + |
|---|
| 1499 | +void rockchip_system_monitor_unregister_notifier(struct notifier_block *nb) |
|---|
| 1500 | +{ |
|---|
| 1501 | + blocking_notifier_chain_unregister(&system_monitor_notifier_list, nb); |
|---|
| 1502 | +} |
|---|
| 1503 | +EXPORT_SYMBOL(rockchip_system_monitor_unregister_notifier); |
|---|
| 1504 | + |
|---|
| 1505 | +static int rockchip_system_monitor_temp_notify(int temp) |
|---|
| 1506 | +{ |
|---|
| 1507 | + struct system_monitor_event_data event_data; |
|---|
| 1508 | + int ret; |
|---|
| 1509 | + |
|---|
| 1510 | + event_data.temp = temp; |
|---|
| 1511 | + ret = blocking_notifier_call_chain(&system_monitor_notifier_list, |
|---|
| 1512 | + SYSTEM_MONITOR_CHANGE_TEMP, |
|---|
| 1513 | + (void *)&event_data); |
|---|
| 1514 | + |
|---|
| 1515 | + return notifier_to_errno(ret); |
|---|
| 1516 | +} |
|---|
| 1517 | + |
|---|
| 1518 | +static int notify_dummy(struct thermal_zone_device *tz, int trip) |
|---|
| 1519 | +{ |
|---|
| 1520 | + return 0; |
|---|
| 1521 | +} |
|---|
| 1522 | + |
|---|
| 1523 | +static struct thermal_governor thermal_gov_dummy = { |
|---|
| 1524 | + .name = "dummy", |
|---|
| 1525 | + .throttle = notify_dummy, |
|---|
| 1526 | +}; |
|---|
| 1169 | 1527 | |
|---|
| 1170 | 1528 | static int rockchip_system_monitor_parse_dt(struct system_monitor *monitor) |
|---|
| 1171 | 1529 | { |
|---|
| .. | .. |
|---|
| 1173 | 1531 | const char *tz_name, *buf = NULL; |
|---|
| 1174 | 1532 | |
|---|
| 1175 | 1533 | if (of_property_read_string(np, "rockchip,video-4k-offline-cpus", &buf)) |
|---|
| 1176 | | - cpumask_clear(&system_monitor->video_4k_offline_cpus); |
|---|
| 1534 | + cpumask_clear(&monitor->video_4k_offline_cpus); |
|---|
| 1177 | 1535 | else |
|---|
| 1178 | 1536 | cpulist_parse(buf, &monitor->video_4k_offline_cpus); |
|---|
| 1179 | 1537 | |
|---|
| .. | .. |
|---|
| 1199 | 1557 | system_monitor->offline_cpus_temp = INT_MAX; |
|---|
| 1200 | 1558 | of_property_read_u32(np, "rockchip,temp-hysteresis", |
|---|
| 1201 | 1559 | &system_monitor->temp_hysteresis); |
|---|
| 1560 | + |
|---|
| 1561 | + if (of_find_property(np, "rockchip,thermal-governor-dummy", NULL)) { |
|---|
| 1562 | + if (monitor->tz->governor->unbind_from_tz) |
|---|
| 1563 | + monitor->tz->governor->unbind_from_tz(monitor->tz); |
|---|
| 1564 | + monitor->tz->governor = &thermal_gov_dummy; |
|---|
| 1565 | + } |
|---|
| 1566 | + |
|---|
| 1202 | 1567 | out: |
|---|
| 1203 | 1568 | return 0; |
|---|
| 1204 | 1569 | } |
|---|
| .. | .. |
|---|
| 1224 | 1589 | cpumask_copy(&system_monitor->offline_cpus, &offline_cpus); |
|---|
| 1225 | 1590 | for_each_cpu(cpu, &system_monitor->offline_cpus) { |
|---|
| 1226 | 1591 | if (cpu_online(cpu)) |
|---|
| 1227 | | - cpu_down(cpu); |
|---|
| 1592 | + remove_cpu(cpu); |
|---|
| 1228 | 1593 | } |
|---|
| 1229 | 1594 | |
|---|
| 1230 | 1595 | cpumask_clear(&online_cpus); |
|---|
| .. | .. |
|---|
| 1234 | 1599 | if (cpumask_empty(&online_cpus)) |
|---|
| 1235 | 1600 | goto out; |
|---|
| 1236 | 1601 | for_each_cpu(cpu, &online_cpus) |
|---|
| 1237 | | - cpu_up(cpu); |
|---|
| 1602 | + add_cpu(cpu); |
|---|
| 1238 | 1603 | |
|---|
| 1239 | 1604 | out: |
|---|
| 1240 | 1605 | mutex_unlock(&cpu_on_off_mutex); |
|---|
| .. | .. |
|---|
| 1266 | 1631 | { |
|---|
| 1267 | 1632 | int temp, ret; |
|---|
| 1268 | 1633 | struct monitor_dev_info *info; |
|---|
| 1269 | | - static int last_temp = INT_MAX; |
|---|
| 1270 | 1634 | |
|---|
| 1271 | 1635 | ret = thermal_zone_get_temp(system_monitor->tz, &temp); |
|---|
| 1272 | 1636 | if (ret || temp == THERMAL_TEMP_INVALID) |
|---|
| .. | .. |
|---|
| 1274 | 1638 | |
|---|
| 1275 | 1639 | dev_dbg(system_monitor->dev, "temperature=%d\n", temp); |
|---|
| 1276 | 1640 | |
|---|
| 1277 | | - if (temp < last_temp && last_temp - temp <= 2000) |
|---|
| 1641 | + if (temp < system_monitor->last_temp && |
|---|
| 1642 | + system_monitor->last_temp - temp <= 2000) |
|---|
| 1278 | 1643 | goto out; |
|---|
| 1279 | | - last_temp = temp; |
|---|
| 1644 | + system_monitor->last_temp = temp; |
|---|
| 1645 | + |
|---|
| 1646 | + rockchip_system_monitor_temp_notify(temp); |
|---|
| 1280 | 1647 | |
|---|
| 1281 | 1648 | down_read(&mdev_list_sem); |
|---|
| 1282 | 1649 | list_for_each_entry(info, &monitor_dev_list, node) |
|---|
| .. | .. |
|---|
| 1302 | 1669 | unsigned long status) |
|---|
| 1303 | 1670 | { |
|---|
| 1304 | 1671 | unsigned int target_freq = 0; |
|---|
| 1305 | | - bool is_freq_fixed = false; |
|---|
| 1306 | | - int cpu; |
|---|
| 1672 | + |
|---|
| 1673 | + if (!freq_qos_request_active(&info->min_sta_freq_req)) |
|---|
| 1674 | + return; |
|---|
| 1675 | + if (!freq_qos_request_active(&info->max_sta_freq_req)) |
|---|
| 1676 | + return; |
|---|
| 1307 | 1677 | |
|---|
| 1308 | 1678 | if (status & SYS_STATUS_REBOOT) { |
|---|
| 1309 | | - info->status_min_limit = info->reboot_freq; |
|---|
| 1310 | | - info->status_max_limit = info->reboot_freq; |
|---|
| 1311 | | - info->is_status_freq_fixed = true; |
|---|
| 1312 | | - goto next; |
|---|
| 1679 | + freq_qos_update_request(&info->max_sta_freq_req, |
|---|
| 1680 | + info->reboot_freq); |
|---|
| 1681 | + freq_qos_update_request(&info->min_sta_freq_req, |
|---|
| 1682 | + info->reboot_freq); |
|---|
| 1683 | + return; |
|---|
| 1313 | 1684 | } |
|---|
| 1314 | 1685 | |
|---|
| 1315 | 1686 | if (info->video_4k_freq && (status & SYS_STATUS_VIDEO_4K)) |
|---|
| 1316 | 1687 | target_freq = info->video_4k_freq; |
|---|
| 1317 | | - if (target_freq == info->status_max_limit && |
|---|
| 1318 | | - info->is_status_freq_fixed == is_freq_fixed) |
|---|
| 1688 | + |
|---|
| 1689 | + if (target_freq == info->status_max_limit) |
|---|
| 1319 | 1690 | return; |
|---|
| 1320 | 1691 | info->status_max_limit = target_freq; |
|---|
| 1321 | | - info->is_status_freq_fixed = is_freq_fixed; |
|---|
| 1322 | | -next: |
|---|
| 1323 | | - cpu = cpumask_any(&info->devp->allowed_cpus); |
|---|
| 1324 | | - cpufreq_update_policy(cpu); |
|---|
| 1692 | + if (info->status_max_limit) |
|---|
| 1693 | + freq_qos_update_request(&info->max_sta_freq_req, |
|---|
| 1694 | + info->status_max_limit); |
|---|
| 1695 | + else |
|---|
| 1696 | + freq_qos_update_request(&info->max_sta_freq_req, |
|---|
| 1697 | + FREQ_QOS_MAX_DEFAULT_VALUE); |
|---|
| 1325 | 1698 | } |
|---|
| 1326 | 1699 | |
|---|
| 1327 | 1700 | static void rockchip_system_status_limit_freq(unsigned long status) |
|---|
| .. | .. |
|---|
| 1330 | 1703 | |
|---|
| 1331 | 1704 | down_read(&mdev_list_sem); |
|---|
| 1332 | 1705 | list_for_each_entry(info, &monitor_dev_list, node) { |
|---|
| 1333 | | - if (info->devp->type == MONITOR_TPYE_CPU) |
|---|
| 1706 | + if (info->devp->type == MONITOR_TYPE_CPU) |
|---|
| 1334 | 1707 | rockchip_system_status_cpu_limit_freq(info, status); |
|---|
| 1335 | 1708 | } |
|---|
| 1336 | 1709 | up_read(&mdev_list_sem); |
|---|
| .. | .. |
|---|
| 1364 | 1737 | return NOTIFY_OK; |
|---|
| 1365 | 1738 | } |
|---|
| 1366 | 1739 | |
|---|
| 1740 | +static int rockchip_system_monitor_set_cpu_uevent_suppress(bool is_suppress) |
|---|
| 1741 | +{ |
|---|
| 1742 | + struct monitor_dev_info *info; |
|---|
| 1743 | + struct cpufreq_policy *policy; |
|---|
| 1744 | + |
|---|
| 1745 | + list_for_each_entry(info, &monitor_dev_list, node) { |
|---|
| 1746 | + if (info->devp->type != MONITOR_TYPE_CPU) |
|---|
| 1747 | + continue; |
|---|
| 1748 | + policy = (struct cpufreq_policy *)info->devp->data; |
|---|
| 1749 | + if (!policy || !policy->cdev) |
|---|
| 1750 | + continue; |
|---|
| 1751 | + if (is_suppress) |
|---|
| 1752 | + dev_set_uevent_suppress(&policy->cdev->device, 1); |
|---|
| 1753 | + else |
|---|
| 1754 | + dev_set_uevent_suppress(&policy->cdev->device, 0); |
|---|
| 1755 | + } |
|---|
| 1756 | + |
|---|
| 1757 | + return 0; |
|---|
| 1758 | +} |
|---|
| 1759 | + |
|---|
| 1367 | 1760 | static int monitor_pm_notify(struct notifier_block *nb, |
|---|
| 1368 | 1761 | unsigned long mode, void *_unused) |
|---|
| 1369 | 1762 | { |
|---|
| .. | .. |
|---|
| 1372 | 1765 | case PM_RESTORE_PREPARE: |
|---|
| 1373 | 1766 | case PM_SUSPEND_PREPARE: |
|---|
| 1374 | 1767 | atomic_set(&monitor_in_suspend, 1); |
|---|
| 1768 | + rockchip_system_monitor_set_cpu_uevent_suppress(true); |
|---|
| 1375 | 1769 | break; |
|---|
| 1376 | 1770 | case PM_POST_HIBERNATION: |
|---|
| 1377 | 1771 | case PM_POST_RESTORE: |
|---|
| .. | .. |
|---|
| 1379 | 1773 | if (system_monitor->tz) |
|---|
| 1380 | 1774 | rockchip_system_monitor_thermal_update(); |
|---|
| 1381 | 1775 | atomic_set(&monitor_in_suspend, 0); |
|---|
| 1776 | + rockchip_system_monitor_set_cpu_uevent_suppress(false); |
|---|
| 1777 | + system_monitor->last_temp = INT_MAX; |
|---|
| 1382 | 1778 | break; |
|---|
| 1383 | 1779 | default: |
|---|
| 1384 | 1780 | break; |
|---|
| .. | .. |
|---|
| 1388 | 1784 | |
|---|
| 1389 | 1785 | static struct notifier_block monitor_pm_nb = { |
|---|
| 1390 | 1786 | .notifier_call = monitor_pm_notify, |
|---|
| 1391 | | -}; |
|---|
| 1392 | | - |
|---|
| 1393 | | -static int rockchip_monitor_cpufreq_policy_notifier(struct notifier_block *nb, |
|---|
| 1394 | | - unsigned long event, |
|---|
| 1395 | | - void *data) |
|---|
| 1396 | | -{ |
|---|
| 1397 | | - struct monitor_dev_info *info; |
|---|
| 1398 | | - struct cpufreq_policy *policy = data; |
|---|
| 1399 | | - int cpu = policy->cpu; |
|---|
| 1400 | | - unsigned int limit_freq = UINT_MAX; |
|---|
| 1401 | | - |
|---|
| 1402 | | - if (event != CPUFREQ_ADJUST) |
|---|
| 1403 | | - return NOTIFY_OK; |
|---|
| 1404 | | - |
|---|
| 1405 | | - down_read(&mdev_list_sem); |
|---|
| 1406 | | - list_for_each_entry(info, &monitor_dev_list, node) { |
|---|
| 1407 | | - if (info->devp->type != MONITOR_TPYE_CPU) |
|---|
| 1408 | | - continue; |
|---|
| 1409 | | - if (!cpumask_test_cpu(cpu, &info->devp->allowed_cpus)) |
|---|
| 1410 | | - continue; |
|---|
| 1411 | | - |
|---|
| 1412 | | - if (info->wide_temp_limit) { |
|---|
| 1413 | | - if (limit_freq > info->wide_temp_limit / 1000) |
|---|
| 1414 | | - limit_freq = info->wide_temp_limit / 1000; |
|---|
| 1415 | | - } |
|---|
| 1416 | | - if (info->status_max_limit && |
|---|
| 1417 | | - limit_freq > info->status_max_limit) |
|---|
| 1418 | | - limit_freq = info->status_max_limit; |
|---|
| 1419 | | - |
|---|
| 1420 | | - if (info->is_status_freq_fixed) { |
|---|
| 1421 | | - cpufreq_verify_within_limits(policy, limit_freq, |
|---|
| 1422 | | - limit_freq); |
|---|
| 1423 | | - dev_info(info->dev, "min=%u, max=%u\n", policy->min, |
|---|
| 1424 | | - policy->max); |
|---|
| 1425 | | - } else if (limit_freq < policy->max) { |
|---|
| 1426 | | - cpufreq_verify_within_limits(policy, 0, limit_freq); |
|---|
| 1427 | | - } |
|---|
| 1428 | | - } |
|---|
| 1429 | | - up_read(&mdev_list_sem); |
|---|
| 1430 | | - |
|---|
| 1431 | | - return NOTIFY_OK; |
|---|
| 1432 | | -} |
|---|
| 1433 | | - |
|---|
| 1434 | | -static struct notifier_block rockchip_monitor_cpufreq_policy_nb = { |
|---|
| 1435 | | - .notifier_call = rockchip_monitor_cpufreq_policy_notifier, |
|---|
| 1436 | 1787 | }; |
|---|
| 1437 | 1788 | |
|---|
| 1438 | 1789 | static int rockchip_monitor_reboot_notifier(struct notifier_block *nb, |
|---|
| .. | .. |
|---|
| 1454 | 1805 | { |
|---|
| 1455 | 1806 | struct fb_event *event = ptr; |
|---|
| 1456 | 1807 | |
|---|
| 1457 | | - switch (action) { |
|---|
| 1458 | | - case FB_EARLY_EVENT_BLANK: |
|---|
| 1459 | | - switch (*((int *)event->data)) { |
|---|
| 1460 | | - case FB_BLANK_UNBLANK: |
|---|
| 1461 | | - rockchip_clear_system_status(SYS_STATUS_SUSPEND); |
|---|
| 1462 | | - break; |
|---|
| 1463 | | - default: |
|---|
| 1464 | | - break; |
|---|
| 1465 | | - } |
|---|
| 1808 | + if (action != FB_EVENT_BLANK) |
|---|
| 1809 | + return NOTIFY_OK; |
|---|
| 1810 | + |
|---|
| 1811 | + switch (*((int *)event->data)) { |
|---|
| 1812 | + case FB_BLANK_UNBLANK: |
|---|
| 1813 | + rockchip_clear_system_status(SYS_STATUS_SUSPEND); |
|---|
| 1466 | 1814 | break; |
|---|
| 1467 | | - case FB_EVENT_BLANK: |
|---|
| 1468 | | - switch (*((int *)event->data)) { |
|---|
| 1469 | | - case FB_BLANK_POWERDOWN: |
|---|
| 1470 | | - rockchip_set_system_status(SYS_STATUS_SUSPEND); |
|---|
| 1471 | | - break; |
|---|
| 1472 | | - default: |
|---|
| 1473 | | - break; |
|---|
| 1474 | | - } |
|---|
| 1815 | + case FB_BLANK_POWERDOWN: |
|---|
| 1816 | + rockchip_set_system_status(SYS_STATUS_SUSPEND); |
|---|
| 1475 | 1817 | break; |
|---|
| 1476 | 1818 | default: |
|---|
| 1477 | 1819 | break; |
|---|
| .. | .. |
|---|
| 1494 | 1836 | case EBC_OFF: |
|---|
| 1495 | 1837 | rockchip_set_system_status(SYS_STATUS_LOW_POWER); |
|---|
| 1496 | 1838 | break; |
|---|
| 1497 | | - case EBC_FB_BLANK: |
|---|
| 1498 | | - rockchip_set_system_status(SYS_STATUS_SUSPEND); |
|---|
| 1499 | | - break; |
|---|
| 1500 | | - case EBC_FB_UNBLANK: |
|---|
| 1501 | | - rockchip_clear_system_status(SYS_STATUS_SUSPEND); |
|---|
| 1502 | | - break; |
|---|
| 1503 | 1839 | default: |
|---|
| 1504 | 1840 | break; |
|---|
| 1505 | 1841 | } |
|---|
| .. | .. |
|---|
| 1510 | 1846 | static struct notifier_block rockchip_monitor_ebc_nb = { |
|---|
| 1511 | 1847 | .notifier_call = rockchip_eink_devfs_notifier, |
|---|
| 1512 | 1848 | }; |
|---|
| 1849 | + |
|---|
| 1850 | +static void system_monitor_early_min_volt_function(struct work_struct *work) |
|---|
| 1851 | +{ |
|---|
| 1852 | + struct monitor_dev_info *info; |
|---|
| 1853 | + struct regulator_dev *rdev; |
|---|
| 1854 | + int min_uV, max_uV; |
|---|
| 1855 | + int ret; |
|---|
| 1856 | + |
|---|
| 1857 | + down_read(&mdev_list_sem); |
|---|
| 1858 | + list_for_each_entry(info, &monitor_dev_list, node) { |
|---|
| 1859 | + if (!info->early_min_volt || !info->early_reg) |
|---|
| 1860 | + continue; |
|---|
| 1861 | + rdev = info->early_reg->rdev; |
|---|
| 1862 | + min_uV = rdev->constraints->min_uV; |
|---|
| 1863 | + max_uV = rdev->constraints->max_uV; |
|---|
| 1864 | + ret = regulator_set_voltage(info->early_reg, min_uV, max_uV); |
|---|
| 1865 | + if (ret) |
|---|
| 1866 | + dev_err(&rdev->dev, |
|---|
| 1867 | + "%s: failed to set volt\n", __func__); |
|---|
| 1868 | + regulator_put(info->early_reg); |
|---|
| 1869 | + } |
|---|
| 1870 | + up_read(&mdev_list_sem); |
|---|
| 1871 | +} |
|---|
| 1872 | + |
|---|
| 1873 | +static DECLARE_DELAYED_WORK(system_monitor_early_min_volt_work, |
|---|
| 1874 | + system_monitor_early_min_volt_function); |
|---|
| 1513 | 1875 | |
|---|
| 1514 | 1876 | static int rockchip_system_monitor_probe(struct platform_device *pdev) |
|---|
| 1515 | 1877 | { |
|---|
| .. | .. |
|---|
| 1532 | 1894 | |
|---|
| 1533 | 1895 | rockchip_system_monitor_parse_dt(system_monitor); |
|---|
| 1534 | 1896 | if (system_monitor->tz) { |
|---|
| 1897 | + system_monitor->last_temp = INT_MAX; |
|---|
| 1535 | 1898 | INIT_DELAYED_WORK(&system_monitor->thermal_work, |
|---|
| 1536 | 1899 | rockchip_system_monitor_thermal_check); |
|---|
| 1537 | 1900 | mod_delayed_work(system_freezable_wq, |
|---|
| .. | .. |
|---|
| 1546 | 1909 | if (register_pm_notifier(&monitor_pm_nb)) |
|---|
| 1547 | 1910 | dev_err(dev, "failed to register suspend notifier\n"); |
|---|
| 1548 | 1911 | |
|---|
| 1549 | | - cpufreq_register_notifier(&rockchip_monitor_cpufreq_policy_nb, |
|---|
| 1550 | | - CPUFREQ_POLICY_NOTIFIER); |
|---|
| 1551 | | - |
|---|
| 1552 | 1912 | register_reboot_notifier(&rockchip_monitor_reboot_nb); |
|---|
| 1553 | 1913 | |
|---|
| 1554 | 1914 | if (fb_register_client(&rockchip_monitor_fb_nb)) |
|---|
| .. | .. |
|---|
| 1556 | 1916 | |
|---|
| 1557 | 1917 | ebc_register_notifier(&rockchip_monitor_ebc_nb); |
|---|
| 1558 | 1918 | |
|---|
| 1919 | + schedule_delayed_work(&system_monitor_early_min_volt_work, |
|---|
| 1920 | + msecs_to_jiffies(30000)); |
|---|
| 1921 | + |
|---|
| 1559 | 1922 | dev_info(dev, "system monitor probe\n"); |
|---|
| 1560 | 1923 | |
|---|
| 1561 | 1924 | return 0; |
|---|