| 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | /* |
| 3 | * Copyright (C) 2020 Invensense, Inc. |
| 4 | */ |
| 5 | |
| 6 | #ifndef INV_SENSORS_TIMESTAMP_H_ |
| 7 | #define INV_SENSORS_TIMESTAMP_H_ |
| 8 | |
| 9 | /** |
| 10 | * struct inv_sensors_timestamp_chip - chip internal properties |
| 11 | * @clock_period: internal clock period in ns |
| 12 | * @jitter: acceptable jitter in per-mille |
| 13 | * @init_period: chip initial period at reset in ns |
| 14 | */ |
| 15 | struct inv_sensors_timestamp_chip { |
| 16 | uint32_t clock_period; |
| 17 | uint32_t jitter; |
| 18 | uint32_t init_period; |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * struct inv_sensors_timestamp_interval - timestamps interval |
| 23 | * @lo: interval lower bound |
| 24 | * @up: interval upper bound |
| 25 | */ |
| 26 | struct inv_sensors_timestamp_interval { |
| 27 | int64_t lo; |
| 28 | int64_t up; |
| 29 | }; |
| 30 | |
| 31 | /** |
| 32 | * struct inv_sensors_timestamp_acc - accumulator for computing an estimation |
| 33 | * @val: current estimation of the value, the mean of all values |
| 34 | * @idx: current index of the next free place in values table |
| 35 | * @values: table of all measured values, use for computing the mean |
| 36 | */ |
| 37 | struct inv_sensors_timestamp_acc { |
| 38 | uint32_t val; |
| 39 | size_t idx; |
| 40 | uint32_t values[32]; |
| 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * struct inv_sensors_timestamp - timestamp management states |
| 45 | * @chip: chip internal characteristics |
| 46 | * @min_period: minimal acceptable clock period |
| 47 | * @max_period: maximal acceptable clock period |
| 48 | * @it: interrupts interval timestamps |
| 49 | * @timestamp: store last timestamp for computing next data timestamp |
| 50 | * @mult: current internal period multiplier |
| 51 | * @new_mult: new set internal period multiplier (not yet effective) |
| 52 | * @period: measured current period of the sensor |
| 53 | * @chip_period: accumulator for computing internal chip period |
| 54 | */ |
| 55 | struct inv_sensors_timestamp { |
| 56 | struct inv_sensors_timestamp_chip chip; |
| 57 | uint32_t min_period; |
| 58 | uint32_t max_period; |
| 59 | struct inv_sensors_timestamp_interval it; |
| 60 | int64_t timestamp; |
| 61 | uint32_t mult; |
| 62 | uint32_t new_mult; |
| 63 | uint32_t period; |
| 64 | struct inv_sensors_timestamp_acc chip_period; |
| 65 | }; |
| 66 | |
| 67 | void inv_sensors_timestamp_init(struct inv_sensors_timestamp *ts, |
| 68 | const struct inv_sensors_timestamp_chip *chip); |
| 69 | |
| 70 | int inv_sensors_timestamp_update_odr(struct inv_sensors_timestamp *ts, |
| 71 | uint32_t period, bool fifo); |
| 72 | |
| 73 | void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts, |
| 74 | size_t sample_nb, int64_t timestamp); |
| 75 | |
| 76 | static inline int64_t inv_sensors_timestamp_pop(struct inv_sensors_timestamp *ts) |
| 77 | { |
| 78 | ts->timestamp += ts->period; |
| 79 | return ts->timestamp; |
| 80 | } |
| 81 | |
| 82 | void inv_sensors_timestamp_apply_odr(struct inv_sensors_timestamp *ts, |
| 83 | uint32_t fifo_period, size_t fifo_nb, |
| 84 | unsigned int fifo_no); |
| 85 | |
| 86 | static inline void inv_sensors_timestamp_reset(struct inv_sensors_timestamp *ts) |
| 87 | { |
| 88 | const struct inv_sensors_timestamp_interval interval_init = {0LL, 0LL}; |
| 89 | |
| 90 | ts->it = interval_init; |
| 91 | ts->timestamp = 0; |
| 92 | } |
| 93 | |
| 94 | #endif |
| 95 | |