All of lore.kernel.org
 help / color / mirror / Atom feed
From: carlos.song@nxp.com
To: jic23@kernel.org, lars@metafoo.de
Cc: rjones@gateworks.com, Jonathan.Cameron@huawei.com,
	haibo.chen@nxp.com, carlos.song@nxp.com, linux-imx@nxp.com,
	linux-iio@vger.kernel.org
Subject: [PATCH v2 4/7] iio: imu: fxos8700: fix IMU data bits returned to user space
Date: Thu,  8 Dec 2022 15:19:08 +0800	[thread overview]
Message-ID: <20221208071911.2405922-5-carlos.song@nxp.com> (raw)
In-Reply-To: <20221208071911.2405922-1-carlos.song@nxp.com>

From: Carlos Song <carlos.song@nxp.com>

ACCEL output data registers contain the X-axis, Y-axis, and Z-axis
14-bit left-justified sample data and MAGN output data registers
contain the X-axis, Y-axis, and Z-axis 16-bit sample data. The ACCEL
raw register output data should be divided by 4 before sent to
userspace.

Apply a 2 bits signed right shift to the raw data from ACCEL output
data register but keep that from MAGN sensor as the origin.

Fixes: 84e5ddd5c46e ("iio: imu: Add support for the FXOS8700 IMU")
Signed-off-by: Carlos Song <carlos.song@nxp.com>
---
Changes for V2:
- Store the shift in the switch and apply a shift by 2 for ACCEL
  and shift by 0 for MAGN
- Confirm the scaling is still correct for the acceleration channels
  given we are effectively dividing by 4 compared to the previous code
- Rework the comment

diff --git a/drivers/iio/imu/fxos8700_core.c b/drivers/iio/imu/fxos8700_core.c
index b62bc92bbacc..d2e784628820 100644
--- a/drivers/iio/imu/fxos8700_core.c
+++ b/drivers/iio/imu/fxos8700_core.c
@@ -394,6 +394,7 @@ static int fxos8700_get_data(struct fxos8700_data *data, int chan_type,
 			     int axis, int *val)
 {
 	u8 base, reg;
+	s16 tmp;
 	int ret;
 
 	/*
@@ -421,8 +422,33 @@ static int fxos8700_get_data(struct fxos8700_data *data, int chan_type,
 	/* Convert axis to buffer index */
 	reg = axis - IIO_MOD_X;
 
+	/*
+	 * Convert to native endianness. The accel data and magn data
+	 * are signed, so a forced type conversion is needed.
+	 */
+	tmp = be16_to_cpu(data->buf[reg]);
+
+	/*
+	 * ACCEL output data registers contain the X-axis, Y-axis, and Z-axis
+	 * 14-bit left-justified sample data and MAGN output data registers
+	 * contain the X-axis, Y-axis, and Z-axis 16-bit sample data. Apply
+	 * a signed 2 bits right shift to the readback raw data from ACCEL
+	 * output data register and keep that from MAGN sensor as the origin.
+	 * Value should be extended to 32 bit.
+	 */
+	switch (chan_type) {
+	case IIO_ACCEL:
+		tmp = tmp >> 2;
+		break;
+	case IIO_MAGN:
+		tmp = tmp >> 0;
+		break;
+	default:
+		return -EINVAL;
+	}
+
 	/* Convert to native endianness */
-	*val = sign_extend32(be16_to_cpu(data->buf[reg]), 15);
+	*val = sign_extend32(tmp, 15);
 
 	return 0;
 }
-- 
2.34.1


  parent reply	other threads:[~2022-12-08  7:19 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-08  7:19 [PATCH v2 0/7] iio: imu: fxos8700: fix few bugs in data readback and mode set carlos.song
2022-12-08  7:19 ` [PATCH v2 1/7] iio: imu: fxos8700: fix map lable of channel type to MAGN sensor carlos.song
2022-12-11 13:42   ` Jonathan Cameron
2022-12-08  7:19 ` [PATCH v2 2/7] iio: imu: fxos8700: fix swapped ACCEL and MAGN channels readback carlos.song
2022-12-11 13:43   ` Jonathan Cameron
2022-12-08  7:19 ` [PATCH v2 3/7] iio: imu: fxos8700: fix incompete " carlos.song
2022-12-11 13:45   ` Jonathan Cameron
2022-12-08  7:19 ` carlos.song [this message]
2022-12-11 13:49   ` [PATCH v2 4/7] iio: imu: fxos8700: fix IMU data bits returned to user space Jonathan Cameron
2022-12-08  7:19 ` [PATCH v2 5/7] iio: imu: fxos8700: fix ACCEL measurement range selection carlos.song
2022-12-11 13:51   ` Jonathan Cameron
2022-12-08  7:19 ` [PATCH v2 6/7] iio: imu: fxos8700: fix ODR register readback and initialization carlos.song
2022-12-11 14:01   ` Jonathan Cameron
2022-12-08  7:19 ` [PATCH v2 7/7] iio: imu: fxos8700: fix MAGN sensor scale and unit carlos.song
2022-12-11 14:05   ` Jonathan Cameron

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221208071911.2405922-5-carlos.song@nxp.com \
    --to=carlos.song@nxp.com \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=haibo.chen@nxp.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-imx@nxp.com \
    --cc=rjones@gateworks.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.