hwmon-kernel-api.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. The Linux Hardware Monitoring kernel API
  2. ========================================
  3. Guenter Roeck
  4. Introduction
  5. ------------
  6. This document describes the API that can be used by hardware monitoring
  7. drivers that want to use the hardware monitoring framework.
  8. This document does not describe what a hardware monitoring (hwmon) Driver or
  9. Device is. It also does not describe the API which can be used by user space
  10. to communicate with a hardware monitoring device. If you want to know this
  11. then please read the following file: Documentation/hwmon/sysfs-interface.rst.
  12. For additional guidelines on how to write and improve hwmon drivers, please
  13. also read Documentation/hwmon/submitting-patches.rst.
  14. The API
  15. -------
  16. Each hardware monitoring driver must #include <linux/hwmon.h> and, in some
  17. cases, <linux/hwmon-sysfs.h>. linux/hwmon.h declares the following
  18. register/unregister functions::
  19. struct device *
  20. hwmon_device_register_with_info(struct device *dev,
  21. const char *name, void *drvdata,
  22. const struct hwmon_chip_info *info,
  23. const struct attribute_group **extra_groups);
  24. struct device *
  25. devm_hwmon_device_register_with_info(struct device *dev,
  26. const char *name,
  27. void *drvdata,
  28. const struct hwmon_chip_info *info,
  29. const struct attribute_group **extra_groups);
  30. void hwmon_device_unregister(struct device *dev);
  31. char *hwmon_sanitize_name(const char *name);
  32. char *devm_hwmon_sanitize_name(struct device *dev, const char *name);
  33. hwmon_device_register_with_info registers a hardware monitoring device.
  34. It creates the standard sysfs attributes in the hardware monitoring core,
  35. letting the driver focus on reading from and writing to the chip instead
  36. of having to bother with sysfs attributes. The parent device parameter
  37. as well as the chip parameter must not be NULL. Its parameters are described
  38. in more detail below.
  39. devm_hwmon_device_register_with_info is similar to
  40. hwmon_device_register_with_info. However, it is device managed, meaning the
  41. hwmon device does not have to be removed explicitly by the removal function.
  42. All other hardware monitoring device registration functions are deprecated
  43. and must not be used in new drivers.
  44. hwmon_device_unregister deregisters a registered hardware monitoring device.
  45. The parameter of this function is the pointer to the registered hardware
  46. monitoring device structure. This function must be called from the driver
  47. remove function if the hardware monitoring device was registered with
  48. hwmon_device_register_with_info.
  49. All supported hwmon device registration functions only accept valid device
  50. names. Device names including invalid characters (whitespace, '*', or '-')
  51. will be rejected. The 'name' parameter is mandatory.
  52. If the driver doesn't use a static device name (for example it uses
  53. dev_name()), and therefore cannot make sure the name only contains valid
  54. characters, hwmon_sanitize_name can be used. This convenience function
  55. will duplicate the string and replace any invalid characters with an
  56. underscore. It will allocate memory for the new string and it is the
  57. responsibility of the caller to release the memory when the device is
  58. removed.
  59. devm_hwmon_sanitize_name is the resource managed version of
  60. hwmon_sanitize_name; the memory will be freed automatically on device
  61. removal.
  62. Using devm_hwmon_device_register_with_info()
  63. --------------------------------------------
  64. hwmon_device_register_with_info() registers a hardware monitoring device.
  65. The parameters to this function are
  66. =============================================== ===============================================
  67. `struct device *dev` Pointer to parent device
  68. `const char *name` Device name
  69. `void *drvdata` Driver private data
  70. `const struct hwmon_chip_info *info` Pointer to chip description.
  71. `const struct attribute_group **extra_groups` Null-terminated list of additional non-standard
  72. sysfs attribute groups.
  73. =============================================== ===============================================
  74. This function returns a pointer to the created hardware monitoring device
  75. on success and a negative error code for failure.
  76. The hwmon_chip_info structure looks as follows::
  77. struct hwmon_chip_info {
  78. const struct hwmon_ops *ops;
  79. const struct hwmon_channel_info * const *info;
  80. };
  81. It contains the following fields:
  82. * ops:
  83. Pointer to device operations.
  84. * info:
  85. NULL-terminated list of device channel descriptors.
  86. The list of hwmon operations is defined as::
  87. struct hwmon_ops {
  88. umode_t (*is_visible)(const void *, enum hwmon_sensor_types type,
  89. u32 attr, int);
  90. int (*read)(struct device *, enum hwmon_sensor_types type,
  91. u32 attr, int, long *);
  92. int (*write)(struct device *, enum hwmon_sensor_types type,
  93. u32 attr, int, long);
  94. };
  95. It defines the following operations.
  96. * is_visible:
  97. Pointer to a function to return the file mode for each supported
  98. attribute. This function is mandatory.
  99. * read:
  100. Pointer to a function for reading a value from the chip. This function
  101. is optional, but must be provided if any readable attributes exist.
  102. * write:
  103. Pointer to a function for writing a value to the chip. This function is
  104. optional, but must be provided if any writeable attributes exist.
  105. Each sensor channel is described with struct hwmon_channel_info, which is
  106. defined as follows::
  107. struct hwmon_channel_info {
  108. enum hwmon_sensor_types type;
  109. u32 *config;
  110. };
  111. It contains following fields:
  112. * type:
  113. The hardware monitoring sensor type.
  114. Supported sensor types are
  115. ================== ==================================================
  116. hwmon_chip A virtual sensor type, used to describe attributes
  117. which are not bound to a specific input or output
  118. hwmon_temp Temperature sensor
  119. hwmon_in Voltage sensor
  120. hwmon_curr Current sensor
  121. hwmon_power Power sensor
  122. hwmon_energy Energy sensor
  123. hwmon_humidity Humidity sensor
  124. hwmon_fan Fan speed sensor
  125. hwmon_pwm PWM control
  126. ================== ==================================================
  127. * config:
  128. Pointer to a 0-terminated list of configuration values for each
  129. sensor of the given type. Each value is a combination of bit values
  130. describing the attributes supposed by a single sensor.
  131. As an example, here is the complete description file for a LM75 compatible
  132. sensor chip. The chip has a single temperature sensor. The driver wants to
  133. register with the thermal subsystem (HWMON_C_REGISTER_TZ), and it supports
  134. the update_interval attribute (HWMON_C_UPDATE_INTERVAL). The chip supports
  135. reading the temperature (HWMON_T_INPUT), it has a maximum temperature
  136. register (HWMON_T_MAX) as well as a maximum temperature hysteresis register
  137. (HWMON_T_MAX_HYST)::
  138. static const u32 lm75_chip_config[] = {
  139. HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
  140. 0
  141. };
  142. static const struct hwmon_channel_info lm75_chip = {
  143. .type = hwmon_chip,
  144. .config = lm75_chip_config,
  145. };
  146. static const u32 lm75_temp_config[] = {
  147. HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST,
  148. 0
  149. };
  150. static const struct hwmon_channel_info lm75_temp = {
  151. .type = hwmon_temp,
  152. .config = lm75_temp_config,
  153. };
  154. static const struct hwmon_channel_info * const lm75_info[] = {
  155. &lm75_chip,
  156. &lm75_temp,
  157. NULL
  158. };
  159. The HWMON_CHANNEL_INFO() macro can and should be used when possible.
  160. With this macro, the above example can be simplified to
  161. static const struct hwmon_channel_info * const lm75_info[] = {
  162. HWMON_CHANNEL_INFO(chip,
  163. HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
  164. HWMON_CHANNEL_INFO(temp,
  165. HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
  166. NULL
  167. };
  168. The remaining declarations are as follows.
  169. static const struct hwmon_ops lm75_hwmon_ops = {
  170. .is_visible = lm75_is_visible,
  171. .read = lm75_read,
  172. .write = lm75_write,
  173. };
  174. static const struct hwmon_chip_info lm75_chip_info = {
  175. .ops = &lm75_hwmon_ops,
  176. .info = lm75_info,
  177. };
  178. A complete list of bit values indicating individual attribute support
  179. is defined in include/linux/hwmon.h. Definition prefixes are as follows.
  180. =============== =================================================
  181. HWMON_C_xxxx Chip attributes, for use with hwmon_chip.
  182. HWMON_T_xxxx Temperature attributes, for use with hwmon_temp.
  183. HWMON_I_xxxx Voltage attributes, for use with hwmon_in.
  184. HWMON_C_xxxx Current attributes, for use with hwmon_curr.
  185. Notice the prefix overlap with chip attributes.
  186. HWMON_P_xxxx Power attributes, for use with hwmon_power.
  187. HWMON_E_xxxx Energy attributes, for use with hwmon_energy.
  188. HWMON_H_xxxx Humidity attributes, for use with hwmon_humidity.
  189. HWMON_F_xxxx Fan speed attributes, for use with hwmon_fan.
  190. HWMON_PWM_xxxx PWM control attributes, for use with hwmon_pwm.
  191. =============== =================================================
  192. Driver callback functions
  193. -------------------------
  194. Each driver provides is_visible, read, and write functions. Parameters
  195. and return values for those functions are as follows::
  196. umode_t is_visible_func(const void *data, enum hwmon_sensor_types type,
  197. u32 attr, int channel)
  198. Parameters:
  199. data:
  200. Pointer to device private data structure.
  201. type:
  202. The sensor type.
  203. attr:
  204. Attribute identifier associated with a specific attribute.
  205. For example, the attribute value for HWMON_T_INPUT would be
  206. hwmon_temp_input. For complete mappings of bit fields to
  207. attribute values please see include/linux/hwmon.h.
  208. channel:
  209. The sensor channel number.
  210. Return value:
  211. The file mode for this attribute. Typically, this will be 0 (the
  212. attribute will not be created), 0444, or 0644.
  213. ::
  214. int read_func(struct device *dev, enum hwmon_sensor_types type,
  215. u32 attr, int channel, long *val)
  216. Parameters:
  217. dev:
  218. Pointer to the hardware monitoring device.
  219. type:
  220. The sensor type.
  221. attr:
  222. Attribute identifier associated with a specific attribute.
  223. For example, the attribute value for HWMON_T_INPUT would be
  224. hwmon_temp_input. For complete mappings please see
  225. include/linux/hwmon.h.
  226. channel:
  227. The sensor channel number.
  228. val:
  229. Pointer to attribute value.
  230. Return value:
  231. 0 on success, a negative error number otherwise.
  232. ::
  233. int write_func(struct device *dev, enum hwmon_sensor_types type,
  234. u32 attr, int channel, long val)
  235. Parameters:
  236. dev:
  237. Pointer to the hardware monitoring device.
  238. type:
  239. The sensor type.
  240. attr:
  241. Attribute identifier associated with a specific attribute.
  242. For example, the attribute value for HWMON_T_INPUT would be
  243. hwmon_temp_input. For complete mappings please see
  244. include/linux/hwmon.h.
  245. channel:
  246. The sensor channel number.
  247. val:
  248. The value to write to the chip.
  249. Return value:
  250. 0 on success, a negative error number otherwise.
  251. Driver-provided sysfs attributes
  252. --------------------------------
  253. In most situations it should not be necessary for a driver to provide sysfs
  254. attributes since the hardware monitoring core creates those internally.
  255. Only additional non-standard sysfs attributes need to be provided.
  256. The header file linux/hwmon-sysfs.h provides a number of useful macros to
  257. declare and use hardware monitoring sysfs attributes.
  258. In many cases, you can use the existing define DEVICE_ATTR or its variants
  259. DEVICE_ATTR_{RW,RO,WO} to declare such attributes. This is feasible if an
  260. attribute has no additional context. However, in many cases there will be
  261. additional information such as a sensor index which will need to be passed
  262. to the sysfs attribute handling function.
  263. SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 can be used to define attributes
  264. which need such additional context information. SENSOR_DEVICE_ATTR requires
  265. one additional argument, SENSOR_DEVICE_ATTR_2 requires two.
  266. Simplified variants of SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 are available
  267. and should be used if standard attribute permissions and function names are
  268. feasible. Standard permissions are 0644 for SENSOR_DEVICE_ATTR[_2]_RW,
  269. 0444 for SENSOR_DEVICE_ATTR[_2]_RO, and 0200 for SENSOR_DEVICE_ATTR[_2]_WO.
  270. Standard functions, similar to DEVICE_ATTR_{RW,RO,WO}, have _show and _store
  271. appended to the provided function name.
  272. SENSOR_DEVICE_ATTR and its variants define a struct sensor_device_attribute
  273. variable. This structure has the following fields::
  274. struct sensor_device_attribute {
  275. struct device_attribute dev_attr;
  276. int index;
  277. };
  278. You can use to_sensor_dev_attr to get the pointer to this structure from the
  279. attribute read or write function. Its parameter is the device to which the
  280. attribute is attached.
  281. SENSOR_DEVICE_ATTR_2 and its variants define a struct sensor_device_attribute_2
  282. variable, which is defined as follows::
  283. struct sensor_device_attribute_2 {
  284. struct device_attribute dev_attr;
  285. u8 index;
  286. u8 nr;
  287. };
  288. Use to_sensor_dev_attr_2 to get the pointer to this structure. Its parameter
  289. is the device to which the attribute is attached.