]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blame - releases/4.14.114/iio-cros_ec-fix-the-maths-for-gyro-scale-calculation.patch
Linux 4.14.114
[thirdparty/kernel/stable-queue.git] / releases / 4.14.114 / iio-cros_ec-fix-the-maths-for-gyro-scale-calculation.patch
CommitLineData
21cf661e
GKH
1From 3d02d7082e5823598090530c3988a35f69689943 Mon Sep 17 00:00:00 2001
2From: Gwendal Grignou <gwendal@chromium.org>
3Date: Wed, 13 Mar 2019 12:40:02 +0100
4Subject: iio: cros_ec: Fix the maths for gyro scale calculation
5
6From: Gwendal Grignou <gwendal@chromium.org>
7
8commit 3d02d7082e5823598090530c3988a35f69689943 upstream.
9
10Calculation did not use IIO_DEGREE_TO_RAD and implemented a variant to
11avoid precision loss as we aim a nano value. The offset added to avoid
12rounding error, though, doesn't give us a close result to the expected
13value. E.g.
14
15For 1000dps, the result should be:
16
17 (1000 * pi ) / 180 >> 15 ~= 0.000532632218
18
19But with current calculation we get
20
21 $ cat scale
22 0.000547890
23
24Fix the calculation by just doing the maths involved for a nano value
25
26 val * pi * 10e12 / (180 * 2^15)
27
28so we get a closer result.
29
30 $ cat scale
31 0.000532632
32
33Fixes: c14dca07a31d ("iio: cros_ec_sensors: add ChromeOS EC Contiguous Sensors driver")
34Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
35Signed-off-by: Enric Balletbo i Serra <enric.balletbo@collabora.com>
36Cc: <Stable@vger.kernel.org>
37Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
38Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
39
40---
41 drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c | 7 ++++---
42 1 file changed, 4 insertions(+), 3 deletions(-)
43
44--- a/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
45+++ b/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
46@@ -104,9 +104,10 @@ static int cros_ec_sensors_read(struct i
47 * Do not use IIO_DEGREE_TO_RAD to avoid precision
48 * loss. Round to the nearest integer.
49 */
50- *val = div_s64(val64 * 314159 + 9000000ULL, 1000);
51- *val2 = 18000 << (CROS_EC_SENSOR_BITS - 1);
52- ret = IIO_VAL_FRACTIONAL;
53+ *val = 0;
54+ *val2 = div_s64(val64 * 3141592653ULL,
55+ 180 << (CROS_EC_SENSOR_BITS - 1));
56+ ret = IIO_VAL_INT_PLUS_NANO;
57 break;
58 case MOTIONSENSE_TYPE_MAG:
59 /*