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_debugfs;
36  struct thermal_attr;
37  
38  enum thermal_trend {
39  	THERMAL_TREND_STABLE, /* temperature is stable */
40  	THERMAL_TREND_RAISING, /* temperature is raising */
41  	THERMAL_TREND_DROPPING, /* temperature is dropping */
42  };
43  
44  /* Thermal notification reason */
45  enum thermal_notify_event {
46  	THERMAL_EVENT_UNSPECIFIED, /* Unspecified event */
47  	THERMAL_EVENT_TEMP_SAMPLE, /* New Temperature sample */
48  	THERMAL_TRIP_VIOLATED, /* TRIP Point violation */
49  	THERMAL_TRIP_CHANGED, /* TRIP Point temperature changed */
50  	THERMAL_DEVICE_DOWN, /* Thermal device is down */
51  	THERMAL_DEVICE_UP, /* Thermal device is up after a down event */
52  	THERMAL_DEVICE_POWER_CAPABILITY_CHANGED, /* power capability changed */
53  	THERMAL_TABLE_CHANGED, /* Thermal table(s) changed */
54  	THERMAL_EVENT_KEEP_ALIVE, /* Request for user space handler to respond */
55  	THERMAL_TZ_BIND_CDEV, /* Cooling dev is bind to the thermal zone */
56  	THERMAL_TZ_UNBIND_CDEV, /* Cooling dev is unbind from the thermal zone */
57  	THERMAL_INSTANCE_WEIGHT_CHANGED, /* Thermal instance weight changed */
58  	THERMAL_TZ_RESUME, /* Thermal zone is resuming after system sleep */
59  };
60  
61  /**
62   * struct thermal_trip - representation of a point in temperature domain
63   * @temperature: temperature value in miliCelsius
64   * @hysteresis: relative hysteresis in miliCelsius
65   * @type: trip point type
66   * @priv: pointer to driver data associated with this trip
67   * @flags: flags representing binary properties of the trip
68   */
69  struct thermal_trip {
70  	int temperature;
71  	int hysteresis;
72  	enum thermal_trip_type type;
73  	u8 flags;
74  	void *priv;
75  };
76  
77  #define THERMAL_TRIP_FLAG_RW_TEMP	BIT(0)
78  #define THERMAL_TRIP_FLAG_RW_HYST	BIT(1)
79  
80  #define THERMAL_TRIP_FLAG_RW	(THERMAL_TRIP_FLAG_RW_TEMP | \
81  				 THERMAL_TRIP_FLAG_RW_HYST)
82  
83  #define THERMAL_TRIP_PRIV_TO_INT(_val_)	(uintptr_t)(_val_)
84  #define THERMAL_INT_TO_TRIP_PRIV(_val_)	(void *)(uintptr_t)(_val_)
85  
86  struct thermal_zone_device;
87  
88  struct cooling_spec {
89  	unsigned long upper;	/* Highest cooling state  */
90  	unsigned long lower;	/* Lowest cooling state  */
91  	unsigned int weight;	/* Cooling device weight */
92  };
93  
94  struct thermal_zone_device_ops {
95  	bool (*should_bind) (struct thermal_zone_device *,
96  			     const struct thermal_trip *,
97  			     struct thermal_cooling_device *,
98  			     struct cooling_spec *);
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 (*set_trip_temp) (struct thermal_zone_device *,
104  			      const struct thermal_trip *, int);
105  	int (*get_crit_temp) (struct thermal_zone_device *, int *);
106  	int (*set_emul_temp) (struct thermal_zone_device *, int);
107  	int (*get_trend) (struct thermal_zone_device *,
108  			  const struct thermal_trip *, enum thermal_trend *);
109  	void (*hot)(struct thermal_zone_device *);
110  	void (*critical)(struct thermal_zone_device *);
111  };
112  
113  struct thermal_cooling_device_ops {
114  	int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
115  	int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
116  	int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
117  	int (*get_requested_power)(struct thermal_cooling_device *, u32 *);
118  	int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *);
119  	int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *);
120  };
121  
122  struct thermal_cooling_device {
123  	int id;
124  	const char *type;
125  	unsigned long max_state;
126  	struct device device;
127  	struct device_node *np;
128  	void *devdata;
129  	void *stats;
130  	const struct thermal_cooling_device_ops *ops;
131  	bool updated; /* true if the cooling device does not need update */
132  	struct mutex lock; /* protect thermal_instances list */
133  	struct list_head thermal_instances;
134  	struct list_head node;
135  #ifdef CONFIG_THERMAL_DEBUGFS
136  	struct thermal_debugfs *debugfs;
137  #endif
138  };
139  
140  /* Structure to define Thermal Zone parameters */
141  struct thermal_zone_params {
142  	const char *governor_name;
143  
144  	/*
145  	 * a boolean to indicate if the thermal to hwmon sysfs interface
146  	 * is required. when no_hwmon == false, a hwmon sysfs interface
147  	 * will be created. when no_hwmon == true, nothing will be done
148  	 */
149  	bool no_hwmon;
150  
151  	/*
152  	 * Sustainable power (heat) that this thermal zone can dissipate in
153  	 * mW
154  	 */
155  	u32 sustainable_power;
156  
157  	/*
158  	 * Proportional parameter of the PID controller when
159  	 * overshooting (i.e., when temperature is below the target)
160  	 */
161  	s32 k_po;
162  
163  	/*
164  	 * Proportional parameter of the PID controller when
165  	 * undershooting
166  	 */
167  	s32 k_pu;
168  
169  	/* Integral parameter of the PID controller */
170  	s32 k_i;
171  
172  	/* Derivative parameter of the PID controller */
173  	s32 k_d;
174  
175  	/* threshold below which the error is no longer accumulated */
176  	s32 integral_cutoff;
177  
178  	/*
179  	 * @slope:	slope of a linear temperature adjustment curve.
180  	 * 		Used by thermal zone drivers.
181  	 */
182  	int slope;
183  	/*
184  	 * @offset:	offset of a linear temperature adjustment curve.
185  	 * 		Used by thermal zone drivers (default 0).
186  	 */
187  	int offset;
188  };
189  
190  /* Function declarations */
191  #ifdef CONFIG_THERMAL_OF
192  struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
193  							  const struct thermal_zone_device_ops *ops);
194  
195  void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz);
196  
197  #else
198  
199  static inline
devm_thermal_of_zone_register(struct device * dev,int id,void * data,const struct thermal_zone_device_ops * ops)200  struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data,
201  							  const struct thermal_zone_device_ops *ops)
202  {
203  	return ERR_PTR(-ENOTSUPP);
204  }
205  
devm_thermal_of_zone_unregister(struct device * dev,struct thermal_zone_device * tz)206  static inline void devm_thermal_of_zone_unregister(struct device *dev,
207  						   struct thermal_zone_device *tz)
208  {
209  }
210  #endif
211  
212  int for_each_thermal_trip(struct thermal_zone_device *tz,
213  			  int (*cb)(struct thermal_trip *, void *),
214  			  void *data);
215  int thermal_zone_for_each_trip(struct thermal_zone_device *tz,
216  			       int (*cb)(struct thermal_trip *, void *),
217  			       void *data);
218  void thermal_zone_set_trip_temp(struct thermal_zone_device *tz,
219  				struct thermal_trip *trip, int temp);
220  
221  int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp);
222  
223  #ifdef CONFIG_THERMAL
224  struct thermal_zone_device *thermal_zone_device_register_with_trips(
225  					const char *type,
226  					const struct thermal_trip *trips,
227  					int num_trips, void *devdata,
228  					const struct thermal_zone_device_ops *ops,
229  					const struct thermal_zone_params *tzp,
230  					unsigned int passive_delay,
231  					unsigned int polling_delay);
232  
233  struct thermal_zone_device *thermal_tripless_zone_device_register(
234  					const char *type,
235  					void *devdata,
236  					const struct thermal_zone_device_ops *ops,
237  					const struct thermal_zone_params *tzp);
238  
239  void thermal_zone_device_unregister(struct thermal_zone_device *tz);
240  
241  void *thermal_zone_device_priv(struct thermal_zone_device *tzd);
242  const char *thermal_zone_device_type(struct thermal_zone_device *tzd);
243  int thermal_zone_device_id(struct thermal_zone_device *tzd);
244  struct device *thermal_zone_device(struct thermal_zone_device *tzd);
245  
246  void thermal_zone_device_update(struct thermal_zone_device *,
247  				enum thermal_notify_event);
248  
249  struct thermal_cooling_device *thermal_cooling_device_register(const char *,
250  		void *, const struct thermal_cooling_device_ops *);
251  struct thermal_cooling_device *
252  thermal_of_cooling_device_register(struct device_node *np, const char *, void *,
253  				   const struct thermal_cooling_device_ops *);
254  struct thermal_cooling_device *
255  devm_thermal_of_cooling_device_register(struct device *dev,
256  				struct device_node *np,
257  				const char *type, void *devdata,
258  				const struct thermal_cooling_device_ops *ops);
259  void thermal_cooling_device_update(struct thermal_cooling_device *);
260  void thermal_cooling_device_unregister(struct thermal_cooling_device *);
261  struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
262  int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
263  int thermal_zone_get_slope(struct thermal_zone_device *tz);
264  int thermal_zone_get_offset(struct thermal_zone_device *tz);
265  bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz,
266  				   const struct thermal_trip *trip,
267  				   struct thermal_cooling_device *cdev);
268  
269  int thermal_zone_device_enable(struct thermal_zone_device *tz);
270  int thermal_zone_device_disable(struct thermal_zone_device *tz);
271  void thermal_zone_device_critical(struct thermal_zone_device *tz);
272  #else
thermal_zone_device_register_with_trips(const char * type,const struct thermal_trip * trips,int num_trips,void * devdata,const struct thermal_zone_device_ops * ops,const struct thermal_zone_params * tzp,int passive_delay,int polling_delay)273  static inline struct thermal_zone_device *thermal_zone_device_register_with_trips(
274  					const char *type,
275  					const struct thermal_trip *trips,
276  					int num_trips, void *devdata,
277  					const struct thermal_zone_device_ops *ops,
278  					const struct thermal_zone_params *tzp,
279  					int passive_delay, int polling_delay)
280  { return ERR_PTR(-ENODEV); }
281  
thermal_tripless_zone_device_register(const char * type,void * devdata,struct thermal_zone_device_ops * ops,const struct thermal_zone_params * tzp)282  static inline struct thermal_zone_device *thermal_tripless_zone_device_register(
283  					const char *type,
284  					void *devdata,
285  					struct thermal_zone_device_ops *ops,
286  					const struct thermal_zone_params *tzp)
287  { return ERR_PTR(-ENODEV); }
288  
thermal_zone_device_unregister(struct thermal_zone_device * tz)289  static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz)
290  { }
291  
292  static inline struct thermal_cooling_device *
thermal_cooling_device_register(const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)293  thermal_cooling_device_register(const char *type, void *devdata,
294  	const struct thermal_cooling_device_ops *ops)
295  { return ERR_PTR(-ENODEV); }
296  static inline struct thermal_cooling_device *
thermal_of_cooling_device_register(struct device_node * np,const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)297  thermal_of_cooling_device_register(struct device_node *np,
298  	const char *type, void *devdata,
299  	const struct thermal_cooling_device_ops *ops)
300  { return ERR_PTR(-ENODEV); }
301  static inline struct thermal_cooling_device *
devm_thermal_of_cooling_device_register(struct device * dev,struct device_node * np,const char * type,void * devdata,const struct thermal_cooling_device_ops * ops)302  devm_thermal_of_cooling_device_register(struct device *dev,
303  				struct device_node *np,
304  				const char *type, void *devdata,
305  				const struct thermal_cooling_device_ops *ops)
306  {
307  	return ERR_PTR(-ENODEV);
308  }
thermal_cooling_device_unregister(struct thermal_cooling_device * cdev)309  static inline void thermal_cooling_device_unregister(
310  	struct thermal_cooling_device *cdev)
311  { }
thermal_zone_get_zone_by_name(const char * name)312  static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
313  		const char *name)
314  { return ERR_PTR(-ENODEV); }
thermal_zone_get_temp(struct thermal_zone_device * tz,int * temp)315  static inline int thermal_zone_get_temp(
316  		struct thermal_zone_device *tz, int *temp)
317  { return -ENODEV; }
thermal_zone_get_slope(struct thermal_zone_device * tz)318  static inline int thermal_zone_get_slope(
319  		struct thermal_zone_device *tz)
320  { return -ENODEV; }
thermal_zone_get_offset(struct thermal_zone_device * tz)321  static inline int thermal_zone_get_offset(
322  		struct thermal_zone_device *tz)
323  { return -ENODEV; }
324  
thermal_zone_device_priv(struct thermal_zone_device * tz)325  static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz)
326  {
327  	return NULL;
328  }
329  
thermal_zone_device_type(struct thermal_zone_device * tzd)330  static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd)
331  {
332  	return NULL;
333  }
334  
thermal_zone_device_id(struct thermal_zone_device * tzd)335  static inline int thermal_zone_device_id(struct thermal_zone_device *tzd)
336  {
337  	return -ENODEV;
338  }
339  
thermal_zone_device_enable(struct thermal_zone_device * tz)340  static inline int thermal_zone_device_enable(struct thermal_zone_device *tz)
341  { return -ENODEV; }
342  
thermal_zone_device_disable(struct thermal_zone_device * tz)343  static inline int thermal_zone_device_disable(struct thermal_zone_device *tz)
344  { return -ENODEV; }
345  #endif /* CONFIG_THERMAL */
346  
347  #endif /* __THERMAL_H__ */
348