summaryrefslogtreecommitdiff
path: root/include/linux/iio/common/inv_sensors_timestamp.h
blob: 4f08204ede3b45e5338b15f61a967a5b6ed40618 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * Copyright (C) 2020 Invensense, Inc.
 */

#ifndef INV_SENSORS_TIMESTAMP_H_
#define INV_SENSORS_TIMESTAMP_H_

/**
 * struct inv_sensors_timestamp_chip - chip internal properties
 * @clock_period:	internal clock period in ns
 * @jitter:		acceptable jitter in per-mille
 * @init_period:	chip initial period at reset in ns
 */
struct inv_sensors_timestamp_chip {
	u32 clock_period;
	u32 jitter;
	u32 init_period;
};

/**
 * struct inv_sensors_timestamp_interval - timestamps interval
 * @lo:	interval lower bound
 * @up:	interval upper bound
 */
struct inv_sensors_timestamp_interval {
	s64 lo;
	s64 up;
};

/**
 * struct inv_sensors_timestamp_acc - accumulator for computing an estimation
 * @val:	current estimation of the value, the mean of all values
 * @idx:	current index of the next free place in values table
 * @values:	table of all measured values, use for computing the mean
 */
struct inv_sensors_timestamp_acc {
	u32 val;
	size_t idx;
	u32 values[32];
};

/**
 * struct inv_sensors_timestamp - timestamp management states
 * @chip:		chip internal characteristics
 * @min_period:		minimal acceptable clock period
 * @max_period:		maximal acceptable clock period
 * @it:			interrupts interval timestamps
 * @delta:		interval timestamps between several interrupts
 * @delta_counter:	number of data samples in the delta interval
 * @timestamp:		store last timestamp for computing next data timestamp
 * @mult:		current internal period multiplier
 * @new_mult:		new set internal period multiplier (not yet effective)
 * @period:		measured current period of the sensor
 * @chip_period:	accumulator for computing internal chip period
 */
struct inv_sensors_timestamp {
	struct inv_sensors_timestamp_chip chip;
	u32 min_period;
	u32 max_period;
	struct inv_sensors_timestamp_interval it;
	struct inv_sensors_timestamp_interval delta;
	u32 delta_counter;
	s64 timestamp;
	u32 mult;
	u32 new_mult;
	u32 period;
	struct inv_sensors_timestamp_acc chip_period;
};

void inv_sensors_timestamp_init(struct inv_sensors_timestamp *ts,
				const struct inv_sensors_timestamp_chip *chip);

int inv_sensors_timestamp_update_odr(struct inv_sensors_timestamp *ts,
				     u32 period, bool fifo);

void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts,
				     size_t sample_nb, s64 timestamp);

static inline s64 inv_sensors_timestamp_pop(struct inv_sensors_timestamp *ts)
{
	ts->timestamp += ts->period;
	return ts->timestamp;
}

void inv_sensors_timestamp_apply_odr(struct inv_sensors_timestamp *ts,
				     u32 fifo_period, size_t fifo_nb,
				     unsigned int fifo_no);

static inline void inv_sensors_timestamp_reset(struct inv_sensors_timestamp *ts)
{
	const struct inv_sensors_timestamp_interval interval_init = {0LL, 0LL};

	ts->it = interval_init;
	ts->delta = interval_init;
	ts->delta_counter = 0;
	ts->timestamp = 0;
}

#endif