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