blob: 96c305167a1e074b2288347c4cf4a51cd6da437e [file] [log] [blame]
Zhang Rui203d3d42008-01-17 15:51:08 +08001/*
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
Eduardo Valentina116b5d2013-09-26 15:55:01 -040028#include <linux/of.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080029#include <linux/idr.h>
30#include <linux/device.h>
Matthew Garrettb1569e92008-12-03 17:55:32 +000031#include <linux/workqueue.h>
Zhang Rui203d3d42008-01-17 15:51:08 +080032
Durgadoss R23064082012-09-18 11:04:52 +053033#define THERMAL_TRIPS_NONE -1
34#define THERMAL_MAX_TRIPS 12
35#define THERMAL_NAME_LENGTH 20
36
Zhang Rui57df8102013-02-08 14:52:06 +080037/* invalid cooling state */
38#define THERMAL_CSTATE_INVALID -1UL
39
Durgadoss R23064082012-09-18 11:04:52 +053040/* No upper/lower limit requirement */
Zhang Rui57df8102013-02-08 14:52:06 +080041#define THERMAL_NO_LIMIT THERMAL_CSTATE_INVALID
Durgadoss R23064082012-09-18 11:04:52 +053042
Zhang Rui69cfaa82015-10-30 16:31:47 +080043/* use value, which < 0K, to indicate an invalid/uninitialized temperature */
44#define THERMAL_TEMP_INVALID -274000
45
Durgadoss R23064082012-09-18 11:04:52 +053046/* 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)
Aaron Lu7b83fd92014-03-25 10:40:09 +080050#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)
Aaron Lu77e337c2014-09-03 15:13:02 +080052#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)
Durgadoss R23064082012-09-18 11:04:52 +053054
55/* Adding event notification support elements */
56#define THERMAL_GENL_FAMILY_NAME "thermal_event"
57#define THERMAL_GENL_VERSION 0x01
Masatake YAMATO73214f52013-03-19 01:47:28 +000058#define THERMAL_GENL_MCAST_GROUP_NAME "thermal_mc_grp"
Durgadoss R23064082012-09-18 11:04:52 +053059
Zhang Rui1f53ef12012-12-12 15:31:37 +080060/* 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#endif
Durgadoss Ra4a15482012-09-18 11:04:57 +053068
Zhang Rui203d3d42008-01-17 15:51:08 +080069struct thermal_zone_device;
70struct thermal_cooling_device;
71
Matthew Garrett6503e5d2008-11-27 17:48:13 +000072enum thermal_device_mode {
73 THERMAL_DEVICE_DISABLED = 0,
74 THERMAL_DEVICE_ENABLED,
75};
76
77enum thermal_trip_type {
78 THERMAL_TRIP_ACTIVE = 0,
79 THERMAL_TRIP_PASSIVE,
80 THERMAL_TRIP_HOT,
81 THERMAL_TRIP_CRITICAL,
82};
83
Zhang Rui601f3d42012-06-27 09:54:33 +080084enum thermal_trend {
85 THERMAL_TREND_STABLE, /* temperature is stable */
86 THERMAL_TREND_RAISING, /* temperature is raising */
87 THERMAL_TREND_DROPPING, /* temperature is dropping */
Zhang Ruid069015e2012-11-19 15:33:51 +080088 THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */
89 THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */
Zhang Rui601f3d42012-06-27 09:54:33 +080090};
91
Durgadoss R23064082012-09-18 11:04:52 +053092/* Events supported by Thermal Netlink */
93enum events {
94 THERMAL_AUX0,
95 THERMAL_AUX1,
96 THERMAL_CRITICAL,
97 THERMAL_DEV_FAULT,
98};
99
100/* attributes of thermal_genl_family */
101enum {
102 THERMAL_GENL_ATTR_UNSPEC,
103 THERMAL_GENL_ATTR_EVENT,
104 __THERMAL_GENL_ATTR_MAX,
105};
106#define THERMAL_GENL_ATTR_MAX (__THERMAL_GENL_ATTR_MAX - 1)
107
108/* commands supported by the thermal_genl_family */
109enum {
110 THERMAL_GENL_CMD_UNSPEC,
111 THERMAL_GENL_CMD_EVENT,
112 __THERMAL_GENL_CMD_MAX,
113};
114#define THERMAL_GENL_CMD_MAX (__THERMAL_GENL_CMD_MAX - 1)
115
Zhang Rui203d3d42008-01-17 15:51:08 +0800116struct thermal_zone_device_ops {
117 int (*bind) (struct thermal_zone_device *,
118 struct thermal_cooling_device *);
119 int (*unbind) (struct thermal_zone_device *,
120 struct thermal_cooling_device *);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000121 int (*get_temp) (struct thermal_zone_device *, unsigned long *);
122 int (*get_mode) (struct thermal_zone_device *,
123 enum thermal_device_mode *);
124 int (*set_mode) (struct thermal_zone_device *,
125 enum thermal_device_mode);
126 int (*get_trip_type) (struct thermal_zone_device *, int,
127 enum thermal_trip_type *);
128 int (*get_trip_temp) (struct thermal_zone_device *, int,
129 unsigned long *);
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800130 int (*set_trip_temp) (struct thermal_zone_device *, int,
131 unsigned long);
Durgadoss R27365a62012-07-25 10:10:59 +0800132 int (*get_trip_hyst) (struct thermal_zone_device *, int,
133 unsigned long *);
134 int (*set_trip_hyst) (struct thermal_zone_device *, int,
135 unsigned long);
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800136 int (*get_crit_temp) (struct thermal_zone_device *, unsigned long *);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000137 int (*set_emul_temp) (struct thermal_zone_device *, unsigned long);
Zhang Rui601f3d42012-06-27 09:54:33 +0800138 int (*get_trend) (struct thermal_zone_device *, int,
139 enum thermal_trend *);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000140 int (*notify) (struct thermal_zone_device *, int,
141 enum thermal_trip_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800142};
143
144struct thermal_cooling_device_ops {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000145 int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
146 int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
147 int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
Zhang Rui203d3d42008-01-17 15:51:08 +0800148};
149
Zhang Rui203d3d42008-01-17 15:51:08 +0800150struct thermal_cooling_device {
151 int id;
152 char type[THERMAL_NAME_LENGTH];
153 struct device device;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400154 struct device_node *np;
Zhang Rui203d3d42008-01-17 15:51:08 +0800155 void *devdata;
Alan Cox5b275ce2010-11-11 15:27:29 +0000156 const struct thermal_cooling_device_ops *ops;
Zhang Ruice119f82012-06-27 14:13:04 +0800157 bool updated; /* true if the cooling device does not need update */
Zhang Ruif4a821c2012-07-24 16:56:21 +0800158 struct mutex lock; /* protect thermal_instances list */
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800159 struct list_head thermal_instances;
Zhang Rui203d3d42008-01-17 15:51:08 +0800160 struct list_head node;
161};
162
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800163struct thermal_attr {
164 struct device_attribute attr;
165 char name[THERMAL_NAME_LENGTH];
166};
167
Javi Merinoc708a982014-06-25 11:00:12 +0100168/**
169 * struct thermal_zone_device - structure for a thermal zone
170 * @id: unique id number for each thermal zone
171 * @type: the thermal zone device type
172 * @device: &struct device for this thermal zone
173 * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
174 * @trip_type_attrs: attributes for trip points for sysfs: trip type
175 * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
176 * @devdata: private pointer for device private data
177 * @trips: number of trip points the thermal zone supports
Zhang Rui79a6f042016-03-18 10:03:24 +0800178 * @trips_disabled; bitmap for disabled trips
Javi Merinoc708a982014-06-25 11:00:12 +0100179 * @passive_delay: number of milliseconds to wait between polls when
180 * performing passive cooling. Currenty only used by the
181 * step-wise governor
182 * @polling_delay: number of milliseconds to wait between polls when
183 * checking whether trip points have been crossed (0 for
184 * interrupt driven systems)
185 * @temperature: current temperature. This is only for core code,
186 * drivers should use thermal_zone_get_temp() to get the
187 * current temperature
188 * @last_temperature: previous temperature read
189 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
190 * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
191 * Currenty only used by the step-wise governor.
192 * @forced_passive: If > 0, temperature at which to switch on all ACPI
193 * processor cooling devices. Currently only used by the
194 * step-wise governor.
Chen Yu914186f2015-10-30 16:32:10 +0800195 * @need_update: if equals 1, thermal_zone_device_update needs to be invoked.
Javi Merinoc708a982014-06-25 11:00:12 +0100196 * @ops: operations this &thermal_zone_device supports
197 * @tzp: thermal zone parameters
198 * @governor: pointer to the governor for this thermal zone
199 * @thermal_instances: list of &struct thermal_instance of this thermal zone
200 * @idr: &struct idr to generate unique id for this zone's cooling
201 * devices
202 * @lock: lock to protect thermal_instances list
203 * @node: node in thermal_tz_list (in thermal_core.c)
204 * @poll_queue: delayed work for polling
205 */
Zhang Rui203d3d42008-01-17 15:51:08 +0800206struct thermal_zone_device {
207 int id;
208 char type[THERMAL_NAME_LENGTH];
209 struct device device;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800210 struct thermal_attr *trip_temp_attrs;
211 struct thermal_attr *trip_type_attrs;
Durgadoss R27365a62012-07-25 10:10:59 +0800212 struct thermal_attr *trip_hyst_attrs;
Zhang Rui203d3d42008-01-17 15:51:08 +0800213 void *devdata;
214 int trips;
Zhang Rui79a6f042016-03-18 10:03:24 +0800215 unsigned long trips_disabled; /* bitmap for disabled trips */
Matthew Garrettb1569e92008-12-03 17:55:32 +0000216 int passive_delay;
217 int polling_delay;
Zhang Rui601f3d42012-06-27 09:54:33 +0800218 int temperature;
Matthew Garrettb1569e92008-12-03 17:55:32 +0000219 int last_temperature;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000220 int emul_temperature;
Zhang Rui908b9fb2012-06-27 14:14:05 +0800221 int passive;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000222 unsigned int forced_passive;
Chen Yu914186f2015-10-30 16:32:10 +0800223 atomic_t need_update;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400224 struct thermal_zone_device_ops *ops;
Durgadoss Ref873942012-09-18 11:04:55 +0530225 const struct thermal_zone_params *tzp;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530226 struct thermal_governor *governor;
Zhang Rui2d374132012-06-27 10:09:00 +0800227 struct list_head thermal_instances;
Zhang Rui203d3d42008-01-17 15:51:08 +0800228 struct idr idr;
Javi Merinoc708a982014-06-25 11:00:12 +0100229 struct mutex lock;
Zhang Rui203d3d42008-01-17 15:51:08 +0800230 struct list_head node;
Matthew Garrettb1569e92008-12-03 17:55:32 +0000231 struct delayed_work poll_queue;
Zhang Rui203d3d42008-01-17 15:51:08 +0800232};
R.Durgadoss4cb18722010-10-27 03:33:29 +0530233
Javi Merinoc708a982014-06-25 11:00:12 +0100234/**
235 * struct thermal_governor - structure that holds thermal governor information
236 * @name: name of the governor
237 * @throttle: callback called for every trip point even if temperature is
238 * below the trip point temperature
239 * @governor_list: node in thermal_governor_list (in thermal_core.c)
240 */
Durgadoss Ra4a15482012-09-18 11:04:57 +0530241struct thermal_governor {
242 char name[THERMAL_NAME_LENGTH];
243 int (*throttle)(struct thermal_zone_device *tz, int trip);
244 struct list_head governor_list;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530245};
246
Durgadoss Ref873942012-09-18 11:04:55 +0530247/* Structure that holds binding parameters for a zone */
248struct thermal_bind_params {
249 struct thermal_cooling_device *cdev;
250
251 /*
252 * This is a measure of 'how effectively these devices can
253 * cool 'this' thermal zone. The shall be determined by platform
254 * characterization. This is on a 'percentage' scale.
255 * See Documentation/thermal/sysfs-api.txt for more information.
256 */
257 int weight;
258
259 /*
260 * This is a bit mask that gives the binding relation between this
261 * thermal zone and cdev, for a particular trip point.
262 * See Documentation/thermal/sysfs-api.txt for more information.
263 */
264 int trip_mask;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400265
266 /*
267 * This is an array of cooling state limits. Must have exactly
268 * 2 * thermal_zone.number_of_trip_points. It is an array consisting
269 * of tuples <lower-state upper-state> of state limits. Each trip
270 * will be associated with one state limit tuple when binding.
271 * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS>
272 * on all trips.
273 */
274 unsigned long *binding_limits;
Durgadoss Ref873942012-09-18 11:04:55 +0530275 int (*match) (struct thermal_zone_device *tz,
276 struct thermal_cooling_device *cdev);
277};
278
279/* Structure to define Thermal Zone parameters */
280struct thermal_zone_params {
Durgadoss Ra4a15482012-09-18 11:04:57 +0530281 char governor_name[THERMAL_NAME_LENGTH];
Eduardo Valentinccba4ff2013-08-15 11:34:17 -0400282
283 /*
284 * a boolean to indicate if the thermal to hwmon sysfs interface
285 * is required. when no_hwmon == false, a hwmon sysfs interface
286 * will be created. when no_hwmon == true, nothing will be done
287 */
288 bool no_hwmon;
289
Durgadoss Ref873942012-09-18 11:04:55 +0530290 int num_tbps; /* Number of tbp entries */
291 struct thermal_bind_params *tbp;
292};
293
R.Durgadoss4cb18722010-10-27 03:33:29 +0530294struct thermal_genl_event {
295 u32 orig;
296 enum events event;
297};
R.Durgadoss4cb18722010-10-27 03:33:29 +0530298
Durgadoss R23064082012-09-18 11:04:52 +0530299/* Function declarations */
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400300#ifdef CONFIG_THERMAL_OF
301struct thermal_zone_device *
302thermal_zone_of_sensor_register(struct device *dev, int id,
303 void *data, int (*get_temp)(void *, long *),
304 int (*get_trend)(void *, long *));
305void thermal_zone_of_sensor_unregister(struct device *dev,
306 struct thermal_zone_device *tz);
307#else
308static inline struct thermal_zone_device *
309thermal_zone_of_sensor_register(struct device *dev, int id,
310 void *data, int (*get_temp)(void *, long *),
311 int (*get_trend)(void *, long *))
312{
313 return NULL;
314}
315
316static inline
317void thermal_zone_of_sensor_unregister(struct device *dev,
318 struct thermal_zone_device *tz)
319{
320}
321
322#endif
Anton Vorontsov4b1bf582012-07-31 04:39:30 -0700323struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400324 void *, struct thermal_zone_device_ops *,
Durgadoss R50125a92012-09-18 11:04:56 +0530325 const struct thermal_zone_params *, int, int);
Zhang Rui203d3d42008-01-17 15:51:08 +0800326void thermal_zone_device_unregister(struct thermal_zone_device *);
327
328int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
Zhang Rui9d998422012-06-26 16:35:57 +0800329 struct thermal_cooling_device *,
330 unsigned long, unsigned long);
Zhang Rui203d3d42008-01-17 15:51:08 +0800331int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
332 struct thermal_cooling_device *);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000333void thermal_zone_device_update(struct thermal_zone_device *);
Durgadoss R23064082012-09-18 11:04:52 +0530334
Zhang Rui203d3d42008-01-17 15:51:08 +0800335struct thermal_cooling_device *thermal_cooling_device_register(char *, void *,
Alan Cox5b275ce2010-11-11 15:27:29 +0000336 const struct thermal_cooling_device_ops *);
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400337struct thermal_cooling_device *
338thermal_of_cooling_device_register(struct device_node *np, char *, void *,
339 const struct thermal_cooling_device_ops *);
Zhang Rui203d3d42008-01-17 15:51:08 +0800340void thermal_cooling_device_unregister(struct thermal_cooling_device *);
Eduardo Valentin63c4d912013-04-05 12:32:28 +0000341struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000342int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp);
Rafael J. Wysockiaf062162011-03-01 01:12:19 +0100343
Durgadoss R9b4298a2012-09-18 11:04:54 +0530344int get_tz_trend(struct thermal_zone_device *, int);
345struct thermal_instance *get_thermal_instance(struct thermal_zone_device *,
346 struct thermal_cooling_device *, int);
Durgadoss Rdc765482012-09-18 11:05:00 +0530347void thermal_cdev_update(struct thermal_cooling_device *);
Eduardo Valentin7b73c992013-04-23 21:48:14 +0000348void thermal_notify_framework(struct thermal_zone_device *, int);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530349
Rafael J. Wysockiaf062162011-03-01 01:12:19 +0100350#ifdef CONFIG_NET
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +0000351extern int thermal_generate_netlink_event(struct thermal_zone_device *tz,
352 enum events event);
Rafael J. Wysockiaf062162011-03-01 01:12:19 +0100353#else
Ezequiel Garciaf8b58702013-03-20 21:38:07 +0000354static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz,
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +0000355 enum events event)
Rafael J. Wysockiaf062162011-03-01 01:12:19 +0100356{
357 return 0;
358}
359#endif
Zhang Rui203d3d42008-01-17 15:51:08 +0800360
Len Browna0dd25b2008-02-09 04:01:48 -0500361#endif /* __THERMAL_H__ */