1 /* SPDX-License-Identifier: (GPL-2.0+ OR MIT) */
2 /*
3 * Copyright (C) 2019, Fuzhou Rockchip Electronics Co., Ltd
4 */
5
6 #ifndef __SOC_ROCKCHIP_SYSTEM_MONITOR_H
7 #define __SOC_ROCKCHIP_SYSTEM_MONITOR_H
8
9 #include <linux/pm_opp.h>
10 #include <linux/pm_qos.h>
11 #include <linux/regulator/consumer.h>
12
13 enum monitor_dev_type {
14 MONITOR_TYPE_CPU = 0, /* CPU */
15 MONITOR_TYPE_DEV, /* GPU, NPU, DMC, and so on */
16 };
17
18 enum system_monitor_event_type {
19 SYSTEM_MONITOR_CHANGE_TEMP = 0,
20 };
21
22 struct system_monitor_event_data {
23 int temp;
24 };
25
26 struct volt_adjust_table {
27 unsigned int min; /* Minimum frequency in MHz */
28 unsigned int max; /* Maximum frequency in MHz */
29 int volt; /* Voltage in microvolt */
30 };
31
32 struct temp_freq_table {
33 int temp; /* millicelsius */
34 unsigned int freq; /* KHz */
35 };
36
37 /**
38 * struct temp_opp_table - System monitor device OPP description structure
39 * @rate: Frequency in hertz
40 * @volt: Target voltage in microvolt
41 * @mem_volt: Target voltage for memory in microvolt
42 * @low_temp_volt: Target voltage when low temperature, in microvolt
43 * @low_temp_mem_volt: Target voltage for memory when low temperature,
44 * in microvolt
45 * @max_volt: Maximum voltage in microvolt
46 * @max_mem_volt: Maximum voltage for memory in microvolt
47 */
48 struct temp_opp_table {
49 unsigned long rate;
50 unsigned long volt;
51 unsigned long mem_volt;
52 unsigned long low_temp_volt;
53 unsigned long low_temp_mem_volt;
54 unsigned long max_volt;
55 unsigned long max_mem_volt;
56 };
57
58 /**
59 * struct monitor_dev_info - structure for a system monitor device
60 * @dev: Device registered by system monitor
61 * @low_temp_adjust_table: Voltage margin for different OPPs when lowe
62 * temperature
63 * @opp_table: Frequency and voltage information of device
64 * @devp: Device-specific system monitor profile
65 * @node: Node in monitor_dev_list
66 * @high_limit_table: Limit maximum frequency at different temperature,
67 * but the frequency is also changed by thermal framework.
68 * @volt_adjust_mutex: A mutex to protect changing voltage.
69 * @max_temp_freq_req: CPU maximum frequency constraint changed according
70 * to temperature.
71 * @min_sta_freq_req: CPU minimum frequency constraint changed according
72 * to system status.
73 * @max_sta_freq_req: CPU maximum frequency constraint changed according
74 * to system status.
75 * @dev_max_freq_req: Devices maximum frequency constraint changed according
76 * to temperature.
77 * @low_limit: Limit maximum frequency when low temperature, in Hz
78 * @high_limit: Limit maximum frequency when high temperature, in Hz
79 * @max_volt: Maximum voltage in microvolt
80 * @low_temp_min_volt: Minimum voltage of OPPs when low temperature, in
81 * microvolt
82 * @high_temp_max_volt: Maximum voltage when high temperature, in microvolt
83 * @wide_temp_limit: Target maximum frequency when low or high temperature,
84 * in Hz
85 * @video_4k_freq: Maximum frequency when paly 4k video, in KHz
86 * @reboot_freq: Limit maximum and minimum frequency when reboot, in KHz
87 * @status_min_limit: Minimum frequency of some status frequency, in KHz
88 * @status_max_limit: Minimum frequency of all status frequency, in KHz
89 * @low_temp: Low temperature trip point, in millicelsius
90 * @high_temp: High temperature trip point, in millicelsius
91 * @temp_hysteresis: A low hysteresis value on low_temp, in millicelsius
92 * @is_low_temp: True if current temperature less than low_temp
93 * @is_high_temp: True if current temperature greater than high_temp
94 * @is_low_temp_enabled: True if device node contains low temperature
95 * configuration
96 * @is_status_freq_fixed: True if enter into some status
97 */
98 struct monitor_dev_info {
99 struct device *dev;
100 struct volt_adjust_table *low_temp_adjust_table;
101 struct temp_opp_table *opp_table;
102 struct monitor_dev_profile *devp;
103 struct list_head node;
104 struct temp_freq_table *high_limit_table;
105 struct mutex volt_adjust_mutex;
106 struct freq_qos_request max_temp_freq_req;
107 struct freq_qos_request min_sta_freq_req;
108 struct freq_qos_request max_sta_freq_req;
109 struct dev_pm_qos_request dev_max_freq_req;
110 struct regulator *early_reg;
111 struct regulator **regulators;
112 struct dev_pm_set_opp_data *set_opp_data;
113 struct clk *clk;
114 unsigned long low_limit;
115 unsigned long high_limit;
116 unsigned long max_volt;
117 unsigned long low_temp_min_volt;
118 unsigned long high_temp_max_volt;
119 unsigned int video_4k_freq;
120 unsigned int reboot_freq;
121 unsigned int init_freq;
122 unsigned int status_min_limit;
123 unsigned int status_max_limit;
124 unsigned int early_min_volt;
125 unsigned int regulator_count;
126 int low_temp;
127 int high_temp;
128 int temp_hysteresis;
129 bool is_low_temp;
130 bool is_high_temp;
131 bool is_low_temp_enabled;
132 };
133
134 struct monitor_dev_profile {
135 enum monitor_dev_type type;
136 void *data;
137 bool is_checked;
138 int (*low_temp_adjust)(struct monitor_dev_info *info, bool is_low);
139 int (*high_temp_adjust)(struct monitor_dev_info *info, bool is_low);
140 int (*update_volt)(struct monitor_dev_info *info);
141 int (*set_opp)(struct dev_pm_set_opp_data *data);
142 struct cpumask allowed_cpus;
143 struct rockchip_opp_info *opp_info;
144 };
145
146 #if IS_REACHABLE(CONFIG_ROCKCHIP_SYSTEM_MONITOR)
147 struct monitor_dev_info *
148 rockchip_system_monitor_register(struct device *dev,
149 struct monitor_dev_profile *devp);
150 void rockchip_system_monitor_unregister(struct monitor_dev_info *info);
151 int rockchip_monitor_cpu_low_temp_adjust(struct monitor_dev_info *info,
152 bool is_low);
153 int rockchip_monitor_cpu_high_temp_adjust(struct monitor_dev_info *info,
154 bool is_high);
155 void rockchip_monitor_volt_adjust_lock(struct monitor_dev_info *info);
156 void rockchip_monitor_volt_adjust_unlock(struct monitor_dev_info *info);
157 int rockchip_monitor_check_rate_volt(struct monitor_dev_info *info);
158 int rockchip_monitor_dev_low_temp_adjust(struct monitor_dev_info *info,
159 bool is_low);
160 int rockchip_monitor_dev_high_temp_adjust(struct monitor_dev_info *info,
161 bool is_high);
162 int rockchip_monitor_suspend_low_temp_adjust(int cpu);
163 int rockchip_system_monitor_register_notifier(struct notifier_block *nb);
164 void rockchip_system_monitor_unregister_notifier(struct notifier_block *nb);
165 #else
166 static inline struct monitor_dev_info *
rockchip_system_monitor_register(struct device * dev,struct monitor_dev_profile * devp)167 rockchip_system_monitor_register(struct device *dev,
168 struct monitor_dev_profile *devp)
169 {
170 return ERR_PTR(-ENOTSUPP);
171 };
172
173 static inline void
rockchip_system_monitor_unregister(struct monitor_dev_info * info)174 rockchip_system_monitor_unregister(struct monitor_dev_info *info)
175 {
176 }
177
178 static inline int
rockchip_monitor_cpu_low_temp_adjust(struct monitor_dev_info * info,bool is_low)179 rockchip_monitor_cpu_low_temp_adjust(struct monitor_dev_info *info, bool is_low)
180 {
181 return 0;
182 };
183
184 static inline int
rockchip_monitor_cpu_high_temp_adjust(struct monitor_dev_info * info,bool is_high)185 rockchip_monitor_cpu_high_temp_adjust(struct monitor_dev_info *info,
186 bool is_high)
187 {
188 return 0;
189 };
190
191 static inline void
rockchip_monitor_volt_adjust_lock(struct monitor_dev_info * info)192 rockchip_monitor_volt_adjust_lock(struct monitor_dev_info *info)
193 {
194 }
195
196 static inline void
rockchip_monitor_volt_adjust_unlock(struct monitor_dev_info * info)197 rockchip_monitor_volt_adjust_unlock(struct monitor_dev_info *info)
198 {
199 }
200
201 static inline int
rockchip_monitor_check_rate_volt(struct monitor_dev_info * info)202 rockchip_monitor_check_rate_volt(struct monitor_dev_info *info)
203 {
204 return 0;
205 }
206
207 static inline int
rockchip_monitor_dev_low_temp_adjust(struct monitor_dev_info * info,bool is_low)208 rockchip_monitor_dev_low_temp_adjust(struct monitor_dev_info *info, bool is_low)
209 {
210 return 0;
211 };
212
213 static inline int
rockchip_monitor_dev_high_temp_adjust(struct monitor_dev_info * info,bool is_high)214 rockchip_monitor_dev_high_temp_adjust(struct monitor_dev_info *info,
215 bool is_high)
216 {
217 return 0;
218 };
219
rockchip_monitor_suspend_low_temp_adjust(int cpu)220 static inline int rockchip_monitor_suspend_low_temp_adjust(int cpu)
221 {
222 return 0;
223 };
224
225 static inline int
rockchip_system_monitor_register_notifier(struct notifier_block * nb)226 rockchip_system_monitor_register_notifier(struct notifier_block *nb)
227 {
228 return 0;
229 };
230
231 static inline void
rockchip_system_monitor_unregister_notifier(struct notifier_block * nb)232 rockchip_system_monitor_unregister_notifier(struct notifier_block *nb)
233 {
234 };
235 #endif /* CONFIG_ROCKCHIP_SYSTEM_MONITOR */
236
237 #endif
238