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 /* invalid cooling state */
22 #define THERMAL_CSTATE_INVALID -1UL
23 
24 /* No upper/lower limit requirement */
25 #define THERMAL_NO_LIMIT	((u32)~0)
26 
27 /* Default weight of a bound cooling device */
28 #define THERMAL_WEIGHT_DEFAULT 0
29 
30 /* use value, which < 0K, to indicate an invalid/uninitialized temperature */
31 #define THERMAL_TEMP_INVALID	-274000
32 
33 struct thermal_zone_device;
34 struct thermal_cooling_device;
35 struct thermal_instance;
36 struct thermal_debugfs;
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 };
44 
45 /* Thermal notification reason */
46 enum thermal_notify_event {
47 	THERMAL_EVENT_UNSPECIFIED, /* Unspecified event */
48 	THERMAL_EVENT_TEMP_SAMPLE, /* New Temperature sample */
49 	THERMAL_TRIP_VIOLATED, /* TRIP Point violation */
50 	THERMAL_TRIP_CHANGED, /* TRIP Point temperature changed */
51 	THERMAL_DEVICE_DOWN, /* Thermal device is down */
52 	THERMAL_DEVICE_UP, /* Thermal device is up after a down event */
53 	THERMAL_DEVICE_POWER_CAPABILITY_CHANGED, /* power capability changed */
54 	THERMAL_TABLE_CHANGED, /* Thermal table(s) changed */
55 	THERMAL_EVENT_KEEP_ALIVE, /* Request for user space handler to respond */
56 	THERMAL_TZ_BIND_CDEV, /* Cooling dev is bind to the thermal zone */
57 	THERMAL_TZ_UNBIND_CDEV, /* Cooling dev is unbind from the thermal zone */
58 	THERMAL_INSTANCE_WEIGHT_CHANGED, /* Thermal instance weight changed */
59 	THERMAL_TZ_RESUME, /* Thermal zone is resuming after system sleep */
60 	THERMAL_TZ_ADD_THRESHOLD, /* Threshold added */
61 	THERMAL_TZ_DEL_THRESHOLD, /* Threshold deleted */
62 	THERMAL_TZ_FLUSH_THRESHOLDS, /* All thresholds deleted */
63 };
64 
65 /**
66  * struct thermal_trip - representation of a point in temperature domain
67  * @temperature: temperature value in miliCelsius
68  * @hysteresis: relative hysteresis in miliCelsius
69  * @type: trip point type
70  * @priv: pointer to driver data associated with this trip
71  * @flags: flags representing binary properties of the trip
72  */
73 struct thermal_trip {
74 	int temperature;
75 	int hysteresis;
76 	enum thermal_trip_type type;
77 	u8 flags;
78 	void *priv;
79 };
80 
81 #define THERMAL_TRIP_FLAG_RW_TEMP	BIT(0)
82 #define THERMAL_TRIP_FLAG_RW_HYST	BIT(1)
83 
84 #define THERMAL_TRIP_FLAG_RW	(THERMAL_TRIP_FLAG_RW_TEMP | \
85 				 THERMAL_TRIP_FLAG_RW_HYST)
86 
87 #define THERMAL_TRIP_PRIV_TO_INT(_val_)	(uintptr_t)(_val_)
88 #define THERMAL_INT_TO_TRIP_PRIV(_val_)	(void *)(uintptr_t)(_val_)
89 
90 struct cooling_spec {
91 	unsigned long upper;	/* Highest cooling state  */
92 	unsigned long lower;	/* Lowest cooling state  */
93 	unsigned int weight;	/* Cooling device weight */
94 };
95 
96 struct thermal_zone_device_ops {
97 	bool (*should_bind) (struct thermal_zone_device *,
98 			     const struct thermal_trip *,
99 			     struct thermal_cooling_device *,
100 			     struct cooling_spec *);
101 	int (*get_temp) (struct thermal_zone_device *, int *);
102 	int (*set_trips) (struct thermal_zone_device *, int, int);
103 	int (*change_mode) (struct thermal_zone_device *,
104 		enum thermal_device_mode);
105 	int (*set_trip_temp) (struct thermal_zone_device *,
106 			      const struct thermal_trip *, int);
107 	int (*get_crit_temp) (struct thermal_zone_device *, int *);
108 	int (*set_emul_temp) (struct thermal_zone_device *, int);
109 	int (*get_trend) (struct thermal_zone_device *,
110 			  const struct thermal_trip *, enum thermal_trend *);
111 	void (*hot)(struct thermal_zone_device *);
112 	void (*critical)(struct thermal_zone_device *);
113 
114 	ANDROID_KABI_RESERVE(1);
115 };
116 
117 struct thermal_cooling_device_ops {
118 	int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
119 	int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
120 	int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
121 	int (*get_requested_power)(struct thermal_cooling_device *, u32 *);
122 	int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *);
123 	int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *);
124 
125 	ANDROID_KABI_RESERVE(1);
126 };
127 
128 struct thermal_cooling_device {
129 	int id;
130 	const char *type;
131 	unsigned long max_state;
132 	struct device device;
133 	struct device_node *np;
134 	void *devdata;
135 	void *stats;
136 	const struct thermal_cooling_device_ops *ops;
137 	bool updated; /* true if the cooling device does not need update */
138 	struct mutex lock; /* protect thermal_instances list */
139 	struct list_head thermal_instances;
140 	struct list_head node;
141 #ifdef CONFIG_THERMAL_DEBUGFS
142 	struct thermal_debugfs *debugfs;
143 #endif
144 
145 	ANDROID_KABI_RESERVE(1);
146 };
147 
148 /* Structure to define Thermal Zone parameters */
149 struct thermal_zone_params {
150 	const char *governor_name;
151 
152 	/*
153 	 * a boolean to indicate if the thermal to hwmon sysfs interface
154 	 * is required. when no_hwmon == false, a hwmon sysfs interface
155 	 * will be created. when no_hwmon == true, nothing will be done
156 	 */
157 	bool no_hwmon;
158 
159 	/*
160 	 * Sustainable power (heat) that this thermal zone can dissipate in
161 	 * mW
162 	 */
163 	u32 sustainable_power;
164 
165 	/*
166 	 * Proportional parameter of the PID controller when
167 	 * overshooting (i.e., when temperature is below the target)
168 	 */
169 	s32 k_po;
170 
171 	/*
172 	 * Proportional parameter of the PID controller when
173 	 * undershooting
174 	 */
175 	s32 k_pu;
176 
177 	/* Integral parameter of the PID controller */
178 	s32 k_i;
179 
180 	/* Derivative parameter of the PID controller */
181 	s32 k_d;
182 
183 	/* threshold below which the error is no longer accumulated */
184 	s32 integral_cutoff;
185 
186 	/*
187 	 * @slope:	slope of a linear temperature adjustment curve.
188 	 * 		Used by thermal zone drivers.
189 	 */
190 	int slope;
191 	/*
192 	 * @offset:	offset of a linear temperature adjustment curve.
193 	 * 		Used by thermal zone drivers (default 0).
194 	 */
195 	int offset;
196 
197 	ANDROID_KABI_RESERVE(1);
198 };
199 
200 /* Function declarations */
201 #ifdef CONFIG_THERMAL_OF
202 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
203 							  const struct thermal_zone_device_ops *ops);
204 
205 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz);
206 
207 #else
208 
209 static inline
devm_thermal_of_zone_register(struct device * dev,int id,void * data,const struct thermal_zone_device_ops * ops)210 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
211 							  const struct thermal_zone_device_ops *ops)
212 {
213 	return ERR_PTR(-ENOTSUPP);
214 }
215 
devm_thermal_of_zone_unregister(struct device * dev,struct thermal_zone_device * tz)216 static inline void devm_thermal_of_zone_unregister(struct device *dev,
217 						   struct thermal_zone_device *tz)
218 {
219 }
220 #endif
221 
222 int for_each_thermal_trip(struct thermal_zone_device *tz,
223 			  int (*cb)(struct thermal_trip *, void *),
224 			  void *data);
225 int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
226 			       int (*cb)(struct thermal_trip *, void *),
227 			       void *data);
228 void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
229 				struct thermal_trip *trip, int temp);
230 
231 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);
232 
233 #ifdef CONFIG_THERMAL
234 struct thermal_zone_device *thermal_zone_device_register_with_trips(
235 					const char *type,
236 					const struct thermal_trip *trips,
237 					int num_trips, void *devdata,
238 					const struct thermal_zone_device_ops *ops,
239 					const struct thermal_zone_params *tzp,
240 					unsigned int passive_delay,
241 					unsigned int polling_delay);
242 
243 struct thermal_zone_device *thermal_tripless_zone_device_register(
244 					const char *type,
245 					void *devdata,
246 					const struct thermal_zone_device_ops *ops,
247 					const struct thermal_zone_params *tzp);
248 
249 void thermal_zone_device_unregister(struct thermal_zone_device *tz);
250 
251 void *thermal_zone_device_priv(struct thermal_zone_device *tzd);
252 const char *thermal_zone_device_type(struct thermal_zone_device *tzd);
253 int thermal_zone_device_id(struct thermal_zone_device *tzd);
254 struct device *thermal_zone_device(struct thermal_zone_device *tzd);
255 
256 void thermal_zone_device_update(struct thermal_zone_device *,
257 				enum thermal_notify_event);
258 
259 struct thermal_cooling_device *thermal_cooling_device_register(const char *,
260 		void *, const struct thermal_cooling_device_ops *);
261 struct thermal_cooling_device *
262 thermal_of_cooling_device_register(struct device_node *np, const char *, void *,
263 				   const struct thermal_cooling_device_ops *);
264 struct thermal_cooling_device *
265 devm_thermal_of_cooling_device_register(struct device *dev,
266 				struct device_node *np,
267 				const char *type, void *devdata,
268 				const struct thermal_cooling_device_ops *ops);
269 void thermal_cooling_device_update(struct thermal_cooling_device *);
270 void thermal_cooling_device_unregister(struct thermal_cooling_device *);
271 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
272 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
273 int thermal_zone_get_slope(struct thermal_zone_device *tz);
274 int thermal_zone_get_offset(struct thermal_zone_device *tz);
275 bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz,
276 				   const struct thermal_trip *trip,
277 				   struct thermal_cooling_device *cdev);
278 
279 int thermal_zone_device_enable(struct thermal_zone_device *tz);
280 int thermal_zone_device_disable(struct thermal_zone_device *tz);
281 void thermal_zone_device_critical(struct thermal_zone_device *tz);
282 #else
thermal_zone_device_register_with_trips(const char * type,const struct thermal_trip * trips,int num_trips,void * devdata,const struct thermal_zone_device_ops * ops,const struct thermal_zone_params * tzp,int passive_delay,int polling_delay)283 static inline struct thermal_zone_device *thermal_zone_device_register_with_trips(
284 					const char *type,
285 					const struct thermal_trip *trips,
286 					int num_trips, void *devdata,
287 					const struct thermal_zone_device_ops *ops,
288 					const struct thermal_zone_params *tzp,
289 					int passive_delay, int polling_delay)
290 { return ERR_PTR(-ENODEV); }
291 
thermal_tripless_zone_device_register(const char * type,void * devdata,struct thermal_zone_device_ops * ops,const struct thermal_zone_params * tzp)292 static inline struct thermal_zone_device *thermal_tripless_zone_device_register(
293 					const char *type,
294 					void *devdata,
295 					struct thermal_zone_device_ops *ops,
296 					const struct thermal_zone_params *tzp)
297 { return ERR_PTR(-ENODEV); }
298 
thermal_zone_device_unregister(struct thermal_zone_device * tz)299 static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz)
300 { }
301 
302 static inline struct thermal_cooling_device *
thermal_cooling_device_register(const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)303 thermal_cooling_device_register(const char *type, void *devdata,
304 	const struct thermal_cooling_device_ops *ops)
305 { return ERR_PTR(-ENODEV); }
306 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)307 thermal_of_cooling_device_register(struct device_node *np,
308 	const char *type, void *devdata,
309 	const struct thermal_cooling_device_ops *ops)
310 { return ERR_PTR(-ENODEV); }
311 static inline struct thermal_cooling_device *
devm_thermal_of_cooling_device_register(struct device * dev,struct device_node * np,const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)312 devm_thermal_of_cooling_device_register(struct device *dev,
313 				struct device_node *np,
314 				const char *type, void *devdata,
315 				const struct thermal_cooling_device_ops *ops)
316 {
317 	return ERR_PTR(-ENODEV);
318 }
thermal_cooling_device_unregister(struct thermal_cooling_device * cdev)319 static inline void thermal_cooling_device_unregister(
320 	struct thermal_cooling_device *cdev)
321 { }
thermal_zone_get_zone_by_name(const char * name)322 static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
323 		const char *name)
324 { return ERR_PTR(-ENODEV); }
thermal_zone_get_temp(struct thermal_zone_device * tz,int * temp)325 static inline int thermal_zone_get_temp(
326 		struct thermal_zone_device *tz, int *temp)
327 { return -ENODEV; }
thermal_zone_get_slope(struct thermal_zone_device * tz)328 static inline int thermal_zone_get_slope(
329 		struct thermal_zone_device *tz)
330 { return -ENODEV; }
thermal_zone_get_offset(struct thermal_zone_device * tz)331 static inline int thermal_zone_get_offset(
332 		struct thermal_zone_device *tz)
333 { return -ENODEV; }
334 
thermal_zone_device_priv(struct thermal_zone_device * tz)335 static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz)
336 {
337 	return NULL;
338 }
339 
thermal_zone_device_type(struct thermal_zone_device * tzd)340 static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
341 {
342 	return NULL;
343 }
344 
thermal_zone_device_id(struct thermal_zone_device * tzd)345 static inline int thermal_zone_device_id(struct thermal_zone_device *tzd)
346 {
347 	return -ENODEV;
348 }
349 
thermal_zone_device_enable(struct thermal_zone_device * tz)350 static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
351 { return -ENODEV; }
352 
thermal_zone_device_disable(struct thermal_zone_device * tz)353 static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
354 { return -ENODEV; }
355 #endif /* CONFIG_THERMAL */
356 
357 #endif /* __THERMAL_H__ */
358