Linux IIO development
 help / color / mirror / Atom feed
From: Isabella Caselli <bellacaselli20@gmail.com>
To: jean-baptiste.maneyrol@tdk.com, jic23@kernel.org,
	lars@metafoo.de, linux-iio@vger.kernel.org
Cc: rodrigo.michelassi@usp.br, marcelo.schmitt1@gmail.com,
	Isabella Caselli <bellacaselli20@gmail.com>
Subject: [PATCH] iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic
Date: Mon, 28 Apr 2025 10:25:43 -0300	[thread overview]
Message-ID: <20250428132551.176788-1-bellacaselli20@gmail.com> (raw)

Extract common transfer logic from inv_mpu_aux_read() and
inv_mpu_aux_write() into a new helper function,
inv_mpu_aux_exec_xfer(), which performs the I2C transfer.

This refactoring removes code duplication and improves maintainability.
No functional changes are intended.

Signed-off-by: Isabella Caselli <bellacaselli20@gmail.com>
Co-developed-by: Rodrigo Michelassi <rodrigo.michelassi@usp.br>
Signed-off-by: Rodrigo Michelassi <rodrigo.michelassi@usp.br>
---
 drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c | 41 +++++++++++------------
 drivers/iio/imu/inv_mpu6050/inv_mpu_aux.h |  2 ++
 2 files changed, 21 insertions(+), 22 deletions(-)

diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c
index 8a7f29119..de013e034 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c
@@ -117,7 +117,6 @@ int inv_mpu_aux_init(const struct inv_mpu6050_state *st)
 int inv_mpu_aux_read(const struct inv_mpu6050_state *st, uint8_t addr,
 		     uint8_t reg, uint8_t *val, size_t size)
 {
-	unsigned int status;
 	int ret;
 
 	if (size > 0x0F)
@@ -136,30 +135,13 @@ int inv_mpu_aux_read(const struct inv_mpu6050_state *st, uint8_t addr,
 	if (ret)
 		return ret;
 
-	/* do i2c xfer */
-	ret = inv_mpu_i2c_master_xfer(st);
-	if (ret)
-		goto error_disable_i2c;
-
-	/* disable i2c slave */
-	ret = regmap_write(st->map, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0);
-	if (ret)
-		goto error_disable_i2c;
-
-	/* check i2c status */
-	ret = regmap_read(st->map, INV_MPU6050_REG_I2C_MST_STATUS, &status);
+	ret = inv_mpu_aux_exec_xfer(st);
 	if (ret)
 		return ret;
-	if (status & INV_MPU6050_BIT_I2C_SLV0_NACK)
-		return -EIO;
 
 	/* read data in registers */
 	return regmap_bulk_read(st->map, INV_MPU6050_REG_EXT_SENS_DATA,
 				val, size);
-
-error_disable_i2c:
-	regmap_write(st->map, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0);
-	return ret;
 }
 
 /**
@@ -174,7 +156,6 @@ int inv_mpu_aux_read(const struct inv_mpu6050_state *st, uint8_t addr,
 int inv_mpu_aux_write(const struct inv_mpu6050_state *st, uint8_t addr,
 		      uint8_t reg, uint8_t val)
 {
-	unsigned int status;
 	int ret;
 
 	/* setup i2c SLV0 control: i2c addr, register, value, enable + size */
@@ -192,6 +173,24 @@ int inv_mpu_aux_write(const struct inv_mpu6050_state *st, uint8_t addr,
 	if (ret)
 		return ret;
 
+	ret = inv_mpu_aux_exec_xfer(st);
+	if (ret)
+	return ret;
+
+	return 0;
+}
+
+/**
+ * inv_mpu_aux_exec_xfer() - executes i2c auxiliary transfer and checks status
+ * @st: driver internal state.
+ *
+ *  Returns 0 on success, a negative error code otherwise.
+ */
+int inv_mpu_aux_exec_xfer(const struct inv_mpu6050_state *st)
+{
+	int ret;
+	unsigned int status;
+
 	/* do i2c xfer */
 	ret = inv_mpu_i2c_master_xfer(st);
 	if (ret)
@@ -209,8 +208,6 @@ int inv_mpu_aux_write(const struct inv_mpu6050_state *st, uint8_t addr,
 	if (status & INV_MPU6050_BIT_I2C_SLV0_NACK)
 		return -EIO;
 
-	return 0;
-
 error_disable_i2c:
 	regmap_write(st->map, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0);
 	return ret;
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_aux.h b/drivers/iio/imu/inv_mpu6050/inv_mpu_aux.h
index b66997545..0353103aa 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_aux.h
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_aux.h
@@ -16,4 +16,6 @@ int inv_mpu_aux_read(const struct inv_mpu6050_state *st, uint8_t addr,
 int inv_mpu_aux_write(const struct inv_mpu6050_state *st, uint8_t addr,
 		      uint8_t reg, uint8_t val);
 
+int inv_mpu_aux_exec_xfer(const struct inv_mpu6050_state *st);
+
 #endif		/* INV_MPU_AUX_H_ */
-- 
2.43.0


             reply	other threads:[~2025-04-28 13:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-28 13:25 Isabella Caselli [this message]
2025-04-29  9:36 ` [PATCH] iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic Jean-Baptiste Maneyrol

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=20250428132551.176788-1-bellacaselli20@gmail.com \
    --to=bellacaselli20@gmail.com \
    --cc=jean-baptiste.maneyrol@tdk.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=marcelo.schmitt1@gmail.com \
    --cc=rodrigo.michelassi@usp.br \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox