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