xref: /linux/Documentation/driver-api/thermal/sysfs-api.rst (revision 6e7fd890f1d6ac83805409e9c346240de2705584)
1===================================
2Generic Thermal Sysfs driver How To
3===================================
4
5Written by Sujith Thomas <sujith.thomas@intel.com>, Zhang Rui <rui.zhang@intel.com>
6
7Copyright (c)  2008 Intel Corporation
8
9
100. Introduction
11===============
12
13The generic thermal sysfs provides a set of interfaces for thermal zone
14devices (sensors) and thermal cooling devices (fan, processor...) to register
15with the thermal management solution and to be a part of it.
16
17This how-to focuses on enabling new thermal zone and cooling devices to
18participate in thermal management.
19This solution is platform independent and any type of thermal zone devices
20and cooling devices should be able to make use of the infrastructure.
21
22The main task of the thermal sysfs driver is to expose thermal zone attributes
23as well as cooling device attributes to the user space.
24An intelligent thermal management application can make decisions based on
25inputs from thermal zone attributes (the current temperature and trip point
26temperature) and throttle appropriate devices.
27
28- `[0-*]`	denotes any positive number starting from 0
29- `[1-*]`	denotes any positive number starting from 1
30
311. thermal sysfs driver interface functions
32===========================================
33
341.1 thermal zone device interface
35---------------------------------
36
37    ::
38
39	struct thermal_zone_device *
40	thermal_zone_device_register_with_trips(const char *type,
41					const struct thermal_trip *trips,
42					int num_trips, void *devdata,
43					const struct thermal_zone_device_ops *ops,
44					const struct thermal_zone_params *tzp,
45					unsigned int passive_delay,
46					unsigned int polling_delay)
47
48    This interface function adds a new thermal zone device (sensor) to the
49    /sys/class/thermal folder as `thermal_zone[0-*]`. It tries to bind all the
50    thermal cooling devices registered to it at the same time.
51
52    type:
53	the thermal zone type.
54    trips:
55	the table of trip points for this thermal zone.
56    devdata:
57	device private data
58    ops:
59	thermal zone device call-backs.
60
61	.bind:
62		bind the thermal zone device with a thermal cooling device.
63	.unbind:
64		unbind the thermal zone device with a thermal cooling device.
65	.get_temp:
66		get the current temperature of the thermal zone.
67	.set_trips:
68		set the trip points window. Whenever the current temperature
69		is updated, the trip points immediately below and above the
70		current temperature are found.
71	.change_mode:
72		change the mode (enabled/disabled) of the thermal zone.
73	.set_trip_temp:
74		set the temperature of a given trip point.
75	.get_crit_temp:
76		get the critical temperature for this thermal zone.
77	.set_emul_temp:
78		set the emulation temperature which helps in debugging
79		different threshold temperature points.
80	.get_trend:
81		get the trend of most recent zone temperature changes.
82	.hot:
83		hot trip point crossing handler.
84	.critical:
85		critical trip point crossing handler.
86    tzp:
87	thermal zone platform parameters.
88    passive_delay:
89	number of milliseconds to wait between polls when performing passive
90	cooling.
91    polling_delay:
92	number of milliseconds to wait between polls when checking
93	whether trip points have been crossed (0 for interrupt driven systems).
94
95    ::
96
97	void thermal_zone_device_unregister(struct thermal_zone_device *tz)
98
99    This interface function removes the thermal zone device.
100    It deletes the corresponding entry from /sys/class/thermal folder and
101    unbinds all the thermal cooling devices it uses.
102
103	::
104
105	   struct thermal_zone_device
106	   *thermal_zone_of_sensor_register(struct device *dev, int sensor_id,
107				void *data,
108				const struct thermal_zone_of_device_ops *ops)
109
110	This interface adds a new sensor to a DT thermal zone.
111	This function will search the list of thermal zones described in
112	device tree and look for the zone that refer to the sensor device
113	pointed by dev->of_node as temperature providers. For the zone
114	pointing to the sensor node, the sensor will be added to the DT
115	thermal zone device.
116
117	The parameters for this interface are:
118
119	dev:
120			Device node of sensor containing valid node pointer in
121			dev->of_node.
122	sensor_id:
123			a sensor identifier, in case the sensor IP has more
124			than one sensors
125	data:
126			a private pointer (owned by the caller) that will be
127			passed back, when a temperature reading is needed.
128	ops:
129			`struct thermal_zone_of_device_ops *`.
130
131			==============  =======================================
132			get_temp	a pointer to a function that reads the
133					sensor temperature. This is mandatory
134					callback provided by sensor driver.
135			set_trips	a pointer to a function that sets a
136					temperature window. When this window is
137					left the driver must inform the thermal
138					core via thermal_zone_device_update.
139			get_trend 	a pointer to a function that reads the
140					sensor temperature trend.
141			set_emul_temp	a pointer to a function that sets
142					sensor emulated temperature.
143			==============  =======================================
144
145	The thermal zone temperature is provided by the get_temp() function
146	pointer of thermal_zone_of_device_ops. When called, it will
147	have the private pointer @data back.
148
149	It returns error pointer if fails otherwise valid thermal zone device
150	handle. Caller should check the return handle with IS_ERR() for finding
151	whether success or not.
152
153	::
154
155	    void thermal_zone_of_sensor_unregister(struct device *dev,
156						   struct thermal_zone_device *tzd)
157
158	This interface unregisters a sensor from a DT thermal zone which was
159	successfully added by interface thermal_zone_of_sensor_register().
160	This function removes the sensor callbacks and private data from the
161	thermal zone device registered with thermal_zone_of_sensor_register()
162	interface. It will also silent the zone by remove the .get_temp() and
163	get_trend() thermal zone device callbacks.
164
165	::
166
167	  struct thermal_zone_device
168	  *devm_thermal_zone_of_sensor_register(struct device *dev,
169				int sensor_id,
170				void *data,
171				const struct thermal_zone_of_device_ops *ops)
172
173	This interface is resource managed version of
174	thermal_zone_of_sensor_register().
175
176	All details of thermal_zone_of_sensor_register() described in
177	section 1.1.3 is applicable here.
178
179	The benefit of using this interface to register sensor is that it
180	is not require to explicitly call thermal_zone_of_sensor_unregister()
181	in error path or during driver unbinding as this is done by driver
182	resource manager.
183
184	::
185
186		void devm_thermal_zone_of_sensor_unregister(struct device *dev,
187						struct thermal_zone_device *tzd)
188
189	This interface is resource managed version of
190	thermal_zone_of_sensor_unregister().
191	All details of thermal_zone_of_sensor_unregister() described in
192	section 1.1.4 is applicable here.
193	Normally this function will not need to be called and the resource
194	management code will ensure that the resource is freed.
195
196	::
197
198		int thermal_zone_get_slope(struct thermal_zone_device *tz)
199
200	This interface is used to read the slope attribute value
201	for the thermal zone device, which might be useful for platform
202	drivers for temperature calculations.
203
204	::
205
206		int thermal_zone_get_offset(struct thermal_zone_device *tz)
207
208	This interface is used to read the offset attribute value
209	for the thermal zone device, which might be useful for platform
210	drivers for temperature calculations.
211
2121.2 thermal cooling device interface
213------------------------------------
214
215
216    ::
217
218	struct thermal_cooling_device
219	*thermal_cooling_device_register(char *name,
220			void *devdata, struct thermal_cooling_device_ops *)
221
222    This interface function adds a new thermal cooling device (fan/processor/...)
223    to /sys/class/thermal/ folder as `cooling_device[0-*]`. It tries to bind itself
224    to all the thermal zone devices registered at the same time.
225
226    name:
227	the cooling device name.
228    devdata:
229	device private data.
230    ops:
231	thermal cooling devices call-backs.
232
233	.get_max_state:
234		get the Maximum throttle state of the cooling device.
235	.get_cur_state:
236		get the Currently requested throttle state of the
237		cooling device.
238	.set_cur_state:
239		set the Current throttle state of the cooling device.
240
241    ::
242
243	void thermal_cooling_device_unregister(struct thermal_cooling_device *cdev)
244
245    This interface function removes the thermal cooling device.
246    It deletes the corresponding entry from /sys/class/thermal folder and
247    unbinds itself from all the thermal zone devices using it.
248
2491.3 interface for binding a thermal zone device with a thermal cooling device
250-----------------------------------------------------------------------------
251
252    ::
253
254	int thermal_zone_bind_cooling_device(struct thermal_zone_device *tz,
255		int trip, struct thermal_cooling_device *cdev,
256		unsigned long upper, unsigned long lower, unsigned int weight);
257
258    This interface function binds a thermal cooling device to a particular trip
259    point of a thermal zone device.
260
261    This function is usually called in the thermal zone device .bind callback.
262
263    tz:
264	  the thermal zone device
265    cdev:
266	  thermal cooling device
267    trip:
268	  indicates which trip point in this thermal zone the cooling device
269	  is associated with.
270    upper:
271	  the Maximum cooling state for this trip point.
272	  THERMAL_NO_LIMIT means no upper limit,
273	  and the cooling device can be in max_state.
274    lower:
275	  the Minimum cooling state can be used for this trip point.
276	  THERMAL_NO_LIMIT means no lower limit,
277	  and the cooling device can be in cooling state 0.
278    weight:
279	  the influence of this cooling device in this thermal
280	  zone.  See 1.4.1 below for more information.
281
282    ::
283
284	int thermal_zone_unbind_cooling_device(struct thermal_zone_device *tz,
285				int trip, struct thermal_cooling_device *cdev);
286
287    This interface function unbinds a thermal cooling device from a particular
288    trip point of a thermal zone device. This function is usually called in
289    the thermal zone device .unbind callback.
290
291    tz:
292	the thermal zone device
293    cdev:
294	thermal cooling device
295    trip:
296	indicates which trip point in this thermal zone the cooling device
297	is associated with.
298
2991.4 Thermal Zone Parameters
300---------------------------
301
302    ::
303
304	struct thermal_zone_params
305
306    This structure defines the platform level parameters for a thermal zone.
307    This data, for each thermal zone should come from the platform layer.
308    This is an optional feature where some platforms can choose not to
309    provide this data.
310
311    .governor_name:
312	       Name of the thermal governor used for this zone
313    .no_hwmon:
314	       a boolean to indicate if the thermal to hwmon sysfs interface
315	       is required. when no_hwmon == false, a hwmon sysfs interface
316	       will be created. when no_hwmon == true, nothing will be done.
317	       In case the thermal_zone_params is NULL, the hwmon interface
318	       will be created (for backward compatibility).
319
3202. sysfs attributes structure
321=============================
322
323==	================
324RO	read only value
325WO	write only value
326RW	read/write value
327==	================
328
329Thermal sysfs attributes will be represented under /sys/class/thermal.
330Hwmon sysfs I/F extension is also available under /sys/class/hwmon
331if hwmon is compiled in or built as a module.
332
333Thermal zone device sys I/F, created once it's registered::
334
335  /sys/class/thermal/thermal_zone[0-*]:
336    |---type:			Type of the thermal zone
337    |---temp:			Current temperature
338    |---mode:			Working mode of the thermal zone
339    |---policy:			Thermal governor used for this zone
340    |---available_policies:	Available thermal governors for this zone
341    |---trip_point_[0-*]_temp:	Trip point temperature
342    |---trip_point_[0-*]_type:	Trip point type
343    |---trip_point_[0-*]_hyst:	Hysteresis value for this trip point
344    |---emul_temp:		Emulated temperature set node
345    |---sustainable_power:      Sustainable dissipatable power
346    |---k_po:                   Proportional term during temperature overshoot
347    |---k_pu:                   Proportional term during temperature undershoot
348    |---k_i:                    PID's integral term in the power allocator gov
349    |---k_d:                    PID's derivative term in the power allocator
350    |---integral_cutoff:        Offset above which errors are accumulated
351    |---slope:                  Slope constant applied as linear extrapolation
352    |---offset:                 Offset constant applied as linear extrapolation
353
354Thermal cooling device sys I/F, created once it's registered::
355
356  /sys/class/thermal/cooling_device[0-*]:
357    |---type:			Type of the cooling device(processor/fan/...)
358    |---max_state:		Maximum cooling state of the cooling device
359    |---cur_state:		Current cooling state of the cooling device
360    |---stats:			Directory containing cooling device's statistics
361    |---stats/reset:		Writing any value resets the statistics
362    |---stats/time_in_state_ms:	Time (msec) spent in various cooling states
363    |---stats/total_trans:	Total number of times cooling state is changed
364    |---stats/trans_table:	Cooling state transition table
365
366
367Then next two dynamic attributes are created/removed in pairs. They represent
368the relationship between a thermal zone and its associated cooling device.
369They are created/removed for each successful execution of
370thermal_zone_bind_cooling_device/thermal_zone_unbind_cooling_device.
371
372::
373
374  /sys/class/thermal/thermal_zone[0-*]:
375    |---cdev[0-*]:		[0-*]th cooling device in current thermal zone
376    |---cdev[0-*]_trip_point:	Trip point that cdev[0-*] is associated with
377    |---cdev[0-*]_weight:       Influence of the cooling device in
378				this thermal zone
379
380Besides the thermal zone device sysfs I/F and cooling device sysfs I/F,
381the generic thermal driver also creates a hwmon sysfs I/F for each _type_
382of thermal zone device. E.g. the generic thermal driver registers one hwmon
383class device and build the associated hwmon sysfs I/F for all the registered
384ACPI thermal zones.
385
386Please read Documentation/ABI/testing/sysfs-class-thermal for thermal
387zone and cooling device attribute details.
388
389::
390
391  /sys/class/hwmon/hwmon[0-*]:
392    |---name:			The type of the thermal zone devices
393    |---temp[1-*]_input:	The current temperature of thermal zone [1-*]
394    |---temp[1-*]_critical:	The critical trip point of thermal zone [1-*]
395
396Please read Documentation/hwmon/sysfs-interface.rst for additional information.
397
3983. A simple implementation
399==========================
400
401ACPI thermal zone may support multiple trip points like critical, hot,
402passive, active. If an ACPI thermal zone supports critical, passive,
403active[0] and active[1] at the same time, it may register itself as a
404thermal_zone_device (thermal_zone1) with 4 trip points in all.
405It has one processor and one fan, which are both registered as
406thermal_cooling_device. Both are considered to have the same
407effectiveness in cooling the thermal zone.
408
409If the processor is listed in _PSL method, and the fan is listed in _AL0
410method, the sys I/F structure will be built like this::
411
412 /sys/class/thermal:
413  |thermal_zone1:
414    |---type:			acpitz
415    |---temp:			37000
416    |---mode:			enabled
417    |---policy:			step_wise
418    |---available_policies:	step_wise fair_share
419    |---trip_point_0_temp:	100000
420    |---trip_point_0_type:	critical
421    |---trip_point_1_temp:	80000
422    |---trip_point_1_type:	passive
423    |---trip_point_2_temp:	70000
424    |---trip_point_2_type:	active0
425    |---trip_point_3_temp:	60000
426    |---trip_point_3_type:	active1
427    |---cdev0:			--->/sys/class/thermal/cooling_device0
428    |---cdev0_trip_point:	1	/* cdev0 can be used for passive */
429    |---cdev0_weight:           1024
430    |---cdev1:			--->/sys/class/thermal/cooling_device3
431    |---cdev1_trip_point:	2	/* cdev1 can be used for active[0]*/
432    |---cdev1_weight:           1024
433
434  |cooling_device0:
435    |---type:			Processor
436    |---max_state:		8
437    |---cur_state:		0
438
439  |cooling_device3:
440    |---type:			Fan
441    |---max_state:		2
442    |---cur_state:		0
443
444 /sys/class/hwmon:
445  |hwmon0:
446    |---name:			acpitz
447    |---temp1_input:		37000
448    |---temp1_crit:		100000
449
4504. Export Symbol APIs
451=====================
452
4534.1. get_tz_trend
454-----------------
455
456This function returns the trend of a thermal zone, i.e the rate of change
457of temperature of the thermal zone. Ideally, the thermal sensor drivers
458are supposed to implement the callback. If they don't, the thermal
459framework calculated the trend by comparing the previous and the current
460temperature values.
461
4624.2. get_thermal_instance
463-------------------------
464
465This function returns the thermal_instance corresponding to a given
466{thermal_zone, cooling_device, trip_point} combination. Returns NULL
467if such an instance does not exist.
468
4694.3. thermal_cdev_update
470------------------------
471
472This function serves as an arbitrator to set the state of a cooling
473device. It sets the cooling device to the deepest cooling state if
474possible.
475
4765. thermal_emergency_poweroff
477=============================
478
479On an event of critical trip temperature crossing the thermal framework
480shuts down the system by calling hw_protection_shutdown(). The
481hw_protection_shutdown() first attempts to perform an orderly shutdown
482but accepts a delay after which it proceeds doing a forced power-off
483or as last resort an emergency_restart.
484
485The delay should be carefully profiled so as to give adequate time for
486orderly poweroff.
487
488If the delay is set to 0 emergency poweroff will not be supported. So a
489carefully profiled non-zero positive value is a must for emergency
490poweroff to be triggered.
491