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