summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGwendal Grignou <gwendal@chromium.org>2018-01-24 08:28:03 -0800
committerChromeOS Commit Bot <chromeos-commit-bot@chromium.org>2018-03-07 08:17:48 +0000
commit0b789b22be42c18248747fca404e972004857893 (patch)
tree7b6f75e7064e21a90c6cdc49e1f204db6f94e97a
parent94ba0567f926d1f3237f77c2ecc85e66db4ca0c4 (diff)
downloadchrome-ec-0b789b22be42c18248747fca404e972004857893.tar.gz
driver: lsm6dsm: fix units
Units must be reported in according to the range. 2g means 1<<15 should be returned when accel is 2g. Actually accelerometer report units in mg. BUG=b:73546254 BRANCH=master TEST=Check with accelinfo with 2g gain Z ~= 1<<14. Change-Id: I218210ca8305ecbe76a681b535f3d75f3a6bea52 Signed-off-by: Gwendal Grignou <gwendal@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/952498 Reviewed-by: Aseda Aboagye <aaboagye@chromium.org> Commit-Queue: Aseda Aboagye <aaboagye@chromium.org> Tested-by: Aseda Aboagye <aaboagye@chromium.org>
-rw-r--r--driver/accelgyro_lsm6dsm.c13
-rw-r--r--driver/accelgyro_lsm6dsm.h19
2 files changed, 9 insertions, 23 deletions
diff --git a/driver/accelgyro_lsm6dsm.c b/driver/accelgyro_lsm6dsm.c
index c25b56b07d..ec4c9f8022 100644
--- a/driver/accelgyro_lsm6dsm.c
+++ b/driver/accelgyro_lsm6dsm.c
@@ -45,7 +45,8 @@ static int set_range(const struct motion_sensor_t *s, int range, int rnd)
if (s->type == MOTIONSENSE_TYPE_ACCEL) {
/* Adjust and check rounded value for acc. */
if (rnd && (newrange < LSM6DSM_ACCEL_NORMALIZE_FS(newrange)))
- newrange <<= 1;
+ newrange *= 2;
+
if (newrange > LSM6DSM_ACCEL_FS_MAX_VAL)
newrange = LSM6DSM_ACCEL_FS_MAX_VAL;
@@ -53,7 +54,8 @@ static int set_range(const struct motion_sensor_t *s, int range, int rnd)
} else {
/* Adjust and check rounded value for gyro. */
if (rnd && (newrange < LSM6DSM_GYRO_NORMALIZE_FS(newrange)))
- newrange <<= 1;
+ newrange *= 2;
+
if (newrange > LSM6DSM_GYRO_FS_MAX_VAL)
newrange = LSM6DSM_GYRO_FS_MAX_VAL;
@@ -65,7 +67,7 @@ static int set_range(const struct motion_sensor_t *s, int range, int rnd)
if (err == EC_SUCCESS)
/* Save internally gain for speed optimization. */
data->base.range = (s->type == MOTIONSENSE_TYPE_ACCEL ?
- LSM6DSM_ACCEL_FS_GAIN(newrange) :
+ newrange :
LSM6DSM_GYRO_FS_GAIN(newrange));
mutex_unlock(s->mutex);
@@ -82,9 +84,8 @@ static int get_range(const struct motion_sensor_t *s)
{
struct stprivate_data *data = s->drv_data;
- if (MOTIONSENSE_TYPE_ACCEL == s->type)
- return LSM6DSM_ACCEL_GAIN_FS(data->base.range);
-
+ if (s->type == MOTIONSENSE_TYPE_ACCEL)
+ return data->base.range;
return LSM6DSM_GYRO_GAIN_FS(data->base.range);
}
diff --git a/driver/accelgyro_lsm6dsm.h b/driver/accelgyro_lsm6dsm.h
index 1c1dea606d..606e19153f 100644
--- a/driver/accelgyro_lsm6dsm.h
+++ b/driver/accelgyro_lsm6dsm.h
@@ -85,31 +85,16 @@ enum lsm6dsm_odr {
#define LSM6DSM_ACCEL_FS_8G_VAL 0x03
#define LSM6DSM_ACCEL_FS_16G_VAL 0x01
-#define LSM6DSM_ACCEL_FS_2G_GAIN 61
-#define LSM6DSM_ACCEL_FS_4G_GAIN 122
-#define LSM6DSM_ACCEL_FS_8G_GAIN 244
-#define LSM6DSM_ACCEL_FS_16G_GAIN 488
-
#define LSM6DSM_ACCEL_FS_MAX_VAL 16
-/* Accel Gain value from selected Full Scale */
-#define LSM6DSM_ACCEL_FS_GAIN(_fs) \
- (_fs == 16 ? LSM6DSM_ACCEL_FS_16G_GAIN : \
- LSM6DSM_ACCEL_FS_2G_GAIN << __fls(_fs / 2))
-
-/* Accel FS Full Scale value from Gain */
-#define LSM6DSM_ACCEL_GAIN_FS(_gain) \
- (1 << (32 - __builtin_clz(_gain / LSM6DSM_ACCEL_FS_2G_GAIN)))
-
/* Accel Reg value from Full Scale */
#define LSM6DSM_ACCEL_FS_REG(_fs) \
(_fs == 2 ? LSM6DSM_ACCEL_FS_2G_VAL : \
_fs == 16 ? LSM6DSM_ACCEL_FS_16G_VAL : \
- (32 - __builtin_clz(_fs / 2)))
+ __fls(_fs))
/* Accel normalized FS value from Full Scale */
-#define LSM6DSM_ACCEL_NORMALIZE_FS(_fs) \
- (1 << (32 - __builtin_clz(_fs / 2)))
+#define LSM6DSM_ACCEL_NORMALIZE_FS(_fs) (1 << __fls(_fs))
/* Full Scale range value and gain for Gyro */
#define LSM6DSM_GYRO_FS_ADDR 0x11