xref: /linux/include/linux/hwmon.h (revision b5bee6ced21ca98389000b7017dd41b0cc37fa50)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3     hwmon.h - part of lm_sensors, Linux kernel modules for hardware monitoring
4 
5     This file declares helper functions for the sysfs class "hwmon",
6     for use by sensors drivers.
7 
8     Copyright (C) 2005 Mark M. Hoffman <mhoffman@lightlink.com>
9 
10 */
11 
12 #ifndef _HWMON_H_
13 #define _HWMON_H_
14 
15 #include <linux/bitops.h>
16 
17 struct device;
18 struct attribute_group;
19 
20 enum hwmon_sensor_types {
21 	hwmon_chip,
22 	hwmon_temp,
23 	hwmon_in,
24 	hwmon_curr,
25 	hwmon_power,
26 	hwmon_energy,
27 	hwmon_humidity,
28 	hwmon_fan,
29 	hwmon_pwm,
30 	hwmon_intrusion,
31 	hwmon_max,
32 };
33 
34 enum hwmon_chip_attributes {
35 	hwmon_chip_temp_reset_history,
36 	hwmon_chip_in_reset_history,
37 	hwmon_chip_curr_reset_history,
38 	hwmon_chip_power_reset_history,
39 	hwmon_chip_register_tz,
40 	hwmon_chip_update_interval,
41 	hwmon_chip_alarms,
42 	hwmon_chip_samples,
43 	hwmon_chip_curr_samples,
44 	hwmon_chip_in_samples,
45 	hwmon_chip_power_samples,
46 	hwmon_chip_temp_samples,
47 };
48 
49 #define HWMON_C_TEMP_RESET_HISTORY	BIT(hwmon_chip_temp_reset_history)
50 #define HWMON_C_IN_RESET_HISTORY	BIT(hwmon_chip_in_reset_history)
51 #define HWMON_C_CURR_RESET_HISTORY	BIT(hwmon_chip_curr_reset_history)
52 #define HWMON_C_POWER_RESET_HISTORY	BIT(hwmon_chip_power_reset_history)
53 #define HWMON_C_REGISTER_TZ		BIT(hwmon_chip_register_tz)
54 #define HWMON_C_UPDATE_INTERVAL		BIT(hwmon_chip_update_interval)
55 #define HWMON_C_ALARMS			BIT(hwmon_chip_alarms)
56 #define HWMON_C_SAMPLES			BIT(hwmon_chip_samples)
57 #define HWMON_C_CURR_SAMPLES		BIT(hwmon_chip_curr_samples)
58 #define HWMON_C_IN_SAMPLES		BIT(hwmon_chip_in_samples)
59 #define HWMON_C_POWER_SAMPLES		BIT(hwmon_chip_power_samples)
60 #define HWMON_C_TEMP_SAMPLES		BIT(hwmon_chip_temp_samples)
61 
62 enum hwmon_temp_attributes {
63 	hwmon_temp_enable,
64 	hwmon_temp_input,
65 	hwmon_temp_type,
66 	hwmon_temp_lcrit,
67 	hwmon_temp_lcrit_hyst,
68 	hwmon_temp_min,
69 	hwmon_temp_min_hyst,
70 	hwmon_temp_max,
71 	hwmon_temp_max_hyst,
72 	hwmon_temp_crit,
73 	hwmon_temp_crit_hyst,
74 	hwmon_temp_emergency,
75 	hwmon_temp_emergency_hyst,
76 	hwmon_temp_alarm,
77 	hwmon_temp_lcrit_alarm,
78 	hwmon_temp_min_alarm,
79 	hwmon_temp_max_alarm,
80 	hwmon_temp_crit_alarm,
81 	hwmon_temp_emergency_alarm,
82 	hwmon_temp_fault,
83 	hwmon_temp_offset,
84 	hwmon_temp_label,
85 	hwmon_temp_lowest,
86 	hwmon_temp_highest,
87 	hwmon_temp_reset_history,
88 	hwmon_temp_rated_min,
89 	hwmon_temp_rated_max,
90 };
91 
92 #define HWMON_T_ENABLE		BIT(hwmon_temp_enable)
93 #define HWMON_T_INPUT		BIT(hwmon_temp_input)
94 #define HWMON_T_TYPE		BIT(hwmon_temp_type)
95 #define HWMON_T_LCRIT		BIT(hwmon_temp_lcrit)
96 #define HWMON_T_LCRIT_HYST	BIT(hwmon_temp_lcrit_hyst)
97 #define HWMON_T_MIN		BIT(hwmon_temp_min)
98 #define HWMON_T_MIN_HYST	BIT(hwmon_temp_min_hyst)
99 #define HWMON_T_MAX		BIT(hwmon_temp_max)
100 #define HWMON_T_MAX_HYST	BIT(hwmon_temp_max_hyst)
101 #define HWMON_T_CRIT		BIT(hwmon_temp_crit)
102 #define HWMON_T_CRIT_HYST	BIT(hwmon_temp_crit_hyst)
103 #define HWMON_T_EMERGENCY	BIT(hwmon_temp_emergency)
104 #define HWMON_T_EMERGENCY_HYST	BIT(hwmon_temp_emergency_hyst)
105 #define HWMON_T_ALARM		BIT(hwmon_temp_alarm)
106 #define HWMON_T_MIN_ALARM	BIT(hwmon_temp_min_alarm)
107 #define HWMON_T_MAX_ALARM	BIT(hwmon_temp_max_alarm)
108 #define HWMON_T_CRIT_ALARM	BIT(hwmon_temp_crit_alarm)
109 #define HWMON_T_LCRIT_ALARM	BIT(hwmon_temp_lcrit_alarm)
110 #define HWMON_T_EMERGENCY_ALARM	BIT(hwmon_temp_emergency_alarm)
111 #define HWMON_T_FAULT		BIT(hwmon_temp_fault)
112 #define HWMON_T_OFFSET		BIT(hwmon_temp_offset)
113 #define HWMON_T_LABEL		BIT(hwmon_temp_label)
114 #define HWMON_T_LOWEST		BIT(hwmon_temp_lowest)
115 #define HWMON_T_HIGHEST		BIT(hwmon_temp_highest)
116 #define HWMON_T_RESET_HISTORY	BIT(hwmon_temp_reset_history)
117 #define HWMON_T_RATED_MIN	BIT(hwmon_temp_rated_min)
118 #define HWMON_T_RATED_MAX	BIT(hwmon_temp_rated_max)
119 
120 enum hwmon_in_attributes {
121 	hwmon_in_enable,
122 	hwmon_in_input,
123 	hwmon_in_min,
124 	hwmon_in_max,
125 	hwmon_in_lcrit,
126 	hwmon_in_crit,
127 	hwmon_in_average,
128 	hwmon_in_lowest,
129 	hwmon_in_highest,
130 	hwmon_in_reset_history,
131 	hwmon_in_label,
132 	hwmon_in_alarm,
133 	hwmon_in_min_alarm,
134 	hwmon_in_max_alarm,
135 	hwmon_in_lcrit_alarm,
136 	hwmon_in_crit_alarm,
137 	hwmon_in_rated_min,
138 	hwmon_in_rated_max,
139 };
140 
141 #define HWMON_I_ENABLE		BIT(hwmon_in_enable)
142 #define HWMON_I_INPUT		BIT(hwmon_in_input)
143 #define HWMON_I_MIN		BIT(hwmon_in_min)
144 #define HWMON_I_MAX		BIT(hwmon_in_max)
145 #define HWMON_I_LCRIT		BIT(hwmon_in_lcrit)
146 #define HWMON_I_CRIT		BIT(hwmon_in_crit)
147 #define HWMON_I_AVERAGE		BIT(hwmon_in_average)
148 #define HWMON_I_LOWEST		BIT(hwmon_in_lowest)
149 #define HWMON_I_HIGHEST		BIT(hwmon_in_highest)
150 #define HWMON_I_RESET_HISTORY	BIT(hwmon_in_reset_history)
151 #define HWMON_I_LABEL		BIT(hwmon_in_label)
152 #define HWMON_I_ALARM		BIT(hwmon_in_alarm)
153 #define HWMON_I_MIN_ALARM	BIT(hwmon_in_min_alarm)
154 #define HWMON_I_MAX_ALARM	BIT(hwmon_in_max_alarm)
155 #define HWMON_I_LCRIT_ALARM	BIT(hwmon_in_lcrit_alarm)
156 #define HWMON_I_CRIT_ALARM	BIT(hwmon_in_crit_alarm)
157 #define HWMON_I_RATED_MIN	BIT(hwmon_in_rated_min)
158 #define HWMON_I_RATED_MAX	BIT(hwmon_in_rated_max)
159 
160 enum hwmon_curr_attributes {
161 	hwmon_curr_enable,
162 	hwmon_curr_input,
163 	hwmon_curr_min,
164 	hwmon_curr_max,
165 	hwmon_curr_lcrit,
166 	hwmon_curr_crit,
167 	hwmon_curr_average,
168 	hwmon_curr_lowest,
169 	hwmon_curr_highest,
170 	hwmon_curr_reset_history,
171 	hwmon_curr_label,
172 	hwmon_curr_alarm,
173 	hwmon_curr_min_alarm,
174 	hwmon_curr_max_alarm,
175 	hwmon_curr_lcrit_alarm,
176 	hwmon_curr_crit_alarm,
177 	hwmon_curr_rated_min,
178 	hwmon_curr_rated_max,
179 };
180 
181 #define HWMON_C_ENABLE		BIT(hwmon_curr_enable)
182 #define HWMON_C_INPUT		BIT(hwmon_curr_input)
183 #define HWMON_C_MIN		BIT(hwmon_curr_min)
184 #define HWMON_C_MAX		BIT(hwmon_curr_max)
185 #define HWMON_C_LCRIT		BIT(hwmon_curr_lcrit)
186 #define HWMON_C_CRIT		BIT(hwmon_curr_crit)
187 #define HWMON_C_AVERAGE		BIT(hwmon_curr_average)
188 #define HWMON_C_LOWEST		BIT(hwmon_curr_lowest)
189 #define HWMON_C_HIGHEST		BIT(hwmon_curr_highest)
190 #define HWMON_C_RESET_HISTORY	BIT(hwmon_curr_reset_history)
191 #define HWMON_C_LABEL		BIT(hwmon_curr_label)
192 #define HWMON_C_ALARM		BIT(hwmon_curr_alarm)
193 #define HWMON_C_MIN_ALARM	BIT(hwmon_curr_min_alarm)
194 #define HWMON_C_MAX_ALARM	BIT(hwmon_curr_max_alarm)
195 #define HWMON_C_LCRIT_ALARM	BIT(hwmon_curr_lcrit_alarm)
196 #define HWMON_C_CRIT_ALARM	BIT(hwmon_curr_crit_alarm)
197 #define HWMON_C_RATED_MIN	BIT(hwmon_curr_rated_min)
198 #define HWMON_C_RATED_MAX	BIT(hwmon_curr_rated_max)
199 
200 enum hwmon_power_attributes {
201 	hwmon_power_enable,
202 	hwmon_power_average,
203 	hwmon_power_average_interval,
204 	hwmon_power_average_interval_max,
205 	hwmon_power_average_interval_min,
206 	hwmon_power_average_highest,
207 	hwmon_power_average_lowest,
208 	hwmon_power_average_max,
209 	hwmon_power_average_min,
210 	hwmon_power_input,
211 	hwmon_power_input_highest,
212 	hwmon_power_input_lowest,
213 	hwmon_power_reset_history,
214 	hwmon_power_accuracy,
215 	hwmon_power_cap,
216 	hwmon_power_cap_hyst,
217 	hwmon_power_cap_max,
218 	hwmon_power_cap_min,
219 	hwmon_power_min,
220 	hwmon_power_max,
221 	hwmon_power_crit,
222 	hwmon_power_lcrit,
223 	hwmon_power_label,
224 	hwmon_power_alarm,
225 	hwmon_power_cap_alarm,
226 	hwmon_power_min_alarm,
227 	hwmon_power_max_alarm,
228 	hwmon_power_lcrit_alarm,
229 	hwmon_power_crit_alarm,
230 	hwmon_power_rated_min,
231 	hwmon_power_rated_max,
232 };
233 
234 #define HWMON_P_ENABLE			BIT(hwmon_power_enable)
235 #define HWMON_P_AVERAGE			BIT(hwmon_power_average)
236 #define HWMON_P_AVERAGE_INTERVAL	BIT(hwmon_power_average_interval)
237 #define HWMON_P_AVERAGE_INTERVAL_MAX	BIT(hwmon_power_average_interval_max)
238 #define HWMON_P_AVERAGE_INTERVAL_MIN	BIT(hwmon_power_average_interval_min)
239 #define HWMON_P_AVERAGE_HIGHEST		BIT(hwmon_power_average_highest)
240 #define HWMON_P_AVERAGE_LOWEST		BIT(hwmon_power_average_lowest)
241 #define HWMON_P_AVERAGE_MAX		BIT(hwmon_power_average_max)
242 #define HWMON_P_AVERAGE_MIN		BIT(hwmon_power_average_min)
243 #define HWMON_P_INPUT			BIT(hwmon_power_input)
244 #define HWMON_P_INPUT_HIGHEST		BIT(hwmon_power_input_highest)
245 #define HWMON_P_INPUT_LOWEST		BIT(hwmon_power_input_lowest)
246 #define HWMON_P_RESET_HISTORY		BIT(hwmon_power_reset_history)
247 #define HWMON_P_ACCURACY		BIT(hwmon_power_accuracy)
248 #define HWMON_P_CAP			BIT(hwmon_power_cap)
249 #define HWMON_P_CAP_HYST		BIT(hwmon_power_cap_hyst)
250 #define HWMON_P_CAP_MAX			BIT(hwmon_power_cap_max)
251 #define HWMON_P_CAP_MIN			BIT(hwmon_power_cap_min)
252 #define HWMON_P_MIN			BIT(hwmon_power_min)
253 #define HWMON_P_MAX			BIT(hwmon_power_max)
254 #define HWMON_P_LCRIT			BIT(hwmon_power_lcrit)
255 #define HWMON_P_CRIT			BIT(hwmon_power_crit)
256 #define HWMON_P_LABEL			BIT(hwmon_power_label)
257 #define HWMON_P_ALARM			BIT(hwmon_power_alarm)
258 #define HWMON_P_CAP_ALARM		BIT(hwmon_power_cap_alarm)
259 #define HWMON_P_MIN_ALARM		BIT(hwmon_power_min_alarm)
260 #define HWMON_P_MAX_ALARM		BIT(hwmon_power_max_alarm)
261 #define HWMON_P_LCRIT_ALARM		BIT(hwmon_power_lcrit_alarm)
262 #define HWMON_P_CRIT_ALARM		BIT(hwmon_power_crit_alarm)
263 #define HWMON_P_RATED_MIN		BIT(hwmon_power_rated_min)
264 #define HWMON_P_RATED_MAX		BIT(hwmon_power_rated_max)
265 
266 enum hwmon_energy_attributes {
267 	hwmon_energy_enable,
268 	hwmon_energy_input,
269 	hwmon_energy_label,
270 };
271 
272 #define HWMON_E_ENABLE			BIT(hwmon_energy_enable)
273 #define HWMON_E_INPUT			BIT(hwmon_energy_input)
274 #define HWMON_E_LABEL			BIT(hwmon_energy_label)
275 
276 enum hwmon_humidity_attributes {
277 	hwmon_humidity_enable,
278 	hwmon_humidity_input,
279 	hwmon_humidity_label,
280 	hwmon_humidity_min,
281 	hwmon_humidity_min_hyst,
282 	hwmon_humidity_max,
283 	hwmon_humidity_max_hyst,
284 	hwmon_humidity_alarm,
285 	hwmon_humidity_fault,
286 	hwmon_humidity_rated_min,
287 	hwmon_humidity_rated_max,
288 };
289 
290 #define HWMON_H_ENABLE			BIT(hwmon_humidity_enable)
291 #define HWMON_H_INPUT			BIT(hwmon_humidity_input)
292 #define HWMON_H_LABEL			BIT(hwmon_humidity_label)
293 #define HWMON_H_MIN			BIT(hwmon_humidity_min)
294 #define HWMON_H_MIN_HYST		BIT(hwmon_humidity_min_hyst)
295 #define HWMON_H_MAX			BIT(hwmon_humidity_max)
296 #define HWMON_H_MAX_HYST		BIT(hwmon_humidity_max_hyst)
297 #define HWMON_H_ALARM			BIT(hwmon_humidity_alarm)
298 #define HWMON_H_FAULT			BIT(hwmon_humidity_fault)
299 #define HWMON_H_RATED_MIN		BIT(hwmon_humidity_rated_min)
300 #define HWMON_H_RATED_MAX		BIT(hwmon_humidity_rated_max)
301 
302 enum hwmon_fan_attributes {
303 	hwmon_fan_enable,
304 	hwmon_fan_input,
305 	hwmon_fan_label,
306 	hwmon_fan_min,
307 	hwmon_fan_max,
308 	hwmon_fan_div,
309 	hwmon_fan_pulses,
310 	hwmon_fan_target,
311 	hwmon_fan_alarm,
312 	hwmon_fan_min_alarm,
313 	hwmon_fan_max_alarm,
314 	hwmon_fan_fault,
315 };
316 
317 #define HWMON_F_ENABLE			BIT(hwmon_fan_enable)
318 #define HWMON_F_INPUT			BIT(hwmon_fan_input)
319 #define HWMON_F_LABEL			BIT(hwmon_fan_label)
320 #define HWMON_F_MIN			BIT(hwmon_fan_min)
321 #define HWMON_F_MAX			BIT(hwmon_fan_max)
322 #define HWMON_F_DIV			BIT(hwmon_fan_div)
323 #define HWMON_F_PULSES			BIT(hwmon_fan_pulses)
324 #define HWMON_F_TARGET			BIT(hwmon_fan_target)
325 #define HWMON_F_ALARM			BIT(hwmon_fan_alarm)
326 #define HWMON_F_MIN_ALARM		BIT(hwmon_fan_min_alarm)
327 #define HWMON_F_MAX_ALARM		BIT(hwmon_fan_max_alarm)
328 #define HWMON_F_FAULT			BIT(hwmon_fan_fault)
329 
330 enum hwmon_pwm_attributes {
331 	hwmon_pwm_input,
332 	hwmon_pwm_enable,
333 	hwmon_pwm_mode,
334 	hwmon_pwm_freq,
335 	hwmon_pwm_auto_channels_temp,
336 };
337 
338 #define HWMON_PWM_INPUT			BIT(hwmon_pwm_input)
339 #define HWMON_PWM_ENABLE		BIT(hwmon_pwm_enable)
340 #define HWMON_PWM_MODE			BIT(hwmon_pwm_mode)
341 #define HWMON_PWM_FREQ			BIT(hwmon_pwm_freq)
342 #define HWMON_PWM_AUTO_CHANNELS_TEMP	BIT(hwmon_pwm_auto_channels_temp)
343 
344 enum hwmon_intrusion_attributes {
345 	hwmon_intrusion_alarm,
346 	hwmon_intrusion_beep,
347 };
348 #define HWMON_INTRUSION_ALARM		BIT(hwmon_intrusion_alarm)
349 #define HWMON_INTRUSION_BEEP		BIT(hwmon_intrusion_beep)
350 
351 /**
352  * struct hwmon_ops - hwmon device operations
353  * @is_visible: Callback to return attribute visibility. Mandatory.
354  *		Parameters are:
355  *		@const void *drvdata:
356  *			Pointer to driver-private data structure passed
357  *			as argument to hwmon_device_register_with_info().
358  *		@type:	Sensor type
359  *		@attr:	Sensor attribute
360  *		@channel:
361  *			Channel number
362  *		The function returns the file permissions.
363  *		If the return value is 0, no attribute will be created.
364  * @read:	Read callback for data attributes. Mandatory if readable
365  *		data attributes are present.
366  *		Parameters are:
367  *		@dev:	Pointer to hardware monitoring device
368  *		@type:	Sensor type
369  *		@attr:	Sensor attribute
370  *		@channel:
371  *			Channel number
372  *		@val:	Pointer to returned value
373  *		The function returns 0 on success or a negative error number.
374  * @read_string:
375  *		Read callback for string attributes. Mandatory if string
376  *		attributes are present.
377  *		Parameters are:
378  *		@dev:	Pointer to hardware monitoring device
379  *		@type:	Sensor type
380  *		@attr:	Sensor attribute
381  *		@channel:
382  *			Channel number
383  *		@str:	Pointer to returned string
384  *		The function returns 0 on success or a negative error number.
385  * @write:	Write callback for data attributes. Mandatory if writeable
386  *		data attributes are present.
387  *		Parameters are:
388  *		@dev:	Pointer to hardware monitoring device
389  *		@type:	Sensor type
390  *		@attr:	Sensor attribute
391  *		@channel:
392  *			Channel number
393  *		@val:	Value to write
394  *		The function returns 0 on success or a negative error number.
395  */
396 struct hwmon_ops {
397 	umode_t (*is_visible)(const void *drvdata, enum hwmon_sensor_types type,
398 			      u32 attr, int channel);
399 	int (*read)(struct device *dev, enum hwmon_sensor_types type,
400 		    u32 attr, int channel, long *val);
401 	int (*read_string)(struct device *dev, enum hwmon_sensor_types type,
402 		    u32 attr, int channel, const char **str);
403 	int (*write)(struct device *dev, enum hwmon_sensor_types type,
404 		     u32 attr, int channel, long val);
405 };
406 
407 /**
408  * struct hwmon_channel_info - Channel information
409  * @type:	Channel type.
410  * @config:	Pointer to NULL-terminated list of channel parameters.
411  *		Use for per-channel attributes.
412  */
413 struct hwmon_channel_info {
414 	enum hwmon_sensor_types type;
415 	const u32 *config;
416 };
417 
418 #define HWMON_CHANNEL_INFO(stype, ...)	\
419 	(&(struct hwmon_channel_info) {	\
420 		.type = hwmon_##stype,	\
421 		.config = (u32 []) {	\
422 			__VA_ARGS__, 0	\
423 		}			\
424 	})
425 
426 /**
427  * struct hwmon_chip_info - Chip configuration
428  * @ops:	Pointer to hwmon operations.
429  * @info:	Null-terminated list of channel information.
430  */
431 struct hwmon_chip_info {
432 	const struct hwmon_ops *ops;
433 	const struct hwmon_channel_info **info;
434 };
435 
436 /* hwmon_device_register() is deprecated */
437 struct device *hwmon_device_register(struct device *dev);
438 
439 struct device *
440 hwmon_device_register_with_groups(struct device *dev, const char *name,
441 				  void *drvdata,
442 				  const struct attribute_group **groups);
443 struct device *
444 devm_hwmon_device_register_with_groups(struct device *dev, const char *name,
445 				       void *drvdata,
446 				       const struct attribute_group **groups);
447 struct device *
448 hwmon_device_register_with_info(struct device *dev,
449 				const char *name, void *drvdata,
450 				const struct hwmon_chip_info *info,
451 				const struct attribute_group **extra_groups);
452 struct device *
453 hwmon_device_register_for_thermal(struct device *dev, const char *name,
454 				  void *drvdata);
455 struct device *
456 devm_hwmon_device_register_with_info(struct device *dev,
457 				const char *name, void *drvdata,
458 				const struct hwmon_chip_info *info,
459 				const struct attribute_group **extra_groups);
460 
461 void hwmon_device_unregister(struct device *dev);
462 void devm_hwmon_device_unregister(struct device *dev);
463 
464 int hwmon_notify_event(struct device *dev, enum hwmon_sensor_types type,
465 		       u32 attr, int channel);
466 
467 char *hwmon_sanitize_name(const char *name);
468 char *devm_hwmon_sanitize_name(struct device *dev, const char *name);
469 
470 /**
471  * hwmon_is_bad_char - Is the char invalid in a hwmon name
472  * @ch: the char to be considered
473  *
474  * hwmon_is_bad_char() can be used to determine if the given character
475  * may not be used in a hwmon name.
476  *
477  * Returns true if the char is invalid, false otherwise.
478  */
479 static inline bool hwmon_is_bad_char(const char ch)
480 {
481 	switch (ch) {
482 	case '-':
483 	case '*':
484 	case ' ':
485 	case '\t':
486 	case '\n':
487 		return true;
488 	default:
489 		return false;
490 	}
491 }
492 
493 #endif
494