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