1 /* SPDX-License-Identifier: GPL-2.0 */
3 * thermal.h ($Revision: 0 $)
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>
14 #include <linux/idr.h>
15 #include <linux/device.h>
16 #include <linux/sysfs.h>
17 #include <linux/workqueue.h>
18 #include <uapi/linux/thermal.h>
20 #define THERMAL_MAX_TRIPS 12
22 /* invalid cooling state */
23 #define THERMAL_CSTATE_INVALID -1UL
25 /* No upper/lower limit requirement */
26 #define THERMAL_NO_LIMIT ((u32)~0)
28 /* Default weight of a bound cooling device */
29 #define THERMAL_WEIGHT_DEFAULT 0
31 /* use value, which < 0K, to indicate an invalid/uninitialized temperature */
32 #define THERMAL_TEMP_INVALID -274000
34 struct thermal_zone_device
;
35 struct thermal_cooling_device
;
36 struct thermal_instance
;
40 THERMAL_TREND_STABLE
, /* temperature is stable */
41 THERMAL_TREND_RAISING
, /* temperature is raising */
42 THERMAL_TREND_DROPPING
, /* temperature is dropping */
43 THERMAL_TREND_RAISE_FULL
, /* apply highest cooling action */
44 THERMAL_TREND_DROP_FULL
, /* apply lowest cooling action */
47 /* Thermal notification reason */
48 enum thermal_notify_event
{
49 THERMAL_EVENT_UNSPECIFIED
, /* Unspecified event */
50 THERMAL_EVENT_TEMP_SAMPLE
, /* New Temperature sample */
51 THERMAL_TRIP_VIOLATED
, /* TRIP Point violation */
52 THERMAL_TRIP_CHANGED
, /* TRIP Point temperature changed */
53 THERMAL_DEVICE_DOWN
, /* Thermal device is down */
54 THERMAL_DEVICE_UP
, /* Thermal device is up after a down event */
55 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED
, /* power capability changed */
56 THERMAL_TABLE_CHANGED
, /* Thermal table(s) changed */
57 THERMAL_EVENT_KEEP_ALIVE
, /* Request for user space handler to respond */
60 struct 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
*);
65 int (*get_temp
) (struct thermal_zone_device
*, int *);
66 int (*set_trips
) (struct thermal_zone_device
*, int, int);
67 int (*change_mode
) (struct thermal_zone_device
*,
68 enum thermal_device_mode
);
69 int (*get_trip_type
) (struct thermal_zone_device
*, int,
70 enum thermal_trip_type
*);
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);
77 int (*get_trend
) (struct thermal_zone_device
*, int,
78 enum thermal_trend
*);
79 void (*hot
)(struct thermal_zone_device
*);
80 void (*critical
)(struct thermal_zone_device
*);
83 struct thermal_cooling_device_ops
{
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);
87 int (*get_requested_power
)(struct thermal_cooling_device
*, u32
*);
88 int (*state2power
)(struct thermal_cooling_device
*, unsigned long, u32
*);
89 int (*power2state
)(struct thermal_cooling_device
*, u32
, unsigned long *);
92 struct thermal_cooling_device
{
96 struct device_node
*np
;
99 const struct thermal_cooling_device_ops
*ops
;
100 bool updated
; /* true if the cooling device does not need update */
101 struct mutex lock
; /* protect thermal_instances list */
102 struct list_head thermal_instances
;
103 struct list_head node
;
107 * struct thermal_zone_device - structure for a thermal zone
108 * @id: unique id number for each thermal zone
109 * @type: the thermal zone device type
110 * @device: &struct device for this thermal zone
111 * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
112 * @trip_type_attrs: attributes for trip points for sysfs: trip type
113 * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
114 * @mode: current mode of this thermal zone
115 * @devdata: private pointer for device private data
116 * @trips: number of trip points the thermal zone supports
117 * @trips_disabled; bitmap for disabled trips
118 * @passive_delay_jiffies: number of jiffies to wait between polls when
119 * performing passive cooling.
120 * @polling_delay_jiffies: number of jiffies to wait between polls when
121 * checking whether trip points have been crossed (0 for
122 * interrupt driven systems)
123 * @temperature: current temperature. This is only for core code,
124 * drivers should use thermal_zone_get_temp() to get the
125 * current temperature
126 * @last_temperature: previous temperature read
127 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
128 * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
129 * @prev_low_trip: the low current temperature if you've crossed a passive
131 * @prev_high_trip: the above current temperature if you've crossed a
133 * @need_update: if equals 1, thermal_zone_device_update needs to be invoked.
134 * @ops: operations this &thermal_zone_device supports
135 * @tzp: thermal zone parameters
136 * @governor: pointer to the governor for this thermal zone
137 * @governor_data: private pointer for governor data
138 * @thermal_instances: list of &struct thermal_instance of this thermal zone
139 * @ida: &struct ida to generate unique id for this zone's cooling
141 * @lock: lock to protect thermal_instances list
142 * @node: node in thermal_tz_list (in thermal_core.c)
143 * @poll_queue: delayed work for polling
144 * @notify_event: Last notification event
146 struct thermal_zone_device
{
148 char type
[THERMAL_NAME_LENGTH
];
149 struct device device
;
150 struct attribute_group trips_attribute_group
;
151 struct thermal_attr
*trip_temp_attrs
;
152 struct thermal_attr
*trip_type_attrs
;
153 struct thermal_attr
*trip_hyst_attrs
;
154 enum thermal_device_mode mode
;
157 unsigned long trips_disabled
; /* bitmap for disabled trips */
158 unsigned long passive_delay_jiffies
;
159 unsigned long polling_delay_jiffies
;
161 int last_temperature
;
162 int emul_temperature
;
166 atomic_t need_update
;
167 struct thermal_zone_device_ops
*ops
;
168 struct thermal_zone_params
*tzp
;
169 struct thermal_governor
*governor
;
171 struct list_head thermal_instances
;
174 struct list_head node
;
175 struct delayed_work poll_queue
;
176 enum thermal_notify_event notify_event
;
180 * struct thermal_governor - structure that holds thermal governor information
181 * @name: name of the governor
182 * @bind_to_tz: callback called when binding to a thermal zone. If it
183 * returns 0, the governor is bound to the thermal zone,
184 * otherwise it fails.
185 * @unbind_from_tz: callback called when a governor is unbound from a
187 * @throttle: callback called for every trip point even if temperature is
188 * below the trip point temperature
189 * @governor_list: node in thermal_governor_list (in thermal_core.c)
191 struct thermal_governor
{
192 char name
[THERMAL_NAME_LENGTH
];
193 int (*bind_to_tz
)(struct thermal_zone_device
*tz
);
194 void (*unbind_from_tz
)(struct thermal_zone_device
*tz
);
195 int (*throttle
)(struct thermal_zone_device
*tz
, int trip
);
196 struct list_head governor_list
;
199 /* Structure that holds binding parameters for a zone */
200 struct thermal_bind_params
{
201 struct thermal_cooling_device
*cdev
;
204 * This is a measure of 'how effectively these devices can
205 * cool 'this' thermal zone. It shall be determined by
206 * platform characterization. This value is relative to the
207 * rest of the weights so a cooling device whose weight is
208 * double that of another cooling device is twice as
209 * effective. See Documentation/driver-api/thermal/sysfs-api.rst for more
215 * This is a bit mask that gives the binding relation between this
216 * thermal zone and cdev, for a particular trip point.
217 * See Documentation/driver-api/thermal/sysfs-api.rst for more information.
222 * This is an array of cooling state limits. Must have exactly
223 * 2 * thermal_zone.number_of_trip_points. It is an array consisting
224 * of tuples <lower-state upper-state> of state limits. Each trip
225 * will be associated with one state limit tuple when binding.
226 * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS>
229 unsigned long *binding_limits
;
230 int (*match
) (struct thermal_zone_device
*tz
,
231 struct thermal_cooling_device
*cdev
);
234 /* Structure to define Thermal Zone parameters */
235 struct thermal_zone_params
{
236 char governor_name
[THERMAL_NAME_LENGTH
];
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
245 int num_tbps
; /* Number of tbp entries */
246 struct thermal_bind_params
*tbp
;
249 * Sustainable power (heat) that this thermal zone can dissipate in
252 u32 sustainable_power
;
255 * Proportional parameter of the PID controller when
256 * overshooting (i.e., when temperature is below the target)
261 * Proportional parameter of the PID controller when
266 /* Integral parameter of the PID controller */
269 /* Derivative parameter of the PID controller */
272 /* threshold below which the error is no longer accumulated */
276 * @slope: slope of a linear temperature adjustment curve.
277 * Used by thermal zone drivers.
281 * @offset: offset of a linear temperature adjustment curve.
282 * Used by thermal zone drivers (default 0).
288 * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones
291 * @get_temp: a pointer to a function that reads the sensor temperature.
294 * @get_trend: a pointer to a function that reads the sensor temperature trend.
295 * @set_trips: a pointer to a function that sets a temperature window. When
296 * this window is left the driver must inform the thermal core via
297 * thermal_zone_device_update.
298 * @set_emul_temp: a pointer to a function that sets sensor emulated
300 * @set_trip_temp: a pointer to a function that sets the trip temperature on
303 struct thermal_zone_of_device_ops
{
304 int (*get_temp
)(void *, int *);
305 int (*get_trend
)(void *, int, enum thermal_trend
*);
306 int (*set_trips
)(void *, int, int);
307 int (*set_emul_temp
)(void *, int);
308 int (*set_trip_temp
)(void *, int, int);
311 /* Function declarations */
312 #ifdef CONFIG_THERMAL_OF
313 int thermal_zone_of_get_sensor_id(struct device_node
*tz_np
,
314 struct device_node
*sensor_np
,
316 struct thermal_zone_device
*
317 thermal_zone_of_sensor_register(struct device
*dev
, int id
, void *data
,
318 const struct thermal_zone_of_device_ops
*ops
);
319 void thermal_zone_of_sensor_unregister(struct device
*dev
,
320 struct thermal_zone_device
*tz
);
321 struct thermal_zone_device
*devm_thermal_zone_of_sensor_register(
322 struct device
*dev
, int id
, void *data
,
323 const struct thermal_zone_of_device_ops
*ops
);
324 void devm_thermal_zone_of_sensor_unregister(struct device
*dev
,
325 struct thermal_zone_device
*tz
);
328 static inline int thermal_zone_of_get_sensor_id(struct device_node
*tz_np
,
329 struct device_node
*sensor_np
,
334 static inline struct thermal_zone_device
*
335 thermal_zone_of_sensor_register(struct device
*dev
, int id
, void *data
,
336 const struct thermal_zone_of_device_ops
*ops
)
338 return ERR_PTR(-ENODEV
);
342 void thermal_zone_of_sensor_unregister(struct device
*dev
,
343 struct thermal_zone_device
*tz
)
347 static inline struct thermal_zone_device
*devm_thermal_zone_of_sensor_register(
348 struct device
*dev
, int id
, void *data
,
349 const struct thermal_zone_of_device_ops
*ops
)
351 return ERR_PTR(-ENODEV
);
355 void devm_thermal_zone_of_sensor_unregister(struct device
*dev
,
356 struct thermal_zone_device
*tz
)
362 #ifdef CONFIG_THERMAL
363 struct thermal_zone_device
*thermal_zone_device_register(const char *, int, int,
364 void *, struct thermal_zone_device_ops
*,
365 struct thermal_zone_params
*, int, int);
366 void thermal_zone_device_unregister(struct thermal_zone_device
*);
368 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*, int,
369 struct thermal_cooling_device
*,
370 unsigned long, unsigned long,
372 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*, int,
373 struct thermal_cooling_device
*);
374 void thermal_zone_device_update(struct thermal_zone_device
*,
375 enum thermal_notify_event
);
377 struct thermal_cooling_device
*thermal_cooling_device_register(const char *,
378 void *, const struct thermal_cooling_device_ops
*);
379 struct thermal_cooling_device
*
380 thermal_of_cooling_device_register(struct device_node
*np
, const char *, void *,
381 const struct thermal_cooling_device_ops
*);
382 struct thermal_cooling_device
*
383 devm_thermal_of_cooling_device_register(struct device
*dev
,
384 struct device_node
*np
,
385 char *type
, void *devdata
,
386 const struct thermal_cooling_device_ops
*ops
);
387 void thermal_cooling_device_unregister(struct thermal_cooling_device
*);
388 struct thermal_zone_device
*thermal_zone_get_zone_by_name(const char *name
);
389 int thermal_zone_get_temp(struct thermal_zone_device
*tz
, int *temp
);
390 int thermal_zone_get_slope(struct thermal_zone_device
*tz
);
391 int thermal_zone_get_offset(struct thermal_zone_device
*tz
);
393 int thermal_zone_device_enable(struct thermal_zone_device
*tz
);
394 int thermal_zone_device_disable(struct thermal_zone_device
*tz
);
395 void thermal_zone_device_critical(struct thermal_zone_device
*tz
);
397 static inline struct thermal_zone_device
*thermal_zone_device_register(
398 const char *type
, int trips
, int mask
, void *devdata
,
399 struct thermal_zone_device_ops
*ops
,
400 struct thermal_zone_params
*tzp
,
401 int passive_delay
, int polling_delay
)
402 { return ERR_PTR(-ENODEV
); }
403 static inline void thermal_zone_device_unregister(
404 struct thermal_zone_device
*tz
)
406 static inline struct thermal_cooling_device
*
407 thermal_cooling_device_register(char *type
, void *devdata
,
408 const struct thermal_cooling_device_ops
*ops
)
409 { return ERR_PTR(-ENODEV
); }
410 static inline struct thermal_cooling_device
*
411 thermal_of_cooling_device_register(struct device_node
*np
,
412 char *type
, void *devdata
, const struct thermal_cooling_device_ops
*ops
)
413 { return ERR_PTR(-ENODEV
); }
414 static inline struct thermal_cooling_device
*
415 devm_thermal_of_cooling_device_register(struct device
*dev
,
416 struct device_node
*np
,
417 char *type
, void *devdata
,
418 const struct thermal_cooling_device_ops
*ops
)
420 return ERR_PTR(-ENODEV
);
422 static inline void thermal_cooling_device_unregister(
423 struct thermal_cooling_device
*cdev
)
425 static inline struct thermal_zone_device
*thermal_zone_get_zone_by_name(
427 { return ERR_PTR(-ENODEV
); }
428 static inline int thermal_zone_get_temp(
429 struct thermal_zone_device
*tz
, int *temp
)
431 static inline int thermal_zone_get_slope(
432 struct thermal_zone_device
*tz
)
434 static inline int thermal_zone_get_offset(
435 struct thermal_zone_device
*tz
)
438 static inline int thermal_zone_device_enable(struct thermal_zone_device
*tz
)
441 static inline int thermal_zone_device_disable(struct thermal_zone_device
*tz
)
443 #endif /* CONFIG_THERMAL */
445 #endif /* __THERMAL_H__ */