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