hwmon.h 15 KB

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