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