hwmon-kernel-api.rst 13 KB

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