Merge tag 'sound-fix-6.9-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai...
[linux-2.6-block.git] / include / linux / thermal.h
CommitLineData
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
32struct thermal_zone_device;
33struct thermal_cooling_device;
35b11d2e 34struct thermal_instance;
755113d7 35struct thermal_debugfs;
c68df440 36struct thermal_attr;
203d3d4a 37
601f3d42
ZR
38enum thermal_trend {
39 THERMAL_TREND_STABLE, /* temperature is stable */
40 THERMAL_TREND_RAISING, /* temperature is raising */
41 THERMAL_TREND_DROPPING, /* temperature is dropping */
42};
43
0e70f466
SP
44/* Thermal notification reason */
45enum 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 */
38e44da5 53 THERMAL_TABLE_CHANGED, /* Thermal table(s) changed */
88052319 54 THERMAL_EVENT_KEEP_ALIVE, /* Request for user space handler to respond */
a8c95940
LL
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 */
bfc57bd1 57 THERMAL_INSTANCE_WEIGHT_CHANGED, /* Thermal instance weight changed */
0e70f466
SP
58};
59
8289d810
RW
60/**
61 * struct thermal_trip - representation of a point in temperature domain
62 * @temperature: temperature value in miliCelsius
63 * @hysteresis: relative hysteresis in miliCelsius
44844db9 64 * @threshold: trip crossing notification threshold miliCelsius
8289d810
RW
65 * @type: trip point type
66 * @priv: pointer to driver data associated with this trip
5340f764 67 * @flags: flags representing binary properties of the trip
8289d810
RW
68 */
69struct thermal_trip {
70 int temperature;
71 int hysteresis;
44844db9 72 int threshold;
8289d810 73 enum thermal_trip_type type;
5340f764 74 u8 flags;
8289d810
RW
75 void *priv;
76};
77
5340f764
RW
78#define THERMAL_TRIP_FLAG_RW_TEMP BIT(0)
79#define THERMAL_TRIP_FLAG_RW_HYST BIT(1)
80
81#define THERMAL_TRIP_FLAG_RW (THERMAL_TRIP_FLAG_RW_TEMP | \
82 THERMAL_TRIP_FLAG_RW_HYST)
83
203d3d4a
ZR
84struct 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 *);
17e8351a 89 int (*get_temp) (struct thermal_zone_device *, int *);
060c034a 90 int (*set_trips) (struct thermal_zone_device *, int, int);
f5e50bf4 91 int (*change_mode) (struct thermal_zone_device *,
6503e5df 92 enum thermal_device_mode);
17e8351a 93 int (*set_trip_temp) (struct thermal_zone_device *, int, int);
17e8351a
SH
94 int (*get_crit_temp) (struct thermal_zone_device *, int *);
95 int (*set_emul_temp) (struct thermal_zone_device *, int);
ebc7abb3
RW
96 int (*get_trend) (struct thermal_zone_device *,
97 const struct thermal_trip *, enum thermal_trend *);
d7203eed
DL
98 void (*hot)(struct thermal_zone_device *);
99 void (*critical)(struct thermal_zone_device *);
203d3d4a
ZR
100};
101
102struct thermal_cooling_device_ops {
6503e5df
MG
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);
ecd1d2a3 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 *);
203d3d4a
ZR
109};
110
203d3d4a
ZR
111struct thermal_cooling_device {
112 int id;
57a427c8 113 const char *type;
c408b3d1 114 unsigned long max_state;
203d3d4a 115 struct device device;
4e5e4705 116 struct device_node *np;
203d3d4a 117 void *devdata;
8ea22951 118 void *stats;
5b275ce2 119 const struct thermal_cooling_device_ops *ops;
ce119f83 120 bool updated; /* true if the cooling device does not need update */
f4a821ce 121 struct mutex lock; /* protect thermal_instances list */
b5e4ae62 122 struct list_head thermal_instances;
203d3d4a 123 struct list_head node;
755113d7
DL
124#ifdef CONFIG_THERMAL_DEBUGFS
125 struct thermal_debugfs *debugfs;
126#endif
203d3d4a
ZR
127};
128
c708a98f
JM
129/**
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
4649620d 134 * @removal: removal completion
c708a98f
JM
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
cbba1d71 138 * @mode: current mode of this thermal zone
c708a98f 139 * @devdata: private pointer for device private data
e5bfcd30 140 * @num_trips: number of trip points the thermal zone supports
17d399cd
DL
141 * @passive_delay_jiffies: number of jiffies to wait between polls when
142 * performing passive cooling.
17d399cd
DL
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)
c708a98f
JM
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.
060c034a
SH
152 * @prev_low_trip: the low current temperature if you've crossed a passive
153 trip point.
154 * @prev_high_trip: the above current temperature if you've crossed a
155 passive trip point.
4511f716 156 * @need_update: if equals 1, thermal_zone_device_update needs to be invoked.
c708a98f
JM
157 * @ops: operations this &thermal_zone_device supports
158 * @tzp: thermal zone parameters
159 * @governor: pointer to the governor for this thermal zone
e33df1d2 160 * @governor_data: private pointer for governor data
c708a98f 161 * @thermal_instances: list of &struct thermal_instance of this thermal zone
b31ef828 162 * @ida: &struct ida to generate unique id for this zone's cooling
c708a98f
JM
163 * devices
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
0e70f466 167 * @notify_event: Last notification event
4e814173 168 * @suspended: thermal zone suspend indicator
9b0a6275 169 * @trips: array of struct thermal_trip objects
c708a98f 170 */
203d3d4a
ZR
171struct thermal_zone_device {
172 int id;
173 char type[THERMAL_NAME_LENGTH];
174 struct device device;
4649620d 175 struct completion removal;
4d0fe749 176 struct attribute_group trips_attribute_group;
c56f5c03
D
177 struct thermal_attr *trip_temp_attrs;
178 struct thermal_attr *trip_type_attrs;
27365a6c 179 struct thermal_attr *trip_hyst_attrs;
cbba1d71 180 enum thermal_device_mode mode;
203d3d4a 181 void *devdata;
e5bfcd30 182 int num_trips;
17d399cd
DL
183 unsigned long passive_delay_jiffies;
184 unsigned long polling_delay_jiffies;
601f3d42 185 int temperature;
b1569e99 186 int last_temperature;
e6e238c3 187 int emul_temperature;
908b9fb7 188 int passive;
060c034a
SH
189 int prev_low_trip;
190 int prev_high_trip;
4511f716 191 atomic_t need_update;
698a1eb1 192 struct thermal_zone_device_ops ops;
6b775e87 193 struct thermal_zone_params *tzp;
a4a15485 194 struct thermal_governor *governor;
e33df1d2 195 void *governor_data;
2d374139 196 struct list_head thermal_instances;
b31ef828 197 struct ida ida;
c708a98f 198 struct mutex lock;
203d3d4a 199 struct list_head node;
b1569e99 200 struct delayed_work poll_queue;
0e70f466 201 enum thermal_notify_event notify_event;
9b0a6275 202 bool suspended;
755113d7
DL
203#ifdef CONFIG_THERMAL_DEBUGFS
204 struct thermal_debugfs *debugfs;
205#endif
9b0a6275 206 struct thermal_trip trips[] __counted_by(num_trips);
203d3d4a 207};
4cb18728 208
c708a98f
JM
209/**
210 * struct thermal_governor - structure that holds thermal governor information
211 * @name: name of the governor
e33df1d2
JM
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
216 * thermal zone.
c708a98f
JM
217 * @throttle: callback called for every trip point even if temperature is
218 * below the trip point temperature
a8c95940
LL
219 * @update_tz: callback called when thermal zone internals have changed, e.g.
220 * thermal cooling instance was added/removed
c708a98f
JM
221 * @governor_list: node in thermal_governor_list (in thermal_core.c)
222 */
a4a15485 223struct thermal_governor {
b377252e 224 const char *name;
e33df1d2
JM
225 int (*bind_to_tz)(struct thermal_zone_device *tz);
226 void (*unbind_from_tz)(struct thermal_zone_device *tz);
8c35b1f4
RW
227 int (*throttle)(struct thermal_zone_device *tz,
228 const struct thermal_trip *trip);
a8c95940
LL
229 void (*update_tz)(struct thermal_zone_device *tz,
230 enum thermal_notify_event reason);
a4a15485 231 struct list_head governor_list;
a4a15485
D
232};
233
ef873947
D
234/* Structure to define Thermal Zone parameters */
235struct thermal_zone_params {
b377252e 236 const char *governor_name;
ccba4ffd
EV
237
238 /*
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
242 */
243 bool no_hwmon;
244
6b775e87
JM
245 /*
246 * Sustainable power (heat) that this thermal zone can dissipate in
247 * mW
248 */
249 u32 sustainable_power;
250
251 /*
252 * Proportional parameter of the PID controller when
253 * overshooting (i.e., when temperature is below the target)
254 */
255 s32 k_po;
256
257 /*
258 * Proportional parameter of the PID controller when
259 * undershooting
260 */
261 s32 k_pu;
262
263 /* Integral parameter of the PID controller */
264 s32 k_i;
265
266 /* Derivative parameter of the PID controller */
267 s32 k_d;
268
269 /* threshold below which the error is no longer accumulated */
270 s32 integral_cutoff;
9d0be7f4
EV
271
272 /*
273 * @slope: slope of a linear temperature adjustment curve.
274 * Used by thermal zone drivers.
275 */
276 int slope;
277 /*
278 * @offset: offset of a linear temperature adjustment curve.
279 * Used by thermal zone drivers (default 0).
280 */
281 int offset;
ef873947
D
282};
283
23064088 284/* Function declarations */
4e5e4705 285#ifdef CONFIG_THERMAL_OF
3fd6d6e2
DL
286struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
287 const struct thermal_zone_device_ops *ops);
288
3fd6d6e2
DL
289void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz);
290
4e5e4705 291#else
4e5e4705 292
f59ac19b
DL
293static inline
294struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
295 const struct thermal_zone_device_ops *ops)
e498b498 296{
f59ac19b 297 return ERR_PTR(-ENOTSUPP);
e498b498
LD
298}
299
f59ac19b
DL
300static inline void devm_thermal_of_zone_unregister(struct device *dev,
301 struct thermal_zone_device *tz)
3fd6d6e2
DL
302{
303}
4e5e4705 304#endif
12ca7188 305
e6ec64f8
JH
306int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
307 struct thermal_trip *trip);
7c3d5c20
DL
308int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
309 struct thermal_trip *trip);
96b8b436
RW
310int for_each_thermal_trip(struct thermal_zone_device *tz,
311 int (*cb)(struct thermal_trip *, void *),
312 void *data);
a56cc0a8
RW
313int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
314 int (*cb)(struct thermal_trip *, void *),
315 void *data);
7c3d5c20 316int thermal_zone_get_num_trips(struct thermal_zone_device *tz);
bdc22c8d
RW
317void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
318 struct thermal_trip *trip, int temp);
7c3d5c20
DL
319
320int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);
321
60518260 322#ifdef CONFIG_THERMAL
9ffa7b92
RW
323struct thermal_zone_device *thermal_zone_device_register_with_trips(
324 const char *type,
9b0a6275 325 const struct thermal_trip *trips,
4a62d588 326 int num_trips, void *devdata,
698a1eb1 327 const struct thermal_zone_device_ops *ops,
9ffa7b92
RW
328 const struct thermal_zone_params *tzp,
329 int passive_delay, int polling_delay);
330
d332db8f
RW
331struct thermal_zone_device *thermal_tripless_zone_device_register(
332 const char *type,
333 void *devdata,
698a1eb1 334 const struct thermal_zone_device_ops *ops,
d332db8f
RW
335 const struct thermal_zone_params *tzp);
336
9ffa7b92 337void thermal_zone_device_unregister(struct thermal_zone_device *tz);
fae11de5 338
a6ff3c00 339void *thermal_zone_device_priv(struct thermal_zone_device *tzd);
072e35c9 340const char *thermal_zone_device_type(struct thermal_zone_device *tzd);
3034f859 341int thermal_zone_device_id(struct thermal_zone_device *tzd);
7cefbaf0 342struct device *thermal_zone_device(struct thermal_zone_device *tzd);
a6ff3c00 343
d069ed6b
RW
344int 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);
203d3d4a 349int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
9d99842f 350 struct thermal_cooling_device *,
6cd9e9f6
KS
351 unsigned long, unsigned long,
352 unsigned int);
d069ed6b
RW
353int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
354 const struct thermal_trip *trip,
355 struct thermal_cooling_device *cdev);
203d3d4a
ZR
356int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
357 struct thermal_cooling_device *);
0e70f466
SP
358void thermal_zone_device_update(struct thermal_zone_device *,
359 enum thermal_notify_event);
23064088 360
f991de53
JFD
361struct thermal_cooling_device *thermal_cooling_device_register(const char *,
362 void *, const struct thermal_cooling_device_ops *);
a116b5d4 363struct thermal_cooling_device *
f991de53 364thermal_of_cooling_device_register(struct device_node *np, const char *, void *,
a116b5d4 365 const struct thermal_cooling_device_ops *);
b4ab114c
GR
366struct thermal_cooling_device *
367devm_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);
790930f4 371void thermal_cooling_device_update(struct thermal_cooling_device *);
203d3d4a 372void thermal_cooling_device_unregister(struct thermal_cooling_device *);
63c4d919 373struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
17e8351a 374int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
4a7069a3
RN
375int thermal_zone_get_slope(struct thermal_zone_device *tz);
376int thermal_zone_get_offset(struct thermal_zone_device *tz);
af06216a 377
ac5d9ecc
AP
378int thermal_zone_device_enable(struct thermal_zone_device *tz);
379int thermal_zone_device_disable(struct thermal_zone_device *tz);
d7203eed 380void thermal_zone_device_critical(struct thermal_zone_device *tz);
12ca7188 381#else
9ffa7b92
RW
382static inline struct thermal_zone_device *thermal_zone_device_register_with_trips(
383 const char *type,
9b0a6275 384 const struct thermal_trip *trips,
4a62d588 385 int num_trips, void *devdata,
698a1eb1 386 const struct thermal_zone_device_ops *ops,
9ffa7b92
RW
387 const struct thermal_zone_params *tzp,
388 int passive_delay, int polling_delay)
12ca7188 389{ return ERR_PTR(-ENODEV); }
9ffa7b92 390
d332db8f
RW
391static inline struct thermal_zone_device *thermal_tripless_zone_device_register(
392 const char *type,
393 void *devdata,
394 struct thermal_zone_device_ops *ops,
395 const struct thermal_zone_params *tzp)
396{ return ERR_PTR(-ENODEV); }
397
9ffa7b92 398static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz)
12ca7188 399{ }
9ffa7b92 400
12ca7188 401static inline struct thermal_cooling_device *
fb836107 402thermal_cooling_device_register(const char *type, void *devdata,
12ca7188
NM
403 const struct thermal_cooling_device_ops *ops)
404{ return ERR_PTR(-ENODEV); }
405static inline struct thermal_cooling_device *
406thermal_of_cooling_device_register(struct device_node *np,
fb836107
AB
407 const char *type, void *devdata,
408 const struct thermal_cooling_device_ops *ops)
12ca7188 409{ return ERR_PTR(-ENODEV); }
b4ab114c
GR
410static inline struct thermal_cooling_device *
411devm_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)
415{
416 return ERR_PTR(-ENODEV);
417}
12ca7188
NM
418static inline void thermal_cooling_device_unregister(
419 struct thermal_cooling_device *cdev)
420{ }
421static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
422 const char *name)
423{ return ERR_PTR(-ENODEV); }
424static inline int thermal_zone_get_temp(
17e8351a 425 struct thermal_zone_device *tz, int *temp)
12ca7188 426{ return -ENODEV; }
4a7069a3
RN
427static inline int thermal_zone_get_slope(
428 struct thermal_zone_device *tz)
429{ return -ENODEV; }
430static inline int thermal_zone_get_offset(
431 struct thermal_zone_device *tz)
432{ return -ENODEV; }
f0129c23 433
a6ff3c00
DL
434static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz)
435{
436 return NULL;
437}
438
072e35c9
DL
439static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
440{
441 return NULL;
442}
443
3034f859
DL
444static inline int thermal_zone_device_id(struct thermal_zone_device *tzd)
445{
446 return -ENODEV;
447}
448
ac5d9ecc
AP
449static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
450{ return -ENODEV; }
451
452static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
453{ return -ENODEV; }
12ca7188
NM
454#endif /* CONFIG_THERMAL */
455
a0dd25b2 456#endif /* __THERMAL_H__ */