Skip to contentSkip to conversation

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

PATCH In review Isabella Caselli 4 participants 6 messages May 7, 2025 – May 31, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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

Refactors inv_mpu_aux_read() and inv_mpu_aux_write() to extract the common I2C transfer sequence into inv_mpu_i2c_master_xfer(), which now handles starting and stopping the I2C master, waiting for completion, disabling SLV0, and checking for NACK errors.

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

---
Sorry about the diff in v2 capturing the changes proposed in v1.
Just to remember the actual changes made after v1:
As requested after sending v1, we removed the newly created refactoring function — which would have only been used inside inv_mpu_aux.c — and moved the duplicated code directly into inv_mpu_i2c_master_xfer(), since the status check was always performed immediately after its execution in both inv_mpu_aux_read() and inv_mpu_aux_write().

---
 drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c | 56 ++++++++---------------
 1 file changed, 20 insertions(+), 36 deletions(-)
drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c +20 -36
index 8a7f29119..970cf5c47 100644
@@ -14,6 +14,8 @@
 /*
  * i2c master auxiliary bus transfer function.
  * Requires the i2c operations to be correctly setup before.
+ * Disables SLV0 and checks for NACK status internally.
+ * Assumes that only SLV0 is used for transfers.
  */
 static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st)
 {
@@ -23,6 +25,7 @@ static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st)
 	uint8_t d;
 	unsigned int user_ctrl;
 	int ret;
+	unsigned int status;
 
 	/* set sample rate */
 	d = INV_MPU6050_FIFO_RATE_TO_DIVIDER(freq);
@@ -51,12 +54,27 @@ static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st)
 	if (ret)
 		goto error_restore_rate;
 
+	/* 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);
+	if (ret)
+		return ret;
+
+	if (status & INV_MPU6050_BIT_I2C_SLV0_NACK)
+		return -EIO;
+
 	return 0;
 
 error_stop_i2c:
 	regmap_write(st->map, st->reg->user_ctrl, st->chip_config.user_ctrl);
 error_restore_rate:
 	regmap_write(st->map, st->reg->sample_rate_div, st->chip_config.divider);
+error_disable_i2c:
+	regmap_write(st->map, INV_MPU6050_REG_I2C_SLV_CTRL(0), 0);
 	return ret;
 }
 
@@ -117,7 +135,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 +153,14 @@ int inv_mpu_aux_read(const struct inv_mpu6050_state *st, uint8_t addr,
 	if (ret)
 		return ret;
 
-	/* do i2c xfer */
+	/* do i2c xfer, disable i2c slave and check status*/
 	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);
 	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 +175,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,26 +192,10 @@ int inv_mpu_aux_write(const struct inv_mpu6050_state *st, uint8_t addr,
 	if (ret)
 		return ret;
 
-	/* do i2c xfer */
+	/* do i2c xfer, disable i2c slave and check status*/
 	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);
 	if (ret)
 		return ret;
-	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;
 }
Signature · 1 line
2.43.0
#1 lore raw

Hi Isabella, Rodrigo,

This patch looks okay to me. Though, I think Jean-Baptiste made a valid point in v1 about inv_mpu_i2c_master_xfer() not implying any specific slave slot previously. I'd suggest to add a slv parameter to inv_mpu_i2c_master_xfer(), but that wouldn't make much difference since inv_mpu_aux_read/write() are still hardcoded for SLV 0.
Besides that, I have one minor comment that's probably not a reason for a v4 if this is deemed to be good.

Regards,
Marcelo

Quoted 12 lines · Isabella Caselli

Refactors inv_mpu_aux_read() and inv_mpu_aux_write() to extract the common I2C transfer sequence into inv_mpu_i2c_master_xfer(), which now handles starting and stopping the I2C master, waiting for completion, disabling SLV0, and checking for NACK errors.

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

---

...

drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st) · line 28

Quoted 7 lines

