Commit | Line | Data |
---|---|---|
7e3c0381 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
203d3d4a ZR |
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> | |
203d3d4a ZR |
8 | */ |
9 | ||
10 | #ifndef __THERMAL_H__ | |
11 | #define __THERMAL_H__ | |
12 | ||
a116b5d4 | 13 | #include <linux/of.h> |
203d3d4a ZR |
14 | #include <linux/idr.h> |
15 | #include <linux/device.h> | |
4d0fe749 | 16 | #include <linux/sysfs.h> |
b1569e99 | 17 | #include <linux/workqueue.h> |
af6c9f16 | 18 | #include <uapi/linux/thermal.h> |
203d3d4a | 19 | |
57df8106 ZR |
20 | /* invalid cooling state */ |
21 | #define THERMAL_CSTATE_INVALID -1UL | |
22 | ||
23064088 | 23 | /* No upper/lower limit requirement */ |
a940cb34 | 24 | #define THERMAL_NO_LIMIT ((u32)~0) |
23064088 | 25 | |
6cd9e9f6 KS |
26 | /* Default weight of a bound cooling device */ |
27 | #define THERMAL_WEIGHT_DEFAULT 0 | |
28 | ||
bb431ba2 ZR |
29 | /* use value, which < 0K, to indicate an invalid/uninitialized temperature */ |
30 | #define THERMAL_TEMP_INVALID -274000 | |
31 | ||
203d3d4a ZR |
32 | struct thermal_zone_device; |
33 | struct thermal_cooling_device; | |
35b11d2e | 34 | struct thermal_instance; |
c68df440 | 35 | struct thermal_attr; |
203d3d4a | 36 | |
601f3d42 ZR |
37 | enum thermal_trend { |
38 | THERMAL_TREND_STABLE, /* temperature is stable */ | |
39 | THERMAL_TREND_RAISING, /* temperature is raising */ | |
40 | THERMAL_TREND_DROPPING, /* temperature is dropping */ | |
41 | }; | |
42 | ||
0e70f466 SP |
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 */ | |
38e44da5 | 52 | THERMAL_TABLE_CHANGED, /* Thermal table(s) changed */ |
88052319 | 53 | THERMAL_EVENT_KEEP_ALIVE, /* Request for user space handler to respond */ |
0e70f466 SP |
54 | }; |
55 | ||
8289d810 RW |
56 | /** |
57 | * struct thermal_trip - representation of a point in temperature domain | |
58 | * @temperature: temperature value in miliCelsius | |
59 | * @hysteresis: relative hysteresis in miliCelsius | |
60 | * @type: trip point type | |
61 | * @priv: pointer to driver data associated with this trip | |
62 | */ | |
63 | struct thermal_trip { | |
64 | int temperature; | |
65 | int hysteresis; | |
66 | enum thermal_trip_type type; | |
67 | void *priv; | |
68 | }; | |
69 | ||
203d3d4a ZR |
70 | struct thermal_zone_device_ops { |
71 | int (*bind) (struct thermal_zone_device *, | |
72 | struct thermal_cooling_device *); | |
73 | int (*unbind) (struct thermal_zone_device *, | |
74 | struct thermal_cooling_device *); | |
17e8351a | 75 | int (*get_temp) (struct thermal_zone_device *, int *); |
060c034a | 76 | int (*set_trips) (struct thermal_zone_device *, int, int); |
f5e50bf4 | 77 | int (*change_mode) (struct thermal_zone_device *, |
6503e5df | 78 | enum thermal_device_mode); |
17e8351a | 79 | int (*set_trip_temp) (struct thermal_zone_device *, int, int); |
17e8351a SH |
80 | int (*set_trip_hyst) (struct thermal_zone_device *, int, int); |
81 | int (*get_crit_temp) (struct thermal_zone_device *, int *); | |
82 | int (*set_emul_temp) (struct thermal_zone_device *, int); | |
ebc7abb3 RW |
83 | int (*get_trend) (struct thermal_zone_device *, |
84 | const struct thermal_trip *, enum thermal_trend *); | |
d7203eed DL |
85 | void (*hot)(struct thermal_zone_device *); |
86 | void (*critical)(struct thermal_zone_device *); | |
203d3d4a ZR |
87 | }; |
88 | ||
89 | struct thermal_cooling_device_ops { | |
6503e5df MG |
90 | int (*get_max_state) (struct thermal_cooling_device *, unsigned long *); |
91 | int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *); | |
92 | int (*set_cur_state) (struct thermal_cooling_device *, unsigned long); | |
ecd1d2a3 | 93 | int (*get_requested_power)(struct thermal_cooling_device *, u32 *); |
94 | int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *); | |
95 | int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *); | |
203d3d4a ZR |
96 | }; |
97 | ||
203d3d4a ZR |
98 | struct thermal_cooling_device { |
99 | int id; | |
58483761 | 100 | char *type; |
c408b3d1 | 101 | unsigned long max_state; |
203d3d4a | 102 | struct device device; |
4e5e4705 | 103 | struct device_node *np; |
203d3d4a | 104 | void *devdata; |
8ea22951 | 105 | void *stats; |
5b275ce2 | 106 | const struct thermal_cooling_device_ops *ops; |
ce119f83 | 107 | bool updated; /* true if the cooling device does not need update */ |
f4a821ce | 108 | struct mutex lock; /* protect thermal_instances list */ |
b5e4ae62 | 109 | struct list_head thermal_instances; |
203d3d4a ZR |
110 | struct list_head node; |
111 | }; | |
112 | ||
c708a98f JM |
113 | /** |
114 | * struct thermal_zone_device - structure for a thermal zone | |
115 | * @id: unique id number for each thermal zone | |
116 | * @type: the thermal zone device type | |
117 | * @device: &struct device for this thermal zone | |
118 | * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature | |
119 | * @trip_type_attrs: attributes for trip points for sysfs: trip type | |
120 | * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis | |
cbba1d71 | 121 | * @mode: current mode of this thermal zone |
c708a98f | 122 | * @devdata: private pointer for device private data |
fae11de5 | 123 | * @trips: an array of struct thermal_trip |
e5bfcd30 | 124 | * @num_trips: number of trip points the thermal zone supports |
81ad4276 | 125 | * @trips_disabled; bitmap for disabled trips |
17d399cd DL |
126 | * @passive_delay_jiffies: number of jiffies to wait between polls when |
127 | * performing passive cooling. | |
17d399cd DL |
128 | * @polling_delay_jiffies: number of jiffies to wait between polls when |
129 | * checking whether trip points have been crossed (0 for | |
130 | * interrupt driven systems) | |
c708a98f JM |
131 | * @temperature: current temperature. This is only for core code, |
132 | * drivers should use thermal_zone_get_temp() to get the | |
133 | * current temperature | |
134 | * @last_temperature: previous temperature read | |
135 | * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION | |
136 | * @passive: 1 if you've crossed a passive trip point, 0 otherwise. | |
060c034a SH |
137 | * @prev_low_trip: the low current temperature if you've crossed a passive |
138 | trip point. | |
139 | * @prev_high_trip: the above current temperature if you've crossed a | |
140 | passive trip point. | |
4511f716 | 141 | * @need_update: if equals 1, thermal_zone_device_update needs to be invoked. |
c708a98f JM |
142 | * @ops: operations this &thermal_zone_device supports |
143 | * @tzp: thermal zone parameters | |
144 | * @governor: pointer to the governor for this thermal zone | |
e33df1d2 | 145 | * @governor_data: private pointer for governor data |
c708a98f | 146 | * @thermal_instances: list of &struct thermal_instance of this thermal zone |
b31ef828 | 147 | * @ida: &struct ida to generate unique id for this zone's cooling |
c708a98f JM |
148 | * devices |
149 | * @lock: lock to protect thermal_instances list | |
150 | * @node: node in thermal_tz_list (in thermal_core.c) | |
151 | * @poll_queue: delayed work for polling | |
0e70f466 | 152 | * @notify_event: Last notification event |
c708a98f | 153 | */ |
203d3d4a ZR |
154 | struct thermal_zone_device { |
155 | int id; | |
156 | char type[THERMAL_NAME_LENGTH]; | |
157 | struct device device; | |
4d0fe749 | 158 | struct attribute_group trips_attribute_group; |
c56f5c03 D |
159 | struct thermal_attr *trip_temp_attrs; |
160 | struct thermal_attr *trip_type_attrs; | |
27365a6c | 161 | struct thermal_attr *trip_hyst_attrs; |
cbba1d71 | 162 | enum thermal_device_mode mode; |
203d3d4a | 163 | void *devdata; |
fae11de5 | 164 | struct thermal_trip *trips; |
e5bfcd30 | 165 | int num_trips; |
81ad4276 | 166 | unsigned long trips_disabled; /* bitmap for disabled trips */ |
17d399cd DL |
167 | unsigned long passive_delay_jiffies; |
168 | unsigned long polling_delay_jiffies; | |
601f3d42 | 169 | int temperature; |
b1569e99 | 170 | int last_temperature; |
e6e238c3 | 171 | int emul_temperature; |
908b9fb7 | 172 | int passive; |
060c034a SH |
173 | int prev_low_trip; |
174 | int prev_high_trip; | |
4511f716 | 175 | atomic_t need_update; |
4e5e4705 | 176 | struct thermal_zone_device_ops *ops; |
6b775e87 | 177 | struct thermal_zone_params *tzp; |
a4a15485 | 178 | struct thermal_governor *governor; |
e33df1d2 | 179 | void *governor_data; |
2d374139 | 180 | struct list_head thermal_instances; |
b31ef828 | 181 | struct ida ida; |
c708a98f | 182 | struct mutex lock; |
203d3d4a | 183 | struct list_head node; |
b1569e99 | 184 | struct delayed_work poll_queue; |
0e70f466 | 185 | enum thermal_notify_event notify_event; |
203d3d4a | 186 | }; |
4cb18728 | 187 | |
c708a98f JM |
188 | /** |
189 | * struct thermal_governor - structure that holds thermal governor information | |
190 | * @name: name of the governor | |
e33df1d2 JM |
191 | * @bind_to_tz: callback called when binding to a thermal zone. If it |
192 | * returns 0, the governor is bound to the thermal zone, | |
193 | * otherwise it fails. | |
194 | * @unbind_from_tz: callback called when a governor is unbound from a | |
195 | * thermal zone. | |
c708a98f JM |
196 | * @throttle: callback called for every trip point even if temperature is |
197 | * below the trip point temperature | |
198 | * @governor_list: node in thermal_governor_list (in thermal_core.c) | |
199 | */ | |
a4a15485 D |
200 | struct thermal_governor { |
201 | char name[THERMAL_NAME_LENGTH]; | |
e33df1d2 JM |
202 | int (*bind_to_tz)(struct thermal_zone_device *tz); |
203 | void (*unbind_from_tz)(struct thermal_zone_device *tz); | |
a4a15485 D |
204 | int (*throttle)(struct thermal_zone_device *tz, int trip); |
205 | struct list_head governor_list; | |
a4a15485 D |
206 | }; |
207 | ||
ef873947 D |
208 | /* Structure to define Thermal Zone parameters */ |
209 | struct thermal_zone_params { | |
a4a15485 | 210 | char governor_name[THERMAL_NAME_LENGTH]; |
ccba4ffd EV |
211 | |
212 | /* | |
213 | * a boolean to indicate if the thermal to hwmon sysfs interface | |
214 | * is required. when no_hwmon == false, a hwmon sysfs interface | |
215 | * will be created. when no_hwmon == true, nothing will be done | |
216 | */ | |
217 | bool no_hwmon; | |
218 | ||
6b775e87 JM |
219 | /* |
220 | * Sustainable power (heat) that this thermal zone can dissipate in | |
221 | * mW | |
222 | */ | |
223 | u32 sustainable_power; | |
224 | ||
225 | /* | |
226 | * Proportional parameter of the PID controller when | |
227 | * overshooting (i.e., when temperature is below the target) | |
228 | */ | |
229 | s32 k_po; | |
230 | ||
231 | /* | |
232 | * Proportional parameter of the PID controller when | |
233 | * undershooting | |
234 | */ | |
235 | s32 k_pu; | |
236 | ||
237 | /* Integral parameter of the PID controller */ | |
238 | s32 k_i; | |
239 | ||
240 | /* Derivative parameter of the PID controller */ | |
241 | s32 k_d; | |
242 | ||
243 | /* threshold below which the error is no longer accumulated */ | |
244 | s32 integral_cutoff; | |
9d0be7f4 EV |
245 | |
246 | /* | |
247 | * @slope: slope of a linear temperature adjustment curve. | |
248 | * Used by thermal zone drivers. | |
249 | */ | |
250 | int slope; | |
251 | /* | |
252 | * @offset: offset of a linear temperature adjustment curve. | |
253 | * Used by thermal zone drivers (default 0). | |
254 | */ | |
255 | int offset; | |
ef873947 D |
256 | }; |
257 | ||
23064088 | 258 | /* Function declarations */ |
4e5e4705 | 259 | #ifdef CONFIG_THERMAL_OF |
3fd6d6e2 DL |
260 | struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data, |
261 | const struct thermal_zone_device_ops *ops); | |
262 | ||
3fd6d6e2 DL |
263 | void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz); |
264 | ||
4e5e4705 | 265 | #else |
4e5e4705 | 266 | |
f59ac19b DL |
267 | static inline |
268 | struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data, | |
269 | const struct thermal_zone_device_ops *ops) | |
e498b498 | 270 | { |
f59ac19b | 271 | return ERR_PTR(-ENOTSUPP); |
e498b498 LD |
272 | } |
273 | ||
f59ac19b DL |
274 | static inline void devm_thermal_of_zone_unregister(struct device *dev, |
275 | struct thermal_zone_device *tz) | |
3fd6d6e2 DL |
276 | { |
277 | } | |
4e5e4705 | 278 | #endif |
12ca7188 | 279 | |
e6ec64f8 JH |
280 | int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id, |
281 | struct thermal_trip *trip); | |
7c3d5c20 DL |
282 | int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id, |
283 | struct thermal_trip *trip); | |
284 | ||
2e38a2a9 DL |
285 | int thermal_zone_set_trip(struct thermal_zone_device *tz, int trip_id, |
286 | const struct thermal_trip *trip); | |
287 | ||
96b8b436 RW |
288 | int for_each_thermal_trip(struct thermal_zone_device *tz, |
289 | int (*cb)(struct thermal_trip *, void *), | |
290 | void *data); | |
7c3d5c20 DL |
291 | int thermal_zone_get_num_trips(struct thermal_zone_device *tz); |
292 | ||
293 | int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp); | |
294 | ||
7a0e3974 | 295 | #ifdef CONFIG_THERMAL_ACPI |
dd3b3d16 RW |
296 | int thermal_acpi_active_trip_temp(struct acpi_device *adev, int id, int *ret_temp); |
297 | int thermal_acpi_passive_trip_temp(struct acpi_device *adev, int *ret_temp); | |
298 | int thermal_acpi_hot_trip_temp(struct acpi_device *adev, int *ret_temp); | |
299 | int thermal_acpi_critical_trip_temp(struct acpi_device *adev, int *ret_temp); | |
7a0e3974 RW |
300 | #endif |
301 | ||
60518260 | 302 | #ifdef CONFIG_THERMAL |
9ffa7b92 RW |
303 | struct thermal_zone_device *thermal_zone_device_register_with_trips( |
304 | const char *type, | |
305 | struct thermal_trip *trips, | |
306 | int num_trips, int mask, | |
307 | void *devdata, | |
308 | struct thermal_zone_device_ops *ops, | |
309 | const struct thermal_zone_params *tzp, | |
310 | int passive_delay, int polling_delay); | |
311 | ||
d332db8f RW |
312 | struct thermal_zone_device *thermal_tripless_zone_device_register( |
313 | const char *type, | |
314 | void *devdata, | |
315 | struct thermal_zone_device_ops *ops, | |
316 | const struct thermal_zone_params *tzp); | |
317 | ||
9ffa7b92 | 318 | void thermal_zone_device_unregister(struct thermal_zone_device *tz); |
fae11de5 | 319 | |
a6ff3c00 | 320 | void *thermal_zone_device_priv(struct thermal_zone_device *tzd); |
072e35c9 | 321 | const char *thermal_zone_device_type(struct thermal_zone_device *tzd); |
3034f859 | 322 | int thermal_zone_device_id(struct thermal_zone_device *tzd); |
7cefbaf0 | 323 | struct device *thermal_zone_device(struct thermal_zone_device *tzd); |
a6ff3c00 | 324 | |
203d3d4a | 325 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int, |
9d99842f | 326 | struct thermal_cooling_device *, |
6cd9e9f6 KS |
327 | unsigned long, unsigned long, |
328 | unsigned int); | |
203d3d4a ZR |
329 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int, |
330 | struct thermal_cooling_device *); | |
0e70f466 SP |
331 | void thermal_zone_device_update(struct thermal_zone_device *, |
332 | enum thermal_notify_event); | |
9a99a996 RW |
333 | void thermal_zone_device_exec(struct thermal_zone_device *tz, |
334 | void (*cb)(struct thermal_zone_device *, | |
335 | unsigned long), | |
336 | unsigned long data); | |
23064088 | 337 | |
f991de53 JFD |
338 | struct thermal_cooling_device *thermal_cooling_device_register(const char *, |
339 | void *, const struct thermal_cooling_device_ops *); | |
a116b5d4 | 340 | struct thermal_cooling_device * |
f991de53 | 341 | thermal_of_cooling_device_register(struct device_node *np, const char *, void *, |
a116b5d4 | 342 | const struct thermal_cooling_device_ops *); |
b4ab114c GR |
343 | struct thermal_cooling_device * |
344 | devm_thermal_of_cooling_device_register(struct device *dev, | |
345 | struct device_node *np, | |
346 | char *type, void *devdata, | |
347 | const struct thermal_cooling_device_ops *ops); | |
790930f4 | 348 | void thermal_cooling_device_update(struct thermal_cooling_device *); |
203d3d4a | 349 | void thermal_cooling_device_unregister(struct thermal_cooling_device *); |
63c4d919 | 350 | struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name); |
17e8351a | 351 | int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp); |
4a7069a3 RN |
352 | int thermal_zone_get_slope(struct thermal_zone_device *tz); |
353 | int thermal_zone_get_offset(struct thermal_zone_device *tz); | |
af06216a | 354 | |
ac5d9ecc AP |
355 | int thermal_zone_device_enable(struct thermal_zone_device *tz); |
356 | int thermal_zone_device_disable(struct thermal_zone_device *tz); | |
d7203eed | 357 | void thermal_zone_device_critical(struct thermal_zone_device *tz); |
12ca7188 | 358 | #else |
9ffa7b92 RW |
359 | static inline struct thermal_zone_device *thermal_zone_device_register_with_trips( |
360 | const char *type, | |
361 | struct thermal_trip *trips, | |
362 | int num_trips, int mask, | |
363 | void *devdata, | |
364 | struct thermal_zone_device_ops *ops, | |
365 | const struct thermal_zone_params *tzp, | |
366 | int passive_delay, int polling_delay) | |
12ca7188 | 367 | { return ERR_PTR(-ENODEV); } |
9ffa7b92 | 368 | |
d332db8f RW |
369 | static inline struct thermal_zone_device *thermal_tripless_zone_device_register( |
370 | const char *type, | |
371 | void *devdata, | |
372 | struct thermal_zone_device_ops *ops, | |
373 | const struct thermal_zone_params *tzp) | |
374 | { return ERR_PTR(-ENODEV); } | |
375 | ||
9ffa7b92 | 376 | static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz) |
12ca7188 | 377 | { } |
9ffa7b92 | 378 | |
12ca7188 | 379 | static inline struct thermal_cooling_device * |
fb836107 | 380 | thermal_cooling_device_register(const char *type, void *devdata, |
12ca7188 NM |
381 | const struct thermal_cooling_device_ops *ops) |
382 | { return ERR_PTR(-ENODEV); } | |
383 | static inline struct thermal_cooling_device * | |
384 | thermal_of_cooling_device_register(struct device_node *np, | |
fb836107 AB |
385 | const char *type, void *devdata, |
386 | const struct thermal_cooling_device_ops *ops) | |
12ca7188 | 387 | { return ERR_PTR(-ENODEV); } |
b4ab114c GR |
388 | static inline struct thermal_cooling_device * |
389 | devm_thermal_of_cooling_device_register(struct device *dev, | |
390 | struct device_node *np, | |
391 | char *type, void *devdata, | |
392 | const struct thermal_cooling_device_ops *ops) | |
393 | { | |
394 | return ERR_PTR(-ENODEV); | |
395 | } | |
12ca7188 NM |
396 | static inline void thermal_cooling_device_unregister( |
397 | struct thermal_cooling_device *cdev) | |
398 | { } | |
399 | static inline struct thermal_zone_device *thermal_zone_get_zone_by_name( | |
400 | const char *name) | |
401 | { return ERR_PTR(-ENODEV); } | |
402 | static inline int thermal_zone_get_temp( | |
17e8351a | 403 | struct thermal_zone_device *tz, int *temp) |
12ca7188 | 404 | { return -ENODEV; } |
4a7069a3 RN |
405 | static inline int thermal_zone_get_slope( |
406 | struct thermal_zone_device *tz) | |
407 | { return -ENODEV; } | |
408 | static inline int thermal_zone_get_offset( | |
409 | struct thermal_zone_device *tz) | |
410 | { return -ENODEV; } | |
f0129c23 | 411 | |
a6ff3c00 DL |
412 | static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz) |
413 | { | |
414 | return NULL; | |
415 | } | |
416 | ||
072e35c9 DL |
417 | static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd) |
418 | { | |
419 | return NULL; | |
420 | } | |
421 | ||
3034f859 DL |
422 | static inline int thermal_zone_device_id(struct thermal_zone_device *tzd) |
423 | { | |
424 | return -ENODEV; | |
425 | } | |
426 | ||
ac5d9ecc AP |
427 | static inline int thermal_zone_device_enable(struct thermal_zone_device *tz) |
428 | { return -ENODEV; } | |
429 | ||
430 | static inline int thermal_zone_device_disable(struct thermal_zone_device *tz) | |
431 | { return -ENODEV; } | |
12ca7188 NM |
432 | #endif /* CONFIG_THERMAL */ |
433 | ||
a0dd25b2 | 434 | #endif /* __THERMAL_H__ */ |