1 /* SPDX-License-Identifier: GPL-2.0 */
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>
8 */
9
10 #ifndef __THERMAL_H__
11 #define __THERMAL_H__
12
13 #include <linux/of.h>
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>
19
20 #define THERMAL_MAX_TRIPS 12
21
22 /* invalid cooling state */
23 #define THERMAL_CSTATE_INVALID -1UL
24
25 /* No upper/lower limit requirement */
26 #define THERMAL_NO_LIMIT ((u32)~0)
27
28 /* Default weight of a bound cooling device */
29 #define THERMAL_WEIGHT_DEFAULT 0
30
31 /* use value, which < 0K, to indicate an invalid/uninitialized temperature */
32 #define THERMAL_TEMP_INVALID -274000
33
34 struct thermal_zone_device;
35 struct thermal_cooling_device;
36 struct thermal_instance;
37 struct thermal_attr;
38
39 enum thermal_trend {
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 */
45 };
46
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 */
58 };
59
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 *);
81 };
82
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 *);
90 };
91
92 struct thermal_cooling_device {
93 int id;
94 char *type;
95 struct device device;
96 struct device_node *np;
97 void *devdata;
98 void *stats;
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;
104 };
105
106 /**
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
130 trip point.
131 * @prev_high_trip: the above current temperature if you've crossed a
132 passive trip point.
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
140 * devices
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
145 */
146 struct thermal_zone_device {
147 int id;
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;
155 void *devdata;
156 int trips;
157 unsigned long trips_disabled; /* bitmap for disabled trips */
158 unsigned long passive_delay_jiffies;
159 unsigned long polling_delay_jiffies;
160 int temperature;
161 int last_temperature;
162 int emul_temperature;
163 int passive;
164 int prev_low_trip;
165 int prev_high_trip;
166 atomic_t need_update;
167 struct thermal_zone_device_ops *ops;
168 struct thermal_zone_params *tzp;
169 struct thermal_governor *governor;
170 void *governor_data;
171 struct list_head thermal_instances;
172 struct ida ida;
173 struct mutex lock;
174 struct list_head node;
175 struct delayed_work poll_queue;
176 enum thermal_notify_event notify_event;
177 };
178
179 /**
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
186 * thermal zone.
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)
190 */
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;
197 };
198
199 /* Structure that holds binding parameters for a zone */
200 struct thermal_bind_params {
201 struct thermal_cooling_device *cdev;
202
203 /*
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
210 * information.
211 */
212 int weight;
213
214 /*
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.
218 */
219 int trip_mask;
220
221 /*
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>
227 * on all trips.
228 */
229 unsigned long *binding_limits;
230 int (*match) (struct thermal_zone_device *tz,
231 struct thermal_cooling_device *cdev);
232 };
233
234 /* Structure to define Thermal Zone parameters */
235 struct thermal_zone_params {
236 char governor_name[THERMAL_NAME_LENGTH];
237
238 /*
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
242 */
243 bool no_hwmon;
244
245 int num_tbps; /* Number of tbp entries */
246 struct thermal_bind_params *tbp;
247
248 /*
249 * Sustainable power (heat) that this thermal zone can dissipate in
250 * mW
251 */
252 u32 sustainable_power;
253
254 /*
255 * Proportional parameter of the PID controller when
256 * overshooting (i.e., when temperature is below the target)
257 */
258 s32 k_po;
259
260 /*
261 * Proportional parameter of the PID controller when
262 * undershooting
263 */
264 s32 k_pu;
265
266 /* Integral parameter of the PID controller */
267 s32 k_i;
268
269 /* Derivative parameter of the PID controller */
270 s32 k_d;
271
272 /* threshold below which the error is no longer accumulated */
273 s32 integral_cutoff;
274
275 /*
276 * @slope: slope of a linear temperature adjustment curve.
277 * Used by thermal zone drivers.
278 */
279 int slope;
280 /*
281 * @offset: offset of a linear temperature adjustment curve.
282 * Used by thermal zone drivers (default 0).
283 */
284 int offset;
285 };
286
287 /**
288 * struct thermal_zone_of_device_ops - callbacks for handling DT based zones
289 *
290 * Mandatory:
291 * @get_temp: a pointer to a function that reads the sensor temperature.
292 *
293 * Optional:
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
299 * temperature.
300 * @set_trip_temp: a pointer to a function that sets the trip temperature on
301 * hardware.
302 */
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);
309 };
310
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,
315 u32 *id);
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);
326 #else
327
thermal_zone_of_get_sensor_id(struct device_node * tz_np,struct device_node * sensor_np,u32 * id)328 static inline int thermal_zone_of_get_sensor_id(struct device_node *tz_np,
329 struct device_node *sensor_np,
330 u32 *id)
331 {
332 return -ENOENT;
333 }
334 static inline struct thermal_zone_device *
thermal_zone_of_sensor_register(struct device * dev,int id,void * data,const struct thermal_zone_of_device_ops * ops)335 thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
336 const struct thermal_zone_of_device_ops *ops)
337 {
338 return ERR_PTR(-ENODEV);
339 }
340
341 static inline
thermal_zone_of_sensor_unregister(struct device * dev,struct thermal_zone_device * tz)342 void thermal_zone_of_sensor_unregister(struct device *dev,
343 struct thermal_zone_device *tz)
344 {
345 }
346
devm_thermal_zone_of_sensor_register(struct device * dev,int id,void * data,const struct thermal_zone_of_device_ops * ops)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)
350 {
351 return ERR_PTR(-ENODEV);
352 }
353
354 static inline
devm_thermal_zone_of_sensor_unregister(struct device * dev,struct thermal_zone_device * tz)355 void devm_thermal_zone_of_sensor_unregister(struct device *dev,
356 struct thermal_zone_device *tz)
357 {
358 }
359
360 #endif
361
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 *);
367
368 int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
369 struct thermal_cooling_device *,
370 unsigned long, unsigned long,
371 unsigned int);
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);
376
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);
392
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);
396 #else
thermal_zone_device_register(const char * type,int trips,int mask,void * devdata,struct thermal_zone_device_ops * ops,struct thermal_zone_params * tzp,int passive_delay,int polling_delay)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); }
thermal_zone_device_unregister(struct thermal_zone_device * tz)403 static inline void thermal_zone_device_unregister(
404 struct thermal_zone_device *tz)
405 { }
406 static inline struct thermal_cooling_device *
thermal_cooling_device_register(const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)407 thermal_cooling_device_register(const char *type, void *devdata,
408 const struct thermal_cooling_device_ops *ops)
409 { return ERR_PTR(-ENODEV); }
410 static inline struct thermal_cooling_device *
thermal_of_cooling_device_register(struct device_node * np,const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)411 thermal_of_cooling_device_register(struct device_node *np,
412 const char *type, void *devdata,
413 const struct thermal_cooling_device_ops *ops)
414 { return ERR_PTR(-ENODEV); }
415 static inline struct thermal_cooling_device *
devm_thermal_of_cooling_device_register(struct device * dev,struct device_node * np,char * type,void * devdata,const struct thermal_cooling_device_ops * ops)416 devm_thermal_of_cooling_device_register(struct device *dev,
417 struct device_node *np,
418 char *type, void *devdata,
419 const struct thermal_cooling_device_ops *ops)
420 {
421 return ERR_PTR(-ENODEV);
422 }
thermal_cooling_device_unregister(struct thermal_cooling_device * cdev)423 static inline void thermal_cooling_device_unregister(
424 struct thermal_cooling_device *cdev)
425 { }
thermal_zone_get_zone_by_name(const char * name)426 static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
427 const char *name)
428 { return ERR_PTR(-ENODEV); }
thermal_zone_get_temp(struct thermal_zone_device * tz,int * temp)429 static inline int thermal_zone_get_temp(
430 struct thermal_zone_device *tz, int *temp)
431 { return -ENODEV; }
thermal_zone_get_slope(struct thermal_zone_device * tz)432 static inline int thermal_zone_get_slope(
433 struct thermal_zone_device *tz)
434 { return -ENODEV; }
thermal_zone_get_offset(struct thermal_zone_device * tz)435 static inline int thermal_zone_get_offset(
436 struct thermal_zone_device *tz)
437 { return -ENODEV; }
438
thermal_zone_device_enable(struct thermal_zone_device * tz)439 static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
440 { return -ENODEV; }
441
thermal_zone_device_disable(struct thermal_zone_device * tz)442 static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
443 { return -ENODEV; }
444 #endif /* CONFIG_THERMAL */
445
446 #endif /* __THERMAL_H__ */
447