static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st) {

drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c +1 -0
@@ -23,6 +25,7 @@ static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st)
 	uint8_t d;
 	unsigned int user_ctrl;
 	int ret;
+	unsigned int status;

I'd declare status above ret, or together with user_ctrl. Just minor code style thing.

maintainer
#2 lore raw
Quoted 9 lines · On Sat, 10 May 2025 15:39:33 -0300 Marcelo Schmitt…

Hi Isabella, Rodrigo,

This patch looks okay to me. Though, I think Jean-Baptiste made a valid point in v1 about inv_mpu_i2c_master_xfer() not implying any specific slave slot previously. I'd suggest to add a slv parameter to inv_mpu_i2c_master_xfer(), but that wouldn't make much difference since inv_mpu_aux_read/write() are still hardcoded for SLV 0.
Besides that, I have one minor comment that's probably not a reason for a v4 if this is deemed to be good.

I'm looking for a tag from Jean-Baptiste on this.

Jonathan

Quoted 27 lines

Regards,
Marcelo

Quoted 12 lines · Isabella Caselli

Refactors inv_mpu_aux_read() and inv_mpu_aux_write() to extract the common I2C transfer sequence into inv_mpu_i2c_master_xfer(), which now handles starting and stopping the I2C master, waiting for completion, disabling SLV0, and checking for NACK errors.

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

---

...

drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st) · line 28

Quoted 7 lines

static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st) {

drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c +1 -0
@@ -23,6 +25,7 @@ static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st)
 	uint8_t d;
 	unsigned int user_ctrl;
 	int ret;
+	unsigned int status;  

I'd declare status above ret, or together with user_ctrl. Just minor code style thing.

maintainer
#3 lore raw
Quoted 13 lines · On Thu, 15 May 2025 17:47:11 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 9 lines · On Sat, 10 May 2025 15:39:33 -0300 Marcelo Schmitt…

Hi Isabella, Rodrigo,

This patch looks okay to me. Though, I think Jean-Baptiste made a valid point in v1 about inv_mpu_i2c_master_xfer() not implying any specific slave slot previously. I'd suggest to add a slv parameter to inv_mpu_i2c_master_xfer(), but that wouldn't make much difference since inv_mpu_aux_read/write() are still hardcoded for SLV 0.
Besides that, I have one minor comment that's probably not a reason for a v4 if this is deemed to be good.

Quoted 1 line

I'm looking for a tag from Jean-Baptiste on this.

Jean-Baptiste. If you have time to look at this version that would be great.

Quoted 32 lines

Jonathan

Quoted 27 lines

Regards,
Marcelo

Quoted 12 lines · Isabella Caselli

Refactors inv_mpu_aux_read() and inv_mpu_aux_write() to extract the common I2C transfer sequence into inv_mpu_i2c_master_xfer(), which now handles starting and stopping the I2C master, waiting for completion, disabling SLV0, and checking for NACK errors.

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

---

...

drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st) · line 28

Quoted 7 lines

static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st) {

drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c +1 -0
@@ -23,6 +25,7 @@ static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st)
 	uint8_t d;
 	unsigned int user_ctrl;
 	int ret;
+	unsigned int status;    

I'd declare status above ret, or together with user_ctrl. Just minor code style thing.

maintainer Acked-by
#4 lore raw

Hello Jonathan,

sorry for the delay.

It looks good like that. It is OK for me.

Thanks,
JB

________________________________________
From: Jonathan Cameron <[email protected]>
Sent: Sunday, May 25, 2025 19:23
To: Marcelo Schmitt <[email protected]>
Cc: Isabella Caselli <[email protected]>; Jean-Baptiste Maneyrol <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]> Subject: Re: [PATCH v3] iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic

This Message Is From an External Sender
This message came from outside your organization.

Quoted 13 lines · On Thu, 15 May 2025 17:47:11 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 9 lines · On Sat, 10 May 2025 15:39:33 -0300 Marcelo Schmitt…

