1 /* SPDX-License-Identifier: GPL-2.0 */
3 * thermal.h ($Revision: 0 $)
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>
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 * @threshold: trip crossing notification threshold miliCelsius
65 * @type: trip point type
66 * @priv: pointer to driver data associated with this trip
67 * @flags: flags representing binary properties of the trip
73 enum thermal_trip_type type;
78 #define THERMAL_TRIP_FLAG_RW_TEMP BIT(0)
79 #define THERMAL_TRIP_FLAG_RW_HYST BIT(1)
81 #define THERMAL_TRIP_FLAG_RW (THERMAL_TRIP_FLAG_RW_TEMP | \
82 THERMAL_TRIP_FLAG_RW_HYST)
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 *);
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 *);
111 struct thermal_cooling_device {
114 unsigned long max_state;
115 struct device device;
116 struct device_node *np;
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;
130 * struct thermal_zone_device - structure for a thermal zone
131 * @id: unique id number for each thermal zone
132 * @type: the thermal zone device type
133 * @device: &struct device for this thermal zone
134 * @removal: removal completion
135 * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
136 * @trip_type_attrs: attributes for trip points for sysfs: trip type
137 * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
138 * @mode: current mode of this thermal zone
139 * @devdata: private pointer for device private data
140 * @num_trips: number of trip points the thermal zone supports
141 * @passive_delay_jiffies: number of jiffies to wait between polls when
142 * performing passive cooling.
143 * @polling_delay_jiffies: number of jiffies to wait between polls when
144 * checking whether trip points have been crossed (0 for
145 * interrupt driven systems)
146 * @temperature: current temperature. This is only for core code,
147 * drivers should use thermal_zone_get_temp() to get the
148 * current temperature
149 * @last_temperature: previous temperature read
150 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
151 * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
152 * @prev_low_trip: the low current temperature if you've crossed a passive
154 * @prev_high_trip: the above current temperature if you've crossed a
156 * @need_update: if equals 1, thermal_zone_device_update needs to be invoked.
157 * @ops: operations this &thermal_zone_device supports
158 * @tzp: thermal zone parameters
159 * @governor: pointer to the governor for this thermal zone
160 * @governor_data: private pointer for governor data
161 * @thermal_instances: list of &struct thermal_instance of this thermal zone
162 * @ida: &struct ida to generate unique id for this zone's cooling
164 * @lock: lock to protect thermal_instances list
165 * @node: node in thermal_tz_list (in thermal_core.c)
166 * @poll_queue: delayed work for polling
167 * @notify_event: Last notification event
168 * @suspended: thermal zone suspend indicator
169 * @trips: array of struct thermal_trip objects
171 struct thermal_zone_device {
173 char type[THERMAL_NAME_LENGTH];
174 struct device device;
175 struct completion removal;
176 struct attribute_group trips_attribute_group;
177 struct thermal_attr *trip_temp_attrs;
178 struct thermal_attr *trip_type_attrs;
179 struct thermal_attr *trip_hyst_attrs;
180 enum thermal_device_mode mode;
183 unsigned long passive_delay_jiffies;
184 unsigned long polling_delay_jiffies;
186 int last_temperature;
187 int emul_temperature;
191 atomic_t need_update;
192 struct thermal_zone_device_ops ops;
193 struct thermal_zone_params *tzp;
194 struct thermal_governor *governor;
196 struct list_head thermal_instances;
199 struct list_head node;
200 struct delayed_work poll_queue;
201 enum thermal_notify_event notify_event;
203 #ifdef CONFIG_THERMAL_DEBUGFS
204 struct thermal_debugfs *debugfs;
206 struct thermal_trip trips[] __counted_by(num_trips);
210 * struct thermal_governor - structure that holds thermal governor information
211 * @name: name of the governor
212 * @bind_to_tz: callback called when binding to a thermal zone. If it
213 * returns 0, the governor is bound to the thermal zone,
214 * otherwise it fails.
215 * @unbind_from_tz: callback called when a governor is unbound from a
217 * @throttle: callback called for every trip point even if temperature is
218 * below the trip point temperature
219 * @update_tz: callback called when thermal zone internals have changed, e.g.
220 * thermal cooling instance was added/removed
221 * @governor_list: node in thermal_governor_list (in thermal_core.c)
223 struct thermal_governor {
225 int (*bind_to_tz)(struct thermal_zone_device *tz);
226 void (*unbind_from_tz)(struct thermal_zone_device *tz);
227 int (*throttle)(struct thermal_zone_device *tz,
228 const struct thermal_trip *trip);
229 void (*update_tz)(struct thermal_zone_device *tz,
230 enum thermal_notify_event reason);
231 struct list_head governor_list;
234 /* Structure to define Thermal Zone parameters */
235 struct thermal_zone_params {
236 const char *governor_name;
239 * a boolean to indicate if the thermal to hwmon sysfs interface
240 * is required. when no_hwmon == false, a hwmon sysfs interface
241 * will be created. when no_hwmon == true, nothing will be done
246 * Sustainable power (heat) that this thermal zone can dissipate in
249 u32 sustainable_power;
252 * Proportional parameter of the PID controller when
253 * overshooting (i.e., when temperature is below the target)
258 * Proportional parameter of the PID controller when
263 /* Integral parameter of the PID controller */
266 /* Derivative parameter of the PID controller */
269 /* threshold below which the error is no longer accumulated */
273 * @slope: slope of a linear temperature adjustment curve.
274 * Used by thermal zone drivers.
278 * @offset: offset of a linear temperature adjustment curve.
279 * Used by thermal zone drivers (default 0).
284 /* Function declarations */
285 #ifdef CONFIG_THERMAL_OF
286 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
287 const struct thermal_zone_device_ops *ops);
289 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz);
294 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
295 const struct thermal_zone_device_ops *ops)
297 return ERR_PTR(-ENOTSUPP);
300 static inline void devm_thermal_of_zone_unregister(struct device *dev,
301 struct thermal_zone_device *tz)
306 int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
307 struct thermal_trip *trip);
308 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
309 struct thermal_trip *trip);
310 int for_each_thermal_trip(struct thermal_zone_device *tz,
311 int (*cb)(struct thermal_trip *, void *),
313 int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
314 int (*cb)(struct thermal_trip *, void *),
316 int thermal_zone_get_num_trips(struct thermal_zone_device *tz);
317 void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
318 struct thermal_trip *trip, int temp);
320 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);
322 #ifdef CONFIG_THERMAL
323 struct thermal_zone_device *thermal_zone_device_register_with_trips(
325 const struct thermal_trip *trips,
326 int num_trips, void *devdata,
327 const struct thermal_zone_device_ops *ops,
328 const struct thermal_zone_params *tzp,
329 int passive_delay, int polling_delay);
331 struct thermal_zone_device *thermal_tripless_zone_device_register(
334 const struct thermal_zone_device_ops *ops,
335 const struct thermal_zone_params *tzp);
337 void thermal_zone_device_unregister(struct thermal_zone_device *tz);
339 void *thermal_zone_device_priv(struct thermal_zone_device *tzd);
340 const char *thermal_zone_device_type(struct thermal_zone_device *tzd);
341 int thermal_zone_device_id(struct thermal_zone_device *tzd);
342 struct device *thermal_zone_device(struct thermal_zone_device *tzd);
344 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
345 const struct thermal_trip *trip,
346 struct thermal_cooling_device *cdev,
347 unsigned long upper, unsigned long lower,
348 unsigned int weight);
349 int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
350 struct thermal_cooling_device *,
351 unsigned long, unsigned long,
353 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
354 const struct thermal_trip *trip,
355 struct thermal_cooling_device *cdev);
356 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
357 struct thermal_cooling_device *);
358 void thermal_zone_device_update(struct thermal_zone_device *,
359 enum thermal_notify_event);
361 struct thermal_cooling_device *thermal_cooling_device_register(const char *,
362 void *, const struct thermal_cooling_device_ops *);
363 struct thermal_cooling_device *
364 thermal_of_cooling_device_register(struct device_node *np, const char *, void *,
365 const struct thermal_cooling_device_ops *);
366 struct thermal_cooling_device *
367 devm_thermal_of_cooling_device_register(struct device *dev,
368 struct device_node *np,
369 char *type, void *devdata,
370 const struct thermal_cooling_device_ops *ops);
371 void thermal_cooling_device_update(struct thermal_cooling_device *);
372 void thermal_cooling_device_unregister(struct thermal_cooling_device *);
373 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
374 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
375 int thermal_zone_get_slope(struct thermal_zone_device *tz);
376 int thermal_zone_get_offset(struct thermal_zone_device *tz);
378 int thermal_zone_device_enable(struct thermal_zone_device *tz);
379 int thermal_zone_device_disable(struct thermal_zone_device *tz);
380 void thermal_zone_device_critical(struct thermal_zone_device *tz);
382 static inline struct thermal_zone_device *thermal_zone_device_register_with_trips(
384 const struct thermal_trip *trips,
385 int num_trips, void *devdata,
386 const struct thermal_zone_device_ops *ops,
387 const struct thermal_zone_params *tzp,
388 int passive_delay, int polling_delay)
389 { return ERR_PTR(-ENODEV); }
391 static inline struct thermal_zone_device *thermal_tripless_zone_device_register(
394 struct thermal_zone_device_ops *ops,
395 const struct thermal_zone_params *tzp)
396 { return ERR_PTR(-ENODEV); }
398 static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz)
401 static inline struct thermal_cooling_device *
402 thermal_cooling_device_register(const char *type, void *devdata,
403 const struct thermal_cooling_device_ops *ops)
404 { return ERR_PTR(-ENODEV); }
405 static inline struct thermal_cooling_device *
406 thermal_of_cooling_device_register(struct device_node *np,
407 const char *type, void *devdata,
408 const struct thermal_cooling_device_ops *ops)
409 { return ERR_PTR(-ENODEV); }
410 static inline struct thermal_cooling_device *
411 devm_thermal_of_cooling_device_register(struct device *dev,
412 struct device_node *np,
413 char *type, void *devdata,
414 const struct thermal_cooling_device_ops *ops)
416 return ERR_PTR(-ENODEV);
418 static inline void thermal_cooling_device_unregister(
419 struct thermal_cooling_device *cdev)
421 static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
423 { return ERR_PTR(-ENODEV); }
424 static inline int thermal_zone_get_temp(
425 struct thermal_zone_device *tz, int *temp)
427 static inline int thermal_zone_get_slope(
428 struct thermal_zone_device *tz)
430 static inline int thermal_zone_get_offset(
431 struct thermal_zone_device *tz)
434 static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz)
439 static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
444 static inline int thermal_zone_device_id(struct thermal_zone_device *tzd)
449 static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
452 static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
454 #endif /* CONFIG_THERMAL */
456 #endif /* __THERMAL_H__ */