blob: 79ce6b94884ad628657790e2c3e4823b99027c1b [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
43/* Unit conversion macros */
44#define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732 >= 0) ? \
45 ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
46#define CELSIUS_TO_KELVIN(t) ((t)*10+2732)
Aaron Lu7b83fd92014-03-25 10:40:09 +080047#define DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, off) (((t) - (off)) * 100)
48#define DECI_KELVIN_TO_MILLICELSIUS(t) DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, 2732)
Durgadoss R23064082012-09-18 11:04:52 +053049
50/* Adding event notification support elements */
51#define THERMAL_GENL_FAMILY_NAME "thermal_event"
52#define THERMAL_GENL_VERSION 0x01
Masatake YAMATO73214f52013-03-19 01:47:28 +000053#define THERMAL_GENL_MCAST_GROUP_NAME "thermal_mc_grp"
Durgadoss R23064082012-09-18 11:04:52 +053054
Zhang Rui1f53ef12012-12-12 15:31:37 +080055/* 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"
62#endif
Durgadoss Ra4a15482012-09-18 11:04:57 +053063
Zhang Rui203d3d42008-01-17 15:51:08 +080064struct thermal_zone_device;
65struct thermal_cooling_device;
66
Matthew Garrett6503e5d2008-11-27 17:48:13 +000067enum thermal_device_mode {
68 THERMAL_DEVICE_DISABLED = 0,
69 THERMAL_DEVICE_ENABLED,
70};
71
72enum thermal_trip_type {
73 THERMAL_TRIP_ACTIVE = 0,
74 THERMAL_TRIP_PASSIVE,
75 THERMAL_TRIP_HOT,
76 THERMAL_TRIP_CRITICAL,
77};
78
Zhang Rui601f3d42012-06-27 09:54:33 +080079enum thermal_trend {
80 THERMAL_TREND_STABLE, /* temperature is stable */
81 THERMAL_TREND_RAISING, /* temperature is raising */
82 THERMAL_TREND_DROPPING, /* temperature is dropping */
Zhang Ruid069015e2012-11-19 15:33:51 +080083 THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */
84 THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */
Zhang Rui601f3d42012-06-27 09:54:33 +080085};
86
Durgadoss R23064082012-09-18 11:04:52 +053087/* Events supported by Thermal Netlink */
88enum events {
89 THERMAL_AUX0,
90 THERMAL_AUX1,
91 THERMAL_CRITICAL,
92 THERMAL_DEV_FAULT,
93};
94
95/* attributes of thermal_genl_family */
96enum {
97 THERMAL_GENL_ATTR_UNSPEC,
98 THERMAL_GENL_ATTR_EVENT,
99 __THERMAL_GENL_ATTR_MAX,
100};
101#define THERMAL_GENL_ATTR_MAX (__THERMAL_GENL_ATTR_MAX - 1)
102
103/* commands supported by the thermal_genl_family */
104enum {
105 THERMAL_GENL_CMD_UNSPEC,
106 THERMAL_GENL_CMD_EVENT,
107 __THERMAL_GENL_CMD_MAX,
108};
109#define THERMAL_GENL_CMD_MAX (__THERMAL_GENL_CMD_MAX - 1)
110
Zhang Rui203d3d42008-01-17 15:51:08 +0800111struct thermal_zone_device_ops {
112 int (*bind) (struct thermal_zone_device *,
113 struct thermal_cooling_device *);
114 int (*unbind) (struct thermal_zone_device *,
115 struct thermal_cooling_device *);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000116 int (*get_temp) (struct thermal_zone_device *, unsigned long *);
117 int (*get_mode) (struct thermal_zone_device *,
118 enum thermal_device_mode *);
119 int (*set_mode) (struct thermal_zone_device *,
120 enum thermal_device_mode);
121 int (*get_trip_type) (struct thermal_zone_device *, int,
122 enum thermal_trip_type *);
123 int (*get_trip_temp) (struct thermal_zone_device *, int,
124 unsigned long *);
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800125 int (*set_trip_temp) (struct thermal_zone_device *, int,
126 unsigned long);
Durgadoss R27365a62012-07-25 10:10:59 +0800127 int (*get_trip_hyst) (struct thermal_zone_device *, int,
128 unsigned long *);
129 int (*set_trip_hyst) (struct thermal_zone_device *, int,
130 unsigned long);
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800131 int (*get_crit_temp) (struct thermal_zone_device *, unsigned long *);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000132 int (*set_emul_temp) (struct thermal_zone_device *, unsigned long);
Zhang Rui601f3d42012-06-27 09:54:33 +0800133 int (*get_trend) (struct thermal_zone_device *, int,
134 enum thermal_trend *);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000135 int (*notify) (struct thermal_zone_device *, int,
136 enum thermal_trip_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800137};
138
139struct thermal_cooling_device_ops {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000140 int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
141 int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
142 int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
Zhang Rui203d3d42008-01-17 15:51:08 +0800143};
144
Zhang Rui203d3d42008-01-17 15:51:08 +0800145struct thermal_cooling_device {
146 int id;
147 char type[THERMAL_NAME_LENGTH];
148 struct device device;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400149 struct device_node *np;
Zhang Rui203d3d42008-01-17 15:51:08 +0800150 void *devdata;
Alan Cox5b275ce2010-11-11 15:27:29 +0000151 const struct thermal_cooling_device_ops *ops;
Zhang Ruice119f82012-06-27 14:13:04 +0800152 bool updated; /* true if the cooling device does not need update */
Zhang Ruif4a821c2012-07-24 16:56:21 +0800153 struct mutex lock; /* protect thermal_instances list */
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800154 struct list_head thermal_instances;
Zhang Rui203d3d42008-01-17 15:51:08 +0800155 struct list_head node;
156};
157
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800158struct thermal_attr {
159 struct device_attribute attr;
160 char name[THERMAL_NAME_LENGTH];
161};
162
Javi Merinoc708a982014-06-25 11:00:12 +0100163/**
164 * struct thermal_zone_device - structure for a thermal zone
165 * @id: unique id number for each thermal zone
166 * @type: the thermal zone device type
167 * @device: &struct device for this thermal zone
168 * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
169 * @trip_type_attrs: attributes for trip points for sysfs: trip type
170 * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
171 * @devdata: private pointer for device private data
172 * @trips: number of trip points the thermal zone supports
173 * @passive_delay: number of milliseconds to wait between polls when
174 * performing passive cooling. Currenty only used by the
175 * step-wise governor
176 * @polling_delay: number of milliseconds to wait between polls when
177 * checking whether trip points have been crossed (0 for
178 * interrupt driven systems)
179 * @temperature: current temperature. This is only for core code,
180 * drivers should use thermal_zone_get_temp() to get the
181 * current temperature
182 * @last_temperature: previous temperature read
183 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
184 * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
185 * Currenty only used by the step-wise governor.
186 * @forced_passive: If > 0, temperature at which to switch on all ACPI
187 * processor cooling devices. Currently only used by the
188 * step-wise governor.
189 * @ops: operations this &thermal_zone_device supports
190 * @tzp: thermal zone parameters
191 * @governor: pointer to the governor for this thermal zone
192 * @thermal_instances: list of &struct thermal_instance of this thermal zone
193 * @idr: &struct idr to generate unique id for this zone's cooling
194 * devices
195 * @lock: lock to protect thermal_instances list
196 * @node: node in thermal_tz_list (in thermal_core.c)
197 * @poll_queue: delayed work for polling
198 */
Zhang Rui203d3d42008-01-17 15:51:08 +0800199struct thermal_zone_device {
200 int id;
201 char type[THERMAL_NAME_LENGTH];
202 struct device device;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800203 struct thermal_attr *trip_temp_attrs;
204 struct thermal_attr *trip_type_attrs;
Durgadoss R27365a62012-07-25 10:10:59 +0800205 struct thermal_attr *trip_hyst_attrs;
Zhang Rui203d3d42008-01-17 15:51:08 +0800206 void *devdata;
207 int trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +0000208 int passive_delay;
209 int polling_delay;
Zhang Rui601f3d42012-06-27 09:54:33 +0800210 int temperature;
Matthew Garrettb1569e92008-12-03 17:55:32 +0000211 int last_temperature;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000212 int emul_temperature;
Zhang Rui908b9fb2012-06-27 14:14:05 +0800213 int passive;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000214 unsigned int forced_passive;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400215 struct thermal_zone_device_ops *ops;
Durgadoss Ref873942012-09-18 11:04:55 +0530216 const struct thermal_zone_params *tzp;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530217 struct thermal_governor *governor;
Zhang Rui2d374132012-06-27 10:09:00 +0800218 struct list_head thermal_instances;
Zhang Rui203d3d42008-01-17 15:51:08 +0800219 struct idr idr;
Javi Merinoc708a982014-06-25 11:00:12 +0100220 struct mutex lock;
Zhang Rui203d3d42008-01-17 15:51:08 +0800221 struct list_head node;
Matthew Garrettb1569e92008-12-03 17:55:32 +0000222 struct delayed_work poll_queue;
Zhang Rui203d3d42008-01-17 15:51:08 +0800223};
R.Durgadoss4cb18722010-10-27 03:33:29 +0530224
Javi Merinoc708a982014-06-25 11:00:12 +0100225/**
226 * struct thermal_governor - structure that holds thermal governor information
227 * @name: name of the governor
228 * @throttle: callback called for every trip point even if temperature is
229 * below the trip point temperature
230 * @governor_list: node in thermal_governor_list (in thermal_core.c)
231 */
Durgadoss Ra4a15482012-09-18 11:04:57 +0530232struct thermal_governor {
233 char name[THERMAL_NAME_LENGTH];
234 int (*throttle)(struct thermal_zone_device *tz, int trip);
235 struct list_head governor_list;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530236};
237
Durgadoss Ref873942012-09-18 11:04:55 +0530238/* Structure that holds binding parameters for a zone */
239struct thermal_bind_params {
240 struct thermal_cooling_device *cdev;
241
242 /*
243 * This is a measure of 'how effectively these devices can
244 * cool 'this' thermal zone. The shall be determined by platform
245 * characterization. This is on a 'percentage' scale.
246 * See Documentation/thermal/sysfs-api.txt for more information.
247 */
248 int weight;
249
250 /*
251 * This is a bit mask that gives the binding relation between this
252 * thermal zone and cdev, for a particular trip point.
253 * See Documentation/thermal/sysfs-api.txt for more information.
254 */
255 int trip_mask;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400256
257 /*
258 * This is an array of cooling state limits. Must have exactly
259 * 2 * thermal_zone.number_of_trip_points. It is an array consisting
260 * of tuples <lower-state upper-state> of state limits. Each trip
261 * will be associated with one state limit tuple when binding.
262 * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS>
263 * on all trips.
264 */
265 unsigned long *binding_limits;
Durgadoss Ref873942012-09-18 11:04:55 +0530266 int (*match) (struct thermal_zone_device *tz,
267 struct thermal_cooling_device *cdev);
268};
269
270/* Structure to define Thermal Zone parameters */
271struct thermal_zone_params {
Durgadoss Ra4a15482012-09-18 11:04:57 +0530272 char governor_name[THERMAL_NAME_LENGTH];
Eduardo Valentinccba4ff2013-08-15 11:34:17 -0400273
274 /*
275 * a boolean to indicate if the thermal to hwmon sysfs interface
276 * is required. when no_hwmon == false, a hwmon sysfs interface
277 * will be created. when no_hwmon == true, nothing will be done
278 */
279 bool no_hwmon;
280
Durgadoss Ref873942012-09-18 11:04:55 +0530281 int num_tbps; /* Number of tbp entries */
282 struct thermal_bind_params *tbp;
283};
284
R.Durgadoss4cb18722010-10-27 03:33:29 +0530285struct thermal_genl_event {
286 u32 orig;
287 enum events event;
288};
R.Durgadoss4cb18722010-10-27 03:33:29 +0530289
Durgadoss R23064082012-09-18 11:04:52 +0530290/* Function declarations */
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400291#ifdef CONFIG_THERMAL_OF
292struct thermal_zone_device *
293thermal_zone_of_sensor_register(struct device *dev, int id,
294 void *data, int (*get_temp)(void *, long *),
295 int (*get_trend)(void *, long *));
296void thermal_zone_of_sensor_unregister(struct device *dev,
297 struct thermal_zone_device *tz);
298#else
299static inline struct thermal_zone_device *
300thermal_zone_of_sensor_register(struct device *dev, int id,
301 void *data, int (*get_temp)(void *, long *),
302 int (*get_trend)(void *, long *))
303{
304 return NULL;
305}
306
307static inline
308void thermal_zone_of_sensor_unregister(struct device *dev,
309 struct thermal_zone_device *tz)
310{
311}
312
313#endif
Anton Vorontsov4b1bf582012-07-31 04:39:30 -0700314struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400315 void *, struct thermal_zone_device_ops *,
Durgadoss R50125a92012-09-18 11:04:56 +0530316 const struct thermal_zone_params *, int, int);
Zhang Rui203d3d42008-01-17 15:51:08 +0800317void thermal_zone_device_unregister(struct thermal_zone_device *);
318
319int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
Zhang Rui9d998422012-06-26 16:35:57 +0800320 struct thermal_cooling_device *,
321 unsigned long, unsigned long);
Zhang Rui203d3d42008-01-17 15:51:08 +0800322int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
323 struct thermal_cooling_device *);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000324void thermal_zone_device_update(struct thermal_zone_device *);
Durgadoss R23064082012-09-18 11:04:52 +0530325
Zhang Rui203d3d42008-01-17 15:51:08 +0800326struct thermal_cooling_device *thermal_cooling_device_register(char *, void *,
Alan Cox5b275ce2010-11-11 15:27:29 +0000327 const struct thermal_cooling_device_ops *);
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400328struct thermal_cooling_device *
329thermal_of_cooling_device_register(struct device_node *np, char *, void *,
330 const struct thermal_cooling_device_ops *);
Zhang Rui203d3d42008-01-17 15:51:08 +0800331void thermal_cooling_device_unregister(struct thermal_cooling_device *);
Eduardo Valentin63c4d912013-04-05 12:32:28 +0000332struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000333int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp);
Rafael J. Wysockiaf062162011-03-01 01:12:19 +0100334
Durgadoss R9b4298a2012-09-18 11:04:54 +0530335int get_tz_trend(struct thermal_zone_device *, int);
336struct thermal_instance *get_thermal_instance(struct thermal_zone_device *,
337 struct thermal_cooling_device *, int);
Durgadoss Rdc765482012-09-18 11:05:00 +0530338void thermal_cdev_update(struct thermal_cooling_device *);
Eduardo Valentin7b73c992013-04-23 21:48:14 +0000339void thermal_notify_framework(struct thermal_zone_device *, int);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530340
Rafael J. Wysockiaf062162011-03-01 01:12:19 +0100341#ifdef CONFIG_NET
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +0000342extern int thermal_generate_netlink_event(struct thermal_zone_device *tz,
343 enum events event);
Rafael J. Wysockiaf062162011-03-01 01:12:19 +0100344#else
Ezequiel Garciaf8b58702013-03-20 21:38:07 +0000345static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz,
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +0000346 enum events event)
Rafael J. Wysockiaf062162011-03-01 01:12:19 +0100347{
348 return 0;
349}
350#endif
Zhang Rui203d3d42008-01-17 15:51:08 +0800351
Len Browna0dd25b2008-02-09 04:01:48 -0500352#endif /* __THERMAL_H__ */