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