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