blob: 99be7fc79c3bd4a8661dd50999c87885dae426f1 [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)
Aaron Lu77e337c2014-09-03 15:13:02 +080049#define MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, off) (((t) / 100) + (off))
50#define MILLICELSIUS_TO_DECI_KELVIN(t) MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, 2732)
Durgadoss R23064082012-09-18 11:04:52 +053051
52/* Adding event notification support elements */
53#define THERMAL_GENL_FAMILY_NAME "thermal_event"
54#define THERMAL_GENL_VERSION 0x01
Masatake YAMATO73214f52013-03-19 01:47:28 +000055#define THERMAL_GENL_MCAST_GROUP_NAME "thermal_mc_grp"
Durgadoss R23064082012-09-18 11:04:52 +053056
Zhang Rui1f53ef12012-12-12 15:31:37 +080057/* Default Thermal Governor */
58#if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
59#define DEFAULT_THERMAL_GOVERNOR "step_wise"
60#elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE)
61#define DEFAULT_THERMAL_GOVERNOR "fair_share"
62#elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)
63#define DEFAULT_THERMAL_GOVERNOR "user_space"
64#endif
Durgadoss Ra4a15482012-09-18 11:04:57 +053065
Zhang Rui203d3d42008-01-17 15:51:08 +080066struct thermal_zone_device;
67struct thermal_cooling_device;
68
Matthew Garrett6503e5d2008-11-27 17:48:13 +000069enum thermal_device_mode {
70 THERMAL_DEVICE_DISABLED = 0,
71 THERMAL_DEVICE_ENABLED,
72};
73
74enum thermal_trip_type {
75 THERMAL_TRIP_ACTIVE = 0,
76 THERMAL_TRIP_PASSIVE,
77 THERMAL_TRIP_HOT,
78 THERMAL_TRIP_CRITICAL,
79};
80
Zhang Rui601f3d42012-06-27 09:54:33 +080081enum thermal_trend {
82 THERMAL_TREND_STABLE, /* temperature is stable */
83 THERMAL_TREND_RAISING, /* temperature is raising */
84 THERMAL_TREND_DROPPING, /* temperature is dropping */
Zhang Ruid069015e2012-11-19 15:33:51 +080085 THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */
86 THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */
Zhang Rui601f3d42012-06-27 09:54:33 +080087};
88
Durgadoss R23064082012-09-18 11:04:52 +053089/* Events supported by Thermal Netlink */
90enum events {
91 THERMAL_AUX0,
92 THERMAL_AUX1,
93 THERMAL_CRITICAL,
94 THERMAL_DEV_FAULT,
95};
96
97/* attributes of thermal_genl_family */
98enum {
99 THERMAL_GENL_ATTR_UNSPEC,
100 THERMAL_GENL_ATTR_EVENT,
101 __THERMAL_GENL_ATTR_MAX,
102};
103#define THERMAL_GENL_ATTR_MAX (__THERMAL_GENL_ATTR_MAX - 1)
104
105/* commands supported by the thermal_genl_family */
106enum {
107 THERMAL_GENL_CMD_UNSPEC,
108 THERMAL_GENL_CMD_EVENT,
109 __THERMAL_GENL_CMD_MAX,
110};
111#define THERMAL_GENL_CMD_MAX (__THERMAL_GENL_CMD_MAX - 1)
112
Zhang Rui203d3d42008-01-17 15:51:08 +0800113struct thermal_zone_device_ops {
114 int (*bind) (struct thermal_zone_device *,
115 struct thermal_cooling_device *);
116 int (*unbind) (struct thermal_zone_device *,
117 struct thermal_cooling_device *);
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000118 int (*get_temp) (struct thermal_zone_device *, unsigned long *);
119 int (*get_mode) (struct thermal_zone_device *,
120 enum thermal_device_mode *);
121 int (*set_mode) (struct thermal_zone_device *,
122 enum thermal_device_mode);
123 int (*get_trip_type) (struct thermal_zone_device *, int,
124 enum thermal_trip_type *);
125 int (*get_trip_temp) (struct thermal_zone_device *, int,
126 unsigned long *);
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800127 int (*set_trip_temp) (struct thermal_zone_device *, int,
128 unsigned long);
Durgadoss R27365a62012-07-25 10:10:59 +0800129 int (*get_trip_hyst) (struct thermal_zone_device *, int,
130 unsigned long *);
131 int (*set_trip_hyst) (struct thermal_zone_device *, int,
132 unsigned long);
Zhang, Rui9ec732f2008-04-10 16:13:10 +0800133 int (*get_crit_temp) (struct thermal_zone_device *, unsigned long *);
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000134 int (*set_emul_temp) (struct thermal_zone_device *, unsigned long);
Zhang Rui601f3d42012-06-27 09:54:33 +0800135 int (*get_trend) (struct thermal_zone_device *, int,
136 enum thermal_trend *);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000137 int (*notify) (struct thermal_zone_device *, int,
138 enum thermal_trip_type);
Zhang Rui203d3d42008-01-17 15:51:08 +0800139};
140
141struct thermal_cooling_device_ops {
Matthew Garrett6503e5d2008-11-27 17:48:13 +0000142 int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
143 int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
144 int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
Zhang Rui203d3d42008-01-17 15:51:08 +0800145};
146
Zhang Rui203d3d42008-01-17 15:51:08 +0800147struct thermal_cooling_device {
148 int id;
149 char type[THERMAL_NAME_LENGTH];
150 struct device device;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400151 struct device_node *np;
Zhang Rui203d3d42008-01-17 15:51:08 +0800152 void *devdata;
Alan Cox5b275ce2010-11-11 15:27:29 +0000153 const struct thermal_cooling_device_ops *ops;
Zhang Ruice119f82012-06-27 14:13:04 +0800154 bool updated; /* true if the cooling device does not need update */
Zhang Ruif4a821c2012-07-24 16:56:21 +0800155 struct mutex lock; /* protect thermal_instances list */
Zhang Ruib5e4ae62012-06-27 14:11:52 +0800156 struct list_head thermal_instances;
Zhang Rui203d3d42008-01-17 15:51:08 +0800157 struct list_head node;
158};
159
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800160struct thermal_attr {
161 struct device_attribute attr;
162 char name[THERMAL_NAME_LENGTH];
163};
164
Javi Merinoc708a982014-06-25 11:00:12 +0100165/**
166 * struct thermal_zone_device - structure for a thermal zone
167 * @id: unique id number for each thermal zone
168 * @type: the thermal zone device type
169 * @device: &struct device for this thermal zone
170 * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
171 * @trip_type_attrs: attributes for trip points for sysfs: trip type
172 * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
173 * @devdata: private pointer for device private data
174 * @trips: number of trip points the thermal zone supports
175 * @passive_delay: number of milliseconds to wait between polls when
176 * performing passive cooling. Currenty only used by the
177 * step-wise governor
178 * @polling_delay: number of milliseconds to wait between polls when
179 * checking whether trip points have been crossed (0 for
180 * interrupt driven systems)
181 * @temperature: current temperature. This is only for core code,
182 * drivers should use thermal_zone_get_temp() to get the
183 * current temperature
184 * @last_temperature: previous temperature read
185 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
186 * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
187 * Currenty only used by the step-wise governor.
188 * @forced_passive: If > 0, temperature at which to switch on all ACPI
189 * processor cooling devices. Currently only used by the
190 * step-wise governor.
191 * @ops: operations this &thermal_zone_device supports
192 * @tzp: thermal zone parameters
193 * @governor: pointer to the governor for this thermal zone
194 * @thermal_instances: list of &struct thermal_instance of this thermal zone
195 * @idr: &struct idr to generate unique id for this zone's cooling
196 * devices
197 * @lock: lock to protect thermal_instances list
198 * @node: node in thermal_tz_list (in thermal_core.c)
199 * @poll_queue: delayed work for polling
200 */
Zhang Rui203d3d42008-01-17 15:51:08 +0800201struct thermal_zone_device {
202 int id;
203 char type[THERMAL_NAME_LENGTH];
204 struct device device;
Durgadoss Rc56f5c02012-07-25 10:10:58 +0800205 struct thermal_attr *trip_temp_attrs;
206 struct thermal_attr *trip_type_attrs;
Durgadoss R27365a62012-07-25 10:10:59 +0800207 struct thermal_attr *trip_hyst_attrs;
Zhang Rui203d3d42008-01-17 15:51:08 +0800208 void *devdata;
209 int trips;
Matthew Garrettb1569e92008-12-03 17:55:32 +0000210 int passive_delay;
211 int polling_delay;
Zhang Rui601f3d42012-06-27 09:54:33 +0800212 int temperature;
Matthew Garrettb1569e92008-12-03 17:55:32 +0000213 int last_temperature;
Amit Daniel Kachhape6e238c2013-02-04 00:30:15 +0000214 int emul_temperature;
Zhang Rui908b9fb2012-06-27 14:14:05 +0800215 int passive;
Matthew Garrett03a971a2008-12-03 18:00:38 +0000216 unsigned int forced_passive;
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400217 struct thermal_zone_device_ops *ops;
Durgadoss Ref873942012-09-18 11:04:55 +0530218 const struct thermal_zone_params *tzp;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530219 struct thermal_governor *governor;
Zhang Rui2d374132012-06-27 10:09:00 +0800220 struct list_head thermal_instances;
Zhang Rui203d3d42008-01-17 15:51:08 +0800221 struct idr idr;
Javi Merinoc708a982014-06-25 11:00:12 +0100222 struct mutex lock;
Zhang Rui203d3d42008-01-17 15:51:08 +0800223 struct list_head node;
Matthew Garrettb1569e92008-12-03 17:55:32 +0000224 struct delayed_work poll_queue;
Zhang Rui203d3d42008-01-17 15:51:08 +0800225};
R.Durgadoss4cb18722010-10-27 03:33:29 +0530226
Javi Merinoc708a982014-06-25 11:00:12 +0100227/**
228 * struct thermal_governor - structure that holds thermal governor information
229 * @name: name of the governor
230 * @throttle: callback called for every trip point even if temperature is
231 * below the trip point temperature
232 * @governor_list: node in thermal_governor_list (in thermal_core.c)
233 */
Durgadoss Ra4a15482012-09-18 11:04:57 +0530234struct thermal_governor {
235 char name[THERMAL_NAME_LENGTH];
236 int (*throttle)(struct thermal_zone_device *tz, int trip);
237 struct list_head governor_list;
Durgadoss Ra4a15482012-09-18 11:04:57 +0530238};
239
Durgadoss Ref873942012-09-18 11:04:55 +0530240/* Structure that holds binding parameters for a zone */
241struct thermal_bind_params {
242 struct thermal_cooling_device *cdev;
243
244 /*
245 * This is a measure of 'how effectively these devices can
246 * cool 'this' thermal zone. The shall be determined by platform
247 * characterization. This is on a 'percentage' scale.
248 * See Documentation/thermal/sysfs-api.txt for more information.
249 */
250 int weight;
251
252 /*
253 * This is a bit mask that gives the binding relation between this
254 * thermal zone and cdev, for a particular trip point.
255 * See Documentation/thermal/sysfs-api.txt for more information.
256 */
257 int trip_mask;
Eduardo Valentina8892d832013-07-16 15:26:28 -0400258
259 /*
260 * This is an array of cooling state limits. Must have exactly
261 * 2 * thermal_zone.number_of_trip_points. It is an array consisting
262 * of tuples <lower-state upper-state> of state limits. Each trip
263 * will be associated with one state limit tuple when binding.
264 * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS>
265 * on all trips.
266 */
267 unsigned long *binding_limits;
Durgadoss Ref873942012-09-18 11:04:55 +0530268 int (*match) (struct thermal_zone_device *tz,
269 struct thermal_cooling_device *cdev);
270};
271
272/* Structure to define Thermal Zone parameters */
273struct thermal_zone_params {
Durgadoss Ra4a15482012-09-18 11:04:57 +0530274 char governor_name[THERMAL_NAME_LENGTH];
Eduardo Valentinccba4ff2013-08-15 11:34:17 -0400275
276 /*
277 * a boolean to indicate if the thermal to hwmon sysfs interface
278 * is required. when no_hwmon == false, a hwmon sysfs interface
279 * will be created. when no_hwmon == true, nothing will be done
280 */
281 bool no_hwmon;
282
Durgadoss Ref873942012-09-18 11:04:55 +0530283 int num_tbps; /* Number of tbp entries */
284 struct thermal_bind_params *tbp;
285};
286
R.Durgadoss4cb18722010-10-27 03:33:29 +0530287struct thermal_genl_event {
288 u32 orig;
289 enum events event;
290};
R.Durgadoss4cb18722010-10-27 03:33:29 +0530291
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400292/**
293 * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones
294 *
295 * Mandatory:
296 * @get_temp: a pointer to a function that reads the sensor temperature.
297 *
298 * Optional:
299 * @get_trend: a pointer to a function that reads the sensor temperature trend.
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100300 * @set_emul_temp: a pointer to a function that sets sensor emulated
301 * temperature.
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400302 */
303struct thermal_zone_of_device_ops {
304 int (*get_temp)(void *, long *);
305 int (*get_trend)(void *, long *);
Lukasz Majewski184a4bf2014-12-08 18:04:21 +0100306 int (*set_emul_temp)(void *, unsigned long);
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400307};
308
Lukasz Majewskiad9914a2014-12-08 18:04:19 +0100309/**
310 * struct thermal_trip - representation of a point in temperature domain
311 * @np: pointer to struct device_node that this trip point was created from
312 * @temperature: temperature value in miliCelsius
313 * @hysteresis: relative hysteresis in miliCelsius
314 * @type: trip point type
315 */
316
317struct thermal_trip {
318 struct device_node *np;
319 unsigned long int temperature;
320 unsigned long int hysteresis;
321 enum thermal_trip_type type;
322};
323
Durgadoss R23064082012-09-18 11:04:52 +0530324/* Function declarations */
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400325#ifdef CONFIG_THERMAL_OF
326struct thermal_zone_device *
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400327thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
328 const struct thermal_zone_of_device_ops *ops);
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400329void thermal_zone_of_sensor_unregister(struct device *dev,
330 struct thermal_zone_device *tz);
331#else
332static inline struct thermal_zone_device *
Eduardo Valentin2251aef2014-11-07 21:24:39 -0400333thermal_zone_of_sensor_register(struct device *dev, int id, void *data,
334 const struct thermal_zone_of_device_ops *ops)
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400335{
336 return NULL;
337}
338
339static inline
340void thermal_zone_of_sensor_unregister(struct device *dev,
341 struct thermal_zone_device *tz)
342{
343}
344
345#endif
Anton Vorontsov4b1bf582012-07-31 04:39:30 -0700346struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
Eduardo Valentin4e5e4702013-07-03 15:35:39 -0400347 void *, struct thermal_zone_device_ops *,
Durgadoss R50125a92012-09-18 11:04:56 +0530348 const struct thermal_zone_params *, int, int);
Zhang Rui203d3d42008-01-17 15:51:08 +0800349void thermal_zone_device_unregister(struct thermal_zone_device *);
350
351int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
Zhang Rui9d998422012-06-26 16:35:57 +0800352 struct thermal_cooling_device *,
353 unsigned long, unsigned long);
Zhang Rui203d3d42008-01-17 15:51:08 +0800354int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
355 struct thermal_cooling_device *);
Matthew Garrettb1569e92008-12-03 17:55:32 +0000356void thermal_zone_device_update(struct thermal_zone_device *);
Durgadoss R23064082012-09-18 11:04:52 +0530357
Zhang Rui203d3d42008-01-17 15:51:08 +0800358struct thermal_cooling_device *thermal_cooling_device_register(char *, void *,
Alan Cox5b275ce2010-11-11 15:27:29 +0000359 const struct thermal_cooling_device_ops *);
Eduardo Valentina116b5d2013-09-26 15:55:01 -0400360struct thermal_cooling_device *
361thermal_of_cooling_device_register(struct device_node *np, char *, void *,
362 const struct thermal_cooling_device_ops *);
Zhang Rui203d3d42008-01-17 15:51:08 +0800363void thermal_cooling_device_unregister(struct thermal_cooling_device *);
Eduardo Valentin63c4d912013-04-05 12:32:28 +0000364struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
Eduardo Valentin837b26b2013-04-05 12:32:29 +0000365int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp);
Rafael J. Wysockiaf062162011-03-01 01:12:19 +0100366
Durgadoss R9b4298a2012-09-18 11:04:54 +0530367int get_tz_trend(struct thermal_zone_device *, int);
368struct thermal_instance *get_thermal_instance(struct thermal_zone_device *,
369 struct thermal_cooling_device *, int);
Durgadoss Rdc765482012-09-18 11:05:00 +0530370void thermal_cdev_update(struct thermal_cooling_device *);
Eduardo Valentin7b73c992013-04-23 21:48:14 +0000371void thermal_notify_framework(struct thermal_zone_device *, int);
Durgadoss Ra4a15482012-09-18 11:04:57 +0530372
Rafael J. Wysockiaf062162011-03-01 01:12:19 +0100373#ifdef CONFIG_NET
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +0000374extern int thermal_generate_netlink_event(struct thermal_zone_device *tz,
375 enum events event);
Rafael J. Wysockiaf062162011-03-01 01:12:19 +0100376#else
Ezequiel Garciaf8b58702013-03-20 21:38:07 +0000377static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz,
Eduardo Valentin8ab3e6a2013-01-02 15:29:39 +0000378 enum events event)
Rafael J. Wysockiaf062162011-03-01 01:12:19 +0100379{
380 return 0;
381}
382#endif
Zhang Rui203d3d42008-01-17 15:51:08 +0800383
Len Browna0dd25b2008-02-09 04:01:48 -0500384#endif /* __THERMAL_H__ */