* [PATCH v3] iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic
@ 2025-05-07 18:39 Isabella Caselli
2025-05-10 18:39 ` Marcelo Schmitt
0 siblings, 1 reply; 6+ messages in thread
From: Isabella Caselli @ 2025-05-07 18:39 UTC (permalink / raw)
To: jean-baptiste.maneyrol, jic23, lars, linux-iio
Cc: rodrigo.michelassi, 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.
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>
---
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(-)
diff --git a/drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c b/drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c
index 8a7f29119..970cf5c47 100644
--- a/drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c
+++ b/drivers/iio/imu/inv_mpu6050/inv_mpu_aux.c
@@ -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;
}
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH v3] iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic
2025-05-07 18:39 [PATCH v3] iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic Isabella Caselli
@ 2025-05-10 18:39 ` Marcelo Schmitt
2025-05-15 16:47 ` Jonathan Cameron
0 siblings, 1 reply; 6+ messages in thread
From: Marcelo Schmitt @ 2025-05-10 18:39 UTC (permalink / raw)
To: Isabella Caselli
Cc: jean-baptiste.maneyrol, jic23, lars, linux-iio,
rodrigo.michelassi
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
On 05/07, Isabella Caselli wrote:
> 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.
>
> 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>
> ---
...
> 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;
I'd declare status above ret, or together with user_ctrl.
Just minor code style thing.
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v3] iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic
2025-05-10 18:39 ` Marcelo Schmitt
@ 2025-05-15 16:47 ` Jonathan Cameron
2025-05-25 17:23 ` Jonathan Cameron
0 siblings, 1 reply; 6+ messages in thread
From: Jonathan Cameron @ 2025-05-15 16:47 UTC (permalink / raw)
To: Marcelo Schmitt
Cc: Isabella Caselli, jean-baptiste.maneyrol, lars, linux-iio,
rodrigo.michelassi
On Sat, 10 May 2025 15:39:33 -0300
Marcelo Schmitt <marcelo.schmitt1@gmail.com> wrote:
> 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
>
> Regards,
> Marcelo
>
> On 05/07, Isabella Caselli wrote:
> > 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.
> >
> > 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>
> > ---
> ...
> > 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;
> I'd declare status above ret, or together with user_ctrl.
> Just minor code style thing.
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v3] iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic
2025-05-15 16:47 ` Jonathan Cameron
@ 2025-05-25 17:23 ` Jonathan Cameron
2025-05-27 9:53 ` Jean-Baptiste Maneyrol
0 siblings, 1 reply; 6+ messages in thread
From: Jonathan Cameron @ 2025-05-25 17:23 UTC (permalink / raw)
To: Marcelo Schmitt
Cc: Isabella Caselli, jean-baptiste.maneyrol, lars, linux-iio,
rodrigo.michelassi
On Thu, 15 May 2025 17:47:11 +0100
Jonathan Cameron <jic23@kernel.org> wrote:
> On Sat, 10 May 2025 15:39:33 -0300
> Marcelo Schmitt <marcelo.schmitt1@gmail.com> wrote:
>
> > 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.
Jean-Baptiste. If you have time to look at this version that would be great.
>
> Jonathan
>
> >
> > Regards,
> > Marcelo
> >
> > On 05/07, Isabella Caselli wrote:
> > > 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.
> > >
> > > 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>
> > > ---
> > ...
> > > 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;
> > I'd declare status above ret, or together with user_ctrl.
> > Just minor code style thing.
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v3] iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic
2025-05-25 17:23 ` Jonathan Cameron
@ 2025-05-27 9:53 ` Jean-Baptiste Maneyrol
2025-05-31 15:08 ` Jonathan Cameron
0 siblings, 1 reply; 6+ messages in thread
From: Jean-Baptiste Maneyrol @ 2025-05-27 9:53 UTC (permalink / raw)
To: Jonathan Cameron, Marcelo Schmitt
Cc: Isabella Caselli, lars@metafoo.de, linux-iio@vger.kernel.org,
rodrigo.michelassi@usp.br
Hello Jonathan,
sorry for the delay.
It looks good like that. It is OK for me.
Acked-by: Jean-Baptiste Maneyrol <jean-baptiste.maneyrol@tdk.com>
Thanks,
JB
________________________________________
From: Jonathan Cameron <jic23@kernel.org>
Sent: Sunday, May 25, 2025 19:23
To: Marcelo Schmitt <marcelo.schmitt1@gmail.com>
Cc: Isabella Caselli <bellacaselli20@gmail.com>; Jean-Baptiste Maneyrol <Jean-Baptiste.Maneyrol@tdk.com>; lars@metafoo.de <lars@metafoo.de>; linux-iio@vger.kernel.org <linux-iio@vger.kernel.org>; rodrigo.michelassi@usp.br <rodrigo.michelassi@usp.br>
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.
On Thu, 15 May 2025 17:47:11 +0100
Jonathan Cameron <jic23@kernel.org> wrote:
> On Sat, 10 May 2025 15:39:33 -0300
> Marcelo Schmitt <marcelo.schmitt1@gmail.com> wrote:
>
> > 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.
Jean-Baptiste. If you have time to look at this version that would be great.
>
> Jonathan
>
> >
> > Regards,
> > Marcelo
> >
> > On 05/07, Isabella Caselli wrote:
> > > 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.
> > >
> > > 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>
> > > ---
> > ...
> > > 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;
> > I'd declare status above ret, or together with user_ctrl.
> > Just minor code style thing.
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v3] iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic
2025-05-27 9:53 ` Jean-Baptiste Maneyrol
@ 2025-05-31 15:08 ` Jonathan Cameron
0 siblings, 0 replies; 6+ messages in thread
From: Jonathan Cameron @ 2025-05-31 15:08 UTC (permalink / raw)
To: Jean-Baptiste Maneyrol
Cc: Marcelo Schmitt, Isabella Caselli, lars@metafoo.de,
linux-iio@vger.kernel.org, rodrigo.michelassi@usp.br
On Tue, 27 May 2025 09:53:41 +0000
Jean-Baptiste Maneyrol <Jean-Baptiste.Maneyrol@tdk.com> wrote:
> Hello Jonathan,
>
> sorry for the delay.
>
> It looks good like that. It is OK for me.
>
> Acked-by: Jean-Baptiste Maneyrol <jean-baptiste.maneyrol@tdk.com>
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
>
> Thanks,
> JB
>
> ________________________________________
> From: Jonathan Cameron <jic23@kernel.org>
> Sent: Sunday, May 25, 2025 19:23
> To: Marcelo Schmitt <marcelo.schmitt1@gmail.com>
> Cc: Isabella Caselli <bellacaselli20@gmail.com>; Jean-Baptiste Maneyrol <Jean-Baptiste.Maneyrol@tdk.com>; lars@metafoo.de <lars@metafoo.de>; linux-iio@vger.kernel.org <linux-iio@vger.kernel.org>; rodrigo.michelassi@usp.br <rodrigo.michelassi@usp.br>
> 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.
>
> On Thu, 15 May 2025 17:47:11 +0100
> Jonathan Cameron <jic23@kernel.org> wrote:
>
> > On Sat, 10 May 2025 15:39:33 -0300
> > Marcelo Schmitt <marcelo.schmitt1@gmail.com> wrote:
> >
> > > 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.
>
> Jean-Baptiste. If you have time to look at this version that would be great.
>
> >
> > Jonathan
> >
> > >
> > > Regards,
> > > Marcelo
> > >
> > > On 05/07, Isabella Caselli wrote:
> > > > 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.
> > > >
> > > > 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>
> > > > ---
> > > ...
> > > > 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;
> > > I'd declare status above ret, or together with user_ctrl.
> > > Just minor code style thing.
> >
> >
>
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-05-31 15:08 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-05-07 18:39 [PATCH v3] iio: imu: inv_mpu6050: refactor aux read/write to use shared xfer logic Isabella Caselli
2025-05-10 18:39 ` Marcelo Schmitt
2025-05-15 16:47 ` Jonathan Cameron
2025-05-25 17:23 ` Jonathan Cameron
2025-05-27 9:53 ` Jean-Baptiste Maneyrol
2025-05-31 15:08 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox