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