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