forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/drivers/soc/rockchip/rockchip_system_monitor.c
....@@ -4,9 +4,10 @@
44 * Author: Finley Xiao <finley.xiao@rock-chips.com>
55 */
66
7
-#include <dt-bindings/soc/rockchip-system-status.h>
7
+#include <linux/clk-provider.h>
88 #include <linux/cpu.h>
99 #include <linux/cpufreq.h>
10
+#include <linux/devfreq.h>
1011 #include <linux/device.h>
1112 #include <linux/fb.h>
1213 #include <linux/module.h>
....@@ -15,26 +16,32 @@
1516 #include <linux/of.h>
1617 #include <linux/platform_device.h>
1718 #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>
1925 #include <linux/reboot.h>
26
+#include <linux/rockchip/rockchip_sip.h>
2027 #include <linux/slab.h>
2128 #include <linux/suspend.h>
2229 #include <linux/thermal.h>
30
+#include <linux/uaccess.h>
2331 #include <linux/version.h>
32
+#include <linux/delay.h>
33
+#include <soc/rockchip/rockchip_opp_select.h>
2434 #include <soc/rockchip/rockchip_system_monitor.h>
2535 #include <soc/rockchip/rockchip-system-status.h>
2636
27
-#include "../../opp/opp.h"
28
-#include "../../devfreq/governor.h"
29
-
3037 #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"
3141
3242 #define CPU_REBOOT_FREQ 816000 /* kHz */
3343 #define VIDEO_1080P_SIZE (1920 * 1080)
3444 #define THERMAL_POLLING_DELAY 200 /* milliseconds */
35
-
36
-#define devfreq_nb_to_monitor(nb) container_of(nb, struct monitor_dev_info, \
37
- devfreq_nb)
3845
3946 struct video_info {
4047 unsigned int width;
....@@ -64,11 +71,11 @@
6471
6572 struct thermal_zone_device *tz;
6673 struct delayed_work thermal_work;
74
+ int last_temp;
6775 int offline_cpus_temp;
6876 int temp_hysteresis;
6977 unsigned int delay;
7078 bool is_temp_offline;
71
- bool boosted;
7279 };
7380
7481 static unsigned long system_status;
....@@ -85,6 +92,7 @@
8592 static struct system_monitor *system_monitor;
8693 static atomic_t monitor_in_suspend;
8794
95
+static BLOCKING_NOTIFIER_HEAD(system_monitor_notifier_list);
8896 static BLOCKING_NOTIFIER_HEAD(system_status_notifier_list);
8997
9098 int rockchip_register_system_status_notifier(struct notifier_block *nb)
....@@ -174,9 +182,9 @@
174182 return -EINVAL;
175183 }
176184
177
- return __compat_only_sysfs_link_entry_to_kobj(&dev->kobj,
178
- system_monitor->kobj,
179
- "system_status");
185
+ return compat_only_sysfs_link_entry_to_kobj(&dev->kobj,
186
+ system_monitor->kobj,
187
+ "system_status", NULL);
180188 }
181189 EXPORT_SYMBOL(rockchip_add_system_status_interface);
182190
....@@ -238,34 +246,6 @@
238246 return video_info;
239247 }
240248
241
-static struct video_info *rockchip_find_video_info(const char *buf)
242
-{
243
- struct video_info *info, *video_info;
244
-
245
- video_info = rockchip_parse_video_info(buf);
246
-
247
- if (!video_info)
248
- return NULL;
249
-
250
- mutex_lock(&video_info_mutex);
251
- list_for_each_entry(info, &video_info_list, node) {
252
- if (info->width == video_info->width &&
253
- info->height == video_info->height &&
254
- info->ishevc == video_info->ishevc &&
255
- info->videoFramerate == video_info->videoFramerate &&
256
- info->streamBitrate == video_info->streamBitrate) {
257
- mutex_unlock(&video_info_mutex);
258
- kfree(video_info);
259
- return info;
260
- }
261
- }
262
-
263
- mutex_unlock(&video_info_mutex);
264
- kfree(video_info);
265
-
266
- return NULL;
267
-}
268
-
269249 static void rockchip_add_video_info(struct video_info *video_info)
270250 {
271251 if (video_info) {
....@@ -277,12 +257,25 @@
277257
278258 static void rockchip_del_video_info(struct video_info *video_info)
279259 {
280
- if (video_info) {
281
- mutex_lock(&video_info_mutex);
282
- list_del(&video_info->node);
283
- mutex_unlock(&video_info_mutex);
284
- 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
+ }
285276 }
277
+ kfree(video_info);
278
+ mutex_unlock(&video_info_mutex);
286279 }
287280
288281 static void rockchip_update_video_info(void)
....@@ -330,7 +323,7 @@
330323 switch (buf[0]) {
331324 case '0':
332325 /* clear video flag */
333
- video_info = rockchip_find_video_info(buf);
326
+ video_info = rockchip_parse_video_info(buf);
334327 if (video_info) {
335328 rockchip_del_video_info(video_info);
336329 rockchip_update_video_info();
....@@ -359,6 +352,14 @@
359352 case 'n':
360353 /* clear performance flag */
361354 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);
362363 break;
363364 default:
364365 break;
....@@ -497,8 +498,11 @@
497498 int delta_volt = 0;
498499 int i = 0, max_count;
499500 unsigned long low_limit = 0, high_limit = 0;
501
+ unsigned long low_limit_mem = 0, high_limit_mem = 0;
500502 bool reach_max_volt = false;
503
+ bool reach_max_mem_volt = false;
501504 bool reach_high_temp_max_volt = false;
505
+ bool reach_high_temp_max_mem_volt = false;
502506
503507 max_count = dev_pm_opp_get_opp_count(dev);
504508 if (max_count <= 0)
....@@ -516,6 +520,8 @@
516520 }
517521 mutex_lock(&opp_table->lock);
518522 list_for_each_entry(opp, &opp_table->opp_list, node) {
523
+ if (!opp->available)
524
+ continue;
519525 info->opp_table[i].rate = opp->rate;
520526 info->opp_table[i].volt = opp->supplies[0].u_volt;
521527 info->opp_table[i].max_volt = opp->supplies[0].u_volt_max;
....@@ -547,9 +553,48 @@
547553 info->low_limit = low_limit;
548554 if (high_limit && high_limit != opp->rate)
549555 info->high_limit = high_limit;
550
- 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",
551594 info->opp_table[i].rate, info->opp_table[i].volt,
552
- 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);
553598 i++;
554599 }
555600 mutex_unlock(&opp_table->lock);
....@@ -591,9 +636,12 @@
591636 else
592637 info->high_temp_max_volt = value;
593638 rockchip_init_temp_opp_table(info);
594
- if (rockchip_get_temp_freq_table(np, "rockchip,high-temp-limit-table",
595
- &info->high_limit_table)) {
596
- 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) {
597645 if (!of_property_read_u32(np, "rockchip,high-temp-max-freq",
598646 &value)) {
599647 high_temp_max_freq = value * 1000;
....@@ -645,7 +693,7 @@
645693 &info->video_4k_freq);
646694 ret &= of_property_read_u32(np, "rockchip,reboot-freq",
647695 &info->reboot_freq);
648
- if (info->devp->type == MONITOR_TPYE_CPU) {
696
+ if (info->devp->type == MONITOR_TYPE_CPU) {
649697 if (!info->reboot_freq) {
650698 info->reboot_freq = CPU_REBOOT_FREQ;
651699 ret = 0;
....@@ -655,66 +703,70 @@
655703 return ret;
656704 }
657705
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
+
658732 static int monitor_device_parse_dt(struct device *dev,
659733 struct monitor_dev_info *info)
660734 {
661735 struct device_node *np;
662
- int ret = 0;
663
- bool is_wide_temp_en = false;
664
- bool is_status_limit_en = false;
665
- bool is_temp_freq_en = false;
736
+ int ret;
666737
667738 np = of_parse_phandle(dev->of_node, "operating-points-v2", 0);
668739 if (!np)
669740 return -EINVAL;
670741
671
- if (!monitor_device_parse_wide_temp_config(np, info))
672
- is_wide_temp_en = true;
673
- if (!monitor_device_parse_status_config(np, info))
674
- is_status_limit_en = true;
675
- if (!rockchip_get_temp_freq_table(np, "rockchip,temp-freq-table",
676
- &info->temp_freq_table))
677
- is_temp_freq_en = true;
678
- if (is_wide_temp_en || is_status_limit_en || is_temp_freq_en)
679
- ret = 0;
680
- else
681
- 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);
682749
683750 of_node_put(np);
684751
685752 return ret;
686753 }
687754
688
-int rockchip_monitor_opp_set_rate(struct monitor_dev_info *info,
689
- unsigned long target_freq)
690
-{
691
- int ret = 0;
692
-
693
- mutex_lock(&info->volt_adjust_mutex);
694
- ret = dev_pm_opp_set_rate(info->dev, target_freq);
695
- mutex_unlock(&info->volt_adjust_mutex);
696
-
697
- return ret;
698
-}
699
-EXPORT_SYMBOL(rockchip_monitor_opp_set_rate);
700
-
701755 int rockchip_monitor_cpu_low_temp_adjust(struct monitor_dev_info *info,
702756 bool is_low)
703757 {
704
- struct device *dev = info->dev;
705
- unsigned int cpu = cpumask_any(&info->devp->allowed_cpus);
758
+ if (!info->low_limit)
759
+ return 0;
706760
707
- if (info->low_limit) {
708
- if (is_low)
709
- info->wide_temp_limit = info->low_limit;
710
- else
711
- info->wide_temp_limit = 0;
712
- cpufreq_update_policy(cpu);
713
- }
761
+ if (!freq_qos_request_active(&info->max_temp_freq_req))
762
+ return 0;
714763
715
- mutex_lock(&info->volt_adjust_mutex);
716
- dev_pm_opp_check_rate_volt(dev, false);
717
- 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);
718770
719771 return 0;
720772 }
....@@ -723,69 +775,44 @@
723775 int rockchip_monitor_cpu_high_temp_adjust(struct monitor_dev_info *info,
724776 bool is_high)
725777 {
726
- 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;
727783
728784 if (info->high_limit_table) {
729
- info->wide_temp_limit = info->high_limit;
730
- cpufreq_update_policy(cpu);
731
- } else {
732
- if (info->high_limit) {
733
- if (is_high)
734
- info->wide_temp_limit = info->high_limit;
735
- else
736
- info->wide_temp_limit = 0;
737
- cpufreq_update_policy(cpu);
738
- }
785
+ freq_qos_update_request(&info->max_temp_freq_req,
786
+ info->high_limit / 1000);
787
+ return 0;
739788 }
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);
740796
741797 return 0;
742798 }
743799 EXPORT_SYMBOL(rockchip_monitor_cpu_high_temp_adjust);
744800
745
-void rockchip_monitor_set_boosted(void)
746
-{
747
- if (system_monitor)
748
- system_monitor->boosted = true;
749
-}
750
-EXPORT_SYMBOL(rockchip_monitor_set_boosted);
751
-
752
-void rockchip_monitor_clear_boosted(void)
753
-{
754
- if (system_monitor)
755
- system_monitor->boosted = false;
756
-}
757
-EXPORT_SYMBOL(rockchip_monitor_clear_boosted);
758
-
759
-static int rockchip_monitor_update_devfreq(struct devfreq *df)
760
-{
761
- int ret = 0;
762
-
763
-#ifdef CONFIG_PM_DEVFREQ
764
- mutex_lock(&df->lock);
765
- ret = update_devfreq(df);
766
- mutex_unlock(&df->lock);
767
-#endif
768
- return ret;
769
-};
770
-
771801 int rockchip_monitor_dev_low_temp_adjust(struct monitor_dev_info *info,
772802 bool is_low)
773803 {
774
- struct devfreq *df;
804
+ if (!dev_pm_qos_request_active(&info->dev_max_freq_req))
805
+ return 0;
775806
776
- if (info->low_limit) {
777
- if (is_low)
778
- info->wide_temp_limit = info->low_limit;
779
- else
780
- info->wide_temp_limit = 0;
781
- }
807
+ if (!info->low_limit)
808
+ return 0;
782809
783
- if (info->devp && info->devp->data) {
784
- df = (struct devfreq *)info->devp->data;
785
- rockchip_monitor_update_devfreq(df);
786
- } else if (info->devp && info->devp->low_temp_adjust_volt) {
787
- info->devp->low_temp_adjust_volt(info);
788
- }
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);
789816
790817 return 0;
791818 }
....@@ -794,26 +821,24 @@
794821 int rockchip_monitor_dev_high_temp_adjust(struct monitor_dev_info *info,
795822 bool is_high)
796823 {
797
- 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;
798829
799830 if (info->high_limit_table) {
800
- info->wide_temp_limit = info->high_limit;
801
- if (info->devp && info->devp->data) {
802
- df = (struct devfreq *)info->devp->data;
803
- rockchip_monitor_update_devfreq(df);
804
- }
805
- } else {
806
- if (info->high_limit) {
807
- if (is_high)
808
- info->wide_temp_limit = info->high_limit;
809
- else
810
- info->wide_temp_limit = 0;
811
- if (info->devp && info->devp->data) {
812
- df = (struct devfreq *)info->devp->data;
813
- rockchip_monitor_update_devfreq(df);
814
- }
815
- }
831
+ dev_pm_qos_update_request(&info->dev_max_freq_req,
832
+ info->high_limit / 1000);
833
+ return 0;
816834 }
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);
817842
818843 return 0;
819844 }
....@@ -833,6 +858,8 @@
833858
834859 mutex_lock(&opp_table->lock);
835860 list_for_each_entry(opp, &opp_table->opp_list, node) {
861
+ if (!opp->available)
862
+ continue;
836863 if (is_low_temp) {
837864 if (opp->supplies[0].u_volt_max <
838865 info->opp_table[i].low_temp_volt)
....@@ -841,11 +868,29 @@
841868 opp->supplies[0].u_volt =
842869 info->opp_table[i].low_temp_volt;
843870 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
+ }
844881 } else {
845882 opp->supplies[0].u_volt_min = info->opp_table[i].volt;
846883 opp->supplies[0].u_volt = opp->supplies[0].u_volt_min;
847884 opp->supplies[0].u_volt_max =
848885 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
+ }
849894 }
850895 i++;
851896 }
....@@ -860,6 +905,7 @@
860905 bool is_low)
861906 {
862907 struct monitor_dev_profile *devp = info->devp;
908
+ struct arm_smccc_res res;
863909 int ret = 0;
864910
865911 dev_dbg(info->dev, "low_temp %d\n", is_low);
....@@ -871,6 +917,20 @@
871917 ret = devp->low_temp_adjust(info, is_low);
872918 if (!ret)
873919 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
+ }
874934 }
875935
876936 static void rockchip_high_temp_adjust(struct monitor_dev_info *info,
....@@ -879,12 +939,14 @@
879939 struct monitor_dev_profile *devp = info->devp;
880940 int ret = 0;
881941
942
+ if (!devp->high_temp_adjust)
943
+ return;
944
+
882945 if (info->high_limit_table) {
883946 devp->high_temp_adjust(info, is_high);
884947 } else {
885948 dev_dbg(info->dev, "high_temp %d\n", is_high);
886
- if (devp->high_temp_adjust)
887
- ret = devp->high_temp_adjust(info, is_high);
949
+ ret = devp->high_temp_adjust(info, is_high);
888950 if (!ret)
889951 info->is_high_temp = is_high;
890952 }
....@@ -895,7 +957,7 @@
895957 struct monitor_dev_info *info = NULL, *tmp;
896958
897959 list_for_each_entry(tmp, &monitor_dev_list, node) {
898
- if (tmp->devp->type != MONITOR_TPYE_CPU)
960
+ if (tmp->devp->type != MONITOR_TYPE_CPU)
899961 continue;
900962 if (cpumask_test_cpu(cpu, &tmp->devp->allowed_cpus)) {
901963 info = tmp;
....@@ -962,6 +1024,11 @@
9621024 {
9631025 int ret, temp;
9641026
1027
+ if (!info->opp_table)
1028
+ return;
1029
+ if (!system_monitor->tz)
1030
+ return;
1031
+
9651032 /*
9661033 * set the init state to low temperature that the voltage will be enough
9671034 * when cpu up at low temperature.
....@@ -969,7 +1036,6 @@
9691036 if (!info->is_low_temp) {
9701037 if (info->opp_table)
9711038 rockchip_adjust_low_temp_opp_volt(info, true);
972
- info->wide_temp_limit = info->low_limit;
9731039 info->is_low_temp = true;
9741040 }
9751041
....@@ -984,150 +1050,386 @@
9841050 if (info->opp_table)
9851051 rockchip_adjust_low_temp_opp_volt(info, false);
9861052 info->is_low_temp = false;
987
-
988
- info->wide_temp_limit = info->high_limit;
9891053 info->is_high_temp = true;
9901054 } else if (temp > (info->low_temp + info->temp_hysteresis)) {
9911055 if (info->opp_table)
9921056 rockchip_adjust_low_temp_opp_volt(info, false);
9931057 info->is_low_temp = false;
994
- info->wide_temp_limit = 0;
9951058 }
9961059 }
9971060
998
-static int system_monitor_devfreq_notifier_call(struct notifier_block *nb,
999
- unsigned long event,
1000
- void *data)
1061
+static const char *get_rdev_name(struct regulator_dev *rdev)
10011062 {
1002
- struct monitor_dev_info *info = devfreq_nb_to_monitor(nb);
1003
- struct devfreq_policy *policy = data;
1004
-
1005
- if (event != DEVFREQ_ADJUST)
1006
- return NOTIFY_DONE;
1007
-
1008
- if (info->wide_temp_limit && info->wide_temp_limit < policy->max)
1009
- devfreq_verify_within_limits(policy, 0, info->wide_temp_limit);
1010
-
1011
- 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 "";
10121069 }
10131070
1014
-static int monitor_set_freq_table(struct device *dev,
1015
- struct monitor_dev_info *info)
1071
+static void
1072
+rockchip_system_monitor_early_regulator_init(struct monitor_dev_info *info)
10161073 {
1017
- struct opp_table *opp_table;
1018
- struct dev_pm_opp *opp;
1019
- unsigned long *freq_table;
1020
- int count;
1074
+ struct regulator *reg;
1075
+ struct regulator_dev *rdev;
10211076
1022
- opp_table = dev_pm_opp_get_opp_table(dev);
1023
- if (!opp_table)
1024
- return -ENOMEM;
1077
+ if (!info->early_min_volt || !info->regulators)
1078
+ return;
10251079
1026
- /* Initialize the freq_table from OPP table */
1027
- count = dev_pm_opp_get_opp_count(dev);
1028
- if (count <= 0)
1029
- return -EINVAL;
1030
-
1031
- info->max_state = count;
1032
- freq_table = kcalloc(count, sizeof(*info->freq_table), GFP_KERNEL);
1033
- if (!freq_table) {
1034
- info->max_state = 0;
1035
- 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;
10361086 }
1087
+}
10371088
1038
- mutex_lock(&opp_table->lock);
1039
- list_for_each_entry(opp, &opp_table->opp_list, node) {
1040
- if (opp->available) {
1041
- count--;
1042
- 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;
10431146 }
10441147 }
1045
- 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
+ }
10461182
10471183 dev_pm_opp_put_opp_table(opp_table);
10481184
1049
- 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
+ }
10501215
10511216 return 0;
10521217 }
10531218
1054
-static unsigned long monitor_freq_to_state(struct monitor_dev_info *info,
1055
- unsigned long freq)
1219
+static void rockchip_monitor_disable_opp_clk(struct device *dev,
1220
+ struct rockchip_opp_info *opp_info)
10561221 {
1057
- unsigned long i = 0;
1222
+ if (!opp_info)
1223
+ return;
10581224
1059
- if (!info->freq_table) {
1060
- dev_info(info->dev, "failed to get freq_table");
1061
- return 0;
1062
- }
1063
-
1064
- for (i = 0; i < info->max_state; i++) {
1065
- if (freq >= info->freq_table[i])
1066
- return i;
1067
- }
1068
-
1069
- return info->max_state - 1;
1225
+ clk_bulk_disable_unprepare(opp_info->num_clks, opp_info->clks);
10701226 }
10711227
1072
-static int monitor_temp_to_state(struct monitor_dev_info *info,
1073
- 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)
10741233 {
1075
- unsigned long target_state, target_freq = 0;
1076
- int i;
1234
+ struct dev_pm_set_opp_data *data;
1235
+ int size;
10771236
1078
- 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)
10791271 return 0;
1080
- if (info->temp_freq_table) {
1081
- for (i = 0; info->temp_freq_table[i].freq != UINT_MAX; i++) {
1082
- if (temp > info->temp_freq_table[i].temp)
1083
- 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;
10841295 }
1085
- if (target_freq)
1086
- *state = monitor_freq_to_state(info,
1087
- target_freq * 1000);
1088
- else
1089
- *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;
10901318 }
10911319
1092
- if (info->status_min_limit) {
1093
- target_freq = info->status_min_limit * 1000;
1094
- target_state = monitor_freq_to_state(info, target_freq);
1095
- if (*state > target_state)
1096
- *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
+ }
10971329 }
1330
+ rockchip_monitor_enable_opp_clk(dev, opp_info);
1331
+ rockchip_get_read_margin(dev, opp_info, new_volt, &target_rm);
10981332
1099
- 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;
11001419 }
1101
-
1102
-int
1103
-rockchip_system_monitor_adjust_cdev_state(struct thermal_cooling_device *cdev,
1104
- int temp, unsigned long *state)
1105
-{
1106
- struct monitor_dev_info *info;
1107
-
1108
- down_read(&mdev_list_sem);
1109
- list_for_each_entry(info, &monitor_dev_list, node) {
1110
- if (cdev->np != info->dev->of_node)
1111
- continue;
1112
- monitor_temp_to_state(info, temp, state);
1113
- break;
1114
- }
1115
- up_read(&mdev_list_sem);
1116
-
1117
- return 0;
1118
-}
1119
-EXPORT_SYMBOL(rockchip_system_monitor_adjust_cdev_state);
1420
+EXPORT_SYMBOL(rockchip_monitor_check_rate_volt);
11201421
11211422 struct monitor_dev_info *
11221423 rockchip_system_monitor_register(struct device *dev,
11231424 struct monitor_dev_profile *devp)
11241425 {
11251426 struct monitor_dev_info *info;
1126
- struct devfreq *devfreq;
1127
- int ret;
11281427
11291428 if (!system_monitor)
11301429 return ERR_PTR(-ENOMEM);
1430
+
1431
+ if (!devp)
1432
+ return ERR_PTR(-EINVAL);
11311433
11321434 info = kzalloc(sizeof(*info), GFP_KERNEL);
11331435 if (!info)
....@@ -1135,39 +1437,33 @@
11351437 info->dev = dev;
11361438 info->devp = devp;
11371439
1138
- ret = monitor_device_parse_dt(dev, info);
1139
- if (ret)
1140
- goto free_info;
1440
+ mutex_init(&info->volt_adjust_mutex);
11411441
1142
- monitor_set_freq_table(dev, info);
1143
-
1144
- if (info->devp->type == MONITOR_TPYE_DEV && info->devp->data) {
1145
- info->devfreq_nb.notifier_call =
1146
- system_monitor_devfreq_notifier_call;
1147
- devfreq = (struct devfreq *)info->devp->data;
1148
- devm_devfreq_register_notifier(dev, devfreq, &info->devfreq_nb,
1149
- 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);
11501449 }
11511450
1451
+ rockchip_system_monitor_early_regulator_init(info);
11521452 rockchip_system_monitor_wide_temp_init(info);
1153
- 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);
11541456
11551457 down_write(&mdev_list_sem);
11561458 list_add(&info->node, &monitor_dev_list);
11571459 up_write(&mdev_list_sem);
11581460
11591461 return info;
1160
-
1161
-free_info:
1162
- kfree(info);
1163
- return ERR_PTR(-EINVAL);
11641462 }
11651463 EXPORT_SYMBOL(rockchip_system_monitor_register);
11661464
11671465 void rockchip_system_monitor_unregister(struct monitor_dev_info *info)
11681466 {
1169
- struct devfreq *devfreq;
1170
-
11711467 if (!info)
11721468 return;
11731469
....@@ -1175,19 +1471,59 @@
11751471 list_del(&info->node);
11761472 up_write(&mdev_list_sem);
11771473
1178
- if (info->devp->type == MONITOR_TPYE_DEV && info->devp->data) {
1179
- devfreq = (struct devfreq *)info->devp->data;
1180
- devm_devfreq_unregister_notifier(info->dev, devfreq,
1181
- &info->devfreq_nb,
1182
- 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);
11831484 }
11841485
11851486 kfree(info->low_temp_adjust_table);
11861487 kfree(info->opp_table);
1187
- kfree(info->freq_table);
1488
+ kfree(info->set_opp_data);
11881489 kfree(info);
11891490 }
11901491 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
+};
11911527
11921528 static int rockchip_system_monitor_parse_dt(struct system_monitor *monitor)
11931529 {
....@@ -1195,7 +1531,7 @@
11951531 const char *tz_name, *buf = NULL;
11961532
11971533 if (of_property_read_string(np, "rockchip,video-4k-offline-cpus", &buf))
1198
- cpumask_clear(&system_monitor->video_4k_offline_cpus);
1534
+ cpumask_clear(&monitor->video_4k_offline_cpus);
11991535 else
12001536 cpulist_parse(buf, &monitor->video_4k_offline_cpus);
12011537
....@@ -1221,6 +1557,13 @@
12211557 system_monitor->offline_cpus_temp = INT_MAX;
12221558 of_property_read_u32(np, "rockchip,temp-hysteresis",
12231559 &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
+
12241567 out:
12251568 return 0;
12261569 }
....@@ -1246,7 +1589,7 @@
12461589 cpumask_copy(&system_monitor->offline_cpus, &offline_cpus);
12471590 for_each_cpu(cpu, &system_monitor->offline_cpus) {
12481591 if (cpu_online(cpu))
1249
- cpu_down(cpu);
1592
+ remove_cpu(cpu);
12501593 }
12511594
12521595 cpumask_clear(&online_cpus);
....@@ -1256,7 +1599,7 @@
12561599 if (cpumask_empty(&online_cpus))
12571600 goto out;
12581601 for_each_cpu(cpu, &online_cpus)
1259
- cpu_up(cpu);
1602
+ add_cpu(cpu);
12601603
12611604 out:
12621605 mutex_unlock(&cpu_on_off_mutex);
....@@ -1288,7 +1631,6 @@
12881631 {
12891632 int temp, ret;
12901633 struct monitor_dev_info *info;
1291
- static int last_temp = INT_MAX;
12921634
12931635 ret = thermal_zone_get_temp(system_monitor->tz, &temp);
12941636 if (ret || temp == THERMAL_TEMP_INVALID)
....@@ -1296,9 +1638,12 @@
12961638
12971639 dev_dbg(system_monitor->dev, "temperature=%d\n", temp);
12981640
1299
- if (temp < last_temp && last_temp - temp <= 2000)
1641
+ if (temp < system_monitor->last_temp &&
1642
+ system_monitor->last_temp - temp <= 2000)
13001643 goto out;
1301
- last_temp = temp;
1644
+ system_monitor->last_temp = temp;
1645
+
1646
+ rockchip_system_monitor_temp_notify(temp);
13021647
13031648 down_read(&mdev_list_sem);
13041649 list_for_each_entry(info, &monitor_dev_list, node)
....@@ -1324,26 +1669,32 @@
13241669 unsigned long status)
13251670 {
13261671 unsigned int target_freq = 0;
1327
- bool is_freq_fixed = false;
1328
- 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;
13291677
13301678 if (status & SYS_STATUS_REBOOT) {
1331
- info->status_min_limit = info->reboot_freq;
1332
- info->status_max_limit = info->reboot_freq;
1333
- info->is_status_freq_fixed = true;
1334
- 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;
13351684 }
13361685
13371686 if (info->video_4k_freq && (status & SYS_STATUS_VIDEO_4K))
13381687 target_freq = info->video_4k_freq;
1339
- if (target_freq == info->status_max_limit &&
1340
- info->is_status_freq_fixed == is_freq_fixed)
1688
+
1689
+ if (target_freq == info->status_max_limit)
13411690 return;
13421691 info->status_max_limit = target_freq;
1343
- info->is_status_freq_fixed = is_freq_fixed;
1344
-next:
1345
- cpu = cpumask_any(&info->devp->allowed_cpus);
1346
- 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);
13471698 }
13481699
13491700 static void rockchip_system_status_limit_freq(unsigned long status)
....@@ -1352,7 +1703,7 @@
13521703
13531704 down_read(&mdev_list_sem);
13541705 list_for_each_entry(info, &monitor_dev_list, node) {
1355
- if (info->devp->type == MONITOR_TPYE_CPU)
1706
+ if (info->devp->type == MONITOR_TYPE_CPU)
13561707 rockchip_system_status_cpu_limit_freq(info, status);
13571708 }
13581709 up_read(&mdev_list_sem);
....@@ -1386,6 +1737,26 @@
13861737 return NOTIFY_OK;
13871738 }
13881739
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
+
13891760 static int monitor_pm_notify(struct notifier_block *nb,
13901761 unsigned long mode, void *_unused)
13911762 {
....@@ -1394,6 +1765,7 @@
13941765 case PM_RESTORE_PREPARE:
13951766 case PM_SUSPEND_PREPARE:
13961767 atomic_set(&monitor_in_suspend, 1);
1768
+ rockchip_system_monitor_set_cpu_uevent_suppress(true);
13971769 break;
13981770 case PM_POST_HIBERNATION:
13991771 case PM_POST_RESTORE:
....@@ -1401,6 +1773,8 @@
14011773 if (system_monitor->tz)
14021774 rockchip_system_monitor_thermal_update();
14031775 atomic_set(&monitor_in_suspend, 0);
1776
+ rockchip_system_monitor_set_cpu_uevent_suppress(false);
1777
+ system_monitor->last_temp = INT_MAX;
14041778 break;
14051779 default:
14061780 break;
....@@ -1410,52 +1784,6 @@
14101784
14111785 static struct notifier_block monitor_pm_nb = {
14121786 .notifier_call = monitor_pm_notify,
1413
-};
1414
-
1415
-static int rockchip_monitor_cpufreq_policy_notifier(struct notifier_block *nb,
1416
- unsigned long event,
1417
- void *data)
1418
-{
1419
- struct monitor_dev_info *info;
1420
- struct cpufreq_policy *policy = data;
1421
- int cpu = policy->cpu;
1422
- unsigned int limit_freq = UINT_MAX;
1423
-
1424
- if (event != CPUFREQ_ADJUST)
1425
- return NOTIFY_OK;
1426
-
1427
- down_read(&mdev_list_sem);
1428
- list_for_each_entry(info, &monitor_dev_list, node) {
1429
- if (info->devp->type != MONITOR_TPYE_CPU)
1430
- continue;
1431
- if (!cpumask_test_cpu(cpu, &info->devp->allowed_cpus))
1432
- continue;
1433
-
1434
- if (info->wide_temp_limit) {
1435
- if (limit_freq > info->wide_temp_limit / 1000)
1436
- limit_freq = info->wide_temp_limit / 1000;
1437
- }
1438
- if (!system_monitor->boosted &&
1439
- info->status_max_limit &&
1440
- limit_freq > info->status_max_limit)
1441
- limit_freq = info->status_max_limit;
1442
-
1443
- if (info->is_status_freq_fixed) {
1444
- cpufreq_verify_within_limits(policy, limit_freq,
1445
- limit_freq);
1446
- dev_info(info->dev, "min=%u, max=%u\n", policy->min,
1447
- policy->max);
1448
- } else if (limit_freq < policy->max) {
1449
- cpufreq_verify_within_limits(policy, 0, limit_freq);
1450
- }
1451
- }
1452
- up_read(&mdev_list_sem);
1453
-
1454
- return NOTIFY_OK;
1455
-}
1456
-
1457
-static struct notifier_block rockchip_monitor_cpufreq_policy_nb = {
1458
- .notifier_call = rockchip_monitor_cpufreq_policy_notifier,
14591787 };
14601788
14611789 static int rockchip_monitor_reboot_notifier(struct notifier_block *nb,
....@@ -1477,24 +1805,15 @@
14771805 {
14781806 struct fb_event *event = ptr;
14791807
1480
- switch (action) {
1481
- case FB_EARLY_EVENT_BLANK:
1482
- switch (*((int *)event->data)) {
1483
- case FB_BLANK_UNBLANK:
1484
- rockchip_clear_system_status(SYS_STATUS_SUSPEND);
1485
- break;
1486
- default:
1487
- break;
1488
- }
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);
14891814 break;
1490
- case FB_EVENT_BLANK:
1491
- switch (*((int *)event->data)) {
1492
- case FB_BLANK_POWERDOWN:
1493
- rockchip_set_system_status(SYS_STATUS_SUSPEND);
1494
- break;
1495
- default:
1496
- break;
1497
- }
1815
+ case FB_BLANK_POWERDOWN:
1816
+ rockchip_set_system_status(SYS_STATUS_SUSPEND);
14981817 break;
14991818 default:
15001819 break;
....@@ -1517,12 +1836,6 @@
15171836 case EBC_OFF:
15181837 rockchip_set_system_status(SYS_STATUS_LOW_POWER);
15191838 break;
1520
- case EBC_FB_BLANK:
1521
- rockchip_set_system_status(SYS_STATUS_SUSPEND);
1522
- break;
1523
- case EBC_FB_UNBLANK:
1524
- rockchip_clear_system_status(SYS_STATUS_SUSPEND);
1525
- break;
15261839 default:
15271840 break;
15281841 }
....@@ -1533,6 +1846,32 @@
15331846 static struct notifier_block rockchip_monitor_ebc_nb = {
15341847 .notifier_call = rockchip_eink_devfs_notifier,
15351848 };
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);
15361875
15371876 static int rockchip_system_monitor_probe(struct platform_device *pdev)
15381877 {
....@@ -1555,6 +1894,7 @@
15551894
15561895 rockchip_system_monitor_parse_dt(system_monitor);
15571896 if (system_monitor->tz) {
1897
+ system_monitor->last_temp = INT_MAX;
15581898 INIT_DELAYED_WORK(&system_monitor->thermal_work,
15591899 rockchip_system_monitor_thermal_check);
15601900 mod_delayed_work(system_freezable_wq,
....@@ -1569,9 +1909,6 @@
15691909 if (register_pm_notifier(&monitor_pm_nb))
15701910 dev_err(dev, "failed to register suspend notifier\n");
15711911
1572
- cpufreq_register_notifier(&rockchip_monitor_cpufreq_policy_nb,
1573
- CPUFREQ_POLICY_NOTIFIER);
1574
-
15751912 register_reboot_notifier(&rockchip_monitor_reboot_nb);
15761913
15771914 if (fb_register_client(&rockchip_monitor_fb_nb))
....@@ -1579,6 +1916,9 @@
15791916
15801917 ebc_register_notifier(&rockchip_monitor_ebc_nb);
15811918
1919
+ schedule_delayed_work(&system_monitor_early_min_volt_work,
1920
+ msecs_to_jiffies(30000));
1921
+
15821922 dev_info(dev, "system monitor probe\n");
15831923
15841924 return 0;