common.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /* Copyright (c) 2021 Intel Corporation */
  3. #include <linux/mutex.h>
  4. #include <linux/types.h>
  5. #ifndef __PECI_HWMON_COMMON_H
  6. #define __PECI_HWMON_COMMON_H
  7. #define PECI_HWMON_UPDATE_INTERVAL HZ
  8. /**
  9. * struct peci_sensor_state - PECI state information
  10. * @valid: flag to indicate the sensor value is valid
  11. * @last_updated: time of the last update in jiffies
  12. * @lock: mutex to protect sensor access
  13. */
  14. struct peci_sensor_state {
  15. bool valid;
  16. unsigned long last_updated;
  17. struct mutex lock; /* protect sensor access */
  18. };
  19. /**
  20. * struct peci_sensor_data - PECI sensor information
  21. * @value: sensor value in milli units
  22. * @state: sensor update state
  23. */
  24. struct peci_sensor_data {
  25. s32 value;
  26. struct peci_sensor_state state;
  27. };
  28. /**
  29. * peci_sensor_need_update() - check whether sensor update is needed or not
  30. * @sensor: pointer to sensor data struct
  31. *
  32. * Return: true if update is needed, false if not.
  33. */
  34. static inline bool peci_sensor_need_update(struct peci_sensor_state *state)
  35. {
  36. return !state->valid ||
  37. time_after(jiffies, state->last_updated + PECI_HWMON_UPDATE_INTERVAL);
  38. }
  39. /**
  40. * peci_sensor_mark_updated() - mark the sensor is updated
  41. * @sensor: pointer to sensor data struct
  42. */
  43. static inline void peci_sensor_mark_updated(struct peci_sensor_state *state)
  44. {
  45. state->valid = true;
  46. state->last_updated = jiffies;
  47. }
  48. #endif /* __PECI_HWMON_COMMON_H */