hwmon-kernel-api.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 *
  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. struct device *dev Pointer to parent device
  83. const char *name Device name
  84. void *drvdata Driver private data
  85. const struct hwmon_chip_info *info
  86. Pointer to chip description.
  87. const struct attribute_group **extra_groups
  88. Null-terminated list of additional non-standard
  89. sysfs attribute groups.
  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: Pointer to device operations.
  99. * info: NULL-terminated list of device channel descriptors.
  100. The list of hwmon operations is defined as:
  101. struct hwmon_ops {
  102. umode_t (*is_visible)(const void *, enum hwmon_sensor_types type,
  103. u32 attr, int);
  104. int (*read)(struct device *, enum hwmon_sensor_types type,
  105. u32 attr, int, long *);
  106. int (*write)(struct device *, enum hwmon_sensor_types type,
  107. u32 attr, int, long);
  108. };
  109. It defines the following operations.
  110. * is_visible: Pointer to a function to return the file mode for each supported
  111. attribute. This function is mandatory.
  112. * read: Pointer to a function for reading a value from the chip. This function
  113. is optional, but must be provided if any readable attributes exist.
  114. * write: Pointer to a function for writing a value to the chip. This function is
  115. optional, but must be provided if any writeable attributes exist.
  116. Each sensor channel is described with struct hwmon_channel_info, which is
  117. defined as follows.
  118. struct hwmon_channel_info {
  119. enum hwmon_sensor_types type;
  120. u32 *config;
  121. };
  122. It contains following fields:
  123. * type: The hardware monitoring sensor type.
  124. Supported sensor types are
  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_humidity Humidity sensor
  133. * hwmon_fan Fan speed sensor
  134. * hwmon_pwm PWM control
  135. * config: Pointer to a 0-terminated list of configuration values for each
  136. sensor of the given type. Each value is a combination of bit values
  137. describing the attributes supposed by a single sensor.
  138. As an example, here is the complete description file for a LM75 compatible
  139. sensor chip. The chip has a single temperature sensor. The driver wants to
  140. register with the thermal subsystem (HWMON_C_REGISTER_TZ), and it supports
  141. the update_interval attribute (HWMON_C_UPDATE_INTERVAL). The chip supports
  142. reading the temperature (HWMON_T_INPUT), it has a maximum temperature
  143. register (HWMON_T_MAX) as well as a maximum temperature hysteresis register
  144. (HWMON_T_MAX_HYST).
  145. static const u32 lm75_chip_config[] = {
  146. HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
  147. 0
  148. };
  149. static const struct hwmon_channel_info lm75_chip = {
  150. .type = hwmon_chip,
  151. .config = lm75_chip_config,
  152. };
  153. static const u32 lm75_temp_config[] = {
  154. HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST,
  155. 0
  156. };
  157. static const struct hwmon_channel_info lm75_temp = {
  158. .type = hwmon_temp,
  159. .config = lm75_temp_config,
  160. };
  161. static const struct hwmon_channel_info *lm75_info[] = {
  162. &lm75_chip,
  163. &lm75_temp,
  164. NULL
  165. };
  166. static const struct hwmon_ops lm75_hwmon_ops = {
  167. .is_visible = lm75_is_visible,
  168. .read = lm75_read,
  169. .write = lm75_write,
  170. };
  171. static const struct hwmon_chip_info lm75_chip_info = {
  172. .ops = &lm75_hwmon_ops,
  173. .info = lm75_info,
  174. };
  175. A complete list of bit values indicating individual attribute support
  176. is defined in include/linux/hwmon.h. Definition prefixes are as follows.
  177. HWMON_C_xxxx Chip attributes, for use with hwmon_chip.
  178. HWMON_T_xxxx Temperature attributes, for use with hwmon_temp.
  179. HWMON_I_xxxx Voltage attributes, for use with hwmon_in.
  180. HWMON_C_xxxx Current attributes, for use with hwmon_curr.
  181. Notice the prefix overlap with chip attributes.
  182. HWMON_P_xxxx Power attributes, for use with hwmon_power.
  183. HWMON_E_xxxx Energy attributes, for use with hwmon_energy.
  184. HWMON_H_xxxx Humidity attributes, for use with hwmon_humidity.
  185. HWMON_F_xxxx Fan speed attributes, for use with hwmon_fan.
  186. HWMON_PWM_xxxx PWM control attributes, for use with hwmon_pwm.
  187. Driver callback functions
  188. -------------------------
  189. Each driver provides is_visible, read, and write functions. Parameters
  190. and return values for those functions are as follows.
  191. umode_t is_visible_func(const void *data, enum hwmon_sensor_types type,
  192. u32 attr, int channel)
  193. Parameters:
  194. data: Pointer to device private data structure.
  195. type: The sensor type.
  196. attr: Attribute identifier associated with a specific attribute.
  197. For example, the attribute value for HWMON_T_INPUT would be
  198. hwmon_temp_input. For complete mappings of bit fields to
  199. attribute values please see include/linux/hwmon.h.
  200. channel:The sensor channel number.
  201. Return value:
  202. The file mode for this attribute. Typically, this will be 0 (the
  203. attribute will not be created), S_IRUGO, or 'S_IRUGO | S_IWUSR'.
  204. int read_func(struct device *dev, enum hwmon_sensor_types type,
  205. u32 attr, int channel, long *val)
  206. Parameters:
  207. dev: Pointer to the hardware monitoring device.
  208. type: The sensor type.
  209. attr: Attribute identifier associated with a specific attribute.
  210. For example, the attribute value for HWMON_T_INPUT would be
  211. hwmon_temp_input. For complete mappings please see
  212. include/linux/hwmon.h.
  213. channel:The sensor channel number.
  214. val: Pointer to attribute value.
  215. Return value:
  216. 0 on success, a negative error number otherwise.
  217. int write_func(struct device *dev, enum hwmon_sensor_types type,
  218. u32 attr, int channel, long val)
  219. Parameters:
  220. dev: Pointer to the hardware monitoring device.
  221. type: The sensor type.
  222. attr: 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:The sensor channel number.
  227. val: The value to write to the chip.
  228. Return value:
  229. 0 on success, a negative error number otherwise.
  230. Driver-provided sysfs attributes
  231. --------------------------------
  232. If the hardware monitoring device is registered with
  233. hwmon_device_register_with_info or devm_hwmon_device_register_with_info,
  234. it is most likely not necessary to provide sysfs attributes. Only additional
  235. non-standard sysfs attributes need to be provided when one of those registration
  236. functions is used.
  237. The header file linux/hwmon-sysfs.h provides a number of useful macros to
  238. declare and use hardware monitoring sysfs attributes.
  239. In many cases, you can use the exsting define DEVICE_ATTR to declare such
  240. attributes. This is feasible if an attribute has no additional context. However,
  241. in many cases there will be additional information such as a sensor index which
  242. will need to be passed to the sysfs attribute handling function.
  243. SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 can be used to define attributes
  244. which need such additional context information. SENSOR_DEVICE_ATTR requires
  245. one additional argument, SENSOR_DEVICE_ATTR_2 requires two.
  246. SENSOR_DEVICE_ATTR defines a struct sensor_device_attribute variable.
  247. This structure has the following fields.
  248. struct sensor_device_attribute {
  249. struct device_attribute dev_attr;
  250. int index;
  251. };
  252. You can use to_sensor_dev_attr to get the pointer to this structure from the
  253. attribute read or write function. Its parameter is the device to which the
  254. attribute is attached.
  255. SENSOR_DEVICE_ATTR_2 defines a struct sensor_device_attribute_2 variable,
  256. which is defined as follows.
  257. struct sensor_device_attribute_2 {
  258. struct device_attribute dev_attr;
  259. u8 index;
  260. u8 nr;
  261. };
  262. Use to_sensor_dev_attr_2 to get the pointer to this structure. Its parameter
  263. is the device to which the attribute is attached.