linux-mediatek.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] i2c: mediatek: fix potential incorrect use of I2C_MASTER_WRRD
@ 2025-08-16  7:53 Leilk Liu
  2025-08-18  3:53 ` Chen-Yu Tsai
  2025-08-19 21:09 ` Andi Shyti
  0 siblings, 2 replies; 4+ messages in thread
From: Leilk Liu @ 2025-08-16  7:53 UTC (permalink / raw)
  To: Andi Shyti, Matthias Brugger, AngeloGioacchino Del Regno
  Cc: Qii Wang, Wolfram Sang, Liguo Zhang, linux-i2c, linux-kernel,
	linux-arm-kernel, linux-mediatek,
	Project_Global_Chrome_Upstream_Group, Leilk.Liu

From: "Leilk.Liu" <leilk.liu@mediatek.com>

The old IC does not support the I2C_MASTER_WRRD (write-then-read)
function, but the current code’s handling of i2c->auto_restart may
potentially lead to entering the I2C_MASTER_WRRD software flow,
resulting in unexpected bugs.

Instead of repurposing the auto_restart flag, add a separate flag
to signal I2C_MASTER_WRRD operations.

Also fix handling of msgs. If the operation (i2c->op) is
I2C_MASTER_WRRD, then the msgs pointer is incremented by 2.
For all other operations, msgs is simply incremented by 1.

Fixes: 173b77e8d8fe ("i2c: mediatek: add i2c first write then read optimization")

Signed-off-by: Leilk.Liu <leilk.liu@mediatek.com>
---
 drivers/i2c/busses/i2c-mt65xx.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/drivers/i2c/busses/i2c-mt65xx.c b/drivers/i2c/busses/i2c-mt65xx.c
