thermal: Add support for hardware-tracked trip points
[cascardo/linux.git] / include / linux / thermal.h
1 /*
2  *  thermal.h  ($Revision: 0 $)
3  *
4  *  Copyright (C) 2008  Intel Corp
5  *  Copyright (C) 2008  Zhang Rui <rui.zhang@intel.com>
6  *  Copyright (C) 2008  Sujith Thomas <sujith.thomas@intel.com>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; version 2 of the License.
12  *
13  *  This program is distributed in the hope that it will be useful, but
14  *  WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License along
19  *  with this program; if not, write to the Free Software Foundation, Inc.,
20  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
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/workqueue.h>
32 #include <uapi/linux/thermal.h>
33
34 #define THERMAL_TRIPS_NONE      -1
35 #define THERMAL_MAX_TRIPS       12
36
37 /* invalid cooling state */
38 #define THERMAL_CSTATE_INVALID -1UL
39
40 /* No upper/lower limit requirement */
41 #define THERMAL_NO_LIMIT        ((u32)~0)
42
43 /* Default weight of a bound cooling device */
44 #define THERMAL_WEIGHT_DEFAULT 0
45
46 /* use value, which < 0K, to indicate an invalid/uninitialized temperature */
47 #define THERMAL_TEMP_INVALID    -274000
48
49 /* Unit conversion macros */
50 #define DECI_KELVIN_TO_CELSIUS(t)       ({                      \
51         long _t = (t);                                          \
52         ((_t-2732 >= 0) ? (_t-2732+5)/10 : (_t-2732-5)/10);     \
53 })
54 #define CELSIUS_TO_DECI_KELVIN(t)       ((t)*10+2732)
55 #define DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, off) (((t) - (off)) * 100)
56 #define DECI_KELVIN_TO_MILLICELSIUS(t) DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, 2732)
57 #define MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, off) (((t) / 100) + (off))
58 #define MILLICELSIUS_TO_DECI_KELVIN(t) MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, 2732)
59
60 /* Default Thermal Governor */
61 #if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
62 #define DEFAULT_THERMAL_GOVERNOR       "step_wise"
63 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE)
64 #define DEFAULT_THERMAL_GOVERNOR       "fair_share"
65 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)
66 #define DEFAULT_THERMAL_GOVERNOR       "user_space"
67 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_POWER_ALLOCATOR)
68 #define DEFAULT_THERMAL_GOVERNOR       "power_allocator"
69 #endif
70
71 struct thermal_zone_device;
72 struct thermal_cooling_device;
73 struct thermal_instance;
74
75 enum thermal_device_mode {
76         THERMAL_DEVICE_DISABLED = 0,
77         THERMAL_DEVICE_ENABLED,
78 };
79
80 enum thermal_trip_type {
81         THERMAL_TRIP_ACTIVE = 0,
82         THERMAL_TRIP_PASSIVE,
83         THERMAL_TRIP_HOT,
84         THERMAL_TRIP_CRITICAL,
85 };
86
87 enum thermal_trend {
88         THERMAL_TREND_STABLE, /* temperature is stable */
89         THERMAL_TREND_RAISING, /* temperature is raising */
90         THERMAL_TREND_DROPPING, /* temperature is dropping */
91         THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */
92         THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */
93 };
94
95 struct thermal_zone_device_ops {
96         int (*bind) (struct thermal_zone_device *,
97                      struct thermal_cooling_device *);
98         int (*unbind) (struct thermal_zone_device *,
99                        struct thermal_cooling_device *);
100         int (*get_temp) (struct thermal_zone_device *, int *);
101         int (*set_trips) (struct thermal_zone_device *, int, int);
102         int (*get_mode) (struct thermal_zone_device *,
103                          enum thermal_device_mode *);
104         int (*set_mode) (struct thermal_zone_device *,
105                 enum thermal_device_mode);
106         int (*get_trip_type) (struct thermal_zone_device *, int,
107                 enum thermal_trip_type *);
108         int (*get_trip_temp) (struct thermal_zone_device *, int, int *);
109         int (*set_trip_temp) (struct thermal_zone_device *, int, int);
110         int (*get_trip_hyst) (struct thermal_zone_device *, int, int *);
111         int (*set_trip_hyst) (struct thermal_zone_device *, int, int);
112         int (*get_crit_temp) (struct thermal_zone_device *, int *);
113         int (*set_emul_temp) (struct thermal_zone_device *, int);
114         int (*get_trend) (struct thermal_zone_device *, int,
115                           enum thermal_trend *);
116         int (*notify) (struct thermal_zone_device *, int,
117                        enum thermal_trip_type);
118 };
119
120 struct thermal_cooling_device_ops {
121         int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
122         int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
123         int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
124         int (*get_requested_power)(struct thermal_cooling_device *,
125                                    struct thermal_zone_device *, u32 *);
126         int (*state2power)(struct thermal_cooling_device *,
127                            struct thermal_zone_device *, unsigned long, u32 *);
128         int (*power2state)(struct thermal_cooling_device *,
129                            struct thermal_zone_device *, u32, unsigned long *);
130 };
131
132 struct thermal_cooling_device {
133         int id;
134         char type[THERMAL_NAME_LENGTH];
135         struct device device;
136         struct device_node *np;
137         void *devdata;
138         const struct thermal_cooling_device_ops *ops;
139         bool updated; /* true if the cooling device does not need update */
140         struct mutex lock; /* protect thermal_instances list */
141         struct list_head thermal_instances;
142         struct list_head node;
143 };
144
145 struct thermal_attr {
146         struct device_attribute attr;
147         char name[THERMAL_NAME_LENGTH];
148 };
149
150 /**
151  * struct thermal_zone_device - structure for a thermal zone
152  * @id:         unique id number for each thermal zone
153  * @type:       the thermal zone device type
154  * @device:     &struct device for this thermal zone
155  * @trip_temp_attrs:    attributes for trip points for sysfs: trip temperature
156  * @trip_type_attrs:    attributes for trip points for sysfs: trip type
157  * @trip_hyst_attrs:    attributes for trip points for sysfs: trip hysteresis
158  * @devdata:    private pointer for device private data
159  * @trips:      number of trip points the thermal zone supports
160  * @trips_disabled;     bitmap for disabled trips
161  * @passive_delay:      number of milliseconds to wait between polls when
162  *                      performing passive cooling.
163  * @polling_delay:      number of milliseconds to wait between polls when
164  *                      checking whether trip points have been crossed (0 for
165  *                      interrupt driven systems)
166  * @temperature:        current temperature.  This is only for core code,
167  *                      drivers should use thermal_zone_get_temp() to get the
168  *                      current temperature
169  * @last_temperature:   previous temperature read
170  * @emul_temperature:   emulated temperature when using CONFIG_THERMAL_EMULATION
171  * @passive:            1 if you've crossed a passive trip point, 0 otherwise.
172  * @prev_low_trip:      the low current temperature if you've crossed a passive
173                         trip point.
174  * @prev_high_trip:     the above current temperature if you've crossed a
175                         passive trip point.
176  * @forced_passive:     If > 0, temperature at which to switch on all ACPI
177  *                      processor cooling devices.  Currently only used by the
178  *                      step-wise governor.
179  * @need_update:        if equals 1, thermal_zone_device_update needs to be invoked.
180  * @ops:        operations this &thermal_zone_device supports
181  * @tzp:        thermal zone parameters
182  * @governor:   pointer to the governor for this thermal zone
183  * @governor_data:      private pointer for governor data
184  * @thermal_instances:  list of &struct thermal_instance of this thermal zone
185  * @idr:        &struct idr to generate unique id for this zone's cooling
186  *              devices
187  * @lock:       lock to protect thermal_instances list
188  * @node:       node in thermal_tz_list (in thermal_core.c)
189  * @poll_queue: delayed work for polling
190  */
191 struct thermal_zone_device {
192         int id;
193         char type[THERMAL_NAME_LENGTH];
194         struct device device;
195         struct thermal_attr *trip_temp_attrs;
196         struct thermal_attr *trip_type_attrs;
197         struct thermal_attr *trip_hyst_attrs;
198         void *devdata;
199         int trips;
200         unsigned long trips_disabled;   /* bitmap for disabled trips */
201         int passive_delay;
202         int polling_delay;
203         int temperature;
204         int last_temperature;
205         int emul_temperature;
206         int passive;
207         int prev_low_trip;
208         int prev_high_trip;
209         unsigned int forced_passive;
210         atomic_t need_update;
211         struct thermal_zone_device_ops *ops;
212         struct thermal_zone_params *tzp;
213         struct thermal_governor *governor;
214         void *governor_data;
215         struct list_head thermal_instances;
216         struct idr idr;
217         struct mutex lock;
218         struct list_head node;
219         struct delayed_work poll_queue;
220 };
221
222 /**
223  * struct thermal_governor - structure that holds thermal governor information
224  * @name:       name of the governor
225  * @bind_to_tz: callback called when binding to a thermal zone.  If it
226  *              returns 0, the governor is bound to the thermal zone,
227  *              otherwise it fails.
228  * @unbind_from_tz:     callback called when a governor is unbound from a
229  *                      thermal zone.
230  * @throttle:   callback called for every trip point even if temperature is
231  *              below the trip point temperature
232  * @governor_list:      node in thermal_governor_list (in thermal_core.c)
233  */
234 struct thermal_governor {
235         char name[THERMAL_NAME_LENGTH];
236         int (*bind_to_tz)(struct thermal_zone_device *tz);
237         void (*unbind_from_tz)(struct thermal_zone_device *tz);
238         int (*throttle)(struct thermal_zone_device *tz, int trip);
239         struct list_head        governor_list;
240 };
241
242 /* Structure that holds binding parameters for a zone */
243 struct thermal_bind_params {
244         struct thermal_cooling_device *cdev;
245
246         /*
247          * This is a measure of 'how effectively these devices can
248          * cool 'this' thermal zone. It shall be determined by
249          * platform characterization. This value is relative to the
250          * rest of the weights so a cooling device whose weight is
251          * double that of another cooling device is twice as
252          * effective. See Documentation/thermal/sysfs-api.txt for more
253          * information.
254          */
255         int weight;
256
257         /*
258          * This is a bit mask that gives the binding relation between this
259          * thermal zone and cdev, for a particular trip point.
260          * See Documentation/thermal/sysfs-api.txt for more information.
261          */
262         int trip_mask;
263
264         /*
265          * This is an array of cooling state limits. Must have exactly
266          * 2 * thermal_zone.number_of_trip_points. It is an array consisting
267          * of tuples <lower-state upper-state> of state limits. Each trip
268          * will be associated with one state limit tuple when binding.
269          * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS>
270          * on all trips.
271          */
272         unsigned long *binding_limits;
273         int (*match) (struct thermal_zone_device *tz,
274                         struct thermal_cooling_device *cdev);
275 };
276
277 /* Structure to define Thermal Zone parameters */
278 struct thermal_zone_params {
279         char governor_name[THERMAL_NAME_LENGTH];
280
281         /*
282          * a boolean to indicate if the thermal to hwmon sysfs interface
283          * is required. when no_hwmon == false, a hwmon sysfs interface
284          * will be created. when no_hwmon == true, nothing will be done
285          */
286         bool no_hwmon;
287
288         int num_tbps;   /* Number of tbp entries */
289         struct thermal_bind_params *tbp;
290
291         /*
292          * Sustainable power (heat) that this thermal zone can dissipate in
293          * mW
294          */
295         u32 sustainable_power;
296
297         /*
298          * Proportional parameter of the PID controller when
299          * overshooting (i.e., when temperature is below the target)
300          */
301         s32 k_po;
302
303         /*
304          * Proportional parameter of the PID controller when
305          * undershooting
306          */
307         s32 k_pu;
308
309         /* Integral parameter of the PID controller */
310         s32 k_i;
311
312         /* Derivative parameter of the PID controller */
313         s32 k_d;
314
315         /* threshold below which the error is no longer accumulated */
316         s32 integral_cutoff;
317
318         /*
319          * @slope:      slope of a linear temperature adjustment curve.
320          *              Used by thermal zone drivers.
321          */
322         int slope;
323         /*
324          * @offset:     offset of a linear temperature adjustment curve.
325          *              Used by thermal zone drivers (default 0).
326          */
327         int offset;
328 };
329
330 struct thermal_genl_event {
331         u32 orig;
332         enum events event;
333 };
334
335 /**
336  * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones
337  *
338  * Mandatory:
339  * @get_temp: a pointer to a function that reads the sensor temperature.
340  *
341  * Optional:
342  * @get_trend: a pointer to a function that reads the sensor temperature trend.
343  * @set_emul_temp: a pointer to a function that sets sensor emulated
344  *                 temperature.
345  * @set_trip_temp: a pointer to a function that sets the trip temperature on
346  *                 hardware.
347  */
348 struct thermal_zone_of_device_ops {
349         int (*get_temp)(void *, int *);
350         int (*get_trend)(void *, long *);
351         int (*set_emul_temp)(void *, int);
352         int (*set_trip_temp)(void *, int, int);
353 };
354
355 /**
356  * struct thermal_trip - representation of a point in temperature domain
357  * @np: pointer to struct device_node that this trip point was created from
358  * @temperature: temperature value in miliCelsius
359  * @hysteresis: relative hysteresis in miliCelsius
360  * @type: trip point type
361  */
362
363 struct thermal_trip {
364         struct device_node *np;
365         int temperature;
366         int hysteresis;
367         enum thermal_trip_type type;
368 };
369
370 /* Function declarations */
371 #ifdef CONFIG_THERMAL_OF
372 struct thermal_zone_device *
373 thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
374                                 const struct thermal_zone_of_device_ops *ops);
375 void thermal_zone_of_sensor_unregister(struct device *dev,
376                                        struct thermal_zone_device *tz);
377 struct thermal_zone_device *devm_thermal_zone_of_sensor_register(
378                 struct device *dev, int id, void *data,
379                 const struct thermal_zone_of_device_ops *ops);
380 void devm_thermal_zone_of_sensor_unregister(struct device *dev,
381                                             struct thermal_zone_device *tz);
382 #else
383 static inline struct thermal_zone_device *
384 thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
385                                 const struct thermal_zone_of_device_ops *ops)
386 {
387         return ERR_PTR(-ENODEV);
388 }
389
390 static inline
391 void thermal_zone_of_sensor_unregister(struct device *dev,
392                                        struct thermal_zone_device *tz)
393 {
394 }
395
396 static inline struct 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)
399 {
400         return ERR_PTR(-ENODEV);
401 }
402
403 static inline
404 void devm_thermal_zone_of_sensor_unregister(struct device *dev,
405                                             struct thermal_zone_device *tz)
406 {
407 }
408
409 #endif
410
411 #if IS_ENABLED(CONFIG_THERMAL)
412 static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
413 {
414         return cdev->ops->get_requested_power && cdev->ops->state2power &&
415                 cdev->ops->power2state;
416 }
417
418 int power_actor_get_max_power(struct thermal_cooling_device *,
419                               struct thermal_zone_device *tz, u32 *max_power);
420 int power_actor_get_min_power(struct thermal_cooling_device *,
421                               struct thermal_zone_device *tz, u32 *min_power);
422 int power_actor_set_power(struct thermal_cooling_device *,
423                           struct thermal_instance *, u32);
424 struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
425                 void *, struct thermal_zone_device_ops *,
426                 struct thermal_zone_params *, int, int);
427 void thermal_zone_device_unregister(struct thermal_zone_device *);
428
429 int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
430                                      struct thermal_cooling_device *,
431                                      unsigned long, unsigned long,
432                                      unsigned int);
433 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
434                                        struct thermal_cooling_device *);
435 void thermal_zone_device_update(struct thermal_zone_device *);
436 void thermal_zone_set_trips(struct thermal_zone_device *);
437
438 struct thermal_cooling_device *thermal_cooling_device_register(char *, void *,
439                 const struct thermal_cooling_device_ops *);
440 struct thermal_cooling_device *
441 thermal_of_cooling_device_register(struct device_node *np, char *, void *,
442                                    const struct thermal_cooling_device_ops *);
443 void thermal_cooling_device_unregister(struct thermal_cooling_device *);
444 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
445 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
446 int thermal_zone_get_slope(struct thermal_zone_device *tz);
447 int thermal_zone_get_offset(struct thermal_zone_device *tz);
448
449 int get_tz_trend(struct thermal_zone_device *, int);
450 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *,
451                 struct thermal_cooling_device *, int);
452 void thermal_cdev_update(struct thermal_cooling_device *);
453 void thermal_notify_framework(struct thermal_zone_device *, int);
454 #else
455 static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
456 { return false; }
457 static inline int power_actor_get_max_power(struct thermal_cooling_device *cdev,
458                               struct thermal_zone_device *tz, u32 *max_power)
459 { return 0; }
460 static inline int power_actor_get_min_power(struct thermal_cooling_device *cdev,
461                                             struct thermal_zone_device *tz,
462                                             u32 *min_power)
463 { return -ENODEV; }
464 static inline int power_actor_set_power(struct thermal_cooling_device *cdev,
465                           struct thermal_instance *tz, u32 power)
466 { return 0; }
467 static inline struct thermal_zone_device *thermal_zone_device_register(
468         const char *type, int trips, int mask, void *devdata,
469         struct thermal_zone_device_ops *ops,
470         const struct thermal_zone_params *tzp,
471         int passive_delay, int polling_delay)
472 { return ERR_PTR(-ENODEV); }
473 static inline void thermal_zone_device_unregister(
474         struct thermal_zone_device *tz)
475 { }
476 static inline int thermal_zone_bind_cooling_device(
477         struct thermal_zone_device *tz, int trip,
478         struct thermal_cooling_device *cdev,
479         unsigned long upper, unsigned long lower,
480         unsigned int weight)
481 { return -ENODEV; }
482 static inline int thermal_zone_unbind_cooling_device(
483         struct thermal_zone_device *tz, int trip,
484         struct thermal_cooling_device *cdev)
485 { return -ENODEV; }
486 static inline void thermal_zone_device_update(struct thermal_zone_device *tz)
487 { }
488 static inline void thermal_zone_set_trips(struct thermal_zone_device *tz)
489 { }
490 static inline struct thermal_cooling_device *
491 thermal_cooling_device_register(char *type, void *devdata,
492         const struct thermal_cooling_device_ops *ops)
493 { return ERR_PTR(-ENODEV); }
494 static inline struct thermal_cooling_device *
495 thermal_of_cooling_device_register(struct device_node *np,
496         char *type, void *devdata, const struct thermal_cooling_device_ops *ops)
497 { return ERR_PTR(-ENODEV); }
498 static inline void thermal_cooling_device_unregister(
499         struct thermal_cooling_device *cdev)
500 { }
501 static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
502                 const char *name)
503 { return ERR_PTR(-ENODEV); }
504 static inline int thermal_zone_get_temp(
505                 struct thermal_zone_device *tz, int *temp)
506 { return -ENODEV; }
507 static inline int thermal_zone_get_slope(
508                 struct thermal_zone_device *tz)
509 { return -ENODEV; }
510 static inline int thermal_zone_get_offset(
511                 struct thermal_zone_device *tz)
512 { return -ENODEV; }
513 static inline int get_tz_trend(struct thermal_zone_device *tz, int trip)
514 { return -ENODEV; }
515 static inline struct thermal_instance *
516 get_thermal_instance(struct thermal_zone_device *tz,
517         struct thermal_cooling_device *cdev, int trip)
518 { return ERR_PTR(-ENODEV); }
519 static inline void thermal_cdev_update(struct thermal_cooling_device *cdev)
520 { }
521 static inline void thermal_notify_framework(struct thermal_zone_device *tz,
522         int trip)
523 { }
524 #endif /* CONFIG_THERMAL */
525
526 #if defined(CONFIG_NET) && IS_ENABLED(CONFIG_THERMAL)
527 extern int thermal_generate_netlink_event(struct thermal_zone_device *tz,
528                                                 enum events event);
529 #else
530 static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz,
531                                                 enum events event)
532 {
533         return 0;
534 }
535 #endif
536
537 #endif /* __THERMAL_H__ */