thermal/core: Emit a warning if the thermal zone is updated without ops
[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
23064088
D
20#define THERMAL_TRIPS_NONE -1
21#define THERMAL_MAX_TRIPS 12
23064088 22
57df8106
ZR
23/* invalid cooling state */
24#define THERMAL_CSTATE_INVALID -1UL
25
23064088 26/* No upper/lower limit requirement */
a940cb34 27#define THERMAL_NO_LIMIT ((u32)~0)
23064088 28
6cd9e9f6
KS
29/* Default weight of a bound cooling device */
30#define THERMAL_WEIGHT_DEFAULT 0
31
bb431ba2
ZR
32/* use value, which < 0K, to indicate an invalid/uninitialized temperature */
33#define THERMAL_TEMP_INVALID -274000
34
203d3d4a
ZR
35struct thermal_zone_device;
36struct thermal_cooling_device;
35b11d2e 37struct thermal_instance;
c68df440 38struct thermal_attr;
203d3d4a 39
601f3d42
ZR
40enum thermal_trend {
41 THERMAL_TREND_STABLE, /* temperature is stable */
42 THERMAL_TREND_RAISING, /* temperature is raising */
43 THERMAL_TREND_DROPPING, /* temperature is dropping */
d069015e
ZR
44 THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */
45 THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */
601f3d42
ZR
46};
47
0e70f466
SP
48/* Thermal notification reason */
49enum thermal_notify_event {
50 THERMAL_EVENT_UNSPECIFIED, /* Unspecified event */
51 THERMAL_EVENT_TEMP_SAMPLE, /* New Temperature sample */
52 THERMAL_TRIP_VIOLATED, /* TRIP Point violation */
53 THERMAL_TRIP_CHANGED, /* TRIP Point temperature changed */
54 THERMAL_DEVICE_DOWN, /* Thermal device is down */
55 THERMAL_DEVICE_UP, /* Thermal device is up after a down event */
56 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED, /* power capability changed */
38e44da5 57 THERMAL_TABLE_CHANGED, /* Thermal table(s) changed */
88052319 58 THERMAL_EVENT_KEEP_ALIVE, /* Request for user space handler to respond */
0e70f466
SP
59};
60
203d3d4a
ZR
61struct thermal_zone_device_ops {
62 int (*bind) (struct thermal_zone_device *,
63 struct thermal_cooling_device *);
64 int (*unbind) (struct thermal_zone_device *,
65 struct thermal_cooling_device *);
17e8351a 66 int (*get_temp) (struct thermal_zone_device *, int *);
060c034a 67 int (*set_trips) (struct thermal_zone_device *, int, int);
f5e50bf4 68 int (*change_mode) (struct thermal_zone_device *,
6503e5df
MG
69 enum thermal_device_mode);
70 int (*get_trip_type) (struct thermal_zone_device *, int,
71 enum thermal_trip_type *);
17e8351a
SH
72 int (*get_trip_temp) (struct thermal_zone_device *, int, int *);
73 int (*set_trip_temp) (struct thermal_zone_device *, int, int);
74 int (*get_trip_hyst) (struct thermal_zone_device *, int, int *);
75 int (*set_trip_hyst) (struct thermal_zone_device *, int, int);
76 int (*get_crit_temp) (struct thermal_zone_device *, int *);
77 int (*set_emul_temp) (struct thermal_zone_device *, int);
601f3d42
ZR
78 int (*get_trend) (struct thermal_zone_device *, int,
79 enum thermal_trend *);
b1569e99
MG
80 int (*notify) (struct thermal_zone_device *, int,
81 enum thermal_trip_type);
203d3d4a
ZR
82};
83
84struct thermal_cooling_device_ops {
6503e5df
MG
85 int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
86 int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
87 int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
ecd1d2a3 88 int (*get_requested_power)(struct thermal_cooling_device *, u32 *);
89 int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *);
90 int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *);
203d3d4a
ZR
91};
92
203d3d4a
ZR
93struct thermal_cooling_device {
94 int id;
95 char type[THERMAL_NAME_LENGTH];
96 struct device device;
4e5e4705 97 struct device_node *np;
203d3d4a 98 void *devdata;
8ea22951 99 void *stats;
5b275ce2 100 const struct thermal_cooling_device_ops *ops;
ce119f83 101 bool updated; /* true if the cooling device does not need update */
f4a821ce 102 struct mutex lock; /* protect thermal_instances list */
b5e4ae62 103 struct list_head thermal_instances;
203d3d4a
ZR
104 struct list_head node;
105};
106
c708a98f
JM
107/**
108 * struct thermal_zone_device - structure for a thermal zone
109 * @id: unique id number for each thermal zone
110 * @type: the thermal zone device type
111 * @device: &struct device for this thermal zone
112 * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
113 * @trip_type_attrs: attributes for trip points for sysfs: trip type
114 * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
cbba1d71 115 * @mode: current mode of this thermal zone
c708a98f
JM
116 * @devdata: private pointer for device private data
117 * @trips: number of trip points the thermal zone supports
81ad4276 118 * @trips_disabled; bitmap for disabled trips
c708a98f 119 * @passive_delay: number of milliseconds to wait between polls when
6b775e87 120 * performing passive cooling.
c708a98f
JM
121 * @polling_delay: number of milliseconds to wait between polls when
122 * checking whether trip points have been crossed (0 for
123 * interrupt driven systems)
124 * @temperature: current temperature. This is only for core code,
125 * drivers should use thermal_zone_get_temp() to get the
126 * current temperature
127 * @last_temperature: previous temperature read
128 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
129 * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
060c034a
SH
130 * @prev_low_trip: the low current temperature if you've crossed a passive
131 trip point.
132 * @prev_high_trip: the above current temperature if you've crossed a
133 passive trip point.
c708a98f
JM
134 * @forced_passive: If > 0, temperature at which to switch on all ACPI
135 * processor cooling devices. Currently only used by the
136 * step-wise governor.
4511f716 137 * @need_update: if equals 1, thermal_zone_device_update needs to be invoked.
c708a98f
JM
138 * @ops: operations this &thermal_zone_device supports
139 * @tzp: thermal zone parameters
140 * @governor: pointer to the governor for this thermal zone
e33df1d2 141 * @governor_data: private pointer for governor data
c708a98f 142 * @thermal_instances: list of &struct thermal_instance of this thermal zone
b31ef828 143 * @ida: &struct ida to generate unique id for this zone's cooling
c708a98f
JM
144 * devices
145 * @lock: lock to protect thermal_instances list
146 * @node: node in thermal_tz_list (in thermal_core.c)
147 * @poll_queue: delayed work for polling
0e70f466 148 * @notify_event: Last notification event
c708a98f 149 */
203d3d4a
ZR
150struct thermal_zone_device {
151 int id;
152 char type[THERMAL_NAME_LENGTH];
153 struct device device;
4d0fe749 154 struct attribute_group trips_attribute_group;
c56f5c03
D
155 struct thermal_attr *trip_temp_attrs;
156 struct thermal_attr *trip_type_attrs;
27365a6c 157 struct thermal_attr *trip_hyst_attrs;
cbba1d71 158 enum thermal_device_mode mode;
203d3d4a
ZR
159 void *devdata;
160 int trips;
81ad4276 161 unsigned long trips_disabled; /* bitmap for disabled trips */
b1569e99
MG
162 int passive_delay;
163 int polling_delay;
601f3d42 164 int temperature;
b1569e99 165 int last_temperature;
e6e238c3 166 int emul_temperature;
908b9fb7 167 int passive;
060c034a
SH
168 int prev_low_trip;
169 int prev_high_trip;
03a971a2 170 unsigned int forced_passive;
4511f716 171 atomic_t need_update;
4e5e4705 172 struct thermal_zone_device_ops *ops;
6b775e87 173 struct thermal_zone_params *tzp;
a4a15485 174 struct thermal_governor *governor;
e33df1d2 175 void *governor_data;
2d374139 176 struct list_head thermal_instances;
b31ef828 177 struct ida ida;
c708a98f 178 struct mutex lock;
203d3d4a 179 struct list_head node;
b1569e99 180 struct delayed_work poll_queue;
0e70f466 181 enum thermal_notify_event notify_event;
203d3d4a 182};
4cb18728 183
c708a98f
JM
184/**
185 * struct thermal_governor - structure that holds thermal governor information
186 * @name: name of the governor
e33df1d2
JM
187 * @bind_to_tz: callback called when binding to a thermal zone. If it
188 * returns 0, the governor is bound to the thermal zone,
189 * otherwise it fails.
190 * @unbind_from_tz: callback called when a governor is unbound from a
191 * thermal zone.
c708a98f
JM
192 * @throttle: callback called for every trip point even if temperature is
193 * below the trip point temperature
194 * @governor_list: node in thermal_governor_list (in thermal_core.c)
195 */
a4a15485
D
196struct thermal_governor {
197 char name[THERMAL_NAME_LENGTH];
e33df1d2
JM
198 int (*bind_to_tz)(struct thermal_zone_device *tz);
199 void (*unbind_from_tz)(struct thermal_zone_device *tz);
a4a15485
D
200 int (*throttle)(struct thermal_zone_device *tz, int trip);
201 struct list_head governor_list;
a4a15485
D
202};
203
ef873947
D
204/* Structure that holds binding parameters for a zone */
205struct thermal_bind_params {
206 struct thermal_cooling_device *cdev;
207
208 /*
209 * This is a measure of 'how effectively these devices can
bcdcbbc7
JM
210 * cool 'this' thermal zone. It shall be determined by
211 * platform characterization. This value is relative to the
212 * rest of the weights so a cooling device whose weight is
213 * double that of another cooling device is twice as
eaf7b460 214 * effective. See Documentation/driver-api/thermal/sysfs-api.rst for more
bcdcbbc7 215 * information.
ef873947
D
216 */
217 int weight;
218
219 /*
220 * This is a bit mask that gives the binding relation between this
221 * thermal zone and cdev, for a particular trip point.
eaf7b460 222 * See Documentation/driver-api/thermal/sysfs-api.rst for more information.
ef873947
D
223 */
224 int trip_mask;
a8892d83
EV
225
226 /*
227 * This is an array of cooling state limits. Must have exactly
228 * 2 * thermal_zone.number_of_trip_points. It is an array consisting
229 * of tuples <lower-state upper-state> of state limits. Each trip
230 * will be associated with one state limit tuple when binding.
231 * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS>
232 * on all trips.
233 */
234 unsigned long *binding_limits;
ef873947
D
235 int (*match) (struct thermal_zone_device *tz,
236 struct thermal_cooling_device *cdev);
237};
238
239/* Structure to define Thermal Zone parameters */
240struct thermal_zone_params {
a4a15485 241 char governor_name[THERMAL_NAME_LENGTH];
ccba4ffd
EV
242
243 /*
244 * a boolean to indicate if the thermal to hwmon sysfs interface
245 * is required. when no_hwmon == false, a hwmon sysfs interface
246 * will be created. when no_hwmon == true, nothing will be done
247 */
248 bool no_hwmon;
249
ef873947
D
250 int num_tbps; /* Number of tbp entries */
251 struct thermal_bind_params *tbp;
6b775e87
JM
252
253 /*
254 * Sustainable power (heat) that this thermal zone can dissipate in
255 * mW
256 */
257 u32 sustainable_power;
258
259 /*
260 * Proportional parameter of the PID controller when
261 * overshooting (i.e., when temperature is below the target)
262 */
263 s32 k_po;
264
265 /*
266 * Proportional parameter of the PID controller when
267 * undershooting
268 */
269 s32 k_pu;
270
271 /* Integral parameter of the PID controller */
272 s32 k_i;
273
274 /* Derivative parameter of the PID controller */
275 s32 k_d;
276
277 /* threshold below which the error is no longer accumulated */
278 s32 integral_cutoff;
9d0be7f4
EV
279
280 /*
281 * @slope: slope of a linear temperature adjustment curve.
282 * Used by thermal zone drivers.
283 */
284 int slope;
285 /*
286 * @offset: offset of a linear temperature adjustment curve.
287 * Used by thermal zone drivers (default 0).
288 */
289 int offset;
ef873947
D
290};
291
2251aef6
EV
292/**
293 * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones
294 *
295 * Mandatory:
296 * @get_temp: a pointer to a function that reads the sensor temperature.
297 *
298 * Optional:
299 * @get_trend: a pointer to a function that reads the sensor temperature trend.
826386e7
SH
300 * @set_trips: a pointer to a function that sets a temperature window. When
301 * this window is left the driver must inform the thermal core via
302 * thermal_zone_device_update.
184a4bf6
LM
303 * @set_emul_temp: a pointer to a function that sets sensor emulated
304 * temperature.
5a5e78cd
CW
305 * @set_trip_temp: a pointer to a function that sets the trip temperature on
306 * hardware.
2251aef6
EV
307 */
308struct thermal_zone_of_device_ops {
17e8351a 309 int (*get_temp)(void *, int *);
e78eaf45 310 int (*get_trend)(void *, int, enum thermal_trend *);
826386e7 311 int (*set_trips)(void *, int, int);
17e8351a 312 int (*set_emul_temp)(void *, int);
c3509521 313 int (*set_trip_temp)(void *, int, int);
2251aef6
EV
314};
315
23064088 316/* Function declarations */
4e5e4705 317#ifdef CONFIG_THERMAL_OF
34471abf
AH
318int thermal_zone_of_get_sensor_id(struct device_node *tz_np,
319 struct device_node *sensor_np,
320 u32 *id);
4e5e4705 321struct thermal_zone_device *
2251aef6
EV
322thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
323 const struct thermal_zone_of_device_ops *ops);
4e5e4705
EV
324void thermal_zone_of_sensor_unregister(struct device *dev,
325 struct thermal_zone_device *tz);
e498b498
LD
326struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
327 struct device *dev, int id, void *data,
328 const struct thermal_zone_of_device_ops *ops);
329void devm_thermal_zone_of_sensor_unregister(struct device *dev,
330 struct thermal_zone_device *tz);
4e5e4705 331#else
34471abf 332
15a26319 333static inline int thermal_zone_of_get_sensor_id(struct device_node *tz_np,
34471abf
AH
334 struct device_node *sensor_np,
335 u32 *id)
336{
337 return -ENOENT;
338}
4e5e4705 339static inline struct thermal_zone_device *
2251aef6
EV
340thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
341 const struct thermal_zone_of_device_ops *ops)
4e5e4705 342{
cd33dc9a 343 return ERR_PTR(-ENODEV);
4e5e4705
EV
344}
345
346static inline
347void thermal_zone_of_sensor_unregister(struct device *dev,
348 struct thermal_zone_device *tz)
349{
350}
351
e498b498
LD
352static inline struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
353 struct device *dev, int id, void *data,
354 const struct thermal_zone_of_device_ops *ops)
355{
356 return ERR_PTR(-ENODEV);
357}
358
359static inline
360void devm_thermal_zone_of_sensor_unregister(struct device *dev,
361 struct thermal_zone_device *tz)
362{
363}
364
4e5e4705 365#endif
12ca7188 366
60518260 367#ifdef CONFIG_THERMAL
4b1bf587 368struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
4e5e4705 369 void *, struct thermal_zone_device_ops *,
6b775e87 370 struct thermal_zone_params *, int, int);
203d3d4a
ZR
371void thermal_zone_device_unregister(struct thermal_zone_device *);
372
373int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
9d99842f 374 struct thermal_cooling_device *,
6cd9e9f6
KS
375 unsigned long, unsigned long,
376 unsigned int);
203d3d4a
ZR
377int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
378 struct thermal_cooling_device *);
0e70f466
SP
379void thermal_zone_device_update(struct thermal_zone_device *,
380 enum thermal_notify_event);
23064088 381
f991de53
JFD
382struct thermal_cooling_device *thermal_cooling_device_register(const char *,
383 void *, const struct thermal_cooling_device_ops *);
a116b5d4 384struct thermal_cooling_device *
f991de53 385thermal_of_cooling_device_register(struct device_node *np, const char *, void *,
a116b5d4 386 const struct thermal_cooling_device_ops *);
b4ab114c
GR
387struct thermal_cooling_device *
388devm_thermal_of_cooling_device_register(struct device *dev,
389 struct device_node *np,
390 char *type, void *devdata,
391 const struct thermal_cooling_device_ops *ops);
203d3d4a 392void thermal_cooling_device_unregister(struct thermal_cooling_device *);
63c4d919 393struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
17e8351a 394int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
4a7069a3
RN
395int thermal_zone_get_slope(struct thermal_zone_device *tz);
396int thermal_zone_get_offset(struct thermal_zone_device *tz);
af06216a 397
dc765482 398void thermal_cdev_update(struct thermal_cooling_device *);
7b73c993 399void thermal_notify_framework(struct thermal_zone_device *, int);
ac5d9ecc
AP
400int thermal_zone_device_enable(struct thermal_zone_device *tz);
401int thermal_zone_device_disable(struct thermal_zone_device *tz);
12ca7188
NM
402#else
403static inline struct thermal_zone_device *thermal_zone_device_register(
404 const char *type, int trips, int mask, void *devdata,
405 struct thermal_zone_device_ops *ops,
023b7b07 406 struct thermal_zone_params *tzp,
12ca7188
NM
407 int passive_delay, int polling_delay)
408{ return ERR_PTR(-ENODEV); }
409static inline void thermal_zone_device_unregister(
410 struct thermal_zone_device *tz)
411{ }
12ca7188
NM
412static inline struct thermal_cooling_device *
413thermal_cooling_device_register(char *type, void *devdata,
414 const struct thermal_cooling_device_ops *ops)
415{ return ERR_PTR(-ENODEV); }
416static inline struct thermal_cooling_device *
417thermal_of_cooling_device_register(struct device_node *np,
418 char *type, void *devdata, const struct thermal_cooling_device_ops *ops)
419{ return ERR_PTR(-ENODEV); }
b4ab114c
GR
420static inline struct thermal_cooling_device *
421devm_thermal_of_cooling_device_register(struct device *dev,
422 struct device_node *np,
423 char *type, void *devdata,
424 const struct thermal_cooling_device_ops *ops)
425{
426 return ERR_PTR(-ENODEV);
427}
12ca7188
NM
428static inline void thermal_cooling_device_unregister(
429 struct thermal_cooling_device *cdev)
430{ }
431static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
432 const char *name)
433{ return ERR_PTR(-ENODEV); }
434static inline int thermal_zone_get_temp(
17e8351a 435 struct thermal_zone_device *tz, int *temp)
12ca7188 436{ return -ENODEV; }
4a7069a3
RN
437static inline int thermal_zone_get_slope(
438 struct thermal_zone_device *tz)
439{ return -ENODEV; }
440static inline int thermal_zone_get_offset(
441 struct thermal_zone_device *tz)
442{ return -ENODEV; }
f0129c23 443
12ca7188
NM
444static inline void thermal_cdev_update(struct thermal_cooling_device *cdev)
445{ }
446static inline void thermal_notify_framework(struct thermal_zone_device *tz,
447 int trip)
448{ }
ac5d9ecc
AP
449
450static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
451{ return -ENODEV; }
452
453static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
454{ return -ENODEV; }
12ca7188
NM
455#endif /* CONFIG_THERMAL */
456
a0dd25b2 457#endif /* __THERMAL_H__ */