xref: /linux-6.15/include/linux/thermal.h (revision ee940b57)
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_attr;
36 
37 enum thermal_trend {
38 	THERMAL_TREND_STABLE, /* temperature is stable */
39 	THERMAL_TREND_RAISING, /* temperature is raising */
40 	THERMAL_TREND_DROPPING, /* temperature is dropping */
41 };
42 
43 /* Thermal notification reason */
44 enum thermal_notify_event {
45 	THERMAL_EVENT_UNSPECIFIED, /* Unspecified event */
46 	THERMAL_EVENT_TEMP_SAMPLE, /* New Temperature sample */
47 	THERMAL_TRIP_VIOLATED, /* TRIP Point violation */
48 	THERMAL_TRIP_CHANGED, /* TRIP Point temperature changed */
49 	THERMAL_DEVICE_DOWN, /* Thermal device is down */
50 	THERMAL_DEVICE_UP, /* Thermal device is up after a down event */
51 	THERMAL_DEVICE_POWER_CAPABILITY_CHANGED, /* power capability changed */
52 	THERMAL_TABLE_CHANGED, /* Thermal table(s) changed */
53 	THERMAL_EVENT_KEEP_ALIVE, /* Request for user space handler to respond */
54 };
55 
56 struct thermal_zone_device_ops {
57 	int (*bind) (struct thermal_zone_device *,
58 		     struct thermal_cooling_device *);
59 	int (*unbind) (struct thermal_zone_device *,
60 		       struct thermal_cooling_device *);
61 	int (*get_temp) (struct thermal_zone_device *, int *);
62 	int (*set_trips) (struct thermal_zone_device *, int, int);
63 	int (*change_mode) (struct thermal_zone_device *,
64 		enum thermal_device_mode);
65 	int (*get_trip_type) (struct thermal_zone_device *, int,
66 		enum thermal_trip_type *);
67 	int (*get_trip_temp) (struct thermal_zone_device *, int, int *);
68 	int (*set_trip_temp) (struct thermal_zone_device *, int, int);
69 	int (*get_trip_hyst) (struct thermal_zone_device *, int, int *);
70 	int (*set_trip_hyst) (struct thermal_zone_device *, int, int);
71 	int (*get_crit_temp) (struct thermal_zone_device *, int *);
72 	int (*set_emul_temp) (struct thermal_zone_device *, int);
73 	int (*get_trend) (struct thermal_zone_device *, int,
74 			  enum thermal_trend *);
75 	void (*hot)(struct thermal_zone_device *);
76 	void (*critical)(struct thermal_zone_device *);
77 };
78 
79 /**
80  * struct thermal_trip - representation of a point in temperature domain
81  * @temperature: temperature value in miliCelsius
82  * @hysteresis: relative hysteresis in miliCelsius
83  * @type: trip point type
84  * @priv: pointer to driver data associated with this trip
85  */
86 struct thermal_trip {
87 	int temperature;
88 	int hysteresis;
89 	enum thermal_trip_type type;
90 	void *priv;
91 };
92 
93 struct thermal_cooling_device_ops {
94 	int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
95 	int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
96 	int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
97 	int (*get_requested_power)(struct thermal_cooling_device *, u32 *);
98 	int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *);
99 	int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *);
100 };
101 
102 struct thermal_cooling_device {
103 	int id;
104 	char *type;
105 	unsigned long max_state;
106 	struct device device;
107 	struct device_node *np;
108 	void *devdata;
109 	void *stats;
110 	const struct thermal_cooling_device_ops *ops;
111 	bool updated; /* true if the cooling device does not need update */
112 	struct mutex lock; /* protect thermal_instances list */
113 	struct list_head thermal_instances;
114 	struct list_head node;
115 };
116 
117 /**
118  * struct thermal_zone_device - structure for a thermal zone
119  * @id:		unique id number for each thermal zone
120  * @type:	the thermal zone device type
121  * @device:	&struct device for this thermal zone
122  * @trip_temp_attrs:	attributes for trip points for sysfs: trip temperature
123  * @trip_type_attrs:	attributes for trip points for sysfs: trip type
124  * @trip_hyst_attrs:	attributes for trip points for sysfs: trip hysteresis
125  * @mode:		current mode of this thermal zone
126  * @devdata:	private pointer for device private data
127  * @trips:	an array of struct thermal_trip
128  * @num_trips:	number of trip points the thermal zone supports
129  * @trips_disabled;	bitmap for disabled trips
130  * @passive_delay_jiffies: number of jiffies to wait between polls when
131  *			performing passive cooling.
132  * @polling_delay_jiffies: number of jiffies to wait between polls when
133  *			checking whether trip points have been crossed (0 for
134  *			interrupt driven systems)
135  * @temperature:	current temperature.  This is only for core code,
136  *			drivers should use thermal_zone_get_temp() to get the
137  *			current temperature
138  * @last_temperature:	previous temperature read
139  * @emul_temperature:	emulated temperature when using CONFIG_THERMAL_EMULATION
140  * @passive:		1 if you've crossed a passive trip point, 0 otherwise.
141  * @prev_low_trip:	the low current temperature if you've crossed a passive
142 			trip point.
143  * @prev_high_trip:	the above current temperature if you've crossed a
144 			passive trip point.
145  * @need_update:	if equals 1, thermal_zone_device_update needs to be invoked.
146  * @ops:	operations this &thermal_zone_device supports
147  * @tzp:	thermal zone parameters
148  * @governor:	pointer to the governor for this thermal zone
149  * @governor_data:	private pointer for governor data
150  * @thermal_instances:	list of &struct thermal_instance of this thermal zone
151  * @ida:	&struct ida to generate unique id for this zone's cooling
152  *		devices
153  * @lock:	lock to protect thermal_instances list
154  * @node:	node in thermal_tz_list (in thermal_core.c)
155  * @poll_queue:	delayed work for polling
156  * @notify_event: Last notification event
157  */
158 struct thermal_zone_device {
159 	int id;
160 	char type[THERMAL_NAME_LENGTH];
161 	struct device device;
162 	struct attribute_group trips_attribute_group;
163 	struct thermal_attr *trip_temp_attrs;
164 	struct thermal_attr *trip_type_attrs;
165 	struct thermal_attr *trip_hyst_attrs;
166 	enum thermal_device_mode mode;
167 	void *devdata;
168 	struct thermal_trip *trips;
169 	int num_trips;
170 	unsigned long trips_disabled;	/* bitmap for disabled trips */
171 	unsigned long passive_delay_jiffies;
172 	unsigned long polling_delay_jiffies;
173 	int temperature;
174 	int last_temperature;
175 	int emul_temperature;
176 	int passive;
177 	int prev_low_trip;
178 	int prev_high_trip;
179 	atomic_t need_update;
180 	struct thermal_zone_device_ops *ops;
181 	struct thermal_zone_params *tzp;
182 	struct thermal_governor *governor;
183 	void *governor_data;
184 	struct list_head thermal_instances;
185 	struct ida ida;
186 	struct mutex lock;
187 	struct list_head node;
188 	struct delayed_work poll_queue;
189 	enum thermal_notify_event notify_event;
190 };
191 
192 /**
193  * struct thermal_governor - structure that holds thermal governor information
194  * @name:	name of the governor
195  * @bind_to_tz: callback called when binding to a thermal zone.  If it
196  *		returns 0, the governor is bound to the thermal zone,
197  *		otherwise it fails.
198  * @unbind_from_tz:	callback called when a governor is unbound from a
199  *			thermal zone.
200  * @throttle:	callback called for every trip point even if temperature is
201  *		below the trip point temperature
202  * @governor_list:	node in thermal_governor_list (in thermal_core.c)
203  */
204 struct thermal_governor {
205 	char name[THERMAL_NAME_LENGTH];
206 	int (*bind_to_tz)(struct thermal_zone_device *tz);
207 	void (*unbind_from_tz)(struct thermal_zone_device *tz);
208 	int (*throttle)(struct thermal_zone_device *tz, int trip);
209 	struct list_head	governor_list;
210 };
211 
212 /* Structure to define Thermal Zone parameters */
213 struct thermal_zone_params {
214 	char governor_name[THERMAL_NAME_LENGTH];
215 
216 	/*
217 	 * a boolean to indicate if the thermal to hwmon sysfs interface
218 	 * is required. when no_hwmon == false, a hwmon sysfs interface
219 	 * will be created. when no_hwmon == true, nothing will be done
220 	 */
221 	bool no_hwmon;
222 
223 	/*
224 	 * Sustainable power (heat) that this thermal zone can dissipate in
225 	 * mW
226 	 */
227 	u32 sustainable_power;
228 
229 	/*
230 	 * Proportional parameter of the PID controller when
231 	 * overshooting (i.e., when temperature is below the target)
232 	 */
233 	s32 k_po;
234 
235 	/*
236 	 * Proportional parameter of the PID controller when
237 	 * undershooting
238 	 */
239 	s32 k_pu;
240 
241 	/* Integral parameter of the PID controller */
242 	s32 k_i;
243 
244 	/* Derivative parameter of the PID controller */
245 	s32 k_d;
246 
247 	/* threshold below which the error is no longer accumulated */
248 	s32 integral_cutoff;
249 
250 	/*
251 	 * @slope:	slope of a linear temperature adjustment curve.
252 	 * 		Used by thermal zone drivers.
253 	 */
254 	int slope;
255 	/*
256 	 * @offset:	offset of a linear temperature adjustment curve.
257 	 * 		Used by thermal zone drivers (default 0).
258 	 */
259 	int offset;
260 };
261 
262 /* Function declarations */
263 #ifdef CONFIG_THERMAL_OF
264 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
265 							  const struct thermal_zone_device_ops *ops);
266 
267 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz);
268 
269 #else
270 
271 static inline
272 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
273 							  const struct thermal_zone_device_ops *ops)
274 {
275 	return ERR_PTR(-ENOTSUPP);
276 }
277 
278 static inline void devm_thermal_of_zone_unregister(struct device *dev,
279 						   struct thermal_zone_device *tz)
280 {
281 }
282 #endif
283 
284 int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
285 			    struct thermal_trip *trip);
286 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
287 			  struct thermal_trip *trip);
288 
289 int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id,
290 			  const struct thermal_trip *trip);
291 
292 int for_each_thermal_trip(struct thermal_zone_device *tz,
293 			  int (*cb)(struct thermal_trip *, void *),
294 			  void *data);
295 int thermal_zone_get_num_trips(struct thermal_zone_device *tz);
296 
297 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);
298 
299 #ifdef CONFIG_THERMAL_ACPI
300 int thermal_acpi_active_trip_temp(struct acpi_device *adev, int id, int *ret_temp);
301 int thermal_acpi_passive_trip_temp(struct acpi_device *adev, int *ret_temp);
302 int thermal_acpi_hot_trip_temp(struct acpi_device *adev, int *ret_temp);
303 int thermal_acpi_critical_trip_temp(struct acpi_device *adev, int *ret_temp);
304 #endif
305 
306 #ifdef CONFIG_THERMAL
307 struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
308 		void *, struct thermal_zone_device_ops *,
309 		const struct thermal_zone_params *, int, int);
310 
311 void thermal_zone_device_unregister(struct thermal_zone_device *);
312 
313 struct thermal_zone_device *
314 thermal_zone_device_register_with_trips(const char *, struct thermal_trip *, int, int,
315 					void *, struct thermal_zone_device_ops *,
316 					const struct thermal_zone_params *, int, int);
317 
318 void *thermal_zone_device_priv(struct thermal_zone_device *tzd);
319 const char *thermal_zone_device_type(struct thermal_zone_device *tzd);
320 int thermal_zone_device_id(struct thermal_zone_device *tzd);
321 struct device *thermal_zone_device(struct thermal_zone_device *tzd);
322 
323 int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
324 				     struct thermal_cooling_device *,
325 				     unsigned long, unsigned long,
326 				     unsigned int);
327 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
328 				       struct thermal_cooling_device *);
329 void thermal_zone_device_update(struct thermal_zone_device *,
330 				enum thermal_notify_event);
331 void thermal_zone_device_exec(struct thermal_zone_device *tz,
332 			      void (*cb)(struct thermal_zone_device *,
333 					 unsigned long),
334 			      unsigned long data);
335 
336 struct thermal_cooling_device *thermal_cooling_device_register(const char *,
337 		void *, const struct thermal_cooling_device_ops *);
338 struct thermal_cooling_device *
339 thermal_of_cooling_device_register(struct device_node *np, const char *, void *,
340 				   const struct thermal_cooling_device_ops *);
341 struct thermal_cooling_device *
342 devm_thermal_of_cooling_device_register(struct device *dev,
343 				struct device_node *np,
344 				char *type, void *devdata,
345 				const struct thermal_cooling_device_ops *ops);
346 void thermal_cooling_device_update(struct thermal_cooling_device *);
347 void thermal_cooling_device_unregister(struct thermal_cooling_device *);
348 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
349 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
350 int thermal_zone_get_slope(struct thermal_zone_device *tz);
351 int thermal_zone_get_offset(struct thermal_zone_device *tz);
352 
353 int thermal_zone_device_enable(struct thermal_zone_device *tz);
354 int thermal_zone_device_disable(struct thermal_zone_device *tz);
355 void thermal_zone_device_critical(struct thermal_zone_device *tz);
356 #else
357 static inline struct thermal_zone_device *thermal_zone_device_register(
358 	const char *type, int trips, int mask, void *devdata,
359 	struct thermal_zone_device_ops *ops,
360 	const struct thermal_zone_params *tzp,
361 	int passive_delay, int polling_delay)
362 { return ERR_PTR(-ENODEV); }
363 static inline void thermal_zone_device_unregister(
364 	struct thermal_zone_device *tz)
365 { }
366 static inline struct thermal_cooling_device *
367 thermal_cooling_device_register(const char *type, void *devdata,
368 	const struct thermal_cooling_device_ops *ops)
369 { return ERR_PTR(-ENODEV); }
370 static inline struct thermal_cooling_device *
371 thermal_of_cooling_device_register(struct device_node *np,
372 	const char *type, void *devdata,
373 	const struct thermal_cooling_device_ops *ops)
374 { return ERR_PTR(-ENODEV); }
375 static inline struct thermal_cooling_device *
376 devm_thermal_of_cooling_device_register(struct device *dev,
377 				struct device_node *np,
378 				char *type, void *devdata,
379 				const struct thermal_cooling_device_ops *ops)
380 {
381 	return ERR_PTR(-ENODEV);
382 }
383 static inline void thermal_cooling_device_unregister(
384 	struct thermal_cooling_device *cdev)
385 { }
386 static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
387 		const char *name)
388 { return ERR_PTR(-ENODEV); }
389 static inline int thermal_zone_get_temp(
390 		struct thermal_zone_device *tz, int *temp)
391 { return -ENODEV; }
392 static inline int thermal_zone_get_slope(
393 		struct thermal_zone_device *tz)
394 { return -ENODEV; }
395 static inline int thermal_zone_get_offset(
396 		struct thermal_zone_device *tz)
397 { return -ENODEV; }
398 
399 static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz)
400 {
401 	return NULL;
402 }
403 
404 static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
405 {
406 	return NULL;
407 }
408 
409 static inline int thermal_zone_device_id(struct thermal_zone_device *tzd)
410 {
411 	return -ENODEV;
412 }
413 
414 static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
415 { return -ENODEV; }
416 
417 static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
418 { return -ENODEV; }
419 #endif /* CONFIG_THERMAL */
420 
421 #endif /* __THERMAL_H__ */
422