]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - include/linux/thermal.h
Merge tag 'mfd-for-linus-4.4' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd
[mirror_ubuntu-zesty-kernel.git] / include / linux / thermal.h
CommitLineData
203d3d4a
ZR
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
a116b5d4 28#include <linux/of.h>
203d3d4a
ZR
29#include <linux/idr.h>
30#include <linux/device.h>
b1569e99 31#include <linux/workqueue.h>
af6c9f16 32#include <uapi/linux/thermal.h>
203d3d4a 33
23064088
D
34#define THERMAL_TRIPS_NONE -1
35#define THERMAL_MAX_TRIPS 12
23064088 36
57df8106
ZR
37/* invalid cooling state */
38#define THERMAL_CSTATE_INVALID -1UL
39
23064088 40/* No upper/lower limit requirement */
a940cb34 41#define THERMAL_NO_LIMIT ((u32)~0)
23064088 42
6cd9e9f6
KS
43/* Default weight of a bound cooling device */
44#define THERMAL_WEIGHT_DEFAULT 0
45
23064088
D
46/* Unit conversion macros */
47#define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732 >= 0) ? \
48 ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
49#define CELSIUS_TO_KELVIN(t) ((t)*10+2732)
7b83fd9d
AL
50#define DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, off) (((t) - (off)) * 100)
51#define DECI_KELVIN_TO_MILLICELSIUS(t) DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, 2732)
77e337c6
AL
52#define MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, off) (((t) / 100) + (off))
53#define MILLICELSIUS_TO_DECI_KELVIN(t) MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, 2732)
23064088 54
1f53ef17
ZR
55/* Default Thermal Governor */
56#if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
57#define DEFAULT_THERMAL_GOVERNOR "step_wise"
58#elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE)
59#define DEFAULT_THERMAL_GOVERNOR "fair_share"
60#elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)
61#define DEFAULT_THERMAL_GOVERNOR "user_space"
6b775e87
JM
62#elif defined(CONFIG_THERMAL_DEFAULT_GOV_POWER_ALLOCATOR)
63#define DEFAULT_THERMAL_GOVERNOR "power_allocator"
1f53ef17 64#endif
a4a15485 65
203d3d4a
ZR
66struct thermal_zone_device;
67struct thermal_cooling_device;
35b11d2e 68struct thermal_instance;
203d3d4a 69
6503e5df
MG
70enum thermal_device_mode {
71 THERMAL_DEVICE_DISABLED = 0,
72 THERMAL_DEVICE_ENABLED,
73};
74
75enum thermal_trip_type {
76 THERMAL_TRIP_ACTIVE = 0,
77 THERMAL_TRIP_PASSIVE,
78 THERMAL_TRIP_HOT,
79 THERMAL_TRIP_CRITICAL,
80};
81
601f3d42
ZR
82enum thermal_trend {
83 THERMAL_TREND_STABLE, /* temperature is stable */
84 THERMAL_TREND_RAISING, /* temperature is raising */
85 THERMAL_TREND_DROPPING, /* temperature is dropping */
d069015e
ZR
86 THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */
87 THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */
601f3d42
ZR
88};
89
203d3d4a
ZR
90struct thermal_zone_device_ops {
91 int (*bind) (struct thermal_zone_device *,
92 struct thermal_cooling_device *);
93 int (*unbind) (struct thermal_zone_device *,
94 struct thermal_cooling_device *);
17e8351a 95 int (*get_temp) (struct thermal_zone_device *, int *);
6503e5df
MG
96 int (*get_mode) (struct thermal_zone_device *,
97 enum thermal_device_mode *);
98 int (*set_mode) (struct thermal_zone_device *,
99 enum thermal_device_mode);
100 int (*get_trip_type) (struct thermal_zone_device *, int,
101 enum thermal_trip_type *);
17e8351a
SH
102 int (*get_trip_temp) (struct thermal_zone_device *, int, int *);
103 int (*set_trip_temp) (struct thermal_zone_device *, int, int);
104 int (*get_trip_hyst) (struct thermal_zone_device *, int, int *);
105 int (*set_trip_hyst) (struct thermal_zone_device *, int, int);
106 int (*get_crit_temp) (struct thermal_zone_device *, int *);
107 int (*set_emul_temp) (struct thermal_zone_device *, int);
601f3d42
ZR
108 int (*get_trend) (struct thermal_zone_device *, int,
109 enum thermal_trend *);
b1569e99
MG
110 int (*notify) (struct thermal_zone_device *, int,
111 enum thermal_trip_type);
203d3d4a
ZR
112};
113
114struct thermal_cooling_device_ops {
6503e5df
MG
115 int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
116 int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
117 int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
35b11d2e
JM
118 int (*get_requested_power)(struct thermal_cooling_device *,
119 struct thermal_zone_device *, u32 *);
120 int (*state2power)(struct thermal_cooling_device *,
121 struct thermal_zone_device *, unsigned long, u32 *);
122 int (*power2state)(struct thermal_cooling_device *,
123 struct thermal_zone_device *, u32, unsigned long *);
203d3d4a
ZR
124};
125
203d3d4a
ZR
126struct thermal_cooling_device {
127 int id;
128 char type[THERMAL_NAME_LENGTH];
129 struct device device;
4e5e4705 130 struct device_node *np;
203d3d4a 131 void *devdata;
5b275ce2 132 const struct thermal_cooling_device_ops *ops;
ce119f83 133 bool updated; /* true if the cooling device does not need update */
f4a821ce 134 struct mutex lock; /* protect thermal_instances list */
b5e4ae62 135 struct list_head thermal_instances;
203d3d4a
ZR
136 struct list_head node;
137};
138
c56f5c03
D
139struct thermal_attr {
140 struct device_attribute attr;
141 char name[THERMAL_NAME_LENGTH];
142};
143
c708a98f
JM
144/**
145 * struct thermal_zone_device - structure for a thermal zone
146 * @id: unique id number for each thermal zone
147 * @type: the thermal zone device type
148 * @device: &struct device for this thermal zone
149 * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
150 * @trip_type_attrs: attributes for trip points for sysfs: trip type
151 * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
152 * @devdata: private pointer for device private data
153 * @trips: number of trip points the thermal zone supports
154 * @passive_delay: number of milliseconds to wait between polls when
6b775e87 155 * performing passive cooling.
c708a98f
JM
156 * @polling_delay: number of milliseconds to wait between polls when
157 * checking whether trip points have been crossed (0 for
158 * interrupt driven systems)
159 * @temperature: current temperature. This is only for core code,
160 * drivers should use thermal_zone_get_temp() to get the
161 * current temperature
162 * @last_temperature: previous temperature read
163 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
164 * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
c708a98f
JM
165 * @forced_passive: If > 0, temperature at which to switch on all ACPI
166 * processor cooling devices. Currently only used by the
167 * step-wise governor.
168 * @ops: operations this &thermal_zone_device supports
169 * @tzp: thermal zone parameters
170 * @governor: pointer to the governor for this thermal zone
e33df1d2 171 * @governor_data: private pointer for governor data
c708a98f
JM
172 * @thermal_instances: list of &struct thermal_instance of this thermal zone
173 * @idr: &struct idr to generate unique id for this zone's cooling
174 * devices
175 * @lock: lock to protect thermal_instances list
176 * @node: node in thermal_tz_list (in thermal_core.c)
177 * @poll_queue: delayed work for polling
178 */
203d3d4a
ZR
179struct thermal_zone_device {
180 int id;
181 char type[THERMAL_NAME_LENGTH];
182 struct device device;
c56f5c03
D
183 struct thermal_attr *trip_temp_attrs;
184 struct thermal_attr *trip_type_attrs;
27365a6c 185 struct thermal_attr *trip_hyst_attrs;
203d3d4a
ZR
186 void *devdata;
187 int trips;
b1569e99
MG
188 int passive_delay;
189 int polling_delay;
601f3d42 190 int temperature;
b1569e99 191 int last_temperature;
e6e238c3 192 int emul_temperature;
908b9fb7 193 int passive;
03a971a2 194 unsigned int forced_passive;
4e5e4705 195 struct thermal_zone_device_ops *ops;
6b775e87 196 struct thermal_zone_params *tzp;
a4a15485 197 struct thermal_governor *governor;
e33df1d2 198 void *governor_data;
2d374139 199 struct list_head thermal_instances;
203d3d4a 200 struct idr idr;
c708a98f 201 struct mutex lock;
203d3d4a 202 struct list_head node;
b1569e99 203 struct delayed_work poll_queue;
203d3d4a 204};
4cb18728 205
c708a98f
JM
206/**
207 * struct thermal_governor - structure that holds thermal governor information
208 * @name: name of the governor
e33df1d2
JM
209 * @bind_to_tz: callback called when binding to a thermal zone. If it
210 * returns 0, the governor is bound to the thermal zone,
211 * otherwise it fails.
212 * @unbind_from_tz: callback called when a governor is unbound from a
213 * thermal zone.
c708a98f
JM
214 * @throttle: callback called for every trip point even if temperature is
215 * below the trip point temperature
216 * @governor_list: node in thermal_governor_list (in thermal_core.c)
217 */
a4a15485
D
218struct thermal_governor {
219 char name[THERMAL_NAME_LENGTH];
e33df1d2
JM
220 int (*bind_to_tz)(struct thermal_zone_device *tz);
221 void (*unbind_from_tz)(struct thermal_zone_device *tz);
a4a15485
D
222 int (*throttle)(struct thermal_zone_device *tz, int trip);
223 struct list_head governor_list;
a4a15485
D
224};
225
ef873947
D
226/* Structure that holds binding parameters for a zone */
227struct thermal_bind_params {
228 struct thermal_cooling_device *cdev;
229
230 /*
231 * This is a measure of 'how effectively these devices can
bcdcbbc7
JM
232 * cool 'this' thermal zone. It shall be determined by
233 * platform characterization. This value is relative to the
234 * rest of the weights so a cooling device whose weight is
235 * double that of another cooling device is twice as
236 * effective. See Documentation/thermal/sysfs-api.txt for more
237 * information.
ef873947
D
238 */
239 int weight;
240
241 /*
242 * This is a bit mask that gives the binding relation between this
243 * thermal zone and cdev, for a particular trip point.
244 * See Documentation/thermal/sysfs-api.txt for more information.
245 */
246 int trip_mask;
a8892d83
EV
247
248 /*
249 * This is an array of cooling state limits. Must have exactly
250 * 2 * thermal_zone.number_of_trip_points. It is an array consisting
251 * of tuples <lower-state upper-state> of state limits. Each trip
252 * will be associated with one state limit tuple when binding.
253 * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS>
254 * on all trips.
255 */
256 unsigned long *binding_limits;
ef873947
D
257 int (*match) (struct thermal_zone_device *tz,
258 struct thermal_cooling_device *cdev);
259};
260
261/* Structure to define Thermal Zone parameters */
262struct thermal_zone_params {
a4a15485 263 char governor_name[THERMAL_NAME_LENGTH];
ccba4ffd
EV
264
265 /*
266 * a boolean to indicate if the thermal to hwmon sysfs interface
267 * is required. when no_hwmon == false, a hwmon sysfs interface
268 * will be created. when no_hwmon == true, nothing will be done
269 */
270 bool no_hwmon;
271
ef873947
D
272 int num_tbps; /* Number of tbp entries */
273 struct thermal_bind_params *tbp;
6b775e87
JM
274
275 /*
276 * Sustainable power (heat) that this thermal zone can dissipate in
277 * mW
278 */
279 u32 sustainable_power;
280
281 /*
282 * Proportional parameter of the PID controller when
283 * overshooting (i.e., when temperature is below the target)
284 */
285 s32 k_po;
286
287 /*
288 * Proportional parameter of the PID controller when
289 * undershooting
290 */
291 s32 k_pu;
292
293 /* Integral parameter of the PID controller */
294 s32 k_i;
295
296 /* Derivative parameter of the PID controller */
297 s32 k_d;
298
299 /* threshold below which the error is no longer accumulated */
300 s32 integral_cutoff;
9d0be7f4
EV
301
302 /*
303 * @slope: slope of a linear temperature adjustment curve.
304 * Used by thermal zone drivers.
305 */
306 int slope;
307 /*
308 * @offset: offset of a linear temperature adjustment curve.
309 * Used by thermal zone drivers (default 0).
310 */
311 int offset;
ef873947
D
312};
313
4cb18728
D
314struct thermal_genl_event {
315 u32 orig;
316 enum events event;
317};
203d3d4a 318
2251aef6
EV
319/**
320 * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones
321 *
322 * Mandatory:
323 * @get_temp: a pointer to a function that reads the sensor temperature.
324 *
325 * Optional:
326 * @get_trend: a pointer to a function that reads the sensor temperature trend.
184a4bf6
LM
327 * @set_emul_temp: a pointer to a function that sets sensor emulated
328 * temperature.
2251aef6
EV
329 */
330struct thermal_zone_of_device_ops {
17e8351a 331 int (*get_temp)(void *, int *);
2251aef6 332 int (*get_trend)(void *, long *);
17e8351a 333 int (*set_emul_temp)(void *, int);
2251aef6
EV
334};
335
ad9914ac
LM
336/**
337 * struct thermal_trip - representation of a point in temperature domain
338 * @np: pointer to struct device_node that this trip point was created from
339 * @temperature: temperature value in miliCelsius
340 * @hysteresis: relative hysteresis in miliCelsius
341 * @type: trip point type
342 */
343
344struct thermal_trip {
345 struct device_node *np;
346 unsigned long int temperature;
347 unsigned long int hysteresis;
348 enum thermal_trip_type type;
349};
350
23064088 351/* Function declarations */
4e5e4705
EV
352#ifdef CONFIG_THERMAL_OF
353struct thermal_zone_device *
2251aef6
EV
354thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
355 const struct thermal_zone_of_device_ops *ops);
4e5e4705
EV
356void thermal_zone_of_sensor_unregister(struct device *dev,
357 struct thermal_zone_device *tz);
358#else
359static inline struct thermal_zone_device *
2251aef6
EV
360thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
361 const struct thermal_zone_of_device_ops *ops)
4e5e4705 362{
cd33dc9a 363 return ERR_PTR(-ENODEV);
4e5e4705
EV
364}
365
366static inline
367void thermal_zone_of_sensor_unregister(struct device *dev,
368 struct thermal_zone_device *tz)
369{
370}
371
372#endif
12ca7188
NM
373
374#if IS_ENABLED(CONFIG_THERMAL)
35b11d2e
JM
375static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
376{
377 return cdev->ops->get_requested_power && cdev->ops->state2power &&
378 cdev->ops->power2state;
379}
380
381int power_actor_get_max_power(struct thermal_cooling_device *,
382 struct thermal_zone_device *tz, u32 *max_power);
c973c3bc
JM
383int power_actor_get_min_power(struct thermal_cooling_device *,
384 struct thermal_zone_device *tz, u32 *min_power);
35b11d2e
JM
385int power_actor_set_power(struct thermal_cooling_device *,
386 struct thermal_instance *, u32);
4b1bf587 387struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
4e5e4705 388 void *, struct thermal_zone_device_ops *,
6b775e87 389 struct thermal_zone_params *, int, int);
203d3d4a
ZR
390void thermal_zone_device_unregister(struct thermal_zone_device *);
391
392int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
9d99842f 393 struct thermal_cooling_device *,
6cd9e9f6
KS
394 unsigned long, unsigned long,
395 unsigned int);
203d3d4a
ZR
396int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
397 struct thermal_cooling_device *);
b1569e99 398void thermal_zone_device_update(struct thermal_zone_device *);
23064088 399
203d3d4a 400struct thermal_cooling_device *thermal_cooling_device_register(char *, void *,
5b275ce2 401 const struct thermal_cooling_device_ops *);
a116b5d4
EV
402struct thermal_cooling_device *
403thermal_of_cooling_device_register(struct device_node *np, char *, void *,
404 const struct thermal_cooling_device_ops *);
203d3d4a 405void thermal_cooling_device_unregister(struct thermal_cooling_device *);
63c4d919 406struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
17e8351a 407int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
af06216a 408
9b4298a0
D
409int get_tz_trend(struct thermal_zone_device *, int);
410struct thermal_instance *get_thermal_instance(struct thermal_zone_device *,
411 struct thermal_cooling_device *, int);
dc765482 412void thermal_cdev_update(struct thermal_cooling_device *);
7b73c993 413void thermal_notify_framework(struct thermal_zone_device *, int);
12ca7188 414#else
35b11d2e
JM
415static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
416{ return false; }
417static inline int power_actor_get_max_power(struct thermal_cooling_device *cdev,
418 struct thermal_zone_device *tz, u32 *max_power)
419{ return 0; }
c973c3bc
JM
420static inline int power_actor_get_min_power(struct thermal_cooling_device *cdev,
421 struct thermal_zone_device *tz,
422 u32 *min_power)
423{ return -ENODEV; }
35b11d2e
JM
424static inline int power_actor_set_power(struct thermal_cooling_device *cdev,
425 struct thermal_instance *tz, u32 power)
426{ return 0; }
12ca7188
NM
427static inline struct thermal_zone_device *thermal_zone_device_register(
428 const char *type, int trips, int mask, void *devdata,
429 struct thermal_zone_device_ops *ops,
430 const struct thermal_zone_params *tzp,
431 int passive_delay, int polling_delay)
432{ return ERR_PTR(-ENODEV); }
433static inline void thermal_zone_device_unregister(
434 struct thermal_zone_device *tz)
435{ }
436static inline int thermal_zone_bind_cooling_device(
437 struct thermal_zone_device *tz, int trip,
438 struct thermal_cooling_device *cdev,
439 unsigned long upper, unsigned long lower)
440{ return -ENODEV; }
441static inline int thermal_zone_unbind_cooling_device(
442 struct thermal_zone_device *tz, int trip,
443 struct thermal_cooling_device *cdev)
444{ return -ENODEV; }
445static inline void thermal_zone_device_update(struct thermal_zone_device *tz)
446{ }
447static inline struct thermal_cooling_device *
448thermal_cooling_device_register(char *type, void *devdata,
449 const struct thermal_cooling_device_ops *ops)
450{ return ERR_PTR(-ENODEV); }
451static inline struct thermal_cooling_device *
452thermal_of_cooling_device_register(struct device_node *np,
453 char *type, void *devdata, const struct thermal_cooling_device_ops *ops)
454{ return ERR_PTR(-ENODEV); }
455static inline void thermal_cooling_device_unregister(
456 struct thermal_cooling_device *cdev)
457{ }
458static inline struct thermal_zone_device *thermal_zone_get_zone_by_name(
459 const char *name)
460{ return ERR_PTR(-ENODEV); }
461static inline int thermal_zone_get_temp(
17e8351a 462 struct thermal_zone_device *tz, int *temp)
12ca7188
NM
463{ return -ENODEV; }
464static inline int get_tz_trend(struct thermal_zone_device *tz, int trip)
465{ return -ENODEV; }
466static inline struct thermal_instance *
467get_thermal_instance(struct thermal_zone_device *tz,
468 struct thermal_cooling_device *cdev, int trip)
469{ return ERR_PTR(-ENODEV); }
470static inline void thermal_cdev_update(struct thermal_cooling_device *cdev)
471{ }
472static inline void thermal_notify_framework(struct thermal_zone_device *tz,
473 int trip)
474{ }
475#endif /* CONFIG_THERMAL */
476
477#if defined(CONFIG_NET) && IS_ENABLED(CONFIG_THERMAL)
8ab3e6a0
EV
478extern int thermal_generate_netlink_event(struct thermal_zone_device *tz,
479 enum events event);
af06216a 480#else
f8b58705 481static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz,
8ab3e6a0 482 enum events event)
af06216a
RW
483{
484 return 0;
485}
486#endif
203d3d4a 487
a0dd25b2 488#endif /* __THERMAL_H__ */