hwmon-kernel-api.rst 14 KB

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