index ab456c3717db..dee40704825c 100644
--- a/drivers/i2c/busses/i2c-mt65xx.c
+++ b/drivers/i2c/busses/i2c-mt65xx.c
@@ -1243,6 +1243,7 @@ static int mtk_i2c_transfer(struct i2c_adapter *adap,
 {
 	int ret;
 	int left_num = num;
+	bool write_then_read_en = false;
 	struct mtk_i2c *i2c = i2c_get_adapdata(adap);
 
 	ret = clk_bulk_enable(I2C_MT65XX_CLK_MAX, i2c->clocks);
@@ -1256,6 +1257,7 @@ static int mtk_i2c_transfer(struct i2c_adapter *adap,
 		if (!(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD) &&
 		    msgs[0].addr == msgs[1].addr) {
 			i2c->auto_restart = 0;
+			write_then_read_en = true;
 		}
 	}
 
@@ -1280,12 +1282,10 @@ static int mtk_i2c_transfer(struct i2c_adapter *adap,
 		else
 			i2c->op = I2C_MASTER_WR;
 
-		if (!i2c->auto_restart) {
-			if (num > 1) {
-				/* combined two messages into one transaction */
-				i2c->op = I2C_MASTER_WRRD;
-				left_num--;
-			}
+		if (write_then_read_en) {
+			/* combined two messages into one transaction */
+			i2c->op = I2C_MASTER_WRRD;
+			left_num--;
 		}
 
 		/* always use DMA mode. */
@@ -1293,7 +1293,10 @@ static int mtk_i2c_transfer(struct i2c_adapter *adap,
 		if (ret < 0)
 			goto err_exit;
 
-		msgs++;
+		if (i2c->op == I2C_MASTER_WRRD)
+			msgs += 2;
+		else
+			msgs++;
 	}
 	/* the return value is number of executed messages */
 	ret = num;
-- 
2.46.0



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] i2c: mediatek: fix potential incorrect use of I2C_MASTER_WRRD
  2025-08-16  7:53 [PATCH] i2c: mediatek: fix potential incorrect use of I2C_MASTER_WRRD Leilk Liu
@ 2025-08-18  3:53 ` Chen-Yu Tsai
  2025-08-19 21:09 ` Andi Shyti
  1 sibling, 0 replies; 4+ messages in thread
From: Chen-Yu Tsai @ 2025-08-18  3:53 UTC (permalink / raw)
  To: Leilk Liu
  Cc: Andi Shyti, Matthias Brugger, AngeloGioacchino Del Regno,
	Qii Wang, Wolfram Sang, Liguo Zhang, linux-i2c, linux-kernel,
	linux-arm-kernel, linux-mediatek,
	Project_Global_Chrome_Upstream_Group

On Sat, Aug 16, 2025 at 3:55 PM Leilk Liu <leilk.liu@mediatek.com> wrote:
>
> From: "Leilk.Liu" <leilk.liu@mediatek.com>
>
> The old IC does not support the I2C_MASTER_WRRD (write-then-read)
> function, but the current code’s handling of i2c->auto_restart may
> potentially lead to entering the I2C_MASTER_WRRD software flow,
> resulting in unexpected bugs.
>
> Instead of repurposing the auto_restart flag, add a separate flag
> to signal I2C_MASTER_WRRD operations.
>
> Also fix handling of msgs. If the operation (i2c->op) is
> I2C_MASTER_WRRD, then the msgs pointer is incremented by 2.
> For all other operations, msgs is simply incremented by 1.
>
> Fixes: 173b77e8d8fe ("i2c: mediatek: add i2c first write then read optimization")
>
> Signed-off-by: Leilk.Liu <leilk.liu@mediatek.com>

This was

Suggested-by: Chen-Yu Tsai <wenst@chromium.org>

internally because the code looked funny.

Reviewed-by: Chen-Yu Tsai <wenst@chromium.org>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] i2c: mediatek: fix potential incorrect use of I2C_MASTER_WRRD
  2025-08-16  7:53 [PATCH] i2c: mediatek: fix potential incorrect use of I2C_MASTER_WRRD Leilk Liu
  2025-08-18  3:53 ` Chen-Yu Tsai
@ 2025-08-19 21:09 ` Andi Shyti
  2025-09-01  9:25   ` Leilk Liu (刘磊)
  1 sibling, 1 reply; 4+ messages in thread
From: Andi Shyti @ 2025-08-19 21:09 UTC (permalink / raw)
  To: Leilk Liu
  Cc: Matthias Brugger, AngeloGioacchino Del Regno, Qii Wang,
	Wolfram Sang, Liguo Zhang, linux-i2c, linux-kernel,
	linux-arm-kernel, linux-mediatek,
	Project_Global_Chrome_Upstream_Group

Hi Leilk,

On Sat, Aug 16, 2025 at 03:53:54PM +0800, Leilk Liu wrote:
> From: "Leilk.Liu" <leilk.liu@mediatek.com>
> 
> The old IC does not support the I2C_MASTER_WRRD (write-then-read)
> function, but the current code’s handling of i2c->auto_restart may
> potentially lead to entering the I2C_MASTER_WRRD software flow,
> resulting in unexpected bugs.

do you mean unexpected behaviour?

> Instead of repurposing the auto_restart flag, add a separate flag
> to signal I2C_MASTER_WRRD operations.
> 
> Also fix handling of msgs. If the operation (i2c->op) is
> I2C_MASTER_WRRD, then the msgs pointer is incremented by 2.
> For all other operations, msgs is simply incremented by 1.
> 
> Fixes: 173b77e8d8fe ("i2c: mediatek: add i2c first write then read optimization")
> 

No need for blank line here. BTW, this doesn't look the commit
that is introducing the issue.

> Signed-off-by: Leilk.Liu <leilk.liu@mediatek.com>
> ---
>  drivers/i2c/busses/i2c-mt65xx.c | 17 ++++++++++-------
>  1 file changed, 10 insertions(+), 7 deletions(-)
> 
> diff --git a/drivers/i2c/busses/i2c-mt65xx.c b/drivers/i2c/busses/i2c-mt65xx.c
> index ab456c3717db..dee40704825c 100644
> --- a/drivers/i2c/busses/i2c-mt65xx.c
> +++ b/drivers/i2c/busses/i2c-mt65xx.c
> @@ -1243,6 +1243,7 @@ static int mtk_i2c_transfer(struct i2c_adapter *adap,
>  {
>  	int ret;
>  	int left_num = num;
> +	bool write_then_read_en = false;
>  	struct mtk_i2c *i2c = i2c_get_adapdata(adap);
>  
>  	ret = clk_bulk_enable(I2C_MT65XX_CLK_MAX, i2c->clocks);
> @@ -1256,6 +1257,7 @@ static int mtk_i2c_transfer(struct i2c_adapter *adap,
>  		if (!(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD) &&
>  		    msgs[0].addr == msgs[1].addr) {
>  			i2c->auto_restart = 0;
> +			write_then_read_en = true;

why don't we set "i2c->op = I2C_MASTER_WRRD" here and avoid an
extra flag?

Thanks,
Andi

>  		}
>  	}
>  


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] i2c: mediatek: fix potential incorrect use of I2C_MASTER_WRRD
  2025-08-19 21:09 ` Andi Shyti
@ 2025-09-01  9:25   ` Leilk Liu (刘磊)
  0 siblings, 0 replies; 4+ messages in thread
From: Leilk Liu (刘磊) @ 2025-09-01  9:25 UTC (permalink / raw)
  To: andi.shyti@kernel.org
  Cc: linux-kernel@vger.kernel.org, linux-mediatek@lists.infradead.org,
	wsa@kernel.org, linux-i2c@vger.kernel.org,
	Project_Global_Chrome_Upstream_Group,
	Liguo Zhang (张立国),
	linux-arm-kernel@lists.infradead.org, matthias.bgg@gmail.com,
	Qii Wang (王琪), AngeloGioacchino Del Regno

On Tue, 2025-08-19 at 20:09 -0100, Andi Shyti wrote:
> External email : Please do not click links or open attachments until
> you have verified the sender or the content.
> 
> 
Hi Andi, sorry for late reply.

> Hi Leilk,
> 
> On Sat, Aug 16, 2025 at 03:53:54PM +0800, Leilk Liu wrote:
> > From: "Leilk.Liu" <leilk.liu@mediatek.com>
> > 
> > The old IC does not support the I2C_MASTER_WRRD (write-then-read)
> > function, but the current code’s handling of i2c->auto_restart may
> > potentially lead to entering the I2C_MASTER_WRRD software flow,
> > resulting in unexpected bugs.
> 
> do you mean unexpected behaviour?
sure.

> 
> > Instead of repurposing the auto_restart flag, add a separate flag
> > to signal I2C_MASTER_WRRD operations.
> > 
> > Also fix handling of msgs. If the operation (i2c->op) is
> > I2C_MASTER_WRRD, then the msgs pointer is incremented by 2.
> > For all other operations, msgs is simply incremented by 1.
> > 
> > Fixes: 173b77e8d8fe ("i2c: mediatek: add i2c first write then read
> > optimization")
> > 
> 
> No need for blank line here. BTW, this doesn't look the commit
> that is introducing the issue.
> 
OK.
Fixes:b2ed11e224a2 ("I2C: mediatek: Add driver for MediaTek MT8173 I2C
controller")

> > Signed-off-by: Leilk.Liu <leilk.liu@mediatek.com>
> > ---
> >  drivers/i2c/busses/i2c-mt65xx.c | 17 ++++++++++-------
> >  1 file changed, 10 insertions(+), 7 deletions(-)
> > 
> > diff --git a/drivers/i2c/busses/i2c-mt65xx.c
> > b/drivers/i2c/busses/i2c-mt65xx.c
> > index ab456c3717db..dee40704825c 100644
> > --- a/drivers/i2c/busses/i2c-mt65xx.c
> > +++ b/drivers/i2c/busses/i2c-mt65xx.c
> > @@ -1243,6 +1243,7 @@ static int mtk_i2c_transfer(struct
> > i2c_adapter *adap,
> >  {
> >       int ret;
> >       int left_num = num;
> > +     bool write_then_read_en = false;
> >       struct mtk_i2c *i2c = i2c_get_adapdata(adap);
> > 
> >       ret = clk_bulk_enable(I2C_MT65XX_CLK_MAX, i2c->clocks);
> > @@ -1256,6 +1257,7 @@ static int mtk_i2c_transfer(struct
> > i2c_adapter *adap,
> >               if (!(msgs[0].flags & I2C_M_RD) && (msgs[1].flags &
> > I2C_M_RD) &&
> >                   msgs[0].addr == msgs[1].addr) {
> >                       i2c->auto_restart = 0;
> > +                     write_then_read_en = true;
> 
> why don't we set "i2c->op = I2C_MASTER_WRRD" here and avoid an
> extra flag?
> 
we need to set "i2c->op" again in the following code, so an extra flag
is necessary.

> Thanks,
> Andi
> 
> >               }
> >       }
> > 

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-09-01 10:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-16  7:53 [PATCH] i2c: mediatek: fix potential incorrect use of I2C_MASTER_WRRD Leilk Liu
2025-08-18  3:53 ` Chen-Yu Tsai
2025-08-19 21:09 ` Andi Shyti
2025-09-01  9:25   ` Leilk Liu (刘磊)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).