From nobody Sat Oct 4 12:41:04 2025 Received: from mailgw02.mediatek.com (unknown [210.61.82.184]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1AA8E4C98; Sat, 16 Aug 2025 07:55:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=210.61.82.184 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755330909; cv=none; b=OM/wtHmqsQ0WecTVKh7VOG2PPhAfbtZ6xR7Y2CEv4Ufo6u5AyQFjmqaB6l1aJhme7OdlcydBt852aeOxDqY7pclmlPS5K3lN5IsSGJUlfdTzR6SdzEqOiztsB7biFSm9/UVS1heggmcUeW3AJZPC1SEqi3Q0KJC3QOulAUfGhCw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755330909; c=relaxed/simple; bh=srsqYMdPTwRmVbD7Pe2YVDmuQrEFDzbatacOFDUivio=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=Z9k8EQrdfaNN+rd7swdEsXhC/isCBJG09duPhW1QmFyzKeFrR88IABz6W7G5zc3GoCRTLNAYJ31aXgXzTs+us4He5ySc2zUrz2pOnPH6YC44I8rK38V7s3fv7jWY/bWpPSoc3nGLtEJohF7koNUpL6cGWhOrzXGV0yW7yQLAuLQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mediatek.com; spf=pass smtp.mailfrom=mediatek.com; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b=mqo1r5js; arc=none smtp.client-ip=210.61.82.184 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mediatek.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mediatek.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.b="mqo1r5js" X-UUID: 4a9f71bc7a7611f0b33aeb1e7f16c2b6-20250816 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=fnw6L/2KwNT+tgpsNYiO/B84S45uSAGUvD6nwPyE38I=; b=mqo1r5jszh+4Hf5VKTpsE/MowD0eF0vXlI1HJUGI40AYjoSOcsg/9AWcVHFI7m+8+PatYqofmCfj7PUFrYAsO0iymu6n87lJxnx75WarW33ZV893xy8U4vw4MHTmYrfECEKUuD9nZQramx0dUGGjdE1oiz5GVuP8dEWz93NsUr4=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.3,REQID:743bb7ab-2bd3-4f55-a823-9d920a2c3502,IP:0,UR L:0,TC:0,Content:0,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTION:r elease,TS:0 X-CID-META: VersionHash:f1326cf,CLOUDID:d3338944-18c5-4075-a135-4c0afe29f9d6,B ulkID:nil,BulkQuantity:0,Recheck:0,SF:102,TC:-5,Content:0|15|50,EDM:-3,IP: nil,URL:0,File:nil,RT:nil,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0,AV:0,L ES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 4a9f71bc7a7611f0b33aeb1e7f16c2b6-20250816 Received: from mtkmbs11n1.mediatek.inc [(172.21.101.185)] by mailgw02.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 406166382; Sat, 16 Aug 2025 15:54:52 +0800 Received: from mtkmbs13n1.mediatek.inc (172.21.101.193) by MTKMBS14N1.mediatek.inc (172.21.101.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.39; Sat, 16 Aug 2025 15:54:50 +0800 Received: from mhfsdcap04.gcn.mediatek.inc (10.17.3.154) by mtkmbs13n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.1258.39 via Frontend Transport; Sat, 16 Aug 2025 15:54:49 +0800 From: Leilk Liu To: Andi Shyti , Matthias Brugger , AngeloGioacchino Del Regno CC: Qii Wang , Wolfram Sang , Liguo Zhang , , , , , , Leilk.Liu Subject: [PATCH] i2c: mediatek: fix potential incorrect use of I2C_MASTER_WRRD Date: Sat, 16 Aug 2025 15:53:54 +0800 Message-ID: <20250816075434.31780-1-leilk.liu@mediatek.com> X-Mailer: git-send-email 2.46.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-MTK: N From: "Leilk.Liu" The old IC does not support the I2C_MASTER_WRRD (write-then-read) function, but the current code=E2=80=99s 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 optimiza= tion") Signed-off-by: Leilk.Liu Reviewed-by: Chen-Yu Tsai Suggested-by: Chen-Yu Tsai --- 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-mt65x= x.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 =3D num; + bool write_then_read_en =3D false; struct mtk_i2c *i2c =3D i2c_get_adapdata(adap); =20 ret =3D 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 =3D=3D msgs[1].addr) { i2c->auto_restart =3D 0; + write_then_read_en =3D true; } } =20 @@ -1280,12 +1282,10 @@ static int mtk_i2c_transfer(struct i2c_adapter *ada= p, else i2c->op =3D I2C_MASTER_WR; =20 - if (!i2c->auto_restart) { - if (num > 1) { - /* combined two messages into one transaction */ - i2c->op =3D I2C_MASTER_WRRD; - left_num--; - } + if (write_then_read_en) { + /* combined two messages into one transaction */ + i2c->op =3D I2C_MASTER_WRRD; + left_num--; } =20 /* always use DMA mode. */ @@ -1293,7 +1293,10 @@ static int mtk_i2c_transfer(struct i2c_adapter *adap, if (ret < 0) goto err_exit; =20 - msgs++; + if (i2c->op =3D=3D I2C_MASTER_WRRD) + msgs +=3D 2; + else + msgs++; } /* the return value is number of executed messages */ ret =3D num; --=20 2.46.0