Back to home page

OSCL-LXR

 
 

    


0001 The Linux Hardware Monitoring kernel API
0002 ========================================
0003 
0004 Guenter Roeck
0005 
0006 Introduction
0007 ------------
0008 
0009 This document describes the API that can be used by hardware monitoring
0010 drivers that want to use the hardware monitoring framework.
0011 
0012 This document does not describe what a hardware monitoring (hwmon) Driver or
0013 Device is. It also does not describe the API which can be used by user space
0014 to communicate with a hardware monitoring device. If you want to know this
0015 then please read the following file: Documentation/hwmon/sysfs-interface.rst.
0016 
0017 For additional guidelines on how to write and improve hwmon drivers, please
0018 also read Documentation/hwmon/submitting-patches.rst.
0019 
0020 The API
0021 -------
0022 Each hardware monitoring driver must #include <linux/hwmon.h> and, in most
0023 cases, <linux/hwmon-sysfs.h>. linux/hwmon.h declares the following
0024 register/unregister functions::
0025 
0026   struct device *
0027   hwmon_device_register_with_groups(struct device *dev, const char *name,
0028                                     void *drvdata,
0029                                     const struct attribute_group **groups);
0030 
0031   struct device *
0032   devm_hwmon_device_register_with_groups(struct device *dev,
0033                                          const char *name, void *drvdata,
0034                                          const struct attribute_group **groups);
0035 
0036   struct device *
0037   hwmon_device_register_with_info(struct device *dev,
0038                                   const char *name, void *drvdata,
0039                                   const struct hwmon_chip_info *info,
0040                                   const struct attribute_group **extra_groups);
0041 
0042   struct device *
0043   devm_hwmon_device_register_with_info(struct device *dev,
0044                                        const char *name,
0045                                        void *drvdata,
0046                                        const struct hwmon_chip_info *info,
0047                                        const struct attribute_group **extra_groups);
0048 
0049   void hwmon_device_unregister(struct device *dev);
0050 
0051   void devm_hwmon_device_unregister(struct device *dev);
0052 
0053   char *hwmon_sanitize_name(const char *name);
0054 
0055   char *devm_hwmon_sanitize_name(struct device *dev, const char *name);
0056 
0057 hwmon_device_register_with_groups registers a hardware monitoring device.
0058 The first parameter of this function is a pointer to the parent device.
0059 The name parameter is a pointer to the hwmon device name. The registration
0060 function wil create a name sysfs attribute pointing to this name.
0061 The drvdata parameter is the pointer to the local driver data.
0062 hwmon_device_register_with_groups will attach this pointer to the newly
0063 allocated hwmon device. The pointer can be retrieved by the driver using
0064 dev_get_drvdata() on the hwmon device pointer. The groups parameter is
0065 a pointer to a list of sysfs attribute groups. The list must be NULL terminated.
0066 hwmon_device_register_with_groups creates the hwmon device with name attribute
0067 as well as all sysfs attributes attached to the hwmon device.
0068 This function returns a pointer to the newly created hardware monitoring device
0069 or PTR_ERR for failure.
0070 
0071 devm_hwmon_device_register_with_groups is similar to
0072 hwmon_device_register_with_groups. However, it is device managed, meaning the
0073 hwmon device does not have to be removed explicitly by the removal function.
0074 
0075 hwmon_device_register_with_info is the most comprehensive and preferred means
0076 to register a hardware monitoring device. It creates the standard sysfs
0077 attributes in the hardware monitoring core, letting the driver focus on reading
0078 from and writing to the chip instead of having to bother with sysfs attributes.
0079 The parent device parameter as well as the chip parameter must not be NULL. Its
0080 parameters are described in more detail below.
0081 
0082 devm_hwmon_device_register_with_info is similar to
0083 hwmon_device_register_with_info. However, it is device managed, meaning the
0084 hwmon device does not have to be removed explicitly by the removal function.
0085 
0086 hwmon_device_unregister deregisters a registered hardware monitoring device.
0087 The parameter of this function is the pointer to the registered hardware
0088 monitoring device structure. This function must be called from the driver
0089 remove function if the hardware monitoring device was registered with
0090 hwmon_device_register_with_groups or hwmon_device_register_with_info.
0091 
0092 devm_hwmon_device_unregister does not normally have to be called. It is only
0093 needed for error handling, and only needed if the driver probe fails after
0094 the call to devm_hwmon_device_register_with_groups or
0095 hwmon_device_register_with_info and if the automatic (device managed)
0096 removal would be too late.
0097 
0098 All supported hwmon device registration functions only accept valid device
0099 names. Device names including invalid characters (whitespace, '*', or '-')
0100 will be rejected. The 'name' parameter is mandatory.
0101 
0102 If the driver doesn't use a static device name (for example it uses
0103 dev_name()), and therefore cannot make sure the name only contains valid
0104 characters, hwmon_sanitize_name can be used. This convenience function
0105 will duplicate the string and replace any invalid characters with an
0106 underscore. It will allocate memory for the new string and it is the
0107 responsibility of the caller to release the memory when the device is
0108 removed.
0109 
0110 devm_hwmon_sanitize_name is the resource managed version of
0111 hwmon_sanitize_name; the memory will be freed automatically on device
0112 removal.
0113 
0114 Using devm_hwmon_device_register_with_info()
0115 --------------------------------------------
0116 
0117 hwmon_device_register_with_info() registers a hardware monitoring device.
0118 The parameters to this function are
0119 
0120 =============================================== ===============================================
0121 `struct device *dev`                            Pointer to parent device
0122 `const char *name`                              Device name
0123 `void *drvdata`                                 Driver private data
0124 `const struct hwmon_chip_info *info`            Pointer to chip description.
0125 `const struct attribute_group **extra_groups`   Null-terminated list of additional non-standard
0126                                                 sysfs attribute groups.
0127 =============================================== ===============================================
0128 
0129 This function returns a pointer to the created hardware monitoring device
0130 on success and a negative error code for failure.
0131 
0132 The hwmon_chip_info structure looks as follows::
0133 
0134         struct hwmon_chip_info {
0135                 const struct hwmon_ops *ops;
0136                 const struct hwmon_channel_info **info;
0137         };
0138 
0139 It contains the following fields:
0140 
0141 * ops:
0142         Pointer to device operations.
0143 * info:
0144         NULL-terminated list of device channel descriptors.
0145 
0146 The list of hwmon operations is defined as::
0147 
0148   struct hwmon_ops {
0149         umode_t (*is_visible)(const void *, enum hwmon_sensor_types type,
0150                               u32 attr, int);
0151         int (*read)(struct device *, enum hwmon_sensor_types type,
0152                     u32 attr, int, long *);
0153         int (*write)(struct device *, enum hwmon_sensor_types type,
0154                      u32 attr, int, long);
0155   };
0156 
0157 It defines the following operations.
0158 
0159 * is_visible:
0160     Pointer to a function to return the file mode for each supported
0161     attribute. This function is mandatory.
0162 
0163 * read:
0164     Pointer to a function for reading a value from the chip. This function
0165     is optional, but must be provided if any readable attributes exist.
0166 
0167 * write:
0168     Pointer to a function for writing a value to the chip. This function is
0169     optional, but must be provided if any writeable attributes exist.
0170 
0171 Each sensor channel is described with struct hwmon_channel_info, which is
0172 defined as follows::
0173 
0174         struct hwmon_channel_info {
0175                 enum hwmon_sensor_types type;
0176                 u32 *config;
0177         };
0178 
0179 It contains following fields:
0180 
0181 * type:
0182     The hardware monitoring sensor type.
0183 
0184     Supported sensor types are
0185 
0186      ================== ==================================================
0187      hwmon_chip         A virtual sensor type, used to describe attributes
0188                         which are not bound to a specific input or output
0189      hwmon_temp         Temperature sensor
0190      hwmon_in           Voltage sensor
0191      hwmon_curr         Current sensor
0192      hwmon_power                Power sensor
0193      hwmon_energy       Energy sensor
0194      hwmon_humidity     Humidity sensor
0195      hwmon_fan          Fan speed sensor
0196      hwmon_pwm          PWM control
0197      ================== ==================================================
0198 
0199 * config:
0200     Pointer to a 0-terminated list of configuration values for each
0201     sensor of the given type. Each value is a combination of bit values
0202     describing the attributes supposed by a single sensor.
0203 
0204 As an example, here is the complete description file for a LM75 compatible
0205 sensor chip. The chip has a single temperature sensor. The driver wants to
0206 register with the thermal subsystem (HWMON_C_REGISTER_TZ), and it supports
0207 the update_interval attribute (HWMON_C_UPDATE_INTERVAL). The chip supports
0208 reading the temperature (HWMON_T_INPUT), it has a maximum temperature
0209 register (HWMON_T_MAX) as well as a maximum temperature hysteresis register
0210 (HWMON_T_MAX_HYST)::
0211 
0212         static const u32 lm75_chip_config[] = {
0213                 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL,
0214                 0
0215         };
0216 
0217         static const struct hwmon_channel_info lm75_chip = {
0218                 .type = hwmon_chip,
0219                 .config = lm75_chip_config,
0220         };
0221 
0222         static const u32 lm75_temp_config[] = {
0223                 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST,
0224                 0
0225         };
0226 
0227         static const struct hwmon_channel_info lm75_temp = {
0228                 .type = hwmon_temp,
0229                 .config = lm75_temp_config,
0230         };
0231 
0232         static const struct hwmon_channel_info *lm75_info[] = {
0233                 &lm75_chip,
0234                 &lm75_temp,
0235                 NULL
0236         };
0237 
0238         The HWMON_CHANNEL_INFO() macro can and should be used when possible.
0239         With this macro, the above example can be simplified to
0240 
0241         static const struct hwmon_channel_info *lm75_info[] = {
0242                 HWMON_CHANNEL_INFO(chip,
0243                                 HWMON_C_REGISTER_TZ | HWMON_C_UPDATE_INTERVAL),
0244                 HWMON_CHANNEL_INFO(temp,
0245                                 HWMON_T_INPUT | HWMON_T_MAX | HWMON_T_MAX_HYST),
0246                 NULL
0247         };
0248 
0249         The remaining declarations are as follows.
0250 
0251         static const struct hwmon_ops lm75_hwmon_ops = {
0252                 .is_visible = lm75_is_visible,
0253                 .read = lm75_read,
0254                 .write = lm75_write,
0255         };
0256 
0257         static const struct hwmon_chip_info lm75_chip_info = {
0258                 .ops = &lm75_hwmon_ops,
0259                 .info = lm75_info,
0260         };
0261 
0262 A complete list of bit values indicating individual attribute support
0263 is defined in include/linux/hwmon.h. Definition prefixes are as follows.
0264 
0265 =============== =================================================
0266 HWMON_C_xxxx    Chip attributes, for use with hwmon_chip.
0267 HWMON_T_xxxx    Temperature attributes, for use with hwmon_temp.
0268 HWMON_I_xxxx    Voltage attributes, for use with hwmon_in.
0269 HWMON_C_xxxx    Current attributes, for use with hwmon_curr.
0270                 Notice the prefix overlap with chip attributes.
0271 HWMON_P_xxxx    Power attributes, for use with hwmon_power.
0272 HWMON_E_xxxx    Energy attributes, for use with hwmon_energy.
0273 HWMON_H_xxxx    Humidity attributes, for use with hwmon_humidity.
0274 HWMON_F_xxxx    Fan speed attributes, for use with hwmon_fan.
0275 HWMON_PWM_xxxx  PWM control attributes, for use with hwmon_pwm.
0276 =============== =================================================
0277 
0278 Driver callback functions
0279 -------------------------
0280 
0281 Each driver provides is_visible, read, and write functions. Parameters
0282 and return values for those functions are as follows::
0283 
0284   umode_t is_visible_func(const void *data, enum hwmon_sensor_types type,
0285                           u32 attr, int channel)
0286 
0287 Parameters:
0288         data:
0289                 Pointer to device private data structure.
0290         type:
0291                 The sensor type.
0292         attr:
0293                 Attribute identifier associated with a specific attribute.
0294                 For example, the attribute value for HWMON_T_INPUT would be
0295                 hwmon_temp_input. For complete mappings of bit fields to
0296                 attribute values please see include/linux/hwmon.h.
0297         channel:
0298                 The sensor channel number.
0299 
0300 Return value:
0301         The file mode for this attribute. Typically, this will be 0 (the
0302         attribute will not be created), S_IRUGO, or 'S_IRUGO | S_IWUSR'.
0303 
0304 ::
0305 
0306         int read_func(struct device *dev, enum hwmon_sensor_types type,
0307                       u32 attr, int channel, long *val)
0308 
0309 Parameters:
0310         dev:
0311                 Pointer to the hardware monitoring device.
0312         type:
0313                 The sensor type.
0314         attr:
0315                 Attribute identifier associated with a specific attribute.
0316                 For example, the attribute value for HWMON_T_INPUT would be
0317                 hwmon_temp_input. For complete mappings please see
0318                 include/linux/hwmon.h.
0319         channel:
0320                 The sensor channel number.
0321         val:
0322                 Pointer to attribute value.
0323 
0324 Return value:
0325         0 on success, a negative error number otherwise.
0326 
0327 ::
0328 
0329         int write_func(struct device *dev, enum hwmon_sensor_types type,
0330                        u32 attr, int channel, long val)
0331 
0332 Parameters:
0333         dev:
0334                 Pointer to the hardware monitoring device.
0335         type:
0336                 The sensor type.
0337         attr:
0338                 Attribute identifier associated with a specific attribute.
0339                 For example, the attribute value for HWMON_T_INPUT would be
0340                 hwmon_temp_input. For complete mappings please see
0341                 include/linux/hwmon.h.
0342         channel:
0343                 The sensor channel number.
0344         val:
0345                 The value to write to the chip.
0346 
0347 Return value:
0348         0 on success, a negative error number otherwise.
0349 
0350 
0351 Driver-provided sysfs attributes
0352 --------------------------------
0353 
0354 If the hardware monitoring device is registered with
0355 hwmon_device_register_with_info or devm_hwmon_device_register_with_info,
0356 it is most likely not necessary to provide sysfs attributes. Only additional
0357 non-standard sysfs attributes need to be provided when one of those registration
0358 functions is used.
0359 
0360 The header file linux/hwmon-sysfs.h provides a number of useful macros to
0361 declare and use hardware monitoring sysfs attributes.
0362 
0363 In many cases, you can use the exsting define DEVICE_ATTR or its variants
0364 DEVICE_ATTR_{RW,RO,WO} to declare such attributes. This is feasible if an
0365 attribute has no additional context. However, in many cases there will be
0366 additional information such as a sensor index which will need to be passed
0367 to the sysfs attribute handling function.
0368 
0369 SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 can be used to define attributes
0370 which need such additional context information. SENSOR_DEVICE_ATTR requires
0371 one additional argument, SENSOR_DEVICE_ATTR_2 requires two.
0372 
0373 Simplified variants of SENSOR_DEVICE_ATTR and SENSOR_DEVICE_ATTR_2 are available
0374 and should be used if standard attribute permissions and function names are
0375 feasible. Standard permissions are 0644 for SENSOR_DEVICE_ATTR[_2]_RW,
0376 0444 for SENSOR_DEVICE_ATTR[_2]_RO, and 0200 for SENSOR_DEVICE_ATTR[_2]_WO.
0377 Standard functions, similar to DEVICE_ATTR_{RW,RO,WO}, have _show and _store
0378 appended to the provided function name.
0379 
0380 SENSOR_DEVICE_ATTR and its variants define a struct sensor_device_attribute
0381 variable. This structure has the following fields::
0382 
0383         struct sensor_device_attribute {
0384                 struct device_attribute dev_attr;
0385                 int index;
0386         };
0387 
0388 You can use to_sensor_dev_attr to get the pointer to this structure from the
0389 attribute read or write function. Its parameter is the device to which the
0390 attribute is attached.
0391 
0392 SENSOR_DEVICE_ATTR_2 and its variants define a struct sensor_device_attribute_2
0393 variable, which is defined as follows::
0394 
0395         struct sensor_device_attribute_2 {
0396                 struct device_attribute dev_attr;
0397                 u8 index;
0398                 u8 nr;
0399         };
0400 
0401 Use to_sensor_dev_attr_2 to get the pointer to this structure. Its parameter
0402 is the device to which the attribute is attached.