xref: /openbmc/linux/include/linux/thermal.h (revision 5f8b7d4b2e9604d03ae06f1a2dd5a1f34c33e533)
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 <rui.zhang@intel.com>
7  *  Copyright (C) 2008  Sujith Thomas <sujith.thomas@intel.com>
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 /**
57  * struct thermal_trip - representation of a point in temperature domain
58  * @temperature: temperature value in miliCelsius
59  * @hysteresis: relative hysteresis in miliCelsius
60  * @type: trip point type
61  * @priv: pointer to driver data associated with this trip
62  */
63 struct thermal_trip {
64 	int temperature;
65 	int hysteresis;
66 	enum thermal_trip_type type;
67 	void *priv;
68 };
69 
70 struct thermal_zone_device_ops {
71 	int (*bind) (struct thermal_zone_device *,
72 		     struct thermal_cooling_device *);
73 	int (*unbind) (struct thermal_zone_device *,
74 		       struct thermal_cooling_device *);
75 	int (*get_temp) (struct thermal_zone_device *, int *);
76 	int (*set_trips) (struct thermal_zone_device *, int, int);
77 	int (*change_mode) (struct thermal_zone_device *,
78 		enum thermal_device_mode);
79 	int (*set_trip_temp) (struct thermal_zone_device *, int, int);
80 	int (*set_trip_hyst) (struct thermal_zone_device *, int, int);
81 	int (*get_crit_temp) (struct thermal_zone_device *, int *);
82 	int (*set_emul_temp) (struct thermal_zone_device *, int);
83 	int (*get_trend) (struct thermal_zone_device *,
84 			  const struct thermal_trip *, enum thermal_trend *);
85 	void (*hot)(struct thermal_zone_device *);
86 	void (*critical)(struct thermal_zone_device *);
87 };
88 
89 struct thermal_cooling_device_ops {
90 	int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
91 	int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
92 	int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
93 	int (*get_requested_power)(struct thermal_cooling_device *, u32 *);
94 	int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *);
95 	int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *);
96 };
97 
98 struct thermal_cooling_device {
99 	int id;
100 	char *type;
101 	unsigned long max_state;
102 	struct device device;
103 	struct device_node *np;
104 	void *devdata;
105 	void *stats;
106 	const struct thermal_cooling_device_ops *ops;
107 	bool updated; /* true if the cooling device does not need update */
108 	struct mutex lock; /* protect thermal_instances list */
109 	struct list_head thermal_instances;
110 	struct list_head node;
111 };
112 
113 /**
114  * struct thermal_zone_device - structure for a thermal zone
115  * @id:		unique id number for each thermal zone
116  * @type:	the thermal zone device type
117  * @device:	&struct device for this thermal zone
118  * @removal:	removal completion
119  * @trip_temp_attrs:	attributes for trip points for sysfs: trip temperature
120  * @trip_type_attrs:	attributes for trip points for sysfs: trip type
121  * @trip_hyst_attrs:	attributes for trip points for sysfs: trip hysteresis
122  * @mode:		current mode of this thermal zone
123  * @devdata:	private pointer for device private data
124  * @trips:	an array of struct thermal_trip
125  * @num_trips:	number of trip points the thermal zone supports
126  * @trips_disabled;	bitmap for disabled trips
127  * @passive_delay_jiffies: number of jiffies to wait between polls when
128  *			performing passive cooling.
129  * @polling_delay_jiffies: number of jiffies to wait between polls when
130  *			checking whether trip points have been crossed (0 for
131  *			interrupt driven systems)
132  * @temperature:	current temperature.  This is only for core code,
133  *			drivers should use thermal_zone_get_temp() to get the
134  *			current temperature
135  * @last_temperature:	previous temperature read
136  * @emul_temperature:	emulated temperature when using CONFIG_THERMAL_EMULATION
137  * @passive:		1 if you've crossed a passive trip point, 0 otherwise.
138  * @prev_low_trip:	the low current temperature if you've crossed a passive
139 			trip point.
140  * @prev_high_trip:	the above current temperature if you've crossed a
141 			passive trip point.
142  * @need_update:	if equals 1, thermal_zone_device_update needs to be invoked.
143  * @ops:	operations this &thermal_zone_device supports
144  * @tzp:	thermal zone parameters
145  * @governor:	pointer to the governor for this thermal zone
146  * @governor_data:	private pointer for governor data
147  * @thermal_instances:	list of &struct thermal_instance of this thermal zone
148  * @ida:	&struct ida to generate unique id for this zone's cooling
149  *		devices
150  * @lock:	lock to protect thermal_instances list
151  * @node:	node in thermal_tz_list (in thermal_core.c)
152  * @poll_queue:	delayed work for polling
153  * @notify_event: Last notification event
154  * @suspended: thermal zone suspend indicator
155  */
156 struct thermal_zone_device {
157 	int id;
158 	char type[THERMAL_NAME_LENGTH];
159 	struct device device;
160 	struct completion removal;
161 	struct attribute_group trips_attribute_group;
162 	struct thermal_attr *trip_temp_attrs;
163 	struct thermal_attr *trip_type_attrs;
164 	struct thermal_attr *trip_hyst_attrs;
165 	enum thermal_device_mode mode;
166 	void *devdata;
167 	struct thermal_trip *trips;
168 	int num_trips;
169 	unsigned long trips_disabled;	/* bitmap for disabled trips */
170 	unsigned long passive_delay_jiffies;
171 	unsigned long polling_delay_jiffies;
172 	int temperature;
173 	int last_temperature;
174 	int emul_temperature;
175 	int passive;
176 	int prev_low_trip;
177 	int prev_high_trip;
178 	atomic_t need_update;
179 	struct thermal_zone_device_ops *ops;
180 	struct thermal_zone_params *tzp;
181 	struct thermal_governor *governor;
182 	void *governor_data;
183 	struct list_head thermal_instances;
184 	struct ida ida;
185 	struct mutex lock;
186 	struct list_head node;
187 	struct delayed_work poll_queue;
188 	enum thermal_notify_event notify_event;
189 	bool suspended;
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
devm_thermal_of_zone_register(struct device * dev,int id,void * data,const struct thermal_zone_device_ops * ops)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 
devm_thermal_of_zone_unregister(struct device * dev,struct thermal_zone_device * tz)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_with_trips(
308 					const char *type,
309 					struct thermal_trip *trips,
310 					int num_trips, int mask,
311 					void *devdata,
312 					struct thermal_zone_device_ops *ops,
313 					const struct thermal_zone_params *tzp,
314 					int passive_delay, int polling_delay);
315 
316 struct thermal_zone_device *thermal_tripless_zone_device_register(
317 					const char *type,
318 					void *devdata,
319 					struct thermal_zone_device_ops *ops,
320 					const struct thermal_zone_params *tzp);
321 
322 void thermal_zone_device_unregister(struct thermal_zone_device *tz);
323 
324 void *thermal_zone_device_priv(struct thermal_zone_device *tzd);
325 const char *thermal_zone_device_type(struct thermal_zone_device *tzd);
326 int thermal_zone_device_id(struct thermal_zone_device *tzd);
327 struct device *thermal_zone_device(struct thermal_zone_device *tzd);
328 
329 int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
330 				     struct thermal_cooling_device *,
331 				     unsigned long, unsigned long,
332 				     unsigned int);
333 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
334 				       struct thermal_cooling_device *);
335 void thermal_zone_device_update(struct thermal_zone_device *,
336 				enum thermal_notify_event);
337 void thermal_zone_device_exec(struct thermal_zone_device *tz,
338 			      void (*cb)(struct thermal_zone_device *,
339 					 unsigned long),
340 			      unsigned long data);
341 
342 struct thermal_cooling_device *thermal_cooling_device_register(const char *,
343 		void *, const struct thermal_cooling_device_ops *);
344 struct thermal_cooling_device *
345 thermal_of_cooling_device_register(struct device_node *np, const char *, void *,
346 				   const struct thermal_cooling_device_ops *);
347 struct thermal_cooling_device *
348 devm_thermal_of_cooling_device_register(struct device *dev,
349 				struct device_node *np,
350 				char *type, void *devdata,
351 				const struct thermal_cooling_device_ops *ops);
352 void thermal_cooling_device_update(struct thermal_cooling_device *);
353 void thermal_cooling_device_unregister(struct thermal_cooling_device *);
354 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
355 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
356 int thermal_zone_get_slope(struct thermal_zone_device *tz);
357 int thermal_zone_get_offset(struct thermal_zone_device *tz);
358 
359 int thermal_zone_device_enable(struct thermal_zone_device *tz);
360 int thermal_zone_device_disable(struct thermal_zone_device *tz);
361 void thermal_zone_device_critical(struct thermal_zone_device *tz);
362 #else
thermal_zone_device_register_with_trips(const char * type,struct thermal_trip * trips,int num_trips,int mask,void * devdata,struct thermal_zone_device_ops * ops,const struct thermal_zone_params * tzp,int passive_delay,int polling_delay)363 static inline struct thermal_zone_device *thermal_zone_device_register_with_trips(
364 					const char *type,
365 					struct thermal_trip *trips,
366 					int num_trips, int mask,
367 					void *devdata,
368 					struct thermal_zone_device_ops *ops,
369 					const struct thermal_zone_params *tzp,
370 					int passive_delay, int polling_delay)
371 { return ERR_PTR(-ENODEV); }
372 
thermal_tripless_zone_device_register(const char * type,void * devdata,struct thermal_zone_device_ops * ops,const struct thermal_zone_params * tzp)373 static inline struct thermal_zone_device *thermal_tripless_zone_device_register(
374 					const char *type,
375 					void *devdata,
376 					struct thermal_zone_device_ops *ops,
377 					const struct thermal_zone_params *tzp)
378 { return ERR_PTR(-ENODEV); }
379 
thermal_zone_device_unregister(struct thermal_zone_device * tz)380 static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz)
381 { }
382 
383 static inline struct thermal_cooling_device *
thermal_cooling_device_register(const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)384 thermal_cooling_device_register(const char *type, void *devdata,
385 	const struct thermal_cooling_device_ops *ops)
386 { return ERR_PTR(-ENODEV); }
387 static inline struct thermal_cooling_device *
thermal_of_cooling_device_register(struct device_node * np,const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)388 thermal_of_cooling_device_register(struct device_node *np,
389 	const char *type, void *devdata,
390 	const struct thermal_cooling_device_ops *ops)
391 { return ERR_PTR(-ENODEV); }
392 static inline struct thermal_cooling_device *
devm_thermal_of_cooling_device_register(struct device * dev,struct device_node * np,char * type,void * devdata,const struct thermal_cooling_device_ops * ops)393 devm_thermal_of_cooling_device_register(struct device *dev,
394 				struct device_node *np,
395 				char *type, void *devdata,
396 				const struct thermal_cooling_device_ops *ops)
397 {
398 	return ERR_PTR(-ENODEV);
399 }
thermal_cooling_device_unregister(struct thermal_cooling_device * cdev)400 static inline void thermal_cooling_device_unregister(
401 	struct thermal_cooling_device *cdev)
402 { }
thermal_zone_get_zone_by_name(const char * name)403 static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
404 		const char *name)
405 { return ERR_PTR(-ENODEV); }
thermal_zone_get_temp(struct thermal_zone_device * tz,int * temp)406 static inline int thermal_zone_get_temp(
407 		struct thermal_zone_device *tz, int *temp)
408 { return -ENODEV; }
thermal_zone_get_slope(struct thermal_zone_device * tz)409 static inline int thermal_zone_get_slope(
410 		struct thermal_zone_device *tz)
411 { return -ENODEV; }
thermal_zone_get_offset(struct thermal_zone_device * tz)412 static inline int thermal_zone_get_offset(
413 		struct thermal_zone_device *tz)
414 { return -ENODEV; }
415 
thermal_zone_device_priv(struct thermal_zone_device * tz)416 static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz)
417 {
418 	return NULL;
419 }
420 
thermal_zone_device_type(struct thermal_zone_device * tzd)421 static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
422 {
423 	return NULL;
424 }
425 
thermal_zone_device_id(struct thermal_zone_device * tzd)426 static inline int thermal_zone_device_id(struct thermal_zone_device *tzd)
427 {
428 	return -ENODEV;
429 }
430 
thermal_zone_device_enable(struct thermal_zone_device * tz)431 static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
432 { return -ENODEV; }
433 
thermal_zone_device_disable(struct thermal_zone_device * tz)434 static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
435 { return -ENODEV; }
436 #endif /* CONFIG_THERMAL */
437 
438 #endif /* __THERMAL_H__ */
439