1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#ifndef __THERMAL_H__
26#define __THERMAL_H__
27
28#include <linux/of.h>
29#include <linux/idr.h>
30#include <linux/device.h>
31#include <linux/sysfs.h>
32#include <linux/workqueue.h>
33#include <uapi/linux/thermal.h>
34
35#define THERMAL_TRIPS_NONE -1
36#define THERMAL_MAX_TRIPS 12
37
38
39#define THERMAL_CSTATE_INVALID -1UL
40
41
42#define THERMAL_NO_LIMIT ((u32)~0)
43
44
45#define THERMAL_WEIGHT_DEFAULT 0
46
47
48#define THERMAL_TEMP_INVALID -274000
49
50
51#define DECI_KELVIN_TO_CELSIUS(t) ({ \
52 long _t = (t); \
53 ((_t-2732 >= 0) ? (_t-2732+5)/10 : (_t-2732-5)/10); \
54})
55#define CELSIUS_TO_DECI_KELVIN(t) ((t)*10+2732)
56#define DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, off) (((t) - (off)) * 100)
57#define DECI_KELVIN_TO_MILLICELSIUS(t) DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, 2732)
58#define MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, off) (((t) / 100) + (off))
59#define MILLICELSIUS_TO_DECI_KELVIN(t) MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, 2732)
60
61
62#if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
63#define DEFAULT_THERMAL_GOVERNOR "step_wise"
64#elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE)
65#define DEFAULT_THERMAL_GOVERNOR "fair_share"
66#elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)
67#define DEFAULT_THERMAL_GOVERNOR "user_space"
68#elif defined(CONFIG_THERMAL_DEFAULT_GOV_POWER_ALLOCATOR)
69#define DEFAULT_THERMAL_GOVERNOR "power_allocator"
70#endif
71
72struct thermal_zone_device;
73struct thermal_cooling_device;
74struct thermal_instance;
75
76enum thermal_device_mode {
77 THERMAL_DEVICE_DISABLED = 0,
78 THERMAL_DEVICE_ENABLED,
79};
80
81enum thermal_trip_type {
82 THERMAL_TRIP_ACTIVE = 0,
83 THERMAL_TRIP_PASSIVE,
84 THERMAL_TRIP_HOT,
85 THERMAL_TRIP_CRITICAL,
86};
87
88enum thermal_trend {
89 THERMAL_TREND_STABLE,
90 THERMAL_TREND_RAISING,
91 THERMAL_TREND_DROPPING,
92 THERMAL_TREND_RAISE_FULL,
93 THERMAL_TREND_DROP_FULL,
94};
95
96
97enum thermal_notify_event {
98 THERMAL_EVENT_UNSPECIFIED,
99 THERMAL_EVENT_TEMP_SAMPLE,
100 THERMAL_TRIP_VIOLATED,
101 THERMAL_TRIP_CHANGED,
102 THERMAL_DEVICE_DOWN,
103 THERMAL_DEVICE_UP,
104 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED,
105};
106
107struct thermal_zone_device_ops {
108 int (*bind) (struct thermal_zone_device *,
109 struct thermal_cooling_device *);
110 int (*unbind) (struct thermal_zone_device *,
111 struct thermal_cooling_device *);
112 int (*get_temp) (struct thermal_zone_device *, int *);
113 int (*set_trips) (struct thermal_zone_device *, int, int);
114 int (*get_mode) (struct thermal_zone_device *,
115 enum thermal_device_mode *);
116 int (*set_mode) (struct thermal_zone_device *,
117 enum thermal_device_mode);
118 int (*get_trip_type) (struct thermal_zone_device *, int,
119 enum thermal_trip_type *);
120 int (*get_trip_temp) (struct thermal_zone_device *, int, int *);
121 int (*set_trip_temp) (struct thermal_zone_device *, int, int);
122 int (*get_trip_hyst) (struct thermal_zone_device *, int, int *);
123 int (*set_trip_hyst) (struct thermal_zone_device *, int, int);
124 int (*get_crit_temp) (struct thermal_zone_device *, int *);
125 int (*set_emul_temp) (struct thermal_zone_device *, int);
126 int (*get_trend) (struct thermal_zone_device *, int,
127 enum thermal_trend *);
128 int (*notify) (struct thermal_zone_device *, int,
129 enum thermal_trip_type);
130};
131
132struct thermal_cooling_device_ops {
133 int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
134 int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
135 int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
136 int (*get_requested_power)(struct thermal_cooling_device *,
137 struct thermal_zone_device *, u32 *);
138 int (*state2power)(struct thermal_cooling_device *,
139 struct thermal_zone_device *, unsigned long, u32 *);
140 int (*power2state)(struct thermal_cooling_device *,
141 struct thermal_zone_device *, u32, unsigned long *);
142};
143
144struct thermal_cooling_device {
145 int id;
146 char type[THERMAL_NAME_LENGTH];
147 struct device device;
148 struct device_node *np;
149 void *devdata;
150 const struct thermal_cooling_device_ops *ops;
151 bool updated;
152 struct mutex lock;
153 struct list_head thermal_instances;
154 struct list_head node;
155};
156
157struct thermal_attr {
158 struct device_attribute attr;
159 char name[THERMAL_NAME_LENGTH];
160};
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204struct thermal_zone_device {
205 int id;
206 char type[THERMAL_NAME_LENGTH];
207 struct device device;
208 struct attribute_group trips_attribute_group;
209 struct thermal_attr *trip_temp_attrs;
210 struct thermal_attr *trip_type_attrs;
211 struct thermal_attr *trip_hyst_attrs;
212 void *devdata;
213 int trips;
214 unsigned long trips_disabled;
215 int passive_delay;
216 int polling_delay;
217 int temperature;
218 int last_temperature;
219 int emul_temperature;
220 int passive;
221 int prev_low_trip;
222 int prev_high_trip;
223 unsigned int forced_passive;
224 atomic_t need_update;
225 struct thermal_zone_device_ops *ops;
226 struct thermal_zone_params *tzp;
227 struct thermal_governor *governor;
228 void *governor_data;
229 struct list_head thermal_instances;
230 struct ida ida;
231 struct mutex lock;
232 struct list_head node;
233 struct delayed_work poll_queue;
234 enum thermal_notify_event notify_event;
235};
236
237
238
239
240
241
242
243
244
245
246
247
248
249struct thermal_governor {
250 char name[THERMAL_NAME_LENGTH];
251 int (*bind_to_tz)(struct thermal_zone_device *tz);
252 void (*unbind_from_tz)(struct thermal_zone_device *tz);
253 int (*throttle)(struct thermal_zone_device *tz, int trip);
254 struct list_head governor_list;
255};
256
257
258struct thermal_bind_params {
259 struct thermal_cooling_device *cdev;
260
261
262
263
264
265
266
267
268
269
270 int weight;
271
272
273
274
275
276
277 int trip_mask;
278
279
280
281
282
283
284
285
286
287 unsigned long *binding_limits;
288 int (*match) (struct thermal_zone_device *tz,
289 struct thermal_cooling_device *cdev);
290};
291
292
293struct thermal_zone_params {
294 char governor_name[THERMAL_NAME_LENGTH];
295
296
297
298
299
300
301 bool no_hwmon;
302
303 int num_tbps;
304 struct thermal_bind_params *tbp;
305
306
307
308
309
310 u32 sustainable_power;
311
312
313
314
315
316 s32 k_po;
317
318
319
320
321
322 s32 k_pu;
323
324
325 s32 k_i;
326
327
328 s32 k_d;
329
330
331 s32 integral_cutoff;
332
333
334
335
336
337 int slope;
338
339
340
341
342 int offset;
343};
344
345struct thermal_genl_event {
346 u32 orig;
347 enum events event;
348};
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366struct thermal_zone_of_device_ops {
367 int (*get_temp)(void *, int *);
368 int (*get_trend)(void *, int, enum thermal_trend *);
369 int (*set_trips)(void *, int, int);
370 int (*set_emul_temp)(void *, int);
371 int (*set_trip_temp)(void *, int, int);
372};
373
374
375
376
377
378
379
380
381
382struct thermal_trip {
383 struct device_node *np;
384 int temperature;
385 int hysteresis;
386 enum thermal_trip_type type;
387};
388
389
390#ifdef CONFIG_THERMAL_OF
391struct thermal_zone_device *
392thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
393 const struct thermal_zone_of_device_ops *ops);
394void thermal_zone_of_sensor_unregister(struct device *dev,
395 struct thermal_zone_device *tz);
396struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
397 struct device *dev, int id, void *data,
398 const struct thermal_zone_of_device_ops *ops);
399void devm_thermal_zone_of_sensor_unregister(struct device *dev,
400 struct thermal_zone_device *tz);
401#else
402static inline struct thermal_zone_device *
403thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
404 const struct thermal_zone_of_device_ops *ops)
405{
406 return ERR_PTR(-ENODEV);
407}
408
409static inline
410void thermal_zone_of_sensor_unregister(struct device *dev,
411 struct thermal_zone_device *tz)
412{
413}
414
415static inline struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
416 struct device *dev, int id, void *data,
417 const struct thermal_zone_of_device_ops *ops)
418{
419 return ERR_PTR(-ENODEV);
420}
421
422static inline
423void devm_thermal_zone_of_sensor_unregister(struct device *dev,
424 struct thermal_zone_device *tz)
425{
426}
427
428#endif
429
430#if IS_ENABLED(CONFIG_THERMAL)
431static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
432{
433 return cdev->ops->get_requested_power && cdev->ops->state2power &&
434 cdev->ops->power2state;
435}
436
437int power_actor_get_max_power(struct thermal_cooling_device *,
438 struct thermal_zone_device *tz, u32 *max_power);
439int power_actor_get_min_power(struct thermal_cooling_device *,
440 struct thermal_zone_device *tz, u32 *min_power);
441int power_actor_set_power(struct thermal_cooling_device *,
442 struct thermal_instance *, u32);
443struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
444 void *, struct thermal_zone_device_ops *,
445 struct thermal_zone_params *, int, int);
446void thermal_zone_device_unregister(struct thermal_zone_device *);
447
448int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
449 struct thermal_cooling_device *,
450 unsigned long, unsigned long,
451 unsigned int);
452int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
453 struct thermal_cooling_device *);
454void thermal_zone_device_update(struct thermal_zone_device *,
455 enum thermal_notify_event);
456void thermal_zone_set_trips(struct thermal_zone_device *);
457
458struct thermal_cooling_device *thermal_cooling_device_register(char *, void *,
459 const struct thermal_cooling_device_ops *);
460struct thermal_cooling_device *
461thermal_of_cooling_device_register(struct device_node *np, char *, void *,
462 const struct thermal_cooling_device_ops *);
463void thermal_cooling_device_unregister(struct thermal_cooling_device *);
464struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
465int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
466int thermal_zone_get_slope(struct thermal_zone_device *tz);
467int thermal_zone_get_offset(struct thermal_zone_device *tz);
468
469int get_tz_trend(struct thermal_zone_device *, int);
470struct thermal_instance *get_thermal_instance(struct thermal_zone_device *,
471 struct thermal_cooling_device *, int);
472void thermal_cdev_update(struct thermal_cooling_device *);
473void thermal_notify_framework(struct thermal_zone_device *, int);
474#else
475static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
476{ return false; }
477static inline int power_actor_get_max_power(struct thermal_cooling_device *cdev,
478 struct thermal_zone_device *tz, u32 *max_power)
479{ return 0; }
480static inline int power_actor_get_min_power(struct thermal_cooling_device *cdev,
481 struct thermal_zone_device *tz,
482 u32 *min_power)
483{ return -ENODEV; }
484static inline int power_actor_set_power(struct thermal_cooling_device *cdev,
485 struct thermal_instance *tz, u32 power)
486{ return 0; }
487static inline struct thermal_zone_device *thermal_zone_device_register(
488 const char *type, int trips, int mask, void *devdata,
489 struct thermal_zone_device_ops *ops,
490 const struct thermal_zone_params *tzp,
491 int passive_delay, int polling_delay)
492{ return ERR_PTR(-ENODEV); }
493static inline void thermal_zone_device_unregister(
494 struct thermal_zone_device *tz)
495{ }
496static inline int thermal_zone_bind_cooling_device(
497 struct thermal_zone_device *tz, int trip,
498 struct thermal_cooling_device *cdev,
499 unsigned long upper, unsigned long lower,
500 unsigned int weight)
501{ return -ENODEV; }
502static inline int thermal_zone_unbind_cooling_device(
503 struct thermal_zone_device *tz, int trip,
504 struct thermal_cooling_device *cdev)
505{ return -ENODEV; }
506static inline void thermal_zone_device_update(struct thermal_zone_device *tz,
507 enum thermal_notify_event event)
508{ }
509static inline void thermal_zone_set_trips(struct thermal_zone_device *tz)
510{ }
511static inline struct thermal_cooling_device *
512thermal_cooling_device_register(char *type, void *devdata,
513 const struct thermal_cooling_device_ops *ops)
514{ return ERR_PTR(-ENODEV); }
515static inline struct thermal_cooling_device *
516thermal_of_cooling_device_register(struct device_node *np,
517 char *type, void *devdata, const struct thermal_cooling_device_ops *ops)
518{ return ERR_PTR(-ENODEV); }
519static inline void thermal_cooling_device_unregister(
520 struct thermal_cooling_device *cdev)
521{ }
522static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
523 const char *name)
524{ return ERR_PTR(-ENODEV); }
525static inline int thermal_zone_get_temp(
526 struct thermal_zone_device *tz, int *temp)
527{ return -ENODEV; }
528static inline int thermal_zone_get_slope(
529 struct thermal_zone_device *tz)
530{ return -ENODEV; }
531static inline int thermal_zone_get_offset(
532 struct thermal_zone_device *tz)
533{ return -ENODEV; }
534static inline int get_tz_trend(struct thermal_zone_device *tz, int trip)
535{ return -ENODEV; }
536static inline struct thermal_instance *
537get_thermal_instance(struct thermal_zone_device *tz,
538 struct thermal_cooling_device *cdev, int trip)
539{ return ERR_PTR(-ENODEV); }
540static inline void thermal_cdev_update(struct thermal_cooling_device *cdev)
541{ }
542static inline void thermal_notify_framework(struct thermal_zone_device *tz,
543 int trip)
544{ }
545#endif
546
547#if defined(CONFIG_NET) && IS_ENABLED(CONFIG_THERMAL)
548extern int thermal_generate_netlink_event(struct thermal_zone_device *tz,
549 enum events event);
550#else
551static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz,
552 enum events event)
553{
554 return 0;
555}
556#endif
557
558#endif
559