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