2 * thermal.h ($Revision: 0 $)
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>
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.
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.
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.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 #include <linux/idr.h>
30 #include <linux/device.h>
31 #include <linux/workqueue.h>
32 #include <uapi/linux/thermal.h>
34 #define THERMAL_TRIPS_NONE -1
35 #define THERMAL_MAX_TRIPS 12
37 /* invalid cooling state */
38 #define THERMAL_CSTATE_INVALID -1UL
40 /* No upper/lower limit requirement */
41 #define THERMAL_NO_LIMIT ((u32)~0)
43 /* Default weight of a bound cooling device */
44 #define THERMAL_WEIGHT_DEFAULT 0
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)
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)
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)
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"
64 struct thermal_zone_device
;
65 struct thermal_cooling_device
;
67 enum thermal_device_mode
{
68 THERMAL_DEVICE_DISABLED
= 0,
69 THERMAL_DEVICE_ENABLED
,
72 enum thermal_trip_type
{
73 THERMAL_TRIP_ACTIVE
= 0,
76 THERMAL_TRIP_CRITICAL
,
80 THERMAL_TREND_STABLE
, /* temperature is stable */
81 THERMAL_TREND_RAISING
, /* temperature is raising */
82 THERMAL_TREND_DROPPING
, /* temperature is dropping */
83 THERMAL_TREND_RAISE_FULL
, /* apply highest cooling action */
84 THERMAL_TREND_DROP_FULL
, /* apply lowest cooling action */
87 struct thermal_zone_device_ops
{
88 int (*bind
) (struct thermal_zone_device
*,
89 struct thermal_cooling_device
*);
90 int (*unbind
) (struct thermal_zone_device
*,
91 struct thermal_cooling_device
*);
92 int (*get_temp
) (struct thermal_zone_device
*, unsigned long *);
93 int (*get_mode
) (struct thermal_zone_device
*,
94 enum thermal_device_mode
*);
95 int (*set_mode
) (struct thermal_zone_device
*,
96 enum thermal_device_mode
);
97 int (*get_trip_type
) (struct thermal_zone_device
*, int,
98 enum thermal_trip_type
*);
99 int (*get_trip_temp
) (struct thermal_zone_device
*, int,
101 int (*set_trip_temp
) (struct thermal_zone_device
*, int,
103 int (*get_trip_hyst
) (struct thermal_zone_device
*, int,
105 int (*set_trip_hyst
) (struct thermal_zone_device
*, int,
107 int (*get_crit_temp
) (struct thermal_zone_device
*, unsigned long *);
108 int (*set_emul_temp
) (struct thermal_zone_device
*, unsigned long);
109 int (*get_trend
) (struct thermal_zone_device
*, int,
110 enum thermal_trend
*);
111 int (*notify
) (struct thermal_zone_device
*, int,
112 enum thermal_trip_type
);
115 struct thermal_cooling_device_ops
{
116 int (*get_max_state
) (struct thermal_cooling_device
*, unsigned long *);
117 int (*get_cur_state
) (struct thermal_cooling_device
*, unsigned long *);
118 int (*set_cur_state
) (struct thermal_cooling_device
*, unsigned long);
121 struct thermal_cooling_device
{
123 char type
[THERMAL_NAME_LENGTH
];
124 struct device device
;
125 struct device_node
*np
;
127 const struct thermal_cooling_device_ops
*ops
;
128 bool updated
; /* true if the cooling device does not need update */
129 struct mutex lock
; /* protect thermal_instances list */
130 struct list_head thermal_instances
;
131 struct list_head node
;
134 struct thermal_attr
{
135 struct device_attribute attr
;
136 char name
[THERMAL_NAME_LENGTH
];
140 * struct thermal_zone_device - structure for a thermal zone
141 * @id: unique id number for each thermal zone
142 * @type: the thermal zone device type
143 * @device: &struct device for this thermal zone
144 * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
145 * @trip_type_attrs: attributes for trip points for sysfs: trip type
146 * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
147 * @devdata: private pointer for device private data
148 * @trips: number of trip points the thermal zone supports
149 * @passive_delay: number of milliseconds to wait between polls when
150 * performing passive cooling. Currenty only used by the
152 * @polling_delay: number of milliseconds to wait between polls when
153 * checking whether trip points have been crossed (0 for
154 * interrupt driven systems)
155 * @temperature: current temperature. This is only for core code,
156 * drivers should use thermal_zone_get_temp() to get the
157 * current temperature
158 * @last_temperature: previous temperature read
159 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
160 * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
161 * Currenty only used by the step-wise governor.
162 * @forced_passive: If > 0, temperature at which to switch on all ACPI
163 * processor cooling devices. Currently only used by the
164 * step-wise governor.
165 * @ops: operations this &thermal_zone_device supports
166 * @tzp: thermal zone parameters
167 * @governor: pointer to the governor for this thermal zone
168 * @thermal_instances: list of &struct thermal_instance of this thermal zone
169 * @idr: &struct idr to generate unique id for this zone's cooling
171 * @lock: lock to protect thermal_instances list
172 * @node: node in thermal_tz_list (in thermal_core.c)
173 * @poll_queue: delayed work for polling
175 struct thermal_zone_device
{
177 char type
[THERMAL_NAME_LENGTH
];
178 struct device device
;
179 struct thermal_attr
*trip_temp_attrs
;
180 struct thermal_attr
*trip_type_attrs
;
181 struct thermal_attr
*trip_hyst_attrs
;
187 int last_temperature
;
188 int emul_temperature
;
190 unsigned int forced_passive
;
191 struct thermal_zone_device_ops
*ops
;
192 const struct thermal_zone_params
*tzp
;
193 struct thermal_governor
*governor
;
194 struct list_head thermal_instances
;
197 struct list_head node
;
198 struct delayed_work poll_queue
;
202 * struct thermal_governor - structure that holds thermal governor information
203 * @name: name of the governor
204 * @throttle: callback called for every trip point even if temperature is
205 * below the trip point temperature
206 * @governor_list: node in thermal_governor_list (in thermal_core.c)
208 struct thermal_governor
{
209 char name
[THERMAL_NAME_LENGTH
];
210 int (*throttle
)(struct thermal_zone_device
*tz
, int trip
);
211 struct list_head governor_list
;
214 /* Structure that holds binding parameters for a zone */
215 struct thermal_bind_params
{
216 struct thermal_cooling_device
*cdev
;
219 * This is a measure of 'how effectively these devices can
220 * cool 'this' thermal zone. It shall be determined by
221 * platform characterization. This value is relative to the
222 * rest of the weights so a cooling device whose weight is
223 * double that of another cooling device is twice as
224 * effective. See Documentation/thermal/sysfs-api.txt for more
230 * This is a bit mask that gives the binding relation between this
231 * thermal zone and cdev, for a particular trip point.
232 * See Documentation/thermal/sysfs-api.txt for more information.
237 * This is an array of cooling state limits. Must have exactly
238 * 2 * thermal_zone.number_of_trip_points. It is an array consisting
239 * of tuples <lower-state upper-state> of state limits. Each trip
240 * will be associated with one state limit tuple when binding.
241 * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS>
244 unsigned long *binding_limits
;
245 int (*match
) (struct thermal_zone_device
*tz
,
246 struct thermal_cooling_device
*cdev
);
249 /* Structure to define Thermal Zone parameters */
250 struct thermal_zone_params
{
251 char governor_name
[THERMAL_NAME_LENGTH
];
254 * a boolean to indicate if the thermal to hwmon sysfs interface
255 * is required. when no_hwmon == false, a hwmon sysfs interface
256 * will be created. when no_hwmon == true, nothing will be done
260 int num_tbps
; /* Number of tbp entries */
261 struct thermal_bind_params
*tbp
;
264 struct thermal_genl_event
{
270 * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones
273 * @get_temp: a pointer to a function that reads the sensor temperature.
276 * @get_trend: a pointer to a function that reads the sensor temperature trend.
277 * @set_emul_temp: a pointer to a function that sets sensor emulated
280 struct thermal_zone_of_device_ops
{
281 int (*get_temp
)(void *, long *);
282 int (*get_trend
)(void *, long *);
283 int (*set_emul_temp
)(void *, unsigned long);
287 * struct thermal_trip - representation of a point in temperature domain
288 * @np: pointer to struct device_node that this trip point was created from
289 * @temperature: temperature value in miliCelsius
290 * @hysteresis: relative hysteresis in miliCelsius
291 * @type: trip point type
294 struct thermal_trip
{
295 struct device_node
*np
;
296 unsigned long int temperature
;
297 unsigned long int hysteresis
;
298 enum thermal_trip_type type
;
301 /* Function declarations */
302 #ifdef CONFIG_THERMAL_OF
303 struct thermal_zone_device
*
304 thermal_zone_of_sensor_register(struct device
*dev
, int id
, void *data
,
305 const struct thermal_zone_of_device_ops
*ops
);
306 void thermal_zone_of_sensor_unregister(struct device
*dev
,
307 struct thermal_zone_device
*tz
);
309 static inline struct thermal_zone_device
*
310 thermal_zone_of_sensor_register(struct device
*dev
, int id
, void *data
,
311 const struct thermal_zone_of_device_ops
*ops
)
317 void thermal_zone_of_sensor_unregister(struct device
*dev
,
318 struct thermal_zone_device
*tz
)
324 #if IS_ENABLED(CONFIG_THERMAL)
325 struct thermal_zone_device
*thermal_zone_device_register(const char *, int, int,
326 void *, struct thermal_zone_device_ops
*,
327 const struct thermal_zone_params
*, int, int);
328 void thermal_zone_device_unregister(struct thermal_zone_device
*);
330 int thermal_zone_bind_cooling_device(struct thermal_zone_device
*, int,
331 struct thermal_cooling_device
*,
332 unsigned long, unsigned long,
334 int thermal_zone_unbind_cooling_device(struct thermal_zone_device
*, int,
335 struct thermal_cooling_device
*);
336 void thermal_zone_device_update(struct thermal_zone_device
*);
338 struct thermal_cooling_device
*thermal_cooling_device_register(char *, void *,
339 const struct thermal_cooling_device_ops
*);
340 struct thermal_cooling_device
*
341 thermal_of_cooling_device_register(struct device_node
*np
, char *, void *,
342 const struct thermal_cooling_device_ops
*);
343 void thermal_cooling_device_unregister(struct thermal_cooling_device
*);
344 struct thermal_zone_device
*thermal_zone_get_zone_by_name(const char *name
);
345 int thermal_zone_get_temp(struct thermal_zone_device
*tz
, unsigned long *temp
);
347 int get_tz_trend(struct thermal_zone_device
*, int);
348 struct thermal_instance
*get_thermal_instance(struct thermal_zone_device
*,
349 struct thermal_cooling_device
*, int);
350 void thermal_cdev_update(struct thermal_cooling_device
*);
351 void thermal_notify_framework(struct thermal_zone_device
*, int);
353 static inline struct thermal_zone_device
*thermal_zone_device_register(
354 const char *type
, int trips
, int mask
, void *devdata
,
355 struct thermal_zone_device_ops
*ops
,
356 const struct thermal_zone_params
*tzp
,
357 int passive_delay
, int polling_delay
)
358 { return ERR_PTR(-ENODEV
); }
359 static inline void thermal_zone_device_unregister(
360 struct thermal_zone_device
*tz
)
362 static inline int thermal_zone_bind_cooling_device(
363 struct thermal_zone_device
*tz
, int trip
,
364 struct thermal_cooling_device
*cdev
,
365 unsigned long upper
, unsigned long lower
)
367 static inline int thermal_zone_unbind_cooling_device(
368 struct thermal_zone_device
*tz
, int trip
,
369 struct thermal_cooling_device
*cdev
)
371 static inline void thermal_zone_device_update(struct thermal_zone_device
*tz
)
373 static inline struct thermal_cooling_device
*
374 thermal_cooling_device_register(char *type
, void *devdata
,
375 const struct thermal_cooling_device_ops
*ops
)
376 { return ERR_PTR(-ENODEV
); }
377 static inline struct thermal_cooling_device
*
378 thermal_of_cooling_device_register(struct device_node
*np
,
379 char *type
, void *devdata
, const struct thermal_cooling_device_ops
*ops
)
380 { return ERR_PTR(-ENODEV
); }
381 static inline void thermal_cooling_device_unregister(
382 struct thermal_cooling_device
*cdev
)
384 static inline struct thermal_zone_device
*thermal_zone_get_zone_by_name(
386 { return ERR_PTR(-ENODEV
); }
387 static inline int thermal_zone_get_temp(
388 struct thermal_zone_device
*tz
, unsigned long *temp
)
390 static inline int get_tz_trend(struct thermal_zone_device
*tz
, int trip
)
392 static inline struct thermal_instance
*
393 get_thermal_instance(struct thermal_zone_device
*tz
,
394 struct thermal_cooling_device
*cdev
, int trip
)
395 { return ERR_PTR(-ENODEV
); }
396 static inline void thermal_cdev_update(struct thermal_cooling_device
*cdev
)
398 static inline void thermal_notify_framework(struct thermal_zone_device
*tz
,
401 #endif /* CONFIG_THERMAL */
403 #if defined(CONFIG_NET) && IS_ENABLED(CONFIG_THERMAL)
404 extern int thermal_generate_netlink_event(struct thermal_zone_device
*tz
,
407 static inline int thermal_generate_netlink_event(struct thermal_zone_device
*tz
,
414 #endif /* __THERMAL_H__ */