···11+The Linux Hardware Monitoring kernel API.22+=========================================33+44+Guenter Roeck55+66+Introduction77+------------88+99+This document describes the API that can be used by hardware monitoring1010+drivers that want to use the hardware monitoring framework.1111+1212+This document does not describe what a hardware monitoring (hwmon) Driver or1313+Device is. It also does not describe the API which can be used by user space1414+to communicate with a hardware monitoring device. If you want to know this1515+then please read the following file: Documentation/hwmon/sysfs-interface.1616+1717+For additional guidelines on how to write and improve hwmon drivers, please1818+also read Documentation/hwmon/submitting-patches.1919+2020+The API2121+-------2222+Each hardware monitoring driver must #include <linux/hwmon.h> and, in most2323+cases, <linux/hwmon-sysfs.h>. linux/hwmon.h declares the following2424+register/unregister functions:2525+2626+struct device *hwmon_device_register(struct device *dev);2727+struct device *2828+hwmon_device_register_with_groups(struct device *dev, const char *name,2929+ void *drvdata,3030+ const struct attribute_group **groups);3131+3232+struct device *3333+devm_hwmon_device_register_with_groups(struct device *dev,3434+ const char *name, void *drvdata,3535+ const struct attribute_group **groups);3636+3737+void hwmon_device_unregister(struct device *dev);3838+void devm_hwmon_device_unregister(struct device *dev);3939+4040+hwmon_device_register registers a hardware monitoring device. The parameter4141+of this function is a pointer to the parent device.4242+This function returns a pointer to the newly created hardware monitoring device4343+or PTR_ERR for failure. If this registration function is used, hardware4444+monitoring sysfs attributes are expected to have been created and attached to4545+the parent device prior to calling hwmon_device_register. A name attribute must4646+have been created by the caller.4747+4848+hwmon_device_register_with_groups is similar to hwmon_device_register. However,4949+it has additional parameters. The name parameter is a pointer to the hwmon5050+device name. The registration function wil create a name sysfs attribute5151+pointing to this name. The drvdata parameter is the pointer to the local5252+driver data. hwmon_device_register_with_groups will attach this pointer5353+to the newly allocated hwmon device. The pointer can be retrieved by the driver5454+using dev_get_drvdata() on the hwmon device pointer. The groups parameter is5555+a pointer to a list of sysfs attribute groups. The list must be NULL terminated.5656+hwmon_device_register_with_groups creates the hwmon device with name attribute5757+as well as all sysfs attributes attached to the hwmon device.5858+5959+devm_hwmon_device_register_with_groups is similar to6060+hwmon_device_register_with_groups. However, it is device managed, meaning the6161+hwmon device does not have to be removed explicitly by the removal function.6262+6363+hwmon_device_unregister deregisters a registered hardware monitoring device.6464+The parameter of this function is the pointer to the registered hardware6565+monitoring device structure. This function must be called from the driver6666+remove function if the hardware monitoring device was registered with6767+hwmon_device_register or with hwmon_device_register_with_groups.6868+6969+devm_hwmon_device_unregister does not normally have to be called. It is only7070+needed for error handling, and only needed if the driver probe fails after7171+the call to devm_hwmon_device_register_with_groups.7272+7373+The header file linux/hwmon-sysfs.h provides a number of useful macros to7474+declare and use hardware monitoring sysfs attributes.7575+7676+In many cases, you can use the exsting define DEVICE_ATTR to declare such7777+attributes. This is feasible if an attribute has no additional context. However,7878+in many cases there will be additional information such as a sensor index which7979+will need to be passed to the sysfs attribute handling function.8080+8181+SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 can be used to define attributes8282+which need such additional context information. SENSOR_DEVICE_ATTR requires8383+one additional argument, SENSOR_DEVICE_ATTR_2 requires two.8484+8585+SENSOR_DEVICE_ATTR defines a struct sensor_device_attribute variable.8686+This structure has the following fields.8787+8888+struct sensor_device_attribute {8989+ struct device_attribute dev_attr;9090+ int index;9191+};9292+9393+You can use to_sensor_dev_attr to get the pointer to this structure from the9494+attribute read or write function. Its parameter is the device to which the9595+attribute is attached.9696+9797+SENSOR_DEVICE_ATTR_2 defines a struct sensor_device_attribute_2 variable,9898+which is defined as follows.9999+100100+struct sensor_device_attribute_2 {101101+ struct device_attribute dev_attr;102102+ u8 index;103103+ u8 nr;104104+};105105+106106+Use to_sensor_dev_attr_2 to get the pointer to this structure. Its parameter107107+is the device to which the attribute is attached.