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