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;
38 THERMAL_TREND_STABLE, /* temperature is stable */
39 THERMAL_TREND_RAISING, /* temperature is raising */
40 THERMAL_TREND_DROPPING, /* temperature is dropping */
43 /* Thermal notification reason */
44 enum thermal_notify_event {
45 THERMAL_EVENT_UNSPECIFIED, /* Unspecified event */
46 THERMAL_EVENT_TEMP_SAMPLE, /* New Temperature sample */
47 THERMAL_TRIP_VIOLATED, /* TRIP Point violation */
48 THERMAL_TRIP_CHANGED, /* TRIP Point temperature changed */
49 THERMAL_DEVICE_DOWN, /* Thermal device is down */
50 THERMAL_DEVICE_UP, /* Thermal device is up after a down event */
51 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED, /* power capability changed */
52 THERMAL_TABLE_CHANGED, /* Thermal table(s) changed */
53 THERMAL_EVENT_KEEP_ALIVE, /* Request for user space handler to respond */
54 THERMAL_TZ_BIND_CDEV, /* Cooling dev is bind to the thermal zone */
55 THERMAL_TZ_UNBIND_CDEV, /* Cooling dev is unbind from the thermal zone */
56 THERMAL_INSTANCE_WEIGHT_CHANGED, /* Thermal instance weight changed */
60 * struct thermal_trip - representation of a point in temperature domain
61 * @temperature: temperature value in miliCelsius
62 * @hysteresis: relative hysteresis in miliCelsius
63 * @threshold: trip crossing notification threshold miliCelsius
64 * @type: trip point type
65 * @priv: pointer to driver data associated with this trip
71 enum thermal_trip_type type;
75 struct thermal_zone_device_ops {
76 int (*bind) (struct thermal_zone_device *,
77 struct thermal_cooling_device *);
78 int (*unbind) (struct thermal_zone_device *,
79 struct thermal_cooling_device *);
80 int (*get_temp) (struct thermal_zone_device *, int *);
81 int (*set_trips) (struct thermal_zone_device *, int, int);
82 int (*change_mode) (struct thermal_zone_device *,
83 enum thermal_device_mode);
84 int (*set_trip_temp) (struct thermal_zone_device *, int, int);
85 int (*set_trip_hyst) (struct thermal_zone_device *, int, int);
86 int (*get_crit_temp) (struct thermal_zone_device *, int *);
87 int (*set_emul_temp) (struct thermal_zone_device *, int);
88 int (*get_trend) (struct thermal_zone_device *,
89 const struct thermal_trip *, enum thermal_trend *);
90 void (*hot)(struct thermal_zone_device *);
91 void (*critical)(struct thermal_zone_device *);
94 struct thermal_cooling_device_ops {
95 int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
96 int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
97 int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
98 int (*get_requested_power)(struct thermal_cooling_device *, u32 *);
99 int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *);
100 int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *);
103 struct thermal_cooling_device {
106 unsigned long max_state;
107 struct device device;
108 struct device_node *np;
111 const struct thermal_cooling_device_ops *ops;
112 bool updated; /* true if the cooling device does not need update */
113 struct mutex lock; /* protect thermal_instances list */
114 struct list_head thermal_instances;
115 struct list_head node;
119 * struct thermal_zone_device - structure for a thermal zone
120 * @id: unique id number for each thermal zone
121 * @type: the thermal zone device type
122 * @device: &struct device for this thermal zone
123 * @removal: removal completion
124 * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
125 * @trip_type_attrs: attributes for trip points for sysfs: trip type
126 * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
127 * @mode: current mode of this thermal zone
128 * @devdata: private pointer for device private data
129 * @trips: an array of struct thermal_trip
130 * @num_trips: number of trip points the thermal zone supports
131 * @passive_delay_jiffies: number of jiffies to wait between polls when
132 * performing passive cooling.
133 * @polling_delay_jiffies: number of jiffies to wait between polls when
134 * checking whether trip points have been crossed (0 for
135 * interrupt driven systems)
136 * @temperature: current temperature. This is only for core code,
137 * drivers should use thermal_zone_get_temp() to get the
138 * current temperature
139 * @last_temperature: previous temperature read
140 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
141 * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
142 * @prev_low_trip: the low current temperature if you've crossed a passive
144 * @prev_high_trip: the above current temperature if you've crossed a
146 * @need_update: if equals 1, thermal_zone_device_update needs to be invoked.
147 * @ops: operations this &thermal_zone_device supports
148 * @tzp: thermal zone parameters
149 * @governor: pointer to the governor for this thermal zone
150 * @governor_data: private pointer for governor data
151 * @thermal_instances: list of &struct thermal_instance of this thermal zone
152 * @ida: &struct ida to generate unique id for this zone's cooling
154 * @lock: lock to protect thermal_instances list
155 * @node: node in thermal_tz_list (in thermal_core.c)
156 * @poll_queue: delayed work for polling
157 * @notify_event: Last notification event
158 * @suspended: thermal zone suspend indicator
160 struct thermal_zone_device {
162 char type[THERMAL_NAME_LENGTH];
163 struct device device;
164 struct completion removal;
165 struct attribute_group trips_attribute_group;
166 struct thermal_attr *trip_temp_attrs;
167 struct thermal_attr *trip_type_attrs;
168 struct thermal_attr *trip_hyst_attrs;
169 enum thermal_device_mode mode;
171 struct thermal_trip *trips;
173 unsigned long passive_delay_jiffies;
174 unsigned long polling_delay_jiffies;
176 int last_temperature;
177 int emul_temperature;
181 atomic_t need_update;
182 struct thermal_zone_device_ops *ops;
183 struct thermal_zone_params *tzp;
184 struct thermal_governor *governor;
186 struct list_head thermal_instances;
189 struct list_head node;
190 struct delayed_work poll_queue;
191 enum thermal_notify_event notify_event;
196 * struct thermal_governor - structure that holds thermal governor information
197 * @name: name of the governor
198 * @bind_to_tz: callback called when binding to a thermal zone. If it
199 * returns 0, the governor is bound to the thermal zone,
200 * otherwise it fails.
201 * @unbind_from_tz: callback called when a governor is unbound from a
203 * @throttle: callback called for every trip point even if temperature is
204 * below the trip point temperature
205 * @update_tz: callback called when thermal zone internals have changed, e.g.
206 * thermal cooling instance was added/removed
207 * @governor_list: node in thermal_governor_list (in thermal_core.c)
209 struct thermal_governor {
210 char name[THERMAL_NAME_LENGTH];
211 int (*bind_to_tz)(struct thermal_zone_device *tz);
212 void (*unbind_from_tz)(struct thermal_zone_device *tz);
213 int (*throttle)(struct thermal_zone_device *tz,
214 const struct thermal_trip *trip);
215 void (*update_tz)(struct thermal_zone_device *tz,
216 enum thermal_notify_event reason);
217 struct list_head governor_list;
220 /* Structure to define Thermal Zone parameters */
221 struct thermal_zone_params {
222 char governor_name[THERMAL_NAME_LENGTH];
225 * a boolean to indicate if the thermal to hwmon sysfs interface
226 * is required. when no_hwmon == false, a hwmon sysfs interface
227 * will be created. when no_hwmon == true, nothing will be done
232 * Sustainable power (heat) that this thermal zone can dissipate in
235 u32 sustainable_power;
238 * Proportional parameter of the PID controller when
239 * overshooting (i.e., when temperature is below the target)
244 * Proportional parameter of the PID controller when
249 /* Integral parameter of the PID controller */
252 /* Derivative parameter of the PID controller */
255 /* threshold below which the error is no longer accumulated */
259 * @slope: slope of a linear temperature adjustment curve.
260 * Used by thermal zone drivers.
264 * @offset: offset of a linear temperature adjustment curve.
265 * Used by thermal zone drivers (default 0).
270 /* Function declarations */
271 #ifdef CONFIG_THERMAL_OF
272 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
273 const struct thermal_zone_device_ops *ops);
275 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz);
280 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
281 const struct thermal_zone_device_ops *ops)
283 return ERR_PTR(-ENOTSUPP);
286 static inline void devm_thermal_of_zone_unregister(struct device *dev,
287 struct thermal_zone_device *tz)
292 int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
293 struct thermal_trip *trip);
294 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
295 struct thermal_trip *trip);
296 int for_each_thermal_trip(struct thermal_zone_device *tz,
297 int (*cb)(struct thermal_trip *, void *),
299 int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
300 int (*cb)(struct thermal_trip *, void *),
302 int thermal_zone_get_num_trips(struct thermal_zone_device *tz);
303 void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
304 struct thermal_trip *trip, int temp);
306 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);
308 #ifdef CONFIG_THERMAL
309 struct thermal_zone_device *thermal_zone_device_register_with_trips(
311 struct thermal_trip *trips,
312 int num_trips, int mask,
314 struct thermal_zone_device_ops *ops,
315 const struct thermal_zone_params *tzp,
316 int passive_delay, int polling_delay);
318 struct thermal_zone_device *thermal_tripless_zone_device_register(
321 struct thermal_zone_device_ops *ops,
322 const struct thermal_zone_params *tzp);
324 void thermal_zone_device_unregister(struct thermal_zone_device *tz);
326 void *thermal_zone_device_priv(struct thermal_zone_device *tzd);
327 const char *thermal_zone_device_type(struct thermal_zone_device *tzd);
328 int thermal_zone_device_id(struct thermal_zone_device *tzd);
329 struct device *thermal_zone_device(struct thermal_zone_device *tzd);
331 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
332 const struct thermal_trip *trip,
333 struct thermal_cooling_device *cdev,
334 unsigned long upper, unsigned long lower,
335 unsigned int weight);
336 int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
337 struct thermal_cooling_device *,
338 unsigned long, unsigned long,
340 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
341 const struct thermal_trip *trip,
342 struct thermal_cooling_device *cdev);
343 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
344 struct thermal_cooling_device *);
345 void thermal_zone_device_update(struct thermal_zone_device *,
346 enum thermal_notify_event);
348 struct thermal_cooling_device *thermal_cooling_device_register(const char *,
349 void *, const struct thermal_cooling_device_ops *);
350 struct thermal_cooling_device *
351 thermal_of_cooling_device_register(struct device_node *np, const char *, void *,
352 const struct thermal_cooling_device_ops *);
353 struct thermal_cooling_device *
354 devm_thermal_of_cooling_device_register(struct device *dev,
355 struct device_node *np,
356 char *type, void *devdata,
357 const struct thermal_cooling_device_ops *ops);
358 void thermal_cooling_device_update(struct thermal_cooling_device *);
359 void thermal_cooling_device_unregister(struct thermal_cooling_device *);
360 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
361 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
362 int thermal_zone_get_slope(struct thermal_zone_device *tz);
363 int thermal_zone_get_offset(struct thermal_zone_device *tz);
365 int thermal_zone_device_enable(struct thermal_zone_device *tz);
366 int thermal_zone_device_disable(struct thermal_zone_device *tz);
367 void thermal_zone_device_critical(struct thermal_zone_device *tz);
369 static inline struct thermal_zone_device *thermal_zone_device_register_with_trips(
371 struct thermal_trip *trips,
372 int num_trips, int mask,
374 struct thermal_zone_device_ops *ops,
375 const struct thermal_zone_params *tzp,
376 int passive_delay, int polling_delay)
377 { return ERR_PTR(-ENODEV); }
379 static inline struct thermal_zone_device *thermal_tripless_zone_device_register(
382 struct thermal_zone_device_ops *ops,
383 const struct thermal_zone_params *tzp)
384 { return ERR_PTR(-ENODEV); }
386 static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz)
389 static inline struct thermal_cooling_device *
390 thermal_cooling_device_register(const char *type, void *devdata,
391 const struct thermal_cooling_device_ops *ops)
392 { return ERR_PTR(-ENODEV); }
393 static inline struct thermal_cooling_device *
394 thermal_of_cooling_device_register(struct device_node *np,
395 const char *type, void *devdata,
396 const struct thermal_cooling_device_ops *ops)
397 { return ERR_PTR(-ENODEV); }
398 static inline struct thermal_cooling_device *
399 devm_thermal_of_cooling_device_register(struct device *dev,
400 struct device_node *np,
401 char *type, void *devdata,
402 const struct thermal_cooling_device_ops *ops)
404 return ERR_PTR(-ENODEV);
406 static inline void thermal_cooling_device_unregister(
407 struct thermal_cooling_device *cdev)
409 static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
411 { return ERR_PTR(-ENODEV); }
412 static inline int thermal_zone_get_temp(
413 struct thermal_zone_device *tz, int *temp)
415 static inline int thermal_zone_get_slope(
416 struct thermal_zone_device *tz)
418 static inline int thermal_zone_get_offset(
419 struct thermal_zone_device *tz)
422 static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz)
427 static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
432 static inline int thermal_zone_device_id(struct thermal_zone_device *tzd)
437 static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
440 static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
442 #endif /* CONFIG_THERMAL */
444 #endif /* __THERMAL_H__ */