HID: input: avoid polling stylus battery on Chromebook Pompom
[sfrench/cifs-2.6.git] / include / linux / thermal.h
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 /* invalid cooling state */
21 #define THERMAL_CSTATE_INVALID -1UL
22
23 /* No upper/lower limit requirement */
24 #define THERMAL_NO_LIMIT        ((u32)~0)
25
26 /* Default weight of a bound cooling device */
27 #define THERMAL_WEIGHT_DEFAULT 0
28
29 /* use value, which < 0K, to indicate an invalid/uninitialized temperature */
30 #define THERMAL_TEMP_INVALID    -274000
31
32 struct thermal_zone_device;
33 struct thermal_cooling_device;
34 struct thermal_instance;
35 struct thermal_attr;
36
37 enum thermal_trend {
38         THERMAL_TREND_STABLE, /* temperature is stable */
39         THERMAL_TREND_RAISING, /* temperature is raising */
40         THERMAL_TREND_DROPPING, /* temperature is dropping */
41 };
42
43 /* Thermal notification reason */
44 enum thermal_notify_event {
45         THERMAL_EVENT_UNSPECIFIED, /* Unspecified event */
46         THERMAL_EVENT_TEMP_SAMPLE, /* New Temperature sample */
47         THERMAL_TRIP_VIOLATED, /* TRIP Point violation */
48         THERMAL_TRIP_CHANGED, /* TRIP Point temperature changed */
49         THERMAL_DEVICE_DOWN, /* Thermal device is down */
50         THERMAL_DEVICE_UP, /* Thermal device is up after a down event */
51         THERMAL_DEVICE_POWER_CAPABILITY_CHANGED, /* power capability changed */
52         THERMAL_TABLE_CHANGED, /* Thermal table(s) changed */
53         THERMAL_EVENT_KEEP_ALIVE, /* Request for user space handler to respond */
54         THERMAL_TZ_BIND_CDEV, /* Cooling dev is bind to the thermal zone */
55         THERMAL_TZ_UNBIND_CDEV, /* Cooling dev is unbind from the thermal zone */
56         THERMAL_INSTANCE_WEIGHT_CHANGED, /* Thermal instance weight changed */
57 };
58
59 /**
60  * struct thermal_trip - representation of a point in temperature domain
61  * @temperature: temperature value in miliCelsius
62  * @hysteresis: relative hysteresis in miliCelsius
63  * @threshold: trip crossing notification threshold miliCelsius
64  * @type: trip point type
65  * @priv: pointer to driver data associated with this trip
66  */
67 struct thermal_trip {
68         int temperature;
69         int hysteresis;
70         int threshold;
71         enum thermal_trip_type type;
72         void *priv;
73 };
74
75 struct thermal_zone_device_ops {
76         int (*bind) (struct thermal_zone_device *,
77                      struct thermal_cooling_device *);
78         int (*unbind) (struct thermal_zone_device *,
79                        struct thermal_cooling_device *);
80         int (*get_temp) (struct thermal_zone_device *, int *);
81         int (*set_trips) (struct thermal_zone_device *, int, int);
82         int (*change_mode) (struct thermal_zone_device *,
83                 enum thermal_device_mode);
84         int (*set_trip_temp) (struct thermal_zone_device *, int, int);
85         int (*set_trip_hyst) (struct thermal_zone_device *, int, int);
86         int (*get_crit_temp) (struct thermal_zone_device *, int *);
87         int (*set_emul_temp) (struct thermal_zone_device *, int);
88         int (*get_trend) (struct thermal_zone_device *,
89                           const struct thermal_trip *, enum thermal_trend *);
90         void (*hot)(struct thermal_zone_device *);
91         void (*critical)(struct thermal_zone_device *);
92 };
93
94 struct thermal_cooling_device_ops {
95         int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
96         int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
97         int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
98         int (*get_requested_power)(struct thermal_cooling_device *, u32 *);
99         int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *);
100         int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *);
101 };
102
103 struct thermal_cooling_device {
104         int id;
105         char *type;
106         unsigned long max_state;
107         struct device device;
108         struct device_node *np;
109         void *devdata;
110         void *stats;
111         const struct thermal_cooling_device_ops *ops;
112         bool updated; /* true if the cooling device does not need update */
113         struct mutex lock; /* protect thermal_instances list */
114         struct list_head thermal_instances;
115         struct list_head node;
116 };
117
118 /**
119  * struct thermal_zone_device - structure for a thermal zone
120  * @id:         unique id number for each thermal zone
121  * @type:       the thermal zone device type
122  * @device:     &struct device for this thermal zone
123  * @removal:    removal completion
124  * @trip_temp_attrs:    attributes for trip points for sysfs: trip temperature
125  * @trip_type_attrs:    attributes for trip points for sysfs: trip type
126  * @trip_hyst_attrs:    attributes for trip points for sysfs: trip hysteresis
127  * @mode:               current mode of this thermal zone
128  * @devdata:    private pointer for device private data
129  * @trips:      an array of struct thermal_trip
130  * @num_trips:  number of trip points the thermal zone supports
131  * @passive_delay_jiffies: number of jiffies to wait between polls when
132  *                      performing passive cooling.
133  * @polling_delay_jiffies: number of jiffies to wait between polls when
134  *                      checking whether trip points have been crossed (0 for
135  *                      interrupt driven systems)
136  * @temperature:        current temperature.  This is only for core code,
137  *                      drivers should use thermal_zone_get_temp() to get the
138  *                      current temperature
139  * @last_temperature:   previous temperature read
140  * @emul_temperature:   emulated temperature when using CONFIG_THERMAL_EMULATION
141  * @passive:            1 if you've crossed a passive trip point, 0 otherwise.
142  * @prev_low_trip:      the low current temperature if you've crossed a passive
143                         trip point.
144  * @prev_high_trip:     the above current temperature if you've crossed a
145                         passive trip point.
146  * @need_update:        if equals 1, thermal_zone_device_update needs to be invoked.
147  * @ops:        operations this &thermal_zone_device supports
148  * @tzp:        thermal zone parameters
149  * @governor:   pointer to the governor for this thermal zone
150  * @governor_data:      private pointer for governor data
151  * @thermal_instances:  list of &struct thermal_instance of this thermal zone
152  * @ida:        &struct ida to generate unique id for this zone's cooling
153  *              devices
154  * @lock:       lock to protect thermal_instances list
155  * @node:       node in thermal_tz_list (in thermal_core.c)
156  * @poll_queue: delayed work for polling
157  * @notify_event: Last notification event
158  * @suspended: thermal zone suspend indicator
159  */
160 struct thermal_zone_device {
161         int id;
162         char type[THERMAL_NAME_LENGTH];
163         struct device device;
164         struct completion removal;
165         struct attribute_group trips_attribute_group;
166         struct thermal_attr *trip_temp_attrs;
167         struct thermal_attr *trip_type_attrs;
168         struct thermal_attr *trip_hyst_attrs;
169         enum thermal_device_mode mode;
170         void *devdata;
171         struct thermal_trip *trips;
172         int num_trips;
173         unsigned long passive_delay_jiffies;
174         unsigned long polling_delay_jiffies;
175         int temperature;
176         int last_temperature;
177         int emul_temperature;
178         int passive;
179         int prev_low_trip;
180         int prev_high_trip;
181         atomic_t need_update;
182         struct thermal_zone_device_ops *ops;
183         struct thermal_zone_params *tzp;
184         struct thermal_governor *governor;
185         void *governor_data;
186         struct list_head thermal_instances;
187         struct ida ida;
188         struct mutex lock;
189         struct list_head node;
190         struct delayed_work poll_queue;
191         enum thermal_notify_event notify_event;
192         bool suspended;
193 };
194
195 /**
196  * struct thermal_governor - structure that holds thermal governor information
197  * @name:       name of the governor
198  * @bind_to_tz: callback called when binding to a thermal zone.  If it
199  *              returns 0, the governor is bound to the thermal zone,
200  *              otherwise it fails.
201  * @unbind_from_tz:     callback called when a governor is unbound from a
202  *                      thermal zone.
203  * @throttle:   callback called for every trip point even if temperature is
204  *              below the trip point temperature
205  * @update_tz:  callback called when thermal zone internals have changed, e.g.
206  *              thermal cooling instance was added/removed
207  * @governor_list:      node in thermal_governor_list (in thermal_core.c)
208  */
209 struct thermal_governor {
210         char name[THERMAL_NAME_LENGTH];
211         int (*bind_to_tz)(struct thermal_zone_device *tz);
212         void (*unbind_from_tz)(struct thermal_zone_device *tz);
213         int (*throttle)(struct thermal_zone_device *tz,
214                         const struct thermal_trip *trip);
215         void (*update_tz)(struct thermal_zone_device *tz,
216                           enum thermal_notify_event reason);
217         struct list_head        governor_list;
218 };
219
220 /* Structure to define Thermal Zone parameters */
221 struct thermal_zone_params {
222         char governor_name[THERMAL_NAME_LENGTH];
223
224         /*
225          * a boolean to indicate if the thermal to hwmon sysfs interface
226          * is required. when no_hwmon == false, a hwmon sysfs interface
227          * will be created. when no_hwmon == true, nothing will be done
228          */
229         bool no_hwmon;
230
231         /*
232          * Sustainable power (heat) that this thermal zone can dissipate in
233          * mW
234          */
235         u32 sustainable_power;
236
237         /*
238          * Proportional parameter of the PID controller when
239          * overshooting (i.e., when temperature is below the target)
240          */
241         s32 k_po;
242
243         /*
244          * Proportional parameter of the PID controller when
245          * undershooting
246          */
247         s32 k_pu;
248
249         /* Integral parameter of the PID controller */
250         s32 k_i;
251
252         /* Derivative parameter of the PID controller */
253         s32 k_d;
254
255         /* threshold below which the error is no longer accumulated */
256         s32 integral_cutoff;
257
258         /*
259          * @slope:      slope of a linear temperature adjustment curve.
260          *              Used by thermal zone drivers.
261          */
262         int slope;
263         /*
264          * @offset:     offset of a linear temperature adjustment curve.
265          *              Used by thermal zone drivers (default 0).
266          */
267         int offset;
268 };
269
270 /* Function declarations */
271 #ifdef CONFIG_THERMAL_OF
272 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
273                                                           const struct thermal_zone_device_ops *ops);
274
275 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz);
276
277 #else
278
279 static inline
280 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
281                                                           const struct thermal_zone_device_ops *ops)
282 {
283         return ERR_PTR(-ENOTSUPP);
284 }
285
286 static inline void devm_thermal_of_zone_unregister(struct device *dev,
287                                                    struct thermal_zone_device *tz)
288 {
289 }
290 #endif
291
292 int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
293                             struct thermal_trip *trip);
294 int thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
295                           struct thermal_trip *trip);
296 int for_each_thermal_trip(struct thermal_zone_device *tz,
297                           int (*cb)(struct thermal_trip *, void *),
298                           void *data);
299 int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
300                                int (*cb)(struct thermal_trip *, void *),
301                                void *data);
302 int thermal_zone_get_num_trips(struct thermal_zone_device *tz);
303 void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
304                                 struct thermal_trip *trip, int temp);
305
306 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);
307
308 #ifdef CONFIG_THERMAL
309 struct thermal_zone_device *thermal_zone_device_register_with_trips(
310                                         const char *type,
311                                         struct thermal_trip *trips,
312                                         int num_trips, int mask,
313                                         void *devdata,
314                                         struct thermal_zone_device_ops *ops,
315                                         const struct thermal_zone_params *tzp,
316                                         int passive_delay, int polling_delay);
317
318 struct thermal_zone_device *thermal_tripless_zone_device_register(
319                                         const char *type,
320                                         void *devdata,
321                                         struct thermal_zone_device_ops *ops,
322                                         const struct thermal_zone_params *tzp);
323
324 void thermal_zone_device_unregister(struct thermal_zone_device *tz);
325
326 void *thermal_zone_device_priv(struct thermal_zone_device *tzd);
327 const char *thermal_zone_device_type(struct thermal_zone_device *tzd);
328 int thermal_zone_device_id(struct thermal_zone_device *tzd);
329 struct device *thermal_zone_device(struct thermal_zone_device *tzd);
330
331 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz,
332                               const struct thermal_trip *trip,
333                               struct thermal_cooling_device *cdev,
334                               unsigned long upper, unsigned long lower,
335                               unsigned int weight);
336 int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
337                                      struct thermal_cooling_device *,
338                                      unsigned long, unsigned long,
339                                      unsigned int);
340 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz,
341                                   const struct thermal_trip *trip,
342                                   struct thermal_cooling_device *cdev);
343 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
344                                        struct thermal_cooling_device *);
345 void thermal_zone_device_update(struct thermal_zone_device *,
346                                 enum thermal_notify_event);
347
348 struct thermal_cooling_device *thermal_cooling_device_register(const char *,
349                 void *, const struct thermal_cooling_device_ops *);
350 struct thermal_cooling_device *
351 thermal_of_cooling_device_register(struct device_node *np, const char *, void *,
352                                    const struct thermal_cooling_device_ops *);
353 struct thermal_cooling_device *
354 devm_thermal_of_cooling_device_register(struct device *dev,
355                                 struct device_node *np,
356                                 char *type, void *devdata,
357                                 const struct thermal_cooling_device_ops *ops);
358 void thermal_cooling_device_update(struct thermal_cooling_device *);
359 void thermal_cooling_device_unregister(struct thermal_cooling_device *);
360 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
361 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
362 int thermal_zone_get_slope(struct thermal_zone_device *tz);
363 int thermal_zone_get_offset(struct thermal_zone_device *tz);
364
365 int thermal_zone_device_enable(struct thermal_zone_device *tz);
366 int thermal_zone_device_disable(struct thermal_zone_device *tz);
367 void thermal_zone_device_critical(struct thermal_zone_device *tz);
368 #else
369 static inline struct thermal_zone_device *thermal_zone_device_register_with_trips(
370                                         const char *type,
371                                         struct thermal_trip *trips,
372                                         int num_trips, int mask,
373                                         void *devdata,
374                                         struct thermal_zone_device_ops *ops,
375                                         const struct thermal_zone_params *tzp,
376                                         int passive_delay, int polling_delay)
377 { return ERR_PTR(-ENODEV); }
378
379 static inline struct thermal_zone_device *thermal_tripless_zone_device_register(
380                                         const char *type,
381                                         void *devdata,
382                                         struct thermal_zone_device_ops *ops,
383                                         const struct thermal_zone_params *tzp)
384 { return ERR_PTR(-ENODEV); }
385
386 static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz)
387 { }
388
389 static inline struct thermal_cooling_device *
390 thermal_cooling_device_register(const char *type, void *devdata,
391         const struct thermal_cooling_device_ops *ops)
392 { return ERR_PTR(-ENODEV); }
393 static inline struct thermal_cooling_device *
394 thermal_of_cooling_device_register(struct device_node *np,
395         const char *type, void *devdata,
396         const struct thermal_cooling_device_ops *ops)
397 { return ERR_PTR(-ENODEV); }
398 static inline struct thermal_cooling_device *
399 devm_thermal_of_cooling_device_register(struct device *dev,
400                                 struct device_node *np,
401                                 char *type, void *devdata,
402                                 const struct thermal_cooling_device_ops *ops)
403 {
404         return ERR_PTR(-ENODEV);
405 }
406 static inline void thermal_cooling_device_unregister(
407         struct thermal_cooling_device *cdev)
408 { }
409 static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
410                 const char *name)
411 { return ERR_PTR(-ENODEV); }
412 static inline int thermal_zone_get_temp(
413                 struct thermal_zone_device *tz, int *temp)
414 { return -ENODEV; }
415 static inline int thermal_zone_get_slope(
416                 struct thermal_zone_device *tz)
417 { return -ENODEV; }
418 static inline int thermal_zone_get_offset(
419                 struct thermal_zone_device *tz)
420 { return -ENODEV; }
421
422 static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz)
423 {
424         return NULL;
425 }
426
427 static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
428 {
429         return NULL;
430 }
431
432 static inline int thermal_zone_device_id(struct thermal_zone_device *tzd)
433 {
434         return -ENODEV;
435 }
436
437 static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
438 { return -ENODEV; }
439
440 static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
441 { return -ENODEV; }
442 #endif /* CONFIG_THERMAL */
443
444 #endif /* __THERMAL_H__ */