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