xref: /linux-6.15/include/linux/thermal.h (revision a0adc8ea)
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 #define THERMAL_TRIPS_NONE	-1
21 #define THERMAL_MAX_TRIPS	12
22 
23 /* invalid cooling state */
24 #define THERMAL_CSTATE_INVALID -1UL
25 
26 /* No upper/lower limit requirement */
27 #define THERMAL_NO_LIMIT	((u32)~0)
28 
29 /* Default weight of a bound cooling device */
30 #define THERMAL_WEIGHT_DEFAULT 0
31 
32 /* use value, which < 0K, to indicate an invalid/uninitialized temperature */
33 #define THERMAL_TEMP_INVALID	-274000
34 
35 struct thermal_zone_device;
36 struct thermal_cooling_device;
37 struct thermal_instance;
38 struct thermal_attr;
39 
40 enum thermal_trend {
41 	THERMAL_TREND_STABLE, /* temperature is stable */
42 	THERMAL_TREND_RAISING, /* temperature is raising */
43 	THERMAL_TREND_DROPPING, /* temperature is dropping */
44 	THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */
45 	THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */
46 };
47 
48 /* Thermal notification reason */
49 enum thermal_notify_event {
50 	THERMAL_EVENT_UNSPECIFIED, /* Unspecified event */
51 	THERMAL_EVENT_TEMP_SAMPLE, /* New Temperature sample */
52 	THERMAL_TRIP_VIOLATED, /* TRIP Point violation */
53 	THERMAL_TRIP_CHANGED, /* TRIP Point temperature changed */
54 	THERMAL_DEVICE_DOWN, /* Thermal device is down */
55 	THERMAL_DEVICE_UP, /* Thermal device is up after a down event */
56 	THERMAL_DEVICE_POWER_CAPABILITY_CHANGED, /* power capability changed */
57 	THERMAL_TABLE_CHANGED, /* Thermal table(s) changed */
58 	THERMAL_EVENT_KEEP_ALIVE, /* Request for user space handler to respond */
59 };
60 
61 struct thermal_zone_device_ops {
62 	int (*bind) (struct thermal_zone_device *,
63 		     struct thermal_cooling_device *);
64 	int (*unbind) (struct thermal_zone_device *,
65 		       struct thermal_cooling_device *);
66 	int (*get_temp) (struct thermal_zone_device *, int *);
67 	int (*set_trips) (struct thermal_zone_device *, int, int);
68 	int (*change_mode) (struct thermal_zone_device *,
69 		enum thermal_device_mode);
70 	int (*get_trip_type) (struct thermal_zone_device *, int,
71 		enum thermal_trip_type *);
72 	int (*get_trip_temp) (struct thermal_zone_device *, int, int *);
73 	int (*set_trip_temp) (struct thermal_zone_device *, int, int);
74 	int (*get_trip_hyst) (struct thermal_zone_device *, int, int *);
75 	int (*set_trip_hyst) (struct thermal_zone_device *, int, int);
76 	int (*get_crit_temp) (struct thermal_zone_device *, int *);
77 	int (*set_emul_temp) (struct thermal_zone_device *, int);
78 	int (*get_trend) (struct thermal_zone_device *, int,
79 			  enum thermal_trend *);
80 	int (*notify) (struct thermal_zone_device *, int,
81 		       enum thermal_trip_type);
82 	void (*hot)(struct thermal_zone_device *);
83 	void (*critical)(struct thermal_zone_device *);
84 };
85 
86 struct thermal_cooling_device_ops {
87 	int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
88 	int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
89 	int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
90 	int (*get_requested_power)(struct thermal_cooling_device *, u32 *);
91 	int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *);
92 	int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *);
93 };
94 
95 struct thermal_cooling_device {
96 	int id;
97 	char type[THERMAL_NAME_LENGTH];
98 	struct device device;
99 	struct device_node *np;
100 	void *devdata;
101 	void *stats;
102 	const struct thermal_cooling_device_ops *ops;
103 	bool updated; /* true if the cooling device does not need update */
104 	struct mutex lock; /* protect thermal_instances list */
105 	struct list_head thermal_instances;
106 	struct list_head node;
107 };
108 
109 /**
110  * struct thermal_zone_device - structure for a thermal zone
111  * @id:		unique id number for each thermal zone
112  * @type:	the thermal zone device type
113  * @device:	&struct device for this thermal zone
114  * @trip_temp_attrs:	attributes for trip points for sysfs: trip temperature
115  * @trip_type_attrs:	attributes for trip points for sysfs: trip type
116  * @trip_hyst_attrs:	attributes for trip points for sysfs: trip hysteresis
117  * @mode:		current mode of this thermal zone
118  * @devdata:	private pointer for device private data
119  * @trips:	number of trip points the thermal zone supports
120  * @trips_disabled;	bitmap for disabled trips
121  * @passive_delay:	number of milliseconds to wait between polls when
122  *			performing passive cooling.
123  * @polling_delay:	number of milliseconds to wait between polls when
124  *			checking whether trip points have been crossed (0 for
125  *			interrupt driven systems)
126  * @temperature:	current temperature.  This is only for core code,
127  *			drivers should use thermal_zone_get_temp() to get the
128  *			current temperature
129  * @last_temperature:	previous temperature read
130  * @emul_temperature:	emulated temperature when using CONFIG_THERMAL_EMULATION
131  * @passive:		1 if you've crossed a passive trip point, 0 otherwise.
132  * @prev_low_trip:	the low current temperature if you've crossed a passive
133 			trip point.
134  * @prev_high_trip:	the above current temperature if you've crossed a
135 			passive trip point.
136  * @forced_passive:	If > 0, temperature at which to switch on all ACPI
137  *			processor cooling devices.  Currently only used by the
138  *			step-wise governor.
139  * @need_update:	if equals 1, thermal_zone_device_update needs to be invoked.
140  * @ops:	operations this &thermal_zone_device supports
141  * @tzp:	thermal zone parameters
142  * @governor:	pointer to the governor for this thermal zone
143  * @governor_data:	private pointer for governor data
144  * @thermal_instances:	list of &struct thermal_instance of this thermal zone
145  * @ida:	&struct ida to generate unique id for this zone's cooling
146  *		devices
147  * @lock:	lock to protect thermal_instances list
148  * @node:	node in thermal_tz_list (in thermal_core.c)
149  * @poll_queue:	delayed work for polling
150  * @notify_event: Last notification event
151  */
152 struct thermal_zone_device {
153 	int id;
154 	char type[THERMAL_NAME_LENGTH];
155 	struct device device;
156 	struct attribute_group trips_attribute_group;
157 	struct thermal_attr *trip_temp_attrs;
158 	struct thermal_attr *trip_type_attrs;
159 	struct thermal_attr *trip_hyst_attrs;
160 	enum thermal_device_mode mode;
161 	void *devdata;
162 	int trips;
163 	unsigned long trips_disabled;	/* bitmap for disabled trips */
164 	int passive_delay;
165 	int polling_delay;
166 	int temperature;
167 	int last_temperature;
168 	int emul_temperature;
169 	int passive;
170 	int prev_low_trip;
171 	int prev_high_trip;
172 	unsigned int forced_passive;
173 	atomic_t need_update;
174 	struct thermal_zone_device_ops *ops;
175 	struct thermal_zone_params *tzp;
176 	struct thermal_governor *governor;
177 	void *governor_data;
178 	struct list_head thermal_instances;
179 	struct ida ida;
180 	struct mutex lock;
181 	struct list_head node;
182 	struct delayed_work poll_queue;
183 	enum thermal_notify_event notify_event;
184 };
185 
186 /**
187  * struct thermal_governor - structure that holds thermal governor information
188  * @name:	name of the governor
189  * @bind_to_tz: callback called when binding to a thermal zone.  If it
190  *		returns 0, the governor is bound to the thermal zone,
191  *		otherwise it fails.
192  * @unbind_from_tz:	callback called when a governor is unbound from a
193  *			thermal zone.
194  * @throttle:	callback called for every trip point even if temperature is
195  *		below the trip point temperature
196  * @governor_list:	node in thermal_governor_list (in thermal_core.c)
197  */
198 struct thermal_governor {
199 	char name[THERMAL_NAME_LENGTH];
200 	int (*bind_to_tz)(struct thermal_zone_device *tz);
201 	void (*unbind_from_tz)(struct thermal_zone_device *tz);
202 	int (*throttle)(struct thermal_zone_device *tz, int trip);
203 	struct list_head	governor_list;
204 };
205 
206 /* Structure that holds binding parameters for a zone */
207 struct thermal_bind_params {
208 	struct thermal_cooling_device *cdev;
209 
210 	/*
211 	 * This is a measure of 'how effectively these devices can
212 	 * cool 'this' thermal zone. It shall be determined by
213 	 * platform characterization. This value is relative to the
214 	 * rest of the weights so a cooling device whose weight is
215 	 * double that of another cooling device is twice as
216 	 * effective. See Documentation/driver-api/thermal/sysfs-api.rst for more
217 	 * information.
218 	 */
219 	int weight;
220 
221 	/*
222 	 * This is a bit mask that gives the binding relation between this
223 	 * thermal zone and cdev, for a particular trip point.
224 	 * See Documentation/driver-api/thermal/sysfs-api.rst for more information.
225 	 */
226 	int trip_mask;
227 
228 	/*
229 	 * This is an array of cooling state limits. Must have exactly
230 	 * 2 * thermal_zone.number_of_trip_points. It is an array consisting
231 	 * of tuples <lower-state upper-state> of state limits. Each trip
232 	 * will be associated with one state limit tuple when binding.
233 	 * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS>
234 	 * on all trips.
235 	 */
236 	unsigned long *binding_limits;
237 	int (*match) (struct thermal_zone_device *tz,
238 			struct thermal_cooling_device *cdev);
239 };
240 
241 /* Structure to define Thermal Zone parameters */
242 struct thermal_zone_params {
243 	char governor_name[THERMAL_NAME_LENGTH];
244 
245 	/*
246 	 * a boolean to indicate if the thermal to hwmon sysfs interface
247 	 * is required. when no_hwmon == false, a hwmon sysfs interface
248 	 * will be created. when no_hwmon == true, nothing will be done
249 	 */
250 	bool no_hwmon;
251 
252 	int num_tbps;	/* Number of tbp entries */
253 	struct thermal_bind_params *tbp;
254 
255 	/*
256 	 * Sustainable power (heat) that this thermal zone can dissipate in
257 	 * mW
258 	 */
259 	u32 sustainable_power;
260 
261 	/*
262 	 * Proportional parameter of the PID controller when
263 	 * overshooting (i.e., when temperature is below the target)
264 	 */
265 	s32 k_po;
266 
267 	/*
268 	 * Proportional parameter of the PID controller when
269 	 * undershooting
270 	 */
271 	s32 k_pu;
272 
273 	/* Integral parameter of the PID controller */
274 	s32 k_i;
275 
276 	/* Derivative parameter of the PID controller */
277 	s32 k_d;
278 
279 	/* threshold below which the error is no longer accumulated */
280 	s32 integral_cutoff;
281 
282 	/*
283 	 * @slope:	slope of a linear temperature adjustment curve.
284 	 * 		Used by thermal zone drivers.
285 	 */
286 	int slope;
287 	/*
288 	 * @offset:	offset of a linear temperature adjustment curve.
289 	 * 		Used by thermal zone drivers (default 0).
290 	 */
291 	int offset;
292 };
293 
294 /**
295  * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones
296  *
297  * Mandatory:
298  * @get_temp: a pointer to a function that reads the sensor temperature.
299  *
300  * Optional:
301  * @get_trend: a pointer to a function that reads the sensor temperature trend.
302  * @set_trips: a pointer to a function that sets a temperature window. When
303  *	       this window is left the driver must inform the thermal core via
304  *	       thermal_zone_device_update.
305  * @set_emul_temp: a pointer to a function that sets sensor emulated
306  *		   temperature.
307  * @set_trip_temp: a pointer to a function that sets the trip temperature on
308  *		   hardware.
309  */
310 struct thermal_zone_of_device_ops {
311 	int (*get_temp)(void *, int *);
312 	int (*get_trend)(void *, int, enum thermal_trend *);
313 	int (*set_trips)(void *, int, int);
314 	int (*set_emul_temp)(void *, int);
315 	int (*set_trip_temp)(void *, int, int);
316 };
317 
318 /* Function declarations */
319 #ifdef CONFIG_THERMAL_OF
320 int thermal_zone_of_get_sensor_id(struct device_node *tz_np,
321 				  struct device_node *sensor_np,
322 				  u32 *id);
323 struct thermal_zone_device *
324 thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
325 				const struct thermal_zone_of_device_ops *ops);
326 void thermal_zone_of_sensor_unregister(struct device *dev,
327 				       struct thermal_zone_device *tz);
328 struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
329 		struct device *dev, int id, void *data,
330 		const struct thermal_zone_of_device_ops *ops);
331 void devm_thermal_zone_of_sensor_unregister(struct device *dev,
332 					    struct thermal_zone_device *tz);
333 #else
334 
335 static inline int thermal_zone_of_get_sensor_id(struct device_node *tz_np,
336 					 struct device_node *sensor_np,
337 					 u32 *id)
338 {
339 	return -ENOENT;
340 }
341 static inline struct thermal_zone_device *
342 thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
343 				const struct thermal_zone_of_device_ops *ops)
344 {
345 	return ERR_PTR(-ENODEV);
346 }
347 
348 static inline
349 void thermal_zone_of_sensor_unregister(struct device *dev,
350 				       struct thermal_zone_device *tz)
351 {
352 }
353 
354 static inline struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
355 		struct device *dev, int id, void *data,
356 		const struct thermal_zone_of_device_ops *ops)
357 {
358 	return ERR_PTR(-ENODEV);
359 }
360 
361 static inline
362 void devm_thermal_zone_of_sensor_unregister(struct device *dev,
363 					    struct thermal_zone_device *tz)
364 {
365 }
366 
367 #endif
368 
369 #ifdef CONFIG_THERMAL
370 struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
371 		void *, struct thermal_zone_device_ops *,
372 		struct thermal_zone_params *, int, int);
373 void thermal_zone_device_unregister(struct thermal_zone_device *);
374 
375 int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
376 				     struct thermal_cooling_device *,
377 				     unsigned long, unsigned long,
378 				     unsigned int);
379 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
380 				       struct thermal_cooling_device *);
381 void thermal_zone_device_update(struct thermal_zone_device *,
382 				enum thermal_notify_event);
383 
384 struct thermal_cooling_device *thermal_cooling_device_register(const char *,
385 		void *, const struct thermal_cooling_device_ops *);
386 struct thermal_cooling_device *
387 thermal_of_cooling_device_register(struct device_node *np, const char *, void *,
388 				   const struct thermal_cooling_device_ops *);
389 struct thermal_cooling_device *
390 devm_thermal_of_cooling_device_register(struct device *dev,
391 				struct device_node *np,
392 				char *type, void *devdata,
393 				const struct thermal_cooling_device_ops *ops);
394 void thermal_cooling_device_unregister(struct thermal_cooling_device *);
395 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
396 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
397 int thermal_zone_get_slope(struct thermal_zone_device *tz);
398 int thermal_zone_get_offset(struct thermal_zone_device *tz);
399 
400 void thermal_cdev_update(struct thermal_cooling_device *);
401 void thermal_notify_framework(struct thermal_zone_device *, int);
402 int thermal_zone_device_enable(struct thermal_zone_device *tz);
403 int thermal_zone_device_disable(struct thermal_zone_device *tz);
404 void thermal_zone_device_critical(struct thermal_zone_device *tz);
405 #else
406 static inline struct thermal_zone_device *thermal_zone_device_register(
407 	const char *type, int trips, int mask, void *devdata,
408 	struct thermal_zone_device_ops *ops,
409 	struct thermal_zone_params *tzp,
410 	int passive_delay, int polling_delay)
411 { return ERR_PTR(-ENODEV); }
412 static inline void thermal_zone_device_unregister(
413 	struct thermal_zone_device *tz)
414 { }
415 static inline struct thermal_cooling_device *
416 thermal_cooling_device_register(char *type, void *devdata,
417 	const struct thermal_cooling_device_ops *ops)
418 { return ERR_PTR(-ENODEV); }
419 static inline struct thermal_cooling_device *
420 thermal_of_cooling_device_register(struct device_node *np,
421 	char *type, void *devdata, const struct thermal_cooling_device_ops *ops)
422 { return ERR_PTR(-ENODEV); }
423 static inline struct thermal_cooling_device *
424 devm_thermal_of_cooling_device_register(struct device *dev,
425 				struct device_node *np,
426 				char *type, void *devdata,
427 				const struct thermal_cooling_device_ops *ops)
428 {
429 	return ERR_PTR(-ENODEV);
430 }
431 static inline void thermal_cooling_device_unregister(
432 	struct thermal_cooling_device *cdev)
433 { }
434 static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
435 		const char *name)
436 { return ERR_PTR(-ENODEV); }
437 static inline int thermal_zone_get_temp(
438 		struct thermal_zone_device *tz, int *temp)
439 { return -ENODEV; }
440 static inline int thermal_zone_get_slope(
441 		struct thermal_zone_device *tz)
442 { return -ENODEV; }
443 static inline int thermal_zone_get_offset(
444 		struct thermal_zone_device *tz)
445 { return -ENODEV; }
446 
447 static inline void thermal_cdev_update(struct thermal_cooling_device *cdev)
448 { }
449 static inline void thermal_notify_framework(struct thermal_zone_device *tz,
450 	int trip)
451 { }
452 
453 static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
454 { return -ENODEV; }
455 
456 static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
457 { return -ENODEV; }
458 #endif /* CONFIG_THERMAL */
459 
460 #endif /* __THERMAL_H__ */
461