Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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_MAX_TRIPS 12
21
22/* invalid cooling state */
23#define THERMAL_CSTATE_INVALID -1UL
24
25/* No upper/lower limit requirement */
26#define THERMAL_NO_LIMIT ((u32)~0)
27
28/* Default weight of a bound cooling device */
29#define THERMAL_WEIGHT_DEFAULT 0
30
31/* use value, which < 0K, to indicate an invalid/uninitialized temperature */
32#define THERMAL_TEMP_INVALID -274000
33
34struct thermal_zone_device;
35struct thermal_cooling_device;
36struct thermal_instance;
37struct thermal_attr;
38
39enum 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 */
46enum 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};
57
58struct thermal_zone_device_ops {
59 int (*bind) (struct thermal_zone_device *,
60 struct thermal_cooling_device *);
61 int (*unbind) (struct thermal_zone_device *,
62 struct thermal_cooling_device *);
63 int (*get_temp) (struct thermal_zone_device *, int *);
64 int (*set_trips) (struct thermal_zone_device *, int, int);
65 int (*change_mode) (struct thermal_zone_device *,
66 enum thermal_device_mode);
67 int (*get_trip_type) (struct thermal_zone_device *, int,
68 enum thermal_trip_type *);
69 int (*get_trip_temp) (struct thermal_zone_device *, int, int *);
70 int (*set_trip_temp) (struct thermal_zone_device *, int, int);
71 int (*get_trip_hyst) (struct thermal_zone_device *, int, int *);
72 int (*set_trip_hyst) (struct thermal_zone_device *, int, int);
73 int (*get_crit_temp) (struct thermal_zone_device *, int *);
74 int (*set_emul_temp) (struct thermal_zone_device *, int);
75 int (*get_trend) (struct thermal_zone_device *, int,
76 enum thermal_trend *);
77 void (*hot)(struct thermal_zone_device *);
78 void (*critical)(struct thermal_zone_device *);
79};
80
81/**
82 * struct thermal_trip - representation of a point in temperature domain
83 * @temperature: temperature value in miliCelsius
84 * @hysteresis: relative hysteresis in miliCelsius
85 * @type: trip point type
86 */
87struct thermal_trip {
88 int temperature;
89 int hysteresis;
90 enum thermal_trip_type type;
91};
92
93struct thermal_cooling_device_ops {
94 int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
95 int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
96 int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
97 int (*get_requested_power)(struct thermal_cooling_device *, u32 *);
98 int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *);
99 int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *);
100};
101
102struct thermal_cooling_device {
103 int id;
104 char *type;
105 struct device device;
106 struct device_node *np;
107 void *devdata;
108 void *stats;
109 const struct thermal_cooling_device_ops *ops;
110 bool updated; /* true if the cooling device does not need update */
111 struct mutex lock; /* protect thermal_instances list */
112 struct list_head thermal_instances;
113 struct list_head node;
114};
115
116/**
117 * struct thermal_zone_device - structure for a thermal zone
118 * @id: unique id number for each thermal zone
119 * @type: the thermal zone device type
120 * @device: &struct device for this thermal zone
121 * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
122 * @trip_type_attrs: attributes for trip points for sysfs: trip type
123 * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
124 * @mode: current mode of this thermal zone
125 * @devdata: private pointer for device private data
126 * @trips: an array of struct thermal_trip
127 * @num_trips: number of trip points the thermal zone supports
128 * @trips_disabled; bitmap for disabled trips
129 * @passive_delay_jiffies: number of jiffies to wait between polls when
130 * performing passive cooling.
131 * @polling_delay_jiffies: number of jiffies to wait between polls when
132 * checking whether trip points have been crossed (0 for
133 * interrupt driven systems)
134 * @temperature: current temperature. This is only for core code,
135 * drivers should use thermal_zone_get_temp() to get the
136 * current temperature
137 * @last_temperature: previous temperature read
138 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
139 * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
140 * @prev_low_trip: the low current temperature if you've crossed a passive
141 trip point.
142 * @prev_high_trip: the above current temperature if you've crossed a
143 passive trip point.
144 * @need_update: if equals 1, thermal_zone_device_update needs to be invoked.
145 * @ops: operations this &thermal_zone_device supports
146 * @tzp: thermal zone parameters
147 * @governor: pointer to the governor for this thermal zone
148 * @governor_data: private pointer for governor data
149 * @thermal_instances: list of &struct thermal_instance of this thermal zone
150 * @ida: &struct ida to generate unique id for this zone's cooling
151 * devices
152 * @lock: lock to protect thermal_instances list
153 * @node: node in thermal_tz_list (in thermal_core.c)
154 * @poll_queue: delayed work for polling
155 * @notify_event: Last notification event
156 */
157struct thermal_zone_device {
158 int id;
159 char type[THERMAL_NAME_LENGTH];
160 struct device device;
161 struct attribute_group trips_attribute_group;
162 struct thermal_attr *trip_temp_attrs;
163 struct thermal_attr *trip_type_attrs;
164 struct thermal_attr *trip_hyst_attrs;
165 enum thermal_device_mode mode;
166 void *devdata;
167 struct thermal_trip *trips;
168 int num_trips;
169 unsigned long trips_disabled; /* bitmap for disabled trips */
170 unsigned long passive_delay_jiffies;
171 unsigned long polling_delay_jiffies;
172 int temperature;
173 int last_temperature;
174 int emul_temperature;
175 int passive;
176 int prev_low_trip;
177 int prev_high_trip;
178 atomic_t need_update;
179 struct thermal_zone_device_ops *ops;
180 struct thermal_zone_params *tzp;
181 struct thermal_governor *governor;
182 void *governor_data;
183 struct list_head thermal_instances;
184 struct ida ida;
185 struct mutex lock;
186 struct list_head node;
187 struct delayed_work poll_queue;
188 enum thermal_notify_event notify_event;
189};
190
191/**
192 * struct thermal_governor - structure that holds thermal governor information
193 * @name: name of the governor
194 * @bind_to_tz: callback called when binding to a thermal zone. If it
195 * returns 0, the governor is bound to the thermal zone,
196 * otherwise it fails.
197 * @unbind_from_tz: callback called when a governor is unbound from a
198 * thermal zone.
199 * @throttle: callback called for every trip point even if temperature is
200 * below the trip point temperature
201 * @governor_list: node in thermal_governor_list (in thermal_core.c)
202 */
203struct thermal_governor {
204 char name[THERMAL_NAME_LENGTH];
205 int (*bind_to_tz)(struct thermal_zone_device *tz);
206 void (*unbind_from_tz)(struct thermal_zone_device *tz);
207 int (*throttle)(struct thermal_zone_device *tz, int trip);
208 struct list_head governor_list;
209};
210
211/* Structure that holds binding parameters for a zone */
212struct 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
246/* Structure to define Thermal Zone parameters */
247struct thermal_zone_params {
248 char governor_name[THERMAL_NAME_LENGTH];
249
250 /*
251 * a boolean to indicate if the thermal to hwmon sysfs interface
252 * is required. when no_hwmon == false, a hwmon sysfs interface
253 * will be created. when no_hwmon == true, nothing will be done
254 */
255 bool no_hwmon;
256
257 int num_tbps; /* Number of tbp entries */
258 struct thermal_bind_params *tbp;
259
260 /*
261 * Sustainable power (heat) that this thermal zone can dissipate in
262 * mW
263 */
264 u32 sustainable_power;
265
266 /*
267 * Proportional parameter of the PID controller when
268 * overshooting (i.e., when temperature is below the target)
269 */
270 s32 k_po;
271
272 /*
273 * Proportional parameter of the PID controller when
274 * undershooting
275 */
276 s32 k_pu;
277
278 /* Integral parameter of the PID controller */
279 s32 k_i;
280
281 /* Derivative parameter of the PID controller */
282 s32 k_d;
283
284 /* threshold below which the error is no longer accumulated */
285 s32 integral_cutoff;
286
287 /*
288 * @slope: slope of a linear temperature adjustment curve.
289 * Used by thermal zone drivers.
290 */
291 int slope;
292 /*
293 * @offset: offset of a linear temperature adjustment curve.
294 * Used by thermal zone drivers (default 0).
295 */
296 int offset;
297};
298
299/**
300 * struct thermal_zone_of_device_ops - callbacks for handling DT based zones
301 *
302 * Mandatory:
303 * @get_temp: a pointer to a function that reads the sensor temperature.
304 *
305 * Optional:
306 * @get_trend: a pointer to a function that reads the sensor temperature trend.
307 * @set_trips: a pointer to a function that sets a temperature window. When
308 * this window is left the driver must inform the thermal core via
309 * thermal_zone_device_update.
310 * @set_emul_temp: a pointer to a function that sets sensor emulated
311 * temperature.
312 * @set_trip_temp: a pointer to a function that sets the trip temperature on
313 * hardware.
314 * @change_mode: a pointer to a function that notifies the thermal zone
315 * mode change.
316 */
317struct thermal_zone_of_device_ops {
318 int (*get_temp)(void *, int *);
319 int (*get_trend)(void *, int, enum thermal_trend *);
320 int (*set_trips)(void *, int, int);
321 int (*set_emul_temp)(void *, int);
322 int (*set_trip_temp)(void *, int, int);
323 int (*change_mode) (void *, enum thermal_device_mode);
324};
325
326/* Function declarations */
327#ifdef CONFIG_THERMAL_OF
328int thermal_zone_of_get_sensor_id(struct device_node *tz_np,
329 struct device_node *sensor_np,
330 u32 *id);
331struct thermal_zone_device *
332thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
333 const struct thermal_zone_of_device_ops *ops);
334void thermal_zone_of_sensor_unregister(struct device *dev,
335 struct thermal_zone_device *tz);
336struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
337 struct device *dev, int id, void *data,
338 const struct thermal_zone_of_device_ops *ops);
339void devm_thermal_zone_of_sensor_unregister(struct device *dev,
340 struct thermal_zone_device *tz);
341#else
342
343static inline int thermal_zone_of_get_sensor_id(struct device_node *tz_np,
344 struct device_node *sensor_np,
345 u32 *id)
346{
347 return -ENOENT;
348}
349static inline struct thermal_zone_device *
350thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
351 const struct thermal_zone_of_device_ops *ops)
352{
353 return ERR_PTR(-ENODEV);
354}
355
356static inline
357void thermal_zone_of_sensor_unregister(struct device *dev,
358 struct thermal_zone_device *tz)
359{
360}
361
362static inline struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
363 struct device *dev, int id, void *data,
364 const struct thermal_zone_of_device_ops *ops)
365{
366 return ERR_PTR(-ENODEV);
367}
368
369static inline
370void devm_thermal_zone_of_sensor_unregister(struct device *dev,
371 struct thermal_zone_device *tz)
372{
373}
374
375#endif
376
377#ifdef CONFIG_THERMAL
378struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
379 void *, struct thermal_zone_device_ops *,
380 struct thermal_zone_params *, int, int);
381
382void thermal_zone_device_unregister(struct thermal_zone_device *);
383
384struct thermal_zone_device *
385thermal_zone_device_register_with_trips(const char *, struct thermal_trip *, int, int,
386 void *, struct thermal_zone_device_ops *,
387 struct thermal_zone_params *, int, int);
388
389int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
390 struct thermal_cooling_device *,
391 unsigned long, unsigned long,
392 unsigned int);
393int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
394 struct thermal_cooling_device *);
395void thermal_zone_device_update(struct thermal_zone_device *,
396 enum thermal_notify_event);
397
398struct thermal_cooling_device *thermal_cooling_device_register(const char *,
399 void *, const struct thermal_cooling_device_ops *);
400struct thermal_cooling_device *
401thermal_of_cooling_device_register(struct device_node *np, const char *, void *,
402 const struct thermal_cooling_device_ops *);
403struct thermal_cooling_device *
404devm_thermal_of_cooling_device_register(struct device *dev,
405 struct device_node *np,
406 char *type, void *devdata,
407 const struct thermal_cooling_device_ops *ops);
408void thermal_cooling_device_unregister(struct thermal_cooling_device *);
409struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
410int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
411int thermal_zone_get_slope(struct thermal_zone_device *tz);
412int thermal_zone_get_offset(struct thermal_zone_device *tz);
413
414int thermal_zone_device_enable(struct thermal_zone_device *tz);
415int thermal_zone_device_disable(struct thermal_zone_device *tz);
416void thermal_zone_device_critical(struct thermal_zone_device *tz);
417#else
418static inline struct thermal_zone_device *thermal_zone_device_register(
419 const char *type, int trips, int mask, void *devdata,
420 struct thermal_zone_device_ops *ops,
421 struct thermal_zone_params *tzp,
422 int passive_delay, int polling_delay)
423{ return ERR_PTR(-ENODEV); }
424static inline void thermal_zone_device_unregister(
425 struct thermal_zone_device *tz)
426{ }
427static inline struct thermal_cooling_device *
428thermal_cooling_device_register(const char *type, void *devdata,
429 const struct thermal_cooling_device_ops *ops)
430{ return ERR_PTR(-ENODEV); }
431static inline struct thermal_cooling_device *
432thermal_of_cooling_device_register(struct device_node *np,
433 const char *type, void *devdata,
434 const struct thermal_cooling_device_ops *ops)
435{ return ERR_PTR(-ENODEV); }
436static inline struct thermal_cooling_device *
437devm_thermal_of_cooling_device_register(struct device *dev,
438 struct device_node *np,
439 char *type, void *devdata,
440 const struct thermal_cooling_device_ops *ops)
441{
442 return ERR_PTR(-ENODEV);
443}
444static inline void thermal_cooling_device_unregister(
445 struct thermal_cooling_device *cdev)
446{ }
447static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
448 const char *name)
449{ return ERR_PTR(-ENODEV); }
450static inline int thermal_zone_get_temp(
451 struct thermal_zone_device *tz, int *temp)
452{ return -ENODEV; }
453static inline int thermal_zone_get_slope(
454 struct thermal_zone_device *tz)
455{ return -ENODEV; }
456static inline int thermal_zone_get_offset(
457 struct thermal_zone_device *tz)
458{ return -ENODEV; }
459
460static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
461{ return -ENODEV; }
462
463static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
464{ return -ENODEV; }
465#endif /* CONFIG_THERMAL */
466
467#endif /* __THERMAL_H__ */