1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (C) 2020 Invensense, Inc.
4  */
5 
6 #ifndef INV_ICM42600_TIMESTAMP_H_
7 #define INV_ICM42600_TIMESTAMP_H_
8 
9 #include <linux/kernel.h>
10 
11 struct inv_icm42600_state;
12 
13 /**
14  * struct inv_icm42600_timestamp_interval - timestamps interval
15  * @lo:	interval lower bound
16  * @up:	interval upper bound
17  */
18 struct inv_icm42600_timestamp_interval {
19 	int64_t lo;
20 	int64_t up;
21 };
22 
23 /**
24  * struct inv_icm42600_timestamp_acc - accumulator for computing an estimation
25  * @val:	current estimation of the value, the mean of all values
26  * @idx:	current index of the next free place in values table
27  * @values:	table of all measured values, use for computing the mean
28  */
29 struct inv_icm42600_timestamp_acc {
30 	uint32_t val;
31 	size_t idx;
32 	uint32_t values[32];
33 };
34 
35 /**
36  * struct inv_icm42600_timestamp - timestamp management states
37  * @it:			interrupts interval timestamps
38  * @timestamp:		store last timestamp for computing next data timestamp
39  * @mult:		current internal period multiplier
40  * @new_mult:		new set internal period multiplier (not yet effective)
41  * @period:		measured current period of the sensor
42  * @chip_period:	accumulator for computing internal chip period
43  */
44 struct inv_icm42600_timestamp {
45 	struct inv_icm42600_timestamp_interval it;
46 	int64_t timestamp;
47 	uint32_t mult;
48 	uint32_t new_mult;
49 	uint32_t period;
50 	struct inv_icm42600_timestamp_acc chip_period;
51 };
52 
53 void inv_icm42600_timestamp_init(struct inv_icm42600_timestamp *ts,
54 				 uint32_t period);
55 
56 int inv_icm42600_timestamp_setup(struct inv_icm42600_state *st);
57 
58 int inv_icm42600_timestamp_update_odr(struct inv_icm42600_timestamp *ts,
59 				      uint32_t period, bool fifo);
60 
61 void inv_icm42600_timestamp_interrupt(struct inv_icm42600_timestamp *ts,
62 				      uint32_t fifo_period, size_t fifo_nb,
63 				      size_t sensor_nb, int64_t timestamp);
64 
65 static inline int64_t
inv_icm42600_timestamp_pop(struct inv_icm42600_timestamp * ts)66 inv_icm42600_timestamp_pop(struct inv_icm42600_timestamp *ts)
67 {
68 	ts->timestamp += ts->period;
69 	return ts->timestamp;
70 }
71 
72 void inv_icm42600_timestamp_apply_odr(struct inv_icm42600_timestamp *ts,
73 				      uint32_t fifo_period, size_t fifo_nb,
74 				      unsigned int fifo_no);
75 
76 static inline void
inv_icm42600_timestamp_reset(struct inv_icm42600_timestamp * ts)77 inv_icm42600_timestamp_reset(struct inv_icm42600_timestamp *ts)
78 {
79 	const struct inv_icm42600_timestamp_interval interval_init = {0LL, 0LL};
80 
81 	ts->it = interval_init;
82 	ts->timestamp = 0;
83 }
84 
85 #endif
86