xref: /linux-6.15/include/linux/thermal.h (revision eea9507a)
1 /*
2  *  thermal.h  ($Revision: 0 $)
3  *
4  *  Copyright (C) 2008  Intel Corp
5  *  Copyright (C) 2008  Zhang Rui <[email protected]>
6  *  Copyright (C) 2008  Sujith Thomas <[email protected]>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; version 2 of the License.
12  *
13  *  This program is distributed in the hope that it will be useful, but
14  *  WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License along
19  *  with this program; if not, write to the Free Software Foundation, Inc.,
20  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21  *
22  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23  */
24 
25 #ifndef __THERMAL_H__
26 #define __THERMAL_H__
27 
28 #include <linux/of.h>
29 #include <linux/idr.h>
30 #include <linux/device.h>
31 #include <linux/workqueue.h>
32 
33 #define THERMAL_TRIPS_NONE	-1
34 #define THERMAL_MAX_TRIPS	12
35 #define THERMAL_NAME_LENGTH	20
36 
37 /* invalid cooling state */
38 #define THERMAL_CSTATE_INVALID -1UL
39 
40 /* No upper/lower limit requirement */
41 #define THERMAL_NO_LIMIT	THERMAL_CSTATE_INVALID
42 
43 /* Unit conversion macros */
44 #define KELVIN_TO_CELSIUS(t)	(long)(((long)t-2732 >= 0) ?	\
45 				((long)t-2732+5)/10 : ((long)t-2732-5)/10)
46 #define CELSIUS_TO_KELVIN(t)	((t)*10+2732)
47 #define DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, off) (((t) - (off)) * 100)
48 #define DECI_KELVIN_TO_MILLICELSIUS(t) DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, 2732)
49 #define MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, off) (((t) / 100) + (off))
50 #define MILLICELSIUS_TO_DECI_KELVIN(t) MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, 2732)
51 
52 /* Adding event notification support elements */
53 #define THERMAL_GENL_FAMILY_NAME                "thermal_event"
54 #define THERMAL_GENL_VERSION                    0x01
55 #define THERMAL_GENL_MCAST_GROUP_NAME           "thermal_mc_grp"
56 
57 /* Default Thermal Governor */
58 #if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
59 #define DEFAULT_THERMAL_GOVERNOR       "step_wise"
60 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE)
61 #define DEFAULT_THERMAL_GOVERNOR       "fair_share"
62 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)
63 #define DEFAULT_THERMAL_GOVERNOR       "user_space"
64 #endif
65 
66 struct thermal_zone_device;
67 struct thermal_cooling_device;
68 
69 enum thermal_device_mode {
70 	THERMAL_DEVICE_DISABLED = 0,
71 	THERMAL_DEVICE_ENABLED,
72 };
73 
74 enum thermal_trip_type {
75 	THERMAL_TRIP_ACTIVE = 0,
76 	THERMAL_TRIP_PASSIVE,
77 	THERMAL_TRIP_HOT,
78 	THERMAL_TRIP_CRITICAL,
79 };
80 
81 enum thermal_trend {
82 	THERMAL_TREND_STABLE, /* temperature is stable */
83 	THERMAL_TREND_RAISING, /* temperature is raising */
84 	THERMAL_TREND_DROPPING, /* temperature is dropping */
85 	THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */
86 	THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */
87 };
88 
89 /* Events supported by Thermal Netlink */
90 enum events {
91 	THERMAL_AUX0,
92 	THERMAL_AUX1,
93 	THERMAL_CRITICAL,
94 	THERMAL_DEV_FAULT,
95 };
96 
97 /* attributes of thermal_genl_family */
98 enum {
99 	THERMAL_GENL_ATTR_UNSPEC,
100 	THERMAL_GENL_ATTR_EVENT,
101 	__THERMAL_GENL_ATTR_MAX,
102 };
103 #define THERMAL_GENL_ATTR_MAX (__THERMAL_GENL_ATTR_MAX - 1)
104 
105 /* commands supported by the thermal_genl_family */
106 enum {
107 	THERMAL_GENL_CMD_UNSPEC,
108 	THERMAL_GENL_CMD_EVENT,
109 	__THERMAL_GENL_CMD_MAX,
110 };
111 #define THERMAL_GENL_CMD_MAX (__THERMAL_GENL_CMD_MAX - 1)
112 
113 struct thermal_zone_device_ops {
114 	int (*bind) (struct thermal_zone_device *,
115 		     struct thermal_cooling_device *);
116 	int (*unbind) (struct thermal_zone_device *,
117 		       struct thermal_cooling_device *);
118 	int (*get_temp) (struct thermal_zone_device *, unsigned long *);
119 	int (*get_mode) (struct thermal_zone_device *,
120 			 enum thermal_device_mode *);
121 	int (*set_mode) (struct thermal_zone_device *,
122 		enum thermal_device_mode);
123 	int (*get_trip_type) (struct thermal_zone_device *, int,
124 		enum thermal_trip_type *);
125 	int (*get_trip_temp) (struct thermal_zone_device *, int,
126 			      unsigned long *);
127 	int (*set_trip_temp) (struct thermal_zone_device *, int,
128 			      unsigned long);
129 	int (*get_trip_hyst) (struct thermal_zone_device *, int,
130 			      unsigned long *);
131 	int (*set_trip_hyst) (struct thermal_zone_device *, int,
132 			      unsigned long);
133 	int (*get_crit_temp) (struct thermal_zone_device *, unsigned long *);
134 	int (*set_emul_temp) (struct thermal_zone_device *, unsigned long);
135 	int (*get_trend) (struct thermal_zone_device *, int,
136 			  enum thermal_trend *);
137 	int (*notify) (struct thermal_zone_device *, int,
138 		       enum thermal_trip_type);
139 };
140 
141 struct thermal_cooling_device_ops {
142 	int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
143 	int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
144 	int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
145 };
146 
147 struct thermal_cooling_device {
148 	int id;
149 	char type[THERMAL_NAME_LENGTH];
150 	struct device device;
151 	struct device_node *np;
152 	void *devdata;
153 	const struct thermal_cooling_device_ops *ops;
154 	bool updated; /* true if the cooling device does not need update */
155 	struct mutex lock; /* protect thermal_instances list */
156 	struct list_head thermal_instances;
157 	struct list_head node;
158 };
159 
160 struct thermal_attr {
161 	struct device_attribute attr;
162 	char name[THERMAL_NAME_LENGTH];
163 };
164 
165 /**
166  * struct thermal_zone_device - structure for a thermal zone
167  * @id:		unique id number for each thermal zone
168  * @type:	the thermal zone device type
169  * @device:	&struct device for this thermal zone
170  * @trip_temp_attrs:	attributes for trip points for sysfs: trip temperature
171  * @trip_type_attrs:	attributes for trip points for sysfs: trip type
172  * @trip_hyst_attrs:	attributes for trip points for sysfs: trip hysteresis
173  * @devdata:	private pointer for device private data
174  * @trips:	number of trip points the thermal zone supports
175  * @passive_delay:	number of milliseconds to wait between polls when
176  *			performing passive cooling.  Currenty only used by the
177  *			step-wise governor
178  * @polling_delay:	number of milliseconds to wait between polls when
179  *			checking whether trip points have been crossed (0 for
180  *			interrupt driven systems)
181  * @temperature:	current temperature.  This is only for core code,
182  *			drivers should use thermal_zone_get_temp() to get the
183  *			current temperature
184  * @last_temperature:	previous temperature read
185  * @emul_temperature:	emulated temperature when using CONFIG_THERMAL_EMULATION
186  * @passive:		1 if you've crossed a passive trip point, 0 otherwise.
187  *			Currenty only used by the step-wise governor.
188  * @forced_passive:	If > 0, temperature at which to switch on all ACPI
189  *			processor cooling devices.  Currently only used by the
190  *			step-wise governor.
191  * @ops:	operations this &thermal_zone_device supports
192  * @tzp:	thermal zone parameters
193  * @governor:	pointer to the governor for this thermal zone
194  * @thermal_instances:	list of &struct thermal_instance of this thermal zone
195  * @idr:	&struct idr to generate unique id for this zone's cooling
196  *		devices
197  * @lock:	lock to protect thermal_instances list
198  * @node:	node in thermal_tz_list (in thermal_core.c)
199  * @poll_queue:	delayed work for polling
200  */
201 struct thermal_zone_device {
202 	int id;
203 	char type[THERMAL_NAME_LENGTH];
204 	struct device device;
205 	struct thermal_attr *trip_temp_attrs;
206 	struct thermal_attr *trip_type_attrs;
207 	struct thermal_attr *trip_hyst_attrs;
208 	void *devdata;
209 	int trips;
210 	int passive_delay;
211 	int polling_delay;
212 	int temperature;
213 	int last_temperature;
214 	int emul_temperature;
215 	int passive;
216 	unsigned int forced_passive;
217 	struct thermal_zone_device_ops *ops;
218 	const struct thermal_zone_params *tzp;
219 	struct thermal_governor *governor;
220 	struct list_head thermal_instances;
221 	struct idr idr;
222 	struct mutex lock;
223 	struct list_head node;
224 	struct delayed_work poll_queue;
225 };
226 
227 /**
228  * struct thermal_governor - structure that holds thermal governor information
229  * @name:	name of the governor
230  * @throttle:	callback called for every trip point even if temperature is
231  *		below the trip point temperature
232  * @governor_list:	node in thermal_governor_list (in thermal_core.c)
233  */
234 struct thermal_governor {
235 	char name[THERMAL_NAME_LENGTH];
236 	int (*throttle)(struct thermal_zone_device *tz, int trip);
237 	struct list_head	governor_list;
238 };
239 
240 /* Structure that holds binding parameters for a zone */
241 struct thermal_bind_params {
242 	struct thermal_cooling_device *cdev;
243 
244 	/*
245 	 * This is a measure of 'how effectively these devices can
246 	 * cool 'this' thermal zone. The shall be determined by platform
247 	 * characterization. This is on a 'percentage' scale.
248 	 * See Documentation/thermal/sysfs-api.txt for more information.
249 	 */
250 	int weight;
251 
252 	/*
253 	 * This is a bit mask that gives the binding relation between this
254 	 * thermal zone and cdev, for a particular trip point.
255 	 * See Documentation/thermal/sysfs-api.txt for more information.
256 	 */
257 	int trip_mask;
258 
259 	/*
260 	 * This is an array of cooling state limits. Must have exactly
261 	 * 2 * thermal_zone.number_of_trip_points. It is an array consisting
262 	 * of tuples <lower-state upper-state> of state limits. Each trip
263 	 * will be associated with one state limit tuple when binding.
264 	 * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS>
265 	 * on all trips.
266 	 */
267 	unsigned long *binding_limits;
268 	int (*match) (struct thermal_zone_device *tz,
269 			struct thermal_cooling_device *cdev);
270 };
271 
272 /* Structure to define Thermal Zone parameters */
273 struct thermal_zone_params {
274 	char governor_name[THERMAL_NAME_LENGTH];
275 
276 	/*
277 	 * a boolean to indicate if the thermal to hwmon sysfs interface
278 	 * is required. when no_hwmon == false, a hwmon sysfs interface
279 	 * will be created. when no_hwmon == true, nothing will be done
280 	 */
281 	bool no_hwmon;
282 
283 	int num_tbps;	/* Number of tbp entries */
284 	struct thermal_bind_params *tbp;
285 };
286 
287 struct thermal_genl_event {
288 	u32 orig;
289 	enum events event;
290 };
291 
292 /* Function declarations */
293 #ifdef CONFIG_THERMAL_OF
294 struct thermal_zone_device *
295 thermal_zone_of_sensor_register(struct device *dev, int id,
296 				void *data, int (*get_temp)(void *, long *),
297 				int (*get_trend)(void *, long *));
298 void thermal_zone_of_sensor_unregister(struct device *dev,
299 				       struct thermal_zone_device *tz);
300 #else
301 static inline struct thermal_zone_device *
302 thermal_zone_of_sensor_register(struct device *dev, int id,
303 				void *data, int (*get_temp)(void *, long *),
304 				int (*get_trend)(void *, long *))
305 {
306 	return NULL;
307 }
308 
309 static inline
310 void thermal_zone_of_sensor_unregister(struct device *dev,
311 				       struct thermal_zone_device *tz)
312 {
313 }
314 
315 #endif
316 struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
317 		void *, struct thermal_zone_device_ops *,
318 		const struct thermal_zone_params *, int, int);
319 void thermal_zone_device_unregister(struct thermal_zone_device *);
320 
321 int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
322 				     struct thermal_cooling_device *,
323 				     unsigned long, unsigned long);
324 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
325 				       struct thermal_cooling_device *);
326 void thermal_zone_device_update(struct thermal_zone_device *);
327 
328 struct thermal_cooling_device *thermal_cooling_device_register(char *, void *,
329 		const struct thermal_cooling_device_ops *);
330 struct thermal_cooling_device *
331 thermal_of_cooling_device_register(struct device_node *np, char *, void *,
332 				   const struct thermal_cooling_device_ops *);
333 void thermal_cooling_device_unregister(struct thermal_cooling_device *);
334 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
335 int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp);
336 
337 int get_tz_trend(struct thermal_zone_device *, int);
338 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *,
339 		struct thermal_cooling_device *, int);
340 void thermal_cdev_update(struct thermal_cooling_device *);
341 void thermal_notify_framework(struct thermal_zone_device *, int);
342 
343 #ifdef CONFIG_NET
344 extern int thermal_generate_netlink_event(struct thermal_zone_device *tz,
345 						enum events event);
346 #else
347 static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz,
348 						enum events event)
349 {
350 	return 0;
351 }
352 #endif
353 
354 #endif /* __THERMAL_H__ */
355