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