Commit 923f2095 authored by Gregor Boirie's avatar Gregor Boirie Committed by Greg Kroah-Hartman

iio:st_pressure: fix sampling gains (bring inline with ABI)

commit d43a4115 upstream.

Temperature channels report scaled samples in Celsius although expected as
milli degree Celsius in Documentation/ABI/testing/sysfs-bus-iio.
Gains are not implemented at all for LPS001WP pressure and temperature
channels.

This patch ensures that proper offsets and scales are exposed to userpace
for both pressure and temperature channels.
Also fix a NULL pointer exception when userspace reads content of sysfs
scale attribute when gains are not defined.
Signed-off-by: default avatarGregor Boirie <gregor.boirie@parrot.com>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent dda5d00a
...@@ -28,15 +28,21 @@ ...@@ -28,15 +28,21 @@
#include <linux/iio/common/st_sensors.h> #include <linux/iio/common/st_sensors.h>
#include "st_pressure.h" #include "st_pressure.h"
#define MCELSIUS_PER_CELSIUS 1000
/* Default pressure sensitivity */
#define ST_PRESS_LSB_PER_MBAR 4096UL #define ST_PRESS_LSB_PER_MBAR 4096UL
#define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \ #define ST_PRESS_KPASCAL_NANO_SCALE (100000000UL / \
ST_PRESS_LSB_PER_MBAR) ST_PRESS_LSB_PER_MBAR)
/* Default temperature sensitivity */
#define ST_PRESS_LSB_PER_CELSIUS 480UL #define ST_PRESS_LSB_PER_CELSIUS 480UL
#define ST_PRESS_CELSIUS_NANO_SCALE (1000000000UL / \ #define ST_PRESS_MILLI_CELSIUS_OFFSET 42500UL
ST_PRESS_LSB_PER_CELSIUS)
#define ST_PRESS_NUMBER_DATA_CHANNELS 1 #define ST_PRESS_NUMBER_DATA_CHANNELS 1
/* FULLSCALE */ /* FULLSCALE */
#define ST_PRESS_FS_AVL_1100MB 1100
#define ST_PRESS_FS_AVL_1260MB 1260 #define ST_PRESS_FS_AVL_1260MB 1260
#define ST_PRESS_1_OUT_XL_ADDR 0x28 #define ST_PRESS_1_OUT_XL_ADDR 0x28
...@@ -54,18 +60,20 @@ ...@@ -54,18 +60,20 @@
#define ST_PRESS_LPS331AP_PW_MASK 0x80 #define ST_PRESS_LPS331AP_PW_MASK 0x80
#define ST_PRESS_LPS331AP_FS_ADDR 0x23 #define ST_PRESS_LPS331AP_FS_ADDR 0x23
#define ST_PRESS_LPS331AP_FS_MASK 0x30 #define ST_PRESS_LPS331AP_FS_MASK 0x30
#define ST_PRESS_LPS331AP_FS_AVL_1260_VAL 0x00
#define ST_PRESS_LPS331AP_FS_AVL_1260_GAIN ST_PRESS_KPASCAL_NANO_SCALE
#define ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN ST_PRESS_CELSIUS_NANO_SCALE
#define ST_PRESS_LPS331AP_BDU_ADDR 0x20 #define ST_PRESS_LPS331AP_BDU_ADDR 0x20
#define ST_PRESS_LPS331AP_BDU_MASK 0x04 #define ST_PRESS_LPS331AP_BDU_MASK 0x04
#define ST_PRESS_LPS331AP_DRDY_IRQ_ADDR 0x22 #define ST_PRESS_LPS331AP_DRDY_IRQ_ADDR 0x22
#define ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK 0x04 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT1_MASK 0x04
#define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK 0x20 #define ST_PRESS_LPS331AP_DRDY_IRQ_INT2_MASK 0x20
#define ST_PRESS_LPS331AP_MULTIREAD_BIT true #define ST_PRESS_LPS331AP_MULTIREAD_BIT true
#define ST_PRESS_LPS331AP_TEMP_OFFSET 42500
/* CUSTOM VALUES FOR LPS001WP SENSOR */ /* CUSTOM VALUES FOR LPS001WP SENSOR */
/* LPS001WP pressure resolution */
#define ST_PRESS_LPS001WP_LSB_PER_MBAR 16UL
/* LPS001WP temperature resolution */
#define ST_PRESS_LPS001WP_LSB_PER_CELSIUS 64UL
#define ST_PRESS_LPS001WP_WAI_EXP 0xba #define ST_PRESS_LPS001WP_WAI_EXP 0xba
#define ST_PRESS_LPS001WP_ODR_ADDR 0x20 #define ST_PRESS_LPS001WP_ODR_ADDR 0x20
#define ST_PRESS_LPS001WP_ODR_MASK 0x30 #define ST_PRESS_LPS001WP_ODR_MASK 0x30
...@@ -74,6 +82,8 @@ ...@@ -74,6 +82,8 @@
#define ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL 0x03 #define ST_PRESS_LPS001WP_ODR_AVL_13HZ_VAL 0x03
#define ST_PRESS_LPS001WP_PW_ADDR 0x20 #define ST_PRESS_LPS001WP_PW_ADDR 0x20
#define ST_PRESS_LPS001WP_PW_MASK 0x40 #define ST_PRESS_LPS001WP_PW_MASK 0x40
#define ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN \
(100000000UL / ST_PRESS_LPS001WP_LSB_PER_MBAR)
#define ST_PRESS_LPS001WP_BDU_ADDR 0x20 #define ST_PRESS_LPS001WP_BDU_ADDR 0x20
#define ST_PRESS_LPS001WP_BDU_MASK 0x04 #define ST_PRESS_LPS001WP_BDU_MASK 0x04
#define ST_PRESS_LPS001WP_MULTIREAD_BIT true #define ST_PRESS_LPS001WP_MULTIREAD_BIT true
...@@ -90,18 +100,12 @@ ...@@ -90,18 +100,12 @@
#define ST_PRESS_LPS25H_ODR_AVL_25HZ_VAL 0x04 #define ST_PRESS_LPS25H_ODR_AVL_25HZ_VAL 0x04
#define ST_PRESS_LPS25H_PW_ADDR 0x20 #define ST_PRESS_LPS25H_PW_ADDR 0x20
#define ST_PRESS_LPS25H_PW_MASK 0x80 #define ST_PRESS_LPS25H_PW_MASK 0x80
#define ST_PRESS_LPS25H_FS_ADDR 0x00
#define ST_PRESS_LPS25H_FS_MASK 0x00
#define ST_PRESS_LPS25H_FS_AVL_1260_VAL 0x00
#define ST_PRESS_LPS25H_FS_AVL_1260_GAIN ST_PRESS_KPASCAL_NANO_SCALE
#define ST_PRESS_LPS25H_FS_AVL_TEMP_GAIN ST_PRESS_CELSIUS_NANO_SCALE
#define ST_PRESS_LPS25H_BDU_ADDR 0x20 #define ST_PRESS_LPS25H_BDU_ADDR 0x20
#define ST_PRESS_LPS25H_BDU_MASK 0x04 #define ST_PRESS_LPS25H_BDU_MASK 0x04
#define ST_PRESS_LPS25H_DRDY_IRQ_ADDR 0x23 #define ST_PRESS_LPS25H_DRDY_IRQ_ADDR 0x23
#define ST_PRESS_LPS25H_DRDY_IRQ_INT1_MASK 0x01 #define ST_PRESS_LPS25H_DRDY_IRQ_INT1_MASK 0x01
#define ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK 0x10 #define ST_PRESS_LPS25H_DRDY_IRQ_INT2_MASK 0x10
#define ST_PRESS_LPS25H_MULTIREAD_BIT true #define ST_PRESS_LPS25H_MULTIREAD_BIT true
#define ST_PRESS_LPS25H_TEMP_OFFSET 42500
#define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28 #define ST_PRESS_LPS25H_OUT_XL_ADDR 0x28
#define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b #define ST_TEMP_LPS25H_OUT_L_ADDR 0x2b
...@@ -153,7 +157,9 @@ static const struct iio_chan_spec st_press_lps001wp_channels[] = { ...@@ -153,7 +157,9 @@ static const struct iio_chan_spec st_press_lps001wp_channels[] = {
.storagebits = 16, .storagebits = 16,
.endianness = IIO_LE, .endianness = IIO_LE,
}, },
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), .info_mask_separate =
BIT(IIO_CHAN_INFO_RAW) |
BIT(IIO_CHAN_INFO_SCALE),
.modified = 0, .modified = 0,
}, },
{ {
...@@ -169,7 +175,7 @@ static const struct iio_chan_spec st_press_lps001wp_channels[] = { ...@@ -169,7 +175,7 @@ static const struct iio_chan_spec st_press_lps001wp_channels[] = {
}, },
.info_mask_separate = .info_mask_separate =
BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_RAW) |
BIT(IIO_CHAN_INFO_OFFSET), BIT(IIO_CHAN_INFO_SCALE),
.modified = 0, .modified = 0,
}, },
IIO_CHAN_SOFT_TIMESTAMP(1) IIO_CHAN_SOFT_TIMESTAMP(1)
...@@ -204,11 +210,14 @@ static const struct st_sensor_settings st_press_sensors_settings[] = { ...@@ -204,11 +210,14 @@ static const struct st_sensor_settings st_press_sensors_settings[] = {
.addr = ST_PRESS_LPS331AP_FS_ADDR, .addr = ST_PRESS_LPS331AP_FS_ADDR,
.mask = ST_PRESS_LPS331AP_FS_MASK, .mask = ST_PRESS_LPS331AP_FS_MASK,
.fs_avl = { .fs_avl = {
/*
* Pressure and temperature sensitivity values
* as defined in table 3 of LPS331AP datasheet.
*/
[0] = { [0] = {
.num = ST_PRESS_FS_AVL_1260MB, .num = ST_PRESS_FS_AVL_1260MB,
.value = ST_PRESS_LPS331AP_FS_AVL_1260_VAL, .gain = ST_PRESS_KPASCAL_NANO_SCALE,
.gain = ST_PRESS_LPS331AP_FS_AVL_1260_GAIN, .gain2 = ST_PRESS_LSB_PER_CELSIUS,
.gain2 = ST_PRESS_LPS331AP_FS_AVL_TEMP_GAIN,
}, },
}, },
}, },
...@@ -248,7 +257,17 @@ static const struct st_sensor_settings st_press_sensors_settings[] = { ...@@ -248,7 +257,17 @@ static const struct st_sensor_settings st_press_sensors_settings[] = {
.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
}, },
.fs = { .fs = {
.addr = 0, .fs_avl = {
/*
* Pressure and temperature resolution values
* as defined in table 3 of LPS001WP datasheet.
*/
[0] = {
.num = ST_PRESS_FS_AVL_1100MB,
.gain = ST_PRESS_LPS001WP_FS_AVL_PRESS_GAIN,
.gain2 = ST_PRESS_LPS001WP_LSB_PER_CELSIUS,
},
},
}, },
.bdu = { .bdu = {
.addr = ST_PRESS_LPS001WP_BDU_ADDR, .addr = ST_PRESS_LPS001WP_BDU_ADDR,
...@@ -285,14 +304,15 @@ static const struct st_sensor_settings st_press_sensors_settings[] = { ...@@ -285,14 +304,15 @@ static const struct st_sensor_settings st_press_sensors_settings[] = {
.value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE, .value_off = ST_SENSORS_DEFAULT_POWER_OFF_VALUE,
}, },
.fs = { .fs = {
.addr = ST_PRESS_LPS25H_FS_ADDR,
.mask = ST_PRESS_LPS25H_FS_MASK,
.fs_avl = { .fs_avl = {
/*
* Pressure and temperature sensitivity values
* as defined in table 3 of LPS25H datasheet.
*/
[0] = { [0] = {
.num = ST_PRESS_FS_AVL_1260MB, .num = ST_PRESS_FS_AVL_1260MB,
.value = ST_PRESS_LPS25H_FS_AVL_1260_VAL, .gain = ST_PRESS_KPASCAL_NANO_SCALE,
.gain = ST_PRESS_LPS25H_FS_AVL_1260_GAIN, .gain2 = ST_PRESS_LSB_PER_CELSIUS,
.gain2 = ST_PRESS_LPS25H_FS_AVL_TEMP_GAIN,
}, },
}, },
}, },
...@@ -346,26 +366,26 @@ static int st_press_read_raw(struct iio_dev *indio_dev, ...@@ -346,26 +366,26 @@ static int st_press_read_raw(struct iio_dev *indio_dev,
return IIO_VAL_INT; return IIO_VAL_INT;
case IIO_CHAN_INFO_SCALE: case IIO_CHAN_INFO_SCALE:
*val = 0;
switch (ch->type) { switch (ch->type) {
case IIO_PRESSURE: case IIO_PRESSURE:
*val = 0;
*val2 = press_data->current_fullscale->gain; *val2 = press_data->current_fullscale->gain;
break; return IIO_VAL_INT_PLUS_NANO;
case IIO_TEMP: case IIO_TEMP:
*val = MCELSIUS_PER_CELSIUS;
*val2 = press_data->current_fullscale->gain2; *val2 = press_data->current_fullscale->gain2;
break; return IIO_VAL_FRACTIONAL;
default: default:
err = -EINVAL; err = -EINVAL;
goto read_error; goto read_error;
} }
return IIO_VAL_INT_PLUS_NANO;
case IIO_CHAN_INFO_OFFSET: case IIO_CHAN_INFO_OFFSET:
switch (ch->type) { switch (ch->type) {
case IIO_TEMP: case IIO_TEMP:
*val = 425; *val = ST_PRESS_MILLI_CELSIUS_OFFSET *
*val2 = 10; press_data->current_fullscale->gain2;
*val2 = MCELSIUS_PER_CELSIUS;
break; break;
default: default:
err = -EINVAL; err = -EINVAL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment