hwmon-kernel-api.rst 14 KB

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