Skip to content

Commit 8abbf45

Browse files
Mario Tesijic23
authored andcommitted
iio: st_lsm6dsx: Fixed calibrated timestamp calculation
The calibrated timestamp is calculated from the nominal value using the formula: ts_gain[ns] ≈ ts_sensitivity - (ts_trim_coeff * val) / 1000. The values of ts_sensitivity and ts_trim_coeff are not the same for all devices, so it is necessary to differentiate them based on the part name. For the correct values please consult the relevant AN. Fixes: cb3b6b8 ("iio: imu: st_lsm6dsx: add odr calibration feature") Signed-off-by: Mario Tesi <mario.tesi@st.com> Acked-by: Lorenzo Bianconi <lorenzo@kernel.org> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent cb372b4 commit 8abbf45

2 files changed

Lines changed: 26 additions & 11 deletions

File tree

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,31 @@ struct st_lsm6dsx_fifo_ops {
192192
* @fifo_en: Hw timer FIFO enable register info (addr + mask).
193193
* @decimator: Hw timer FIFO decimator register info (addr + mask).
194194
* @freq_fine: Difference in % of ODR with respect to the typical.
195+
* @ts_sensitivity: Nominal timestamp sensitivity.
196+
* @ts_trim_coeff: Coefficient for calculating the calibrated timestamp gain.
197+
* This coefficient comes into play when linearizing the formula
198+
* used to calculate the calibrated timestamp (please see the
199+
* relevant formula in the AN for the specific IMU).
200+
* For example, in the case of LSM6DSO we have:
201+
*
202+
* 1 / (1 + x) ~= 1 - x (Taylor’s Series)
203+
* ttrim[s] = 1 / (40000 * (1 + 0.0015 * val)) (from AN5192)
204+
* ttrim[ns] ~= 25000 - 37.5 * val
205+
* ttrim[ns] ~= 25000 - (37500 * val) / 1000
206+
*
207+
* so, replacing ts_sensitivity = 25000 and
208+
* ts_trim_coeff = 37500
209+
*
210+
* ttrim[ns] ~= ts_sensitivity - (ts_trim_coeff * val) / 1000
195211
*/
196212
struct st_lsm6dsx_hw_ts_settings {
197213
struct st_lsm6dsx_reg timer_en;
198214
struct st_lsm6dsx_reg hr_timer;
199215
struct st_lsm6dsx_reg fifo_en;
200216
struct st_lsm6dsx_reg decimator;
201217
u8 freq_fine;
218+
u16 ts_sensitivity;
219+
u16 ts_trim_coeff;
202220
};
203221

204222
/**

drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@
9494

9595
#define ST_LSM6DSX_REG_WHOAMI_ADDR 0x0f
9696

97-
#define ST_LSM6DSX_TS_SENSITIVITY 25000UL /* 25us */
98-
9997
static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {
10098
ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x28, IIO_MOD_X, 0),
10199
ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x2a, IIO_MOD_Y, 1),
@@ -983,6 +981,8 @@ static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
983981
.mask = GENMASK(7, 6),
984982
},
985983
.freq_fine = 0x63,
984+
.ts_sensitivity = 25000,
985+
.ts_trim_coeff = 37500,
986986
},
987987
.shub_settings = {
988988
.page_mux = {
@@ -1196,6 +1196,8 @@ static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
11961196
.mask = GENMASK(7, 6),
11971197
},
11981198
.freq_fine = 0x63,
1199+
.ts_sensitivity = 25000,
1200+
.ts_trim_coeff = 37500,
11991201
},
12001202
.event_settings = {
12011203
.enable_reg = {
@@ -1371,6 +1373,8 @@ static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
13711373
.mask = GENMASK(7, 6),
13721374
},
13731375
.freq_fine = 0x4f,
1376+
.ts_sensitivity = 21701,
1377+
.ts_trim_coeff = 28212,
13741378
},
13751379
.shub_settings = {
13761380
.page_mux = {
@@ -2248,20 +2252,13 @@ static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw *hw)
22482252
}
22492253

22502254
/* calibrate timestamp sensitivity */
2251-
hw->ts_gain = ST_LSM6DSX_TS_SENSITIVITY;
2255+
hw->ts_gain = ts_settings->ts_sensitivity;
22522256
if (ts_settings->freq_fine) {
22532257
err = regmap_read(hw->regmap, ts_settings->freq_fine, &val);
22542258
if (err < 0)
22552259
return err;
22562260

2257-
/*
2258-
* linearize the AN5192 formula:
2259-
* 1 / (1 + x) ~= 1 - x (Taylor’s Series)
2260-
* ttrim[s] = 1 / (40000 * (1 + 0.0015 * val))
2261-
* ttrim[ns] ~= 25000 - 37.5 * val
2262-
* ttrim[ns] ~= 25000 - (37500 * val) / 1000
2263-
*/
2264-
hw->ts_gain -= ((s8)val * 37500) / 1000;
2261+
hw->ts_gain -= ((s8)val * ts_settings->ts_trim_coeff) / 1000;
22652262
}
22662263

22672264
return 0;

0 commit comments

Comments
 (0)