xref: /linux-6.15/include/linux/thermal.h (revision b4bcdff7)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *  thermal.h  ($Revision: 0 $)
4  *
5  *  Copyright (C) 2008  Intel Corp
6  *  Copyright (C) 2008  Zhang Rui <[email protected]>
7  *  Copyright (C) 2008  Sujith Thomas <[email protected]>
8  */
9 
10 #ifndef __THERMAL_H__
11 #define __THERMAL_H__
12 
13 #include <linux/of.h>
14 #include <linux/idr.h>
15 #include <linux/device.h>
16 #include <linux/sysfs.h>
17 #include <linux/workqueue.h>
18 #include <uapi/linux/thermal.h>
19 
20 /* invalid cooling state */
21 #define THERMAL_CSTATE_INVALID -1UL
22 
23 /* No upper/lower limit requirement */
24 #define THERMAL_NO_LIMIT	((u32)~0)
25 
26 /* Default weight of a bound cooling device */
27 #define THERMAL_WEIGHT_DEFAULT 0
28 
29 /* use value, which < 0K, to indicate an invalid/uninitialized temperature */
30 #define THERMAL_TEMP_INVALID	-274000
31 
32 struct thermal_zone_device;
33 struct thermal_cooling_device;
34 struct thermal_instance;
35 struct thermal_debugfs;
36 struct thermal_attr;
37 
38 enum thermal_trend {
39 	THERMAL_TREND_STABLE, /* temperature is stable */
40 	THERMAL_TREND_RAISING, /* temperature is raising */
41 	THERMAL_TREND_DROPPING, /* temperature is dropping */
42 };
43 
44 /* Thermal notification reason */
45 enum thermal_notify_event {
46 	THERMAL_EVENT_UNSPECIFIED, /* Unspecified event */
47 	THERMAL_EVENT_TEMP_SAMPLE, /* New Temperature sample */
48 	THERMAL_TRIP_VIOLATED, /* TRIP Point violation */
49 	THERMAL_TRIP_CHANGED, /* TRIP Point temperature changed */
50 	THERMAL_DEVICE_DOWN, /* Thermal device is down */
51 	THERMAL_DEVICE_UP, /* Thermal device is up after a down event */
52 	THERMAL_DEVICE_POWER_CAPABILITY_CHANGED, /* power capability changed */
53 	THERMAL_TABLE_CHANGED, /* Thermal table(s) changed */
54 	THERMAL_EVENT_KEEP_ALIVE, /* Request for user space handler to respond */
55 	THERMAL_TZ_BIND_CDEV, /* Cooling dev is bind to the thermal zone */
56 	THERMAL_TZ_UNBIND_CDEV, /* Cooling dev is unbind from the thermal zone */
57 	THERMAL_INSTANCE_WEIGHT_CHANGED, /* Thermal instance weight changed */
58 };
59 
60 /**
61  * struct thermal_trip - representation of a point in temperature domain
62  * @temperature: temperature value in miliCelsius
63  * @hysteresis: relative hysteresis in miliCelsius
64  * @type: trip point type
65  * @priv: pointer to driver data associated with this trip
66  * @flags: flags representing binary properties of the trip
67  */
68 struct thermal_trip {
69 	int temperature;
70 	int hysteresis;
71 	enum thermal_trip_type type;
72 	u8 flags;
73 	void *priv;
74 };
75 
76 #define THERMAL_TRIP_FLAG_RW_TEMP	BIT(0)
77 #define THERMAL_TRIP_FLAG_RW_HYST	BIT(1)
78 
79 #define THERMAL_TRIP_FLAG_RW	(THERMAL_TRIP_FLAG_RW_TEMP | \
80 				 THERMAL_TRIP_FLAG_RW_HYST)
81 
82 #define THERMAL_TRIP_PRIV_TO_INT(_val_)	(uintptr_t)(_val_)
83 #define THERMAL_INT_TO_TRIP_PRIV(_val_)	(void *)(uintptr_t)(_val_)
84 
85 struct thermal_zone_device;
86 
87 struct thermal_zone_device_ops {
88 	int (*bind) (struct thermal_zone_device *,
89 		     struct thermal_cooling_device *);
90 	int (*unbind) (struct thermal_zone_device *,
91 		       struct thermal_cooling_device *);
92 	int (*get_temp) (struct thermal_zone_device *, int *);
93 	int (*set_trips) (struct thermal_zone_device *, int, int);
94 	int (*change_mode) (struct thermal_zone_device *,
95 		enum thermal_device_mode);
96 	int (*set_trip_temp) (struct thermal_zone_device *,
97 			      const struct thermal_trip *, int);
98 	int (*get_crit_temp) (struct thermal_zone_device *, int *);
99 	int (*set_emul_temp) (struct thermal_zone_device *, int);
100 	int (*get_trend) (struct thermal_zone_device *,
101 			  const struct thermal_trip *, enum thermal_trend *);
102 	void (*hot)(struct thermal_zone_device *);
103 	void (*critical)(struct thermal_zone_device *);
104 };
105 
106 struct thermal_cooling_device_ops {
107 	int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
108 	int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
109 	int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
110 	int (*get_requested_power)(struct thermal_cooling_device *, u32 *);
111 	int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *);
112 	int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *);
113 };
114 
115 struct thermal_cooling_device {
116 	int id;
117 	const char *type;
118 	unsigned long max_state;
119 	struct device device;
120 	struct device_node *np;
121 	void *devdata;
122 	void *stats;
123 	const struct thermal_cooling_device_ops *ops;
124 	bool updated; /* true if the cooling device does not need update */
125 	struct mutex lock; /* protect thermal_instances list */
126 	struct list_head thermal_instances;
127 	struct list_head node;
128 #ifdef CONFIG_THERMAL_DEBUGFS
129 	struct thermal_debugfs *debugfs;
130 #endif
131 };
132 
133 /* Structure to define Thermal Zone parameters */
134 struct thermal_zone_params {
135 	const char *governor_name;
136 
137 	/*
138 	 * a boolean to indicate if the thermal to hwmon sysfs interface
139 	 * is required. when no_hwmon == false, a hwmon sysfs interface
140 	 * will be created. when no_hwmon == true, nothing will be done
141 	 */
142 	bool no_hwmon;
143 
144 	/*
145 	 * Sustainable power (heat) that this thermal zone can dissipate in
146 	 * mW
147 	 */
148 	u32 sustainable_power;
149 
150 	/*
151 	 * Proportional parameter of the PID controller when
152 	 * overshooting (i.e., when temperature is below the target)
153 	 */
154 	s32 k_po;
155 
156 	/*
157 	 * Proportional parameter of the PID controller when
158 	 * undershooting
159 	 */
160 	s32 k_pu;
161 
162 	/* Integral parameter of the PID controller */
163 	s32 k_i;
164 
165 	/* Derivative parameter of the PID controller */
166 	s32 k_d;
167 
168 	/* threshold below which the error is no longer accumulated */
169 	s32 integral_cutoff;
170 
171 	/*
172 	 * @slope:	slope of a linear temperature adjustment curve.
173 	 * 		Used by thermal zone drivers.
174 	 */
175 	int slope;
176 	/*
177 	 * @offset:	offset of a linear temperature adjustment curve.
178 	 * 		Used by thermal zone drivers (default 0).
179 	 */
180 	int offset;
181 };
182 
183 /* Function declarations */
184 #ifdef CONFIG_THERMAL_OF
185 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
186 							  const struct thermal_zone_device_ops *ops);
187 
188 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz);
189 
190 #else
191 
192 static inline
193 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
194 							  const struct thermal_zone_device_ops *ops)
195 {
196 	return ERR_PTR(-ENOTSUPP);
197 }
198 
199 static inline void devm_thermal_of_zone_unregister(struct device *dev,
200 						   struct thermal_zone_device *tz)
201 {
202 }
203 #endif
204 
205 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
206 			  struct thermal_trip *trip);
207 int for_each_thermal_trip(struct thermal_zone_device *tz,
208 			  int (*cb)(struct thermal_trip *, void *),
209 			  void *data);
210 int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
211 			       int (*cb)(struct thermal_trip *, void *),
212 			       void *data);
213 int thermal_zone_get_num_trips(struct thermal_zone_device *tz);
214 void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
215 				struct thermal_trip *trip, int temp);
216 
217 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);
218 
219 #ifdef CONFIG_THERMAL
220 struct thermal_zone_device *thermal_zone_device_register_with_trips(
221 					const char *type,
222 					const struct thermal_trip *trips,
223 					int num_trips, void *devdata,
224 					const struct thermal_zone_device_ops *ops,
225 					const struct thermal_zone_params *tzp,
226 					unsigned int passive_delay,
227 					unsigned int polling_delay);
228 
229 struct thermal_zone_device *thermal_tripless_zone_device_register(
230 					const char *type,
231 					void *devdata,
232 					const struct thermal_zone_device_ops *ops,
233 					const struct thermal_zone_params *tzp);
234 
235 void thermal_zone_device_unregister(struct thermal_zone_device *tz);
236 
237 void *thermal_zone_device_priv(struct thermal_zone_device *tzd);
238 const char *thermal_zone_device_type(struct thermal_zone_device *tzd);
239 int thermal_zone_device_id(struct thermal_zone_device *tzd);
240 struct device *thermal_zone_device(struct thermal_zone_device *tzd);
241 
242 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
243 			      const struct thermal_trip *trip,
244 			      struct thermal_cooling_device *cdev,
245 			      unsigned long upper, unsigned long lower,
246 			      unsigned int weight);
247 int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
248 				     struct thermal_cooling_device *,
249 				     unsigned long, unsigned long,
250 				     unsigned int);
251 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
252 				  const struct thermal_trip *trip,
253 				  struct thermal_cooling_device *cdev);
254 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
255 				       struct thermal_cooling_device *);
256 void thermal_zone_device_update(struct thermal_zone_device *,
257 				enum thermal_notify_event);
258 
259 struct thermal_cooling_device *thermal_cooling_device_register(const char *,
260 		void *, const struct thermal_cooling_device_ops *);
261 struct thermal_cooling_device *
262 thermal_of_cooling_device_register(struct device_node *np, const char *, void *,
263 				   const struct thermal_cooling_device_ops *);
264 struct thermal_cooling_device *
265 devm_thermal_of_cooling_device_register(struct device *dev,
266 				struct device_node *np,
267 				const char *type, void *devdata,
268 				const struct thermal_cooling_device_ops *ops);
269 void thermal_cooling_device_update(struct thermal_cooling_device *);
270 void thermal_cooling_device_unregister(struct thermal_cooling_device *);
271 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
272 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
273 int thermal_zone_get_slope(struct thermal_zone_device *tz);
274 int thermal_zone_get_offset(struct thermal_zone_device *tz);
275 bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz,
276 				   const struct thermal_trip *trip,
277 				   struct thermal_cooling_device *cdev);
278 
279 int thermal_zone_device_enable(struct thermal_zone_device *tz);
280 int thermal_zone_device_disable(struct thermal_zone_device *tz);
281 void thermal_zone_device_critical(struct thermal_zone_device *tz);
282 #else
283 static inline struct thermal_zone_device *thermal_zone_device_register_with_trips(
284 					const char *type,
285 					const struct thermal_trip *trips,
286 					int num_trips, void *devdata,
287 					const struct thermal_zone_device_ops *ops,
288 					const struct thermal_zone_params *tzp,
289 					int passive_delay, int polling_delay)
290 { return ERR_PTR(-ENODEV); }
291 
292 static inline struct thermal_zone_device *thermal_tripless_zone_device_register(
293 					const char *type,
294 					void *devdata,
295 					struct thermal_zone_device_ops *ops,
296 					const struct thermal_zone_params *tzp)
297 { return ERR_PTR(-ENODEV); }
298 
299 static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz)
300 { }
301 
302 static inline struct thermal_cooling_device *
303 thermal_cooling_device_register(const char *type, void *devdata,
304 	const struct thermal_cooling_device_ops *ops)
305 { return ERR_PTR(-ENODEV); }
306 static inline struct thermal_cooling_device *
307 thermal_of_cooling_device_register(struct device_node *np,
308 	const char *type, void *devdata,
309 	const struct thermal_cooling_device_ops *ops)
310 { return ERR_PTR(-ENODEV); }
311 static inline struct thermal_cooling_device *
312 devm_thermal_of_cooling_device_register(struct device *dev,
313 				struct device_node *np,
314 				const char *type, void *devdata,
315 				const struct thermal_cooling_device_ops *ops)
316 {
317 	return ERR_PTR(-ENODEV);
318 }
319 static inline void thermal_cooling_device_unregister(
320 	struct thermal_cooling_device *cdev)
321 { }
322 static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
323 		const char *name)
324 { return ERR_PTR(-ENODEV); }
325 static inline int thermal_zone_get_temp(
326 		struct thermal_zone_device *tz, int *temp)
327 { return -ENODEV; }
328 static inline int thermal_zone_get_slope(
329 		struct thermal_zone_device *tz)
330 { return -ENODEV; }
331 static inline int thermal_zone_get_offset(
332 		struct thermal_zone_device *tz)
333 { return -ENODEV; }
334 
335 static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz)
336 {
337 	return NULL;
338 }
339 
340 static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
341 {
342 	return NULL;
343 }
344 
345 static inline int thermal_zone_device_id(struct thermal_zone_device *tzd)
346 {
347 	return -ENODEV;
348 }
349 
350 static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
351 { return -ENODEV; }
352 
353 static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
354 { return -ENODEV; }
355 #endif /* CONFIG_THERMAL */
356 
357 #endif /* __THERMAL_H__ */
358