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