Skip to content

Commit 65e4345

Browse files
Linus Walleijjic23
authored andcommitted
iio: accel: st_accel: fix LIS3LV02 reading and scaling
The LIS3LV02 has a special bit that need to be set to get the read values left aligned. Before this patch we get gibberish like this: iio_generic_buffer -a -c10 -n lis3lv02dl_accel (...) 0.000000 -0.010042 -0.642688 19155832931907 0.000000 -0.010042 -0.642688 19155858751073 Which is because we read a raw value for 1g as 64 which is the nominal 1024 for 1g shifted 4 bits to the left by being right-aligned rather than left aligned. Since all other sensors are left aligned, add some code to set the special DAS (data alignment setting) bit to 1 so that the right value is now read like this: iio_generic_buffer -a -c10 -n lis3lv02dl_accel (...) 0.000000 -0.147095 -10.120135 24761614364956 -0.029419 -0.176514 -10.120135 24761631624540 The scaling was weird as well: we have a gain of 1000 for 1g and 3000 for 6g. I don't even remember how I came up with the old values but they are wrong. Fixes: 3acddf7 ("iio: st-sensors: add support for lis3lv02d accelerometer") Cc: Lorenzo Bianconi <lorenzo.bianconi@st.com> Cc: Giuseppe Barba <giuseppe.barba@st.com> Cc: Denis Ciocca <denis.ciocca@st.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent 65c8aea commit 65e4345

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

‎drivers/iio/accel/st_accel_core.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,19 +353,27 @@ static const struct st_sensor_settings st_accel_sensors_settings[] = {
353353
[0] = {
354354
.num = ST_ACCEL_FS_AVL_2G,
355355
.value = 0x00,
356-
.gain = IIO_G_TO_M_S_2(1024),
356+
.gain = IIO_G_TO_M_S_2(1000),
357357
},
358358
[1] = {
359359
.num = ST_ACCEL_FS_AVL_6G,
360360
.value = 0x01,
361-
.gain = IIO_G_TO_M_S_2(340),
361+
.gain = IIO_G_TO_M_S_2(3000),
362362
},
363363
},
364364
},
365365
.bdu = {
366366
.addr = 0x21,
367367
.mask = 0x40,
368368
},
369+
/*
370+
* Data Alignment Setting - needs to be set to get
371+
* left-justified data like all other sensors.
372+
*/
373+
.das = {
374+
.addr = 0x21,
375+
.mask = 0x01,
376+
},
369377
.drdy_irq = {
370378
.addr = 0x21,
371379
.mask_int1 = 0x04,

‎drivers/iio/common/st_sensors/st_sensors_core.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,15 @@ int st_sensors_init_sensor(struct iio_dev *indio_dev,
401401
return err;
402402
}
403403

404+
/* set DAS */
405+
if (sdata->sensor_settings->das.addr) {
406+
err = st_sensors_write_data_with_mask(indio_dev,
407+
sdata->sensor_settings->das.addr,
408+
sdata->sensor_settings->das.mask, 1);
409+
if (err < 0)
410+
return err;
411+
}
412+
404413
if (sdata->int_pin_open_drain) {
405414
dev_info(&indio_dev->dev,
406415
"set interrupt line to open drain mode\n");

‎include/linux/iio/common/st_sensors.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,16 @@ struct st_sensor_bdu {
115115
u8 mask;
116116
};
117117

118+
/**
119+
* struct st_sensor_das - ST sensor device data alignment selection
120+
* @addr: address of the register.
121+
* @mask: mask to write the das flag for left alignment.
122+
*/
123+
struct st_sensor_das {
124+
u8 addr;
125+
u8 mask;
126+
};
127+
118128
/**
119129
* struct st_sensor_data_ready_irq - ST sensor device data-ready interrupt
120130
* @addr: address of the register.
@@ -185,6 +195,7 @@ struct st_sensor_transfer_function {
185195
* @enable_axis: Enable one or more axis of the sensor.
186196
* @fs: Full scale register and full scale list available.
187197
* @bdu: Block data update register.
198+
* @das: Data Alignment Selection register.
188199
* @drdy_irq: Data ready register of the sensor.
189200
* @multi_read_bit: Use or not particular bit for [I2C/SPI] multi-read.
190201
* @bootime: samples to discard when sensor passing from power-down to power-up.
@@ -200,6 +211,7 @@ struct st_sensor_settings {
200211
struct st_sensor_axis enable_axis;
201212
struct st_sensor_fullscale fs;
202213
struct st_sensor_bdu bdu;
214+
struct st_sensor_das das;
203215
struct st_sensor_data_ready_irq drdy_irq;
204216
bool multi_read_bit;
205217
unsigned int bootime;

0 commit comments

Comments
 (0)