sysfs-api.rst 15 KB

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