hwmon-kernel-api.txt 12 KB

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