hwmon-kernel-api.txt 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. void hwmon_device_unregister(struct device *dev);
  29. void devm_hwmon_device_unregister(struct device *dev);
  30. hwmon_device_register registers a hardware monitoring device. The parameter
  31. of this function is a pointer to the parent device.
  32. This function returns a pointer to the newly created hardware monitoring device
  33. or PTR_ERR for failure. If this registration function is used, hardware
  34. monitoring sysfs attributes are expected to have been created and attached to
  35. the parent device prior to calling hwmon_device_register. A name attribute must
  36. have been created by the caller.
  37. hwmon_device_register_with_groups is similar to hwmon_device_register. However,
  38. it has additional parameters. The name parameter is a pointer to the hwmon
  39. device name. The registration function wil create a name sysfs attribute
  40. pointing to this name. The drvdata parameter is the pointer to the local
  41. driver data. hwmon_device_register_with_groups will attach this pointer
  42. to the newly allocated hwmon device. The pointer can be retrieved by the driver
  43. using dev_get_drvdata() on the hwmon device pointer. The groups parameter is
  44. a pointer to a list of sysfs attribute groups. The list must be NULL terminated.
  45. hwmon_device_register_with_groups creates the hwmon device with name attribute
  46. as well as all sysfs attributes attached to the hwmon device.
  47. devm_hwmon_device_register_with_groups is similar to
  48. hwmon_device_register_with_groups. However, it is device managed, meaning the
  49. hwmon device does not have to be removed explicitly by the removal function.
  50. hwmon_device_unregister deregisters a registered hardware monitoring device.
  51. The parameter of this function is the pointer to the registered hardware
  52. monitoring device structure. This function must be called from the driver
  53. remove function if the hardware monitoring device was registered with
  54. hwmon_device_register or with hwmon_device_register_with_groups.
  55. devm_hwmon_device_unregister does not normally have to be called. It is only
  56. needed for error handling, and only needed if the driver probe fails after
  57. the call to devm_hwmon_device_register_with_groups.
  58. The header file linux/hwmon-sysfs.h provides a number of useful macros to
  59. declare and use hardware monitoring sysfs attributes.
  60. In many cases, you can use the exsting define DEVICE_ATTR to declare such
  61. attributes. This is feasible if an attribute has no additional context. However,
  62. in many cases there will be additional information such as a sensor index which
  63. will need to be passed to the sysfs attribute handling function.
  64. SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 can be used to define attributes
  65. which need such additional context information. SENSOR_DEVICE_ATTR requires
  66. one additional argument, SENSOR_DEVICE_ATTR_2 requires two.
  67. SENSOR_DEVICE_ATTR defines a struct sensor_device_attribute variable.
  68. This structure has the following fields.
  69. struct sensor_device_attribute {
  70. struct device_attribute dev_attr;
  71. int index;
  72. };
  73. You can use to_sensor_dev_attr to get the pointer to this structure from the
  74. attribute read or write function. Its parameter is the device to which the
  75. attribute is attached.
  76. SENSOR_DEVICE_ATTR_2 defines a struct sensor_device_attribute_2 variable,
  77. which is defined as follows.
  78. struct sensor_device_attribute_2 {
  79. struct device_attribute dev_attr;
  80. u8 index;
  81. u8 nr;
  82. };
  83. Use to_sensor_dev_attr_2 to get the pointer to this structure. Its parameter
  84. is the device to which the attribute is attached.