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