Skip to contentSkip to conversation

iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic

PATCH Superseded Isabella Caselli 2 participants 2 messages Apr 28, 2025 – Apr 29, 2025

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic

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.

---
 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(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c +19 -22
index 8a7f29119..de013e034 100644
@@ -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;
drivers/iio/imu/inv_mpu6050/inv_mpu_aux.h +2 -0
index b66997545..0353103aa 100644
@@ -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_ */
Signature · 1 line
2.43.0
#1 lore raw

Hello,

I don't really see the point in this patch.

We already have a common function inv_mpu_i2c_master_xfer() for handling master I2C transfer, and there is no need to create a new external function inv_mpu_aux_exec_xfer() that is never called outside the inv_mpu_aux.c file.

If you want to factorize the I2C transfer status check, better add it to the inv_mpu_i2c_master_xfer function. This way it would make sense. But you need to warn that only SLV0 slot has to be used for transfer if we are enforcing SLV0 transfer check.

In the actual implementation we're free to use the slave slot we want, since all SLV(x) calls are done in the same function. It is easier to understand like this, I think.

Thanks,
JB

________________________________________
From: Isabella Caselli <[email protected]>
Sent: Monday, April 28, 2025 15:25
To: Jean-Baptiste Maneyrol <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]> Cc: [email protected] <[email protected]>; [email protected] <[email protected]>; Isabella Caselli <[email protected]> Subject: [PATCH] iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic

This Message Is From an Untrusted Sender
You have not previously corresponded with this sender.

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.

---
 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(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c +19 -22
index 8a7f29119..de013e034 100644
@@ -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;
drivers/iio/imu/inv_mpu6050/inv_mpu_aux.h +2 -0
index b66997545..0353103aa 100644
@@ -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_ */
Signature · 1 line
2.43.0