Hi Isabella, Rodrigo,

This patch looks okay to me. Though, I think Jean-Baptiste made a valid point in v1 about inv_mpu_i2c_master_xfer() not implying any specific slave slot previously. I'd suggest to add a slv parameter to inv_mpu_i2c_master_xfer(), but that wouldn't make much difference since inv_mpu_aux_read/write() are still hardcoded for SLV 0.
Besides that, I have one minor comment that's probably not a reason for a v4 if this is deemed to be good.

Quoted 1 line

I'm looking for a tag from Jean-Baptiste on this.

Jean-Baptiste. If you have time to look at this version that would be great.

Quoted 32 lines

Jonathan

Quoted 27 lines

Regards,
Marcelo

Quoted 12 lines · Isabella Caselli

Refactors inv_mpu_aux_read() and inv_mpu_aux_write() to extract the common I2C transfer sequence into inv_mpu_i2c_master_xfer(), which now handles starting and stopping the I2C master, waiting for completion, disabling SLV0, and checking for NACK errors.

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

---

...

drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st) · line 28

Quoted 7 lines

static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st) {

drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c +1 -0
@@ -23,6 +25,7 @@ static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st)
 	uint8_t d;
 	unsigned int user_ctrl;
 	int ret;
+	unsigned int status;    

I'd declare status above ret, or together with user_ctrl. Just minor code style thing.

maintainer
#5 lore raw
Quoted 7 lines · On Tue, 27 May 2025 09:53:41 +0000 Jean-Baptiste Maneyrol…

Hello Jonathan,

sorry for the delay.

It looks good like that. It is OK for me.

Applied to the testing branch of iio.git. I'll be rebasing that on rc1 once available and pushing out for linux-next to pick up at that time.

hanks

Jonathan

Quoted 67 lines

Thanks,
JB

________________________________________
From: Jonathan Cameron <[email protected]>
Sent: Sunday, May 25, 2025 19:23
To: Marcelo Schmitt <[email protected]>
Cc: Isabella Caselli <[email protected]>; Jean-Baptiste Maneyrol <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]>; [email protected] <[email protected]> Subject: Re: [PATCH v3] iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic

This Message Is From an External Sender
This message came from outside your organization.

Quoted 13 lines · On Thu, 15 May 2025 17:47:11 +0100 Jonathan Cameron <[email protected]> wrote:
Quoted 9 lines · On Sat, 10 May 2025 15:39:33 -0300 Marcelo Schmitt…

Hi Isabella, Rodrigo,

This patch looks okay to me. Though, I think Jean-Baptiste made a valid point in v1 about inv_mpu_i2c_master_xfer() not implying any specific slave slot previously. I'd suggest to add a slv parameter to inv_mpu_i2c_master_xfer(), but that wouldn't make much difference since inv_mpu_aux_read/write() are still hardcoded for SLV 0.
Besides that, I have one minor comment that's probably not a reason for a v4 if this is deemed to be good.

Quoted 1 line

I'm looking for a tag from Jean-Baptiste on this.

Jean-Baptiste. If you have time to look at this version that would be great.

Quoted 32 lines

Jonathan

Quoted 27 lines

Regards,
Marcelo

Quoted 12 lines · Isabella Caselli

Refactors inv_mpu_aux_read() and inv_mpu_aux_write() to extract the common I2C transfer sequence into inv_mpu_i2c_master_xfer(), which now handles starting and stopping the I2C master, waiting for completion, disabling SLV0, and checking for NACK errors.

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

---

...

drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st) · line 28

Quoted 7 lines

static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st) {

drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c +1 -0
@@ -23,6 +25,7 @@ static int inv_mpu_i2c_master_xfer(const struct inv_mpu6050_state *st)
 	uint8_t d;
 	unsigned int user_ctrl;
 	int ret;
+	unsigned int status;      

I'd declare status above ret, or together with user_ctrl. Just minor code style thing.