1 /* SPDX-License-Identifier: GPL-2.0 */
3 * thermal.h ($Revision: 0 $)
5 * Copyright (C) 2008 Intel Corp
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>
20 /* invalid cooling state */
21 #define THERMAL_CSTATE_INVALID -1UL
23 /* No upper/lower limit requirement */
24 #define THERMAL_NO_LIMIT ((u32)~0)
26 /* Default weight of a bound cooling device */
27 #define THERMAL_WEIGHT_DEFAULT 0
29 /* use value, which < 0K, to indicate an invalid/uninitialized temperature */
30 #define THERMAL_TEMP_INVALID -274000
32 struct thermal_zone_device;
33 struct thermal_cooling_device;
34 struct thermal_instance;
35 struct thermal_debugfs;
39 THERMAL_TREND_STABLE, /* temperature is stable */
40 THERMAL_TREND_RAISING, /* temperature is raising */
41 THERMAL_TREND_DROPPING, /* temperature is dropping */
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 */
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
71 enum thermal_trip_type type;
76 #define THERMAL_TRIP_FLAG_RW_TEMP BIT(0)
77 #define THERMAL_TRIP_FLAG_RW_HYST BIT(1)
79 #define THERMAL_TRIP_FLAG_RW (THERMAL_TRIP_FLAG_RW_TEMP | \
80 THERMAL_TRIP_FLAG_RW_HYST)
82 #define THERMAL_TRIP_PRIV_TO_INT(_val_) (uintptr_t)(_val_)
83 #define THERMAL_INT_TO_TRIP_PRIV(_val_) (void *)(uintptr_t)(_val_)
85 struct thermal_zone_device;
87 struct thermal_zone_device_ops {
88 int (*bind) (struct thermal_zone_device *,
89 struct thermal_cooling_device *);
90 int (*unbind) (struct thermal_zone_device *,
91 struct thermal_cooling_device *);
92 int (*get_temp) (struct thermal_zone_device *, int *);
93 int (*set_trips) (struct thermal_zone_device *, int, int);
94 int (*change_mode) (struct thermal_zone_device *,
95 enum thermal_device_mode);
96 int (*set_trip_temp) (struct thermal_zone_device *,
97 const struct thermal_trip *, int);
98 int (*get_crit_temp) (struct thermal_zone_device *, int *);
99 int (*set_emul_temp) (struct thermal_zone_device *, int);
100 int (*get_trend) (struct thermal_zone_device *,
101 const struct thermal_trip *, enum thermal_trend *);
102 void (*hot)(struct thermal_zone_device *);
103 void (*critical)(struct thermal_zone_device *);
106 struct thermal_cooling_device_ops {
107 int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
108 int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
109 int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
110 int (*get_requested_power)(struct thermal_cooling_device *, u32 *);
111 int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *);
112 int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *);
115 struct thermal_cooling_device {
118 unsigned long max_state;
119 struct device device;
120 struct device_node *np;
123 const struct thermal_cooling_device_ops *ops;
124 bool updated; /* true if the cooling device does not need update */
125 struct mutex lock; /* protect thermal_instances list */
126 struct list_head thermal_instances;
127 struct list_head node;
128 #ifdef CONFIG_THERMAL_DEBUGFS
129 struct thermal_debugfs *debugfs;
133 /* Structure to define Thermal Zone parameters */
134 struct thermal_zone_params {
135 const char *governor_name;
138 * a boolean to indicate if the thermal to hwmon sysfs interface
139 * is required. when no_hwmon == false, a hwmon sysfs interface
140 * will be created. when no_hwmon == true, nothing will be done
145 * Sustainable power (heat) that this thermal zone can dissipate in
148 u32 sustainable_power;
151 * Proportional parameter of the PID controller when
152 * overshooting (i.e., when temperature is below the target)
157 * Proportional parameter of the PID controller when
162 /* Integral parameter of the PID controller */
165 /* Derivative parameter of the PID controller */
168 /* threshold below which the error is no longer accumulated */
172 * @slope: slope of a linear temperature adjustment curve.
173 * Used by thermal zone drivers.
177 * @offset: offset of a linear temperature adjustment curve.
178 * Used by thermal zone drivers (default 0).
183 /* Function declarations */
184 #ifdef CONFIG_THERMAL_OF
185 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
186 const struct thermal_zone_device_ops *ops);
188 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz);
193 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
194 const struct thermal_zone_device_ops *ops)
196 return ERR_PTR(-ENOTSUPP);
199 static inline void devm_thermal_of_zone_unregister(struct device *dev,
200 struct thermal_zone_device *tz)
205 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
206 struct thermal_trip *trip);
207 int for_each_thermal_trip(struct thermal_zone_device *tz,
208 int (*cb)(struct thermal_trip *, void *),
210 int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
211 int (*cb)(struct thermal_trip *, void *),
213 int thermal_zone_get_num_trips(struct thermal_zone_device *tz);
214 void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
215 struct thermal_trip *trip, int temp);
217 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);
219 #ifdef CONFIG_THERMAL
220 struct thermal_zone_device *thermal_zone_device_register_with_trips(
222 const struct thermal_trip *trips,
223 int num_trips, void *devdata,
224 const struct thermal_zone_device_ops *ops,
225 const struct thermal_zone_params *tzp,
226 unsigned int passive_delay,
227 unsigned int polling_delay);
229 struct thermal_zone_device *thermal_tripless_zone_device_register(
232 const struct thermal_zone_device_ops *ops,
233 const struct thermal_zone_params *tzp);
235 void thermal_zone_device_unregister(struct thermal_zone_device *tz);
237 void *thermal_zone_device_priv(struct thermal_zone_device *tzd);
238 const char *thermal_zone_device_type(struct thermal_zone_device *tzd);
239 int thermal_zone_device_id(struct thermal_zone_device *tzd);
240 struct device *thermal_zone_device(struct thermal_zone_device *tzd);
242 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
243 const struct thermal_trip *trip,
244 struct thermal_cooling_device *cdev,
245 unsigned long upper, unsigned long lower,
246 unsigned int weight);
247 int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
248 struct thermal_cooling_device *,
249 unsigned long, unsigned long,
251 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
252 const struct thermal_trip *trip,
253 struct thermal_cooling_device *cdev);
254 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
255 struct thermal_cooling_device *);
256 void thermal_zone_device_update(struct thermal_zone_device *,
257 enum thermal_notify_event);
259 struct thermal_cooling_device *thermal_cooling_device_register(const char *,
260 void *, const struct thermal_cooling_device_ops *);
261 struct thermal_cooling_device *
262 thermal_of_cooling_device_register(struct device_node *np, const char *, void *,
263 const struct thermal_cooling_device_ops *);
264 struct thermal_cooling_device *
265 devm_thermal_of_cooling_device_register(struct device *dev,
266 struct device_node *np,
267 const char *type, void *devdata,
268 const struct thermal_cooling_device_ops *ops);
269 void thermal_cooling_device_update(struct thermal_cooling_device *);
270 void thermal_cooling_device_unregister(struct thermal_cooling_device *);
271 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
272 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
273 int thermal_zone_get_slope(struct thermal_zone_device *tz);
274 int thermal_zone_get_offset(struct thermal_zone_device *tz);
275 bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz,
276 const struct thermal_trip *trip,
277 struct thermal_cooling_device *cdev);
279 int thermal_zone_device_enable(struct thermal_zone_device *tz);
280 int thermal_zone_device_disable(struct thermal_zone_device *tz);
281 void thermal_zone_device_critical(struct thermal_zone_device *tz);
283 static inline struct thermal_zone_device *thermal_zone_device_register_with_trips(
285 const struct thermal_trip *trips,
286 int num_trips, void *devdata,
287 const struct thermal_zone_device_ops *ops,
288 const struct thermal_zone_params *tzp,
289 int passive_delay, int polling_delay)
290 { return ERR_PTR(-ENODEV); }
292 static inline struct thermal_zone_device *thermal_tripless_zone_device_register(
295 struct thermal_zone_device_ops *ops,
296 const struct thermal_zone_params *tzp)
297 { return ERR_PTR(-ENODEV); }
299 static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz)
302 static inline struct thermal_cooling_device *
303 thermal_cooling_device_register(const char *type, void *devdata,
304 const struct thermal_cooling_device_ops *ops)
305 { return ERR_PTR(-ENODEV); }
306 static inline struct thermal_cooling_device *
307 thermal_of_cooling_device_register(struct device_node *np,
308 const char *type, void *devdata,
309 const struct thermal_cooling_device_ops *ops)
310 { return ERR_PTR(-ENODEV); }
311 static inline struct thermal_cooling_device *
312 devm_thermal_of_cooling_device_register(struct device *dev,
313 struct device_node *np,
314 const char *type, void *devdata,
315 const struct thermal_cooling_device_ops *ops)
317 return ERR_PTR(-ENODEV);
319 static inline void thermal_cooling_device_unregister(
320 struct thermal_cooling_device *cdev)
322 static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
324 { return ERR_PTR(-ENODEV); }
325 static inline int thermal_zone_get_temp(
326 struct thermal_zone_device *tz, int *temp)
328 static inline int thermal_zone_get_slope(
329 struct thermal_zone_device *tz)
331 static inline int thermal_zone_get_offset(
332 struct thermal_zone_device *tz)
335 static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz)
340 static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
345 static inline int thermal_zone_device_id(struct thermal_zone_device *tzd)
350 static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
353 static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
355 #endif /* CONFIG_THERMAL */
357 #endif /* __THERMAL_H__ */