From nobody Tue Sep 29 02:33:42 2026 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) (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 C6891331209 for ; Thu, 13 Aug 2026 05:45:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786599919; cv=none; b=ED0ApRUaXuWzTtrrI7WmNsgO2pwANRtpuOqPFCi76tipbsofl0EggIe2t+j4xmYb77vt3/3zIyIJonx+1IJYbEWwNm0cd1wYj9xo1FdFxP0nOgXSDMxTomkvB3SPKM4ki3I4GI8erSam/wrgTi/UyDe8wW9elZPM9I+1PMYIzR8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786599919; c=relaxed/simple; bh=4Ih1IY9sJGO2xZu7SZLwTbHcPlFuLXeuvKTBxpJN7zI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KFH7DmsI5JZ5aHUtAoY271KIlRZEvoZnYnyD4EAzf5ACRIvA75oBFSTgGjeWQdN//uq9ZeXl5r0KNnEu7EOE/eDDQe3dyqKnQFjMFwHJX/0s3xZ4QX1TOWhHP+gnj/tAPGztA37I8idSI+0ROepM4bYyNAv32Xpu6s7sFJrO3es= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=i1BabF9n; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=Paq2fpsm; arc=none smtp.client-ip=205.220.180.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="i1BabF9n"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="Paq2fpsm" Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 67D5gaAP1750106 for ; Thu, 13 Aug 2026 05:45:15 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= NWub6h7L1pIlGN7Rro0PKlrW45DCmQOmhfogtD6KZ9A=; b=i1BabF9nWETJRGgo JXUlRE8MBe81Rx9nZI7syen64Nma9H29FEzDf9Ce/Cqp3bz7JatO+0bKT7aa21sS y7octZlBBKuU7cx2U24mBd+c3FJxi5HkFWp4us3ibHqz98kTSOXQbF7MuYD4OF1G Z1us0ZHdvuKjfm1WZFr5qLdYIc9zDHTkdDgx9O8OZue0ynh7sp35s6XFG5a8xH3d Ar6kZoFelF1U6ne/fhM4dha/wCN8Ytl8FeEE3QrU+OqIpJ1B7Pe3tquv1DcZg6Hk PSEMq5jEpBcQm6EYeL2r7Q2IdISCWDXQQdk0XN/KtZ99fcPslFNPEhL3dOhddbM/ I2k+PA== Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4g0xx1sxax-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Thu, 13 Aug 2026 05:45:15 +0000 (GMT) Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-cbb6433e9d4so2249952a12.3 for ; Wed, 12 Aug 2026 22:45:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1786599914; x=1787204714; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=NWub6h7L1pIlGN7Rro0PKlrW45DCmQOmhfogtD6KZ9A=; b=Paq2fpsmojRrAn6XM3igT3RRh+RjfGaXolNZVqyKg9cd0FD4h3VbSlzjTPHjmmfdgU 7Pa+eloRTHaZgzg2cLeq8oN/jbjMSWWx2wzgAXrdwVgEXfdWV1VYQw05bAD14D0RdVJO 4ewnHx2miRW/WZZBvsXwzUmqb2kIl2prqbDNcsynEgU0OFf58RY3uUTNPt9QsELilpll NU1LQ9mhT3JS6wcALsGcEoUyQ4T0cDSAOcZsetDDNq9yuZ0Q0ZsbggIE5WI+HCFONkpX alcsBOWpiktV4vkoLoW9nnBD0MwL9A8/T72Wd0EvdQlyWV8pgSKILs0HfhKvDjVfJg2E xh4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786599914; x=1787204714; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=NWub6h7L1pIlGN7Rro0PKlrW45DCmQOmhfogtD6KZ9A=; b=GeUOSfHl4s0G+iYPP+oMbwg+YwNjwdrnyiZuv3iTRoWVVncZet7l6yq0RKSH7jN2D6 ZFCV+xiRVYW7+kvUHho2cwcBXREOUm96Lj7PaDSb+7/74kxEETd3TVl7HOnTF/V04n+3 BXWmxFOIazYk+exaTqSNJ5DVvcMJs10GEal/HAm2tTUw+LKux5JjN+cpckCWb+odAiNJ b0pGAirRCDmA+aph0QaUkzfLGMuxU4Dt8eSL5j2GrY6aCcOhcmjixC5B8H3Q2aa9yqKf w+qBWcWnZArivcwQMRH9/AnjsAB/2Ts2YiDgH33Unwv/KRz4OKynEHhc+fpfdXKIq6/q fJ1w== X-Forwarded-Encrypted: i=1; AHgh+Rrm79HkKYGYybDFYwjaU8NBKVOuM9uHtUhKWqUcssIgLVO6N94EkQJ3QLTOmNcpbRvHvubZyPeZyO1sraQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yx8EnT5Ka0zvBHN7fYwKQep4fgi0Sp9vU2rdG8b6KQPAA4xSJCO qzzdnbmWjBzn4ph+v7N+EFzIUb0928huT4EW1xLW/vmAFFH6RnyRzAEtCpsHGO3qz/72shOpguI Vi4IIfeFWwVJxtduMRCwQy27S0xihGe4rlg+8PB4pywEKJnnKI94M8wXnp3+pq6cQZQP8iut7HP 8= X-Gm-Gg: AR+sD12WecbVtdy6J9amyg8pdu+b9jfAkJqbqaawsbCGY9T2Qux3vCQw5eAa9pKY+nz g+ek8zwyRvue/ra3OH2S4e3Jo2uO1Mc2htDPXopz+3hiR4fPLvPloE/hOHdt0xVCvKjiswt54Dx zbQdhbinmY3SXWvzoLDX+HX2a/zzQgHZO9Cx/Gt4+g3EXrE6VJlHXYVo77vKYkiaoKRASwzkc79 S9dSV9T00tGK90RsXZ/GcPikbnqZS5woqjz9Nk+YOVHB5AuJxPfizwIo12wW+gxsX79eLpGU65E cFD8CCmzzt9IZsbRS4cMApJF7ZPXQW4VRn82Hnnfeq4squWa6djdo9jzI4YYTluHq/QrsY3R+S9 nZasjwnCPmWZlGU5jVKaTxmTNmKr0Y7P3nOk= X-Received: by 2002:a05:6a00:4fc9:b0:845:4cb5:2343 with SMTP id d2e1a72fcca58-84fc745dac0mr3096360b3a.19.1786599914235; Wed, 12 Aug 2026 22:45:14 -0700 (PDT) X-Received: by 2002:a05:6a00:4fc9:b0:845:4cb5:2343 with SMTP id d2e1a72fcca58-84fc745dac0mr3096298b3a.19.1786599913630; Wed, 12 Aug 2026 22:45:13 -0700 (PDT) Received: from hu-arandive-hyd.qualcomm.com ([202.46.22.19]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84fc477e53esm381500b3a.14.2026.08.12.22.45.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2026 22:45:13 -0700 (PDT) From: Aniket Randive Date: Thu, 13 Aug 2026 11:15:02 +0530 Subject: [PATCH v7 1/2] i2c: core: Add i2c_update_timeout() helper for dynamic transfer timeouts 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 Message-Id: <20260813-master-v7-1-3f9010cf9229@oss.qualcomm.com> References: <20260813-master-v7-0-3f9010cf9229@oss.qualcomm.com> In-Reply-To: <20260813-master-v7-0-3f9010cf9229@oss.qualcomm.com> To: Wolfram Sang , Andi Shyti , Mukesh Savaliya , Dmitry Guzman Cc: Viken Dadhaniya , linux-i2c@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, Aniket Randive X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1786599906; l=6603; i=aniket.randive@oss.qualcomm.com; s=20260325; h=from:subject:message-id; bh=4Ih1IY9sJGO2xZu7SZLwTbHcPlFuLXeuvKTBxpJN7zI=; b=AHnu1ClrGKYq7Fjcdx+xaazaWz9OK+B9az/wUe6K63YRVNv/KthGFKu7ROWaXNN81GcVzOWi0 h3+csATiZ1OCWhPnz3zrkXN6I3uqEvbPqEG67HijO9d+an5Bdp7dSiF X-Developer-Key: i=aniket.randive@oss.qualcomm.com; a=ed25519; pk=4o37X1ZqGbCO/T2SR8kq/HnWvq0yb1RUlFuNukovbQk= X-Proofpoint-GUID: -LhfKIiGiP93YCpFg7iviOU8y8sqpWXZ X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODEzMDAzNyBTYWx0ZWRfX8CKrBt2LEKk/ ZhNVCNlg3JmQutsYkU3XSVxEFS7HezcPQSM+WSz+e7GP8SEL7xxgf1/md4zcUoG/JTKA3E70tQd JnPJaCiKEa3tA0XEwnv+giAL9jZy2eV1jCJfYX4fPmOtPqUzK3SRNkGCNdagkaoMVPqlMg4fknn 6t5kGUE6iehTb70olWlIKullvlg5Cb7sFWTt7itZba6UWBpR2PpsioWrXCUlKOE+OMr/uGLy/Pg gTcsFoE7LuIovAlhxra6CEYidbJGHeqybwMIy+pbjLbpHQjfNBb77AAt3ILjxlUMgfbbV6FzOV7 YpMoqGq4GuBJoLD6fsKkdO66vQQgc+Xu5wi/oAn15nta9RmeHrUb2wS55q5arIViNHWNXjE1t7r 4IbuC4gRxJORa896p++TTNzDjpuIoCCLnFW5TAlAHSc97F3DH361lwQmtFNZS2EMPPiaJOotMt2 Pl+w5S7PQPKv4GYCsJA== X-Authority-Analysis: v=2.4 cv=J6KaKgnS c=1 sm=1 tr=0 ts=6a7d59eb cx=c_pps a=rz3CxIlbcmazkYymdCej/Q==:117 a=fChuTYTh2wq5r3m49p7fHw==:17 a=IkcTkHD0fZMA:10 a=Sv0fKeRqtYgA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=ZpdpYltYx_vBUK5n70dp:22 a=EUspDBNiAAAA:8 a=Xw8hV2vT4XMzfsrjPA4A:9 a=QEXdDO2ut3YA:10 a=bFCP_H2QrGi7Okbo017w:22 X-Proofpoint-Spam-Info: AW1haW4tMjYwODEzMDAzNyBTYWx0ZWRfX2/V7qE8Z31E4 ow0Ceia4T1PYsu9L+14T13yAyujdrUkQe93WWfKvi2z+mFhSN+eNW08hExf0AvrlolaTno2hxhu 2ichaoAt6mzZmgPY42QDAe4kAdxF46w= X-Proofpoint-ORIG-GUID: -LhfKIiGiP93YCpFg7iviOU8y8sqpWXZ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-08-13_02,2026-08-12_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 clxscore=1015 malwarescore=0 bulkscore=0 impostorscore=0 lowpriorityscore=0 phishscore=0 spamscore=0 adultscore=0 priorityscore=1501 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608130037 The transfer timeout for an I2C controller should reflect the actual message length and bus frequency rather than a static 1-second value. A static timeout causes unnecessary delays on error paths for short messages, and may be insufficient for very long transfers. Add i2c_update_timeout() to i2c-core which computes a transfer-specific timeout and stores it directly in the standard adap->timeout field. The formula accounts for 9 bits per byte (8 data + 1 ACK) at the configured bus frequency. The caller supplies a safety multiplier and a minimum floor so that each driver retains full control over its timing policy without those values becoming public API. Storing the result in adap->timeout makes it visible to all consumers of that field, including the arbitration-loss retry loop in __i2c_transfer(). The function is gated by CONFIG_I2C_DYNAMIC_TIMEOUT. When the config is disabled, i2c_update_timeout() compiles to a no-op inline stub so drivers that call it build cleanly and the existing static 1-second default is preserved unchanged. A timeout explicitly configured by userspace via the I2C_TIMEOUT ioctl is stored in a new adap->user_timeout field and always takes precedence over the kernel-computed value. When userspace has not configured a timeout, the computed value is used. Signed-off-by: Aniket Randive Suggested-by: Dmitry Guzman --- drivers/i2c/Kconfig | 13 +++++++++++++ drivers/i2c/i2c-core-base.c | 40 ++++++++++++++++++++++++++++++++++++++++ drivers/i2c/i2c-dev.c | 4 ++++ include/linux/i2c.h | 12 ++++++++++++ 4 files changed, 69 insertions(+) diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig index c232054fddd6..1d9691ec0e7e 100644 --- a/drivers/i2c/Kconfig +++ b/drivers/i2c/Kconfig @@ -136,6 +136,19 @@ config I2C_SLAVE_TESTUNIT =20 endif =20 +config I2C_DYNAMIC_TIMEOUT + bool "Dynamic per-transfer timeout based on message length" + depends on I2C + help + When enabled, the I2C core computes a per-transfer timeout from the + message length and bus frequency instead of using a static 1-second + default. A timeout explicitly configured via the I2C_TIMEOUT userspace + interface always takes precedence over the computed value. + + When disabled, the existing static 1-second timeout is preserved. + + If unsure, say N. + config I2C_DEBUG_CORE bool "I2C Core debugging messages" help diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index fb25704219c7..caa0748253ab 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -2001,6 +2002,45 @@ void i2c_parse_fw_timings(struct device *dev, struct= i2c_timings *t, bool use_de } EXPORT_SYMBOL_GPL(i2c_parse_fw_timings); =20 +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT +/** + * i2c_update_timeout - compute and set a dynamic transfer timeout on an a= dapter + * @adap: the i2c_adapter whose timeout field will be updated + * @bus_freq_hz: I2C bus clock frequency in Hz + * @len: transfer length in bytes + * @safety_coeff: multiplier applied over the theoretical wire time + * @min_usec: minimum timeout floor in microseconds + * + * Computes a transfer-specific timeout from the message length and bus + * frequency, applies a safety multiplier and a minimum floor, then stores + * the result in adap->timeout (in jiffies). The caller supplies the poli= cy + * constants so they remain internal to the driver. + * + * A timeout explicitly configured by userspace via the I2C_TIMEOUT ioctl + * always takes precedence; the computed value is used only when userspace + * has not configured one. + */ +void i2c_update_timeout(struct i2c_adapter *adap, u32 bus_freq_hz, + size_t len, unsigned int safety_coeff, + unsigned int min_usec) +{ + u64 bit_usec =3D mul_u64_u32_div(len * 9, USEC_PER_SEC, bus_freq_hz); + u64 total_usec =3D bit_usec * safety_coeff + min_usec; + unsigned long jiffies_val; + + /* Userspace-configured timeout always takes precedence. */ + if (adap->user_timeout > 0) { + adap->timeout =3D adap->user_timeout; + return; + } + + jiffies_val =3D usecs_to_jiffies((unsigned int)min_t(u64, total_usec, UIN= T_MAX)); + /* adap->timeout is int; guard against signed overflow. */ + adap->timeout =3D (int)min_t(unsigned long, jiffies_val, INT_MAX); +} +EXPORT_SYMBOL_GPL(i2c_update_timeout); +#endif /* CONFIG_I2C_DYNAMIC_TIMEOUT */ + /* -----------------------------------------------------------------------= -- */ =20 int i2c_for_each_dev(void *data, int (*fn)(struct device *dev, void *data)) diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index ccaac5e29f90..2d918bcbf8f7 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -494,7 +494,11 @@ static long i2cdev_ioctl(struct file *file, unsigned i= nt cmd, unsigned long arg) if (arg > INT_MAX / 10) return -EINVAL; =20 +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT + client->adapter->user_timeout =3D msecs_to_jiffies(arg * 10); +#else client->adapter->timeout =3D msecs_to_jiffies(arg * 10); +#endif break; default: /* NOTE: returning a fault code here could cause trouble diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 14ab4d3055af..068217549266 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -742,6 +742,9 @@ struct i2c_adapter { struct rt_mutex mux_lock; =20 int timeout; /* in jiffies */ +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT + int user_timeout; /* I2C_TIMEOUT ioctl value in jiffies; 0 =3D not set */ +#endif int retries; struct device dev; /* the adapter device */ unsigned long locked_flags; /* owned by the I2C core */ @@ -912,6 +915,15 @@ void i2c_put_adapter(struct i2c_adapter *adap); unsigned int i2c_adapter_depth(struct i2c_adapter *adapter); =20 void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool = use_defaults); +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT +void i2c_update_timeout(struct i2c_adapter *adap, u32 bus_freq_hz, + size_t len, unsigned int safety_coeff, + unsigned int min_usec); +#else +static inline void i2c_update_timeout(struct i2c_adapter *adap, u32 bus_fr= eq_hz, + size_t len, unsigned int safety_coeff, + unsigned int min_usec) {} +#endif =20 /* Return the functionality mask */ static inline u32 i2c_get_functionality(struct i2c_adapter *adap) --=20 2.34.1 From nobody Tue Sep 29 02:33:42 2026 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) (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 9E5753E51F8 for ; Thu, 13 Aug 2026 05:45:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786599921; cv=none; b=aO5IKixlg3gIAyva/EPv1aHVf4axLB1VR+VEyPh5ghBaX10Y9Lr+k0Wtb8YXIyP8gT06wsvq8xXwYfHBR0zTMY79CFcEp3yYpr3WrrjwiPImJsbSzhMhFR0Cjx5RBQBkebEuDM92e4WpWmlVAy1e07Yw5rGuR3wFasfa9mOcdWE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786599921; c=relaxed/simple; bh=XuNbNdezUta+HaP/HnPqF/Qiyl+9RxcAukqvc3MaBLs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CFKLvcxDHkdSPLBgfAfbVnOCUM+PNJVAoRYz6XeebxWXefb4QaBw5dai6wMdh3gX5zpXVZno/nKaPsXPQ1dukRibKdV4wD1s7ozxR/P5x6naJHjcx+Cb5LFHO+Ndd3zxnOABtWhJ3kURoyxBaCpfz+d8oA/bA+DMUbxstLBf6MU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=Ad9p+tYK; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=SNSGZjme; arc=none smtp.client-ip=205.220.168.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="Ad9p+tYK"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="SNSGZjme" Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 67D5g15D1875131 for ; Thu, 13 Aug 2026 05:45:19 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= mU+bqaVAcam260I0995R65cz5gHTBDXU+X7V9XX7Mlw=; b=Ad9p+tYKKl84HWnU +9Hrr6N9TWCM7+PWrYaoG60Rd6D7xCYcW8ICPtGvefa8CElVGvZBRS5HLRX8e3xL BPR0rykWIqu5ZuxTqvbCt7nGtb8A+oOuKS6+LuWiO6KKp1Co3ZWiueEsMUPpy8du 4aSb8PdmHWSH9KO3dubVmWKwZizKMZMnZitY9GJZoMjWuOPS4dwChpUvn8dLvVaZ 3Y/c2iLNFUubiDWdCp3xk5gXsmEztEjhSsW/KZShgh6UwyV0WB5cPkHtNGfhsqqi GNUXjP/DHIAhwCXbVhdv7Te42TznE/N81NmwhOEbXdLIHrWH+ftooxQPSvWXRg6D raHaWw== Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4g0wt8tc6y-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Thu, 13 Aug 2026 05:45:18 +0000 (GMT) Received: by mail-pf1-f199.google.com with SMTP id d2e1a72fcca58-8485d853b08so2550799b3a.1 for ; Wed, 12 Aug 2026 22:45:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1786599918; x=1787204718; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=mU+bqaVAcam260I0995R65cz5gHTBDXU+X7V9XX7Mlw=; b=SNSGZjmevQo04rWmX/3GfOaqH8/YqdKSsjwHmg4XUmF9GHF+qLR0WH1YeIZvBNoHVU 0LwloRJokqlmOZxjbqJ24GPPX8E70Lv+eLba0np+mPLtldE7QiAJODaQ0lqgUfy1GRWA VY7pKah/3IO+sS+YblYUp0cnVqjJ49hJL/5WMv2MReibN1C3ADDGDQJuwpixL04ZPNZC s3BDT0HVXR8/jZhQK+UQysnaCwSWTZwe2wILl2AcywPQOxXDrDKH8SF+mwDeJ3ngkAm7 eU5g3ixsMNMhA91zP4uzfKfiCpDCSnL40tUU4CbzBtzomGxLEnN+KckY7xoGGsdqlDkH j6VQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786599918; x=1787204718; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=mU+bqaVAcam260I0995R65cz5gHTBDXU+X7V9XX7Mlw=; b=fVxTxCIoRLuUIA1d9MGaTqnEOxLYeKhz0o3P2BormVxhsg+fQmowXP211UimBWXuWX JYVf4bghiaNafzQYtHJcu8fS9eYMjh/UqQqMudGVFwVAT8DQH4edfYmyxl1RNGozUiFy DEB6gFjxUn5VLWGfuEpg72coptfdmDFmWbM25pyVOxwH2O8hm/ic3bobKCsOc5ThJF+q yS6KtBBTnIzwWXN4Mh5SABWba6m9DHGHdNATH218XzOvtmE8zHvXtYclPkGoWrIKD+Ee 1hu+ucnWi2+TltwxJbuyGlyC0J63UTgBhnQJQxMvJx79UJ1YwSIuDq9+VpA/0PPFa8W1 n7iA== X-Forwarded-Encrypted: i=1; AHgh+RrB2xV2BE4kFZBRuClkYa+4MnWcq05DnAPcBGC4CqEy7cex7XV/5RFXhBt6EbGL6LCyjwO15hHhrEJdkOs=@vger.kernel.org X-Gm-Message-State: AOJu0YzatRWxut7kFZqEFvS1lsaX3zVwYUPlAm17FKswEqb6y7ZpdQ/i aYzGxsAfzBhkVDCODznzehrsf326GD9d9BeGDOE45wsYi+Q42VMnfF1xk6pZNZ+oiYUKv0nvgrG aRDi5beKJK4NjWXB44qj8mX52zvXZkNPG91qQQvzQjAoa/3UTcvdEkgdLhTQpD8oQ9Ag= X-Gm-Gg: AR+sD10lp7mk4WzxdRpjGYTk7umiKlk37adYx+FJggIgQumwM0w2wyBIB7o7w6XMFXZ n90FitYCWBZqbrUZ+AuD4EbFhIxbk/7BX5wA0hGikSBD/fFC4wmduY5RQDZq2+oG7rvFVr24pIo KtlpP5eSsV5YoBa23efUY/7OfPmy4yvjj5g3fFIAhj7gWxzovn7d0vvxstMUPPx/BeZrl4JenUj UgLJHxO4Fr74Zd+7HER1GEHIZRhMaldfH9xpYxTj0hfbynXVbRGEAMIvFSwdSMaJCHoQf27uXZQ Nj1hnqnAdglMf/mWf+tFS+jJQ84dhgeOmfrcYM/tX+yMHs4OL5Yn9xj1Y/cQx6xYiR34jEivCjT Wn7+Orp74m56LI+LiJU/fstkxeofYIwiHsQQ= X-Received: by 2002:a05:6a00:23d5:b0:837:e9cc:d474 with SMTP id d2e1a72fcca58-84fc6d3d156mr3197899b3a.2.1786599917920; Wed, 12 Aug 2026 22:45:17 -0700 (PDT) X-Received: by 2002:a05:6a00:23d5:b0:837:e9cc:d474 with SMTP id d2e1a72fcca58-84fc6d3d156mr3197843b3a.2.1786599917327; Wed, 12 Aug 2026 22:45:17 -0700 (PDT) Received: from hu-arandive-hyd.qualcomm.com ([202.46.22.19]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84fc477e53esm381500b3a.14.2026.08.12.22.45.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2026 22:45:16 -0700 (PDT) From: Aniket Randive Date: Thu, 13 Aug 2026 11:15:03 +0530 Subject: [PATCH v7 2/2] i2c: qcom-geni: Add dynamic transfer timeout based on transfer length and frequency 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 Message-Id: <20260813-master-v7-2-3f9010cf9229@oss.qualcomm.com> References: <20260813-master-v7-0-3f9010cf9229@oss.qualcomm.com> In-Reply-To: <20260813-master-v7-0-3f9010cf9229@oss.qualcomm.com> To: Wolfram Sang , Andi Shyti , Mukesh Savaliya , Dmitry Guzman Cc: Viken Dadhaniya , linux-i2c@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, Aniket Randive X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1786599906; l=5871; i=aniket.randive@oss.qualcomm.com; s=20260325; h=from:subject:message-id; bh=XuNbNdezUta+HaP/HnPqF/Qiyl+9RxcAukqvc3MaBLs=; b=gTI3LtgjT4uUvYl0gMWjqCV8lwTHFV44qD//aj33NDH+uQBGuUfj5aGuU7AEg+RXy0DBZ1Aq7 JBX1jl8kFtSC7AM1hTTPY+7dmWuR30ILxdMu5Cil+K7pVuB1yx+/f3L X-Developer-Key: i=aniket.randive@oss.qualcomm.com; a=ed25519; pk=4o37X1ZqGbCO/T2SR8kq/HnWvq0yb1RUlFuNukovbQk= X-Authority-Analysis: v=2.4 cv=YLqvDxGx c=1 sm=1 tr=0 ts=6a7d59ee cx=c_pps a=WW5sKcV1LcKqjgzy2JUPuA==:117 a=fChuTYTh2wq5r3m49p7fHw==:17 a=IkcTkHD0fZMA:10 a=Sv0fKeRqtYgA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=yOCtJkima9RkubShWh1s:22 a=EUspDBNiAAAA:8 a=S_uJhbi9Giy6tt7Agy8A:9 a=QEXdDO2ut3YA:10 a=OpyuDcXvxspvyRM73sMx:22 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODEzMDAzNyBTYWx0ZWRfX7IhBrVTRpJyZ XMG9pTWD/BR5Sdmw5NqaNLfEea/+9/wD7E++PZkeWC9iBbAqNN4gIDNNLXGyP2DGUYV+nXuh75m sFunIeqLjm6fzmcIcpAzZTTErnVrq8KBEy4b6fk0hrr4xGlEzpzyaa8NsdoyLs6xGZNpNXCMebK J7i2nl0UOSQfI4v2EHonZH87ggYLLYt36mXNP9rFgL6nibqhbvs1cZ9tVZl4BWqegSLZl29ezas 5Do5LcWfcIWg65kVIHIiWDRsD8chrSVs9BMmdG9SOQjQif3PTRUsxpT0At4+SLmXlrJCkDlV57o UppR/lv0oA7NpDsT4/jtt+0HDxPwWTBwwMhyLy/vAfzFKrrXfzGxS/Uq5Rxdo+QLabzIJSwGjiT FOCviycCVPoL6UqCkRbYR8RXEr1w5alKZkGyDqg6KfzFtBXvE8WNzdaXfKdjAEnz1p2qICAcMG/ qfJWlIPnWv23MtAUDpw== X-Proofpoint-ORIG-GUID: ppht7RTVbUsrnwLHU7ME-6J5vr-uMv_E X-Proofpoint-GUID: ppht7RTVbUsrnwLHU7ME-6J5vr-uMv_E X-Proofpoint-Spam-Info: AW1haW4tMjYwODEzMDAzNyBTYWx0ZWRfX9GaY/+oNdAmg EZUdhQKIOfjVQiGFK8608xMaDydQcMnEEzTKLUsVnLvOQ7du+7013s0sSxgthxikkVZoekdjvzG P15Uf0J677dpCJolAZKfxNkng5M59o8= X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-08-13_02,2026-08-12_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 bulkscore=0 suspectscore=0 adultscore=0 malwarescore=0 impostorscore=0 spamscore=0 priorityscore=1501 clxscore=1015 lowpriorityscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608130037 The driver uses a static XFER_TIMEOUT of HZ (1 second) for all transfers regardless of message length or bus frequency, causing unnecessary delays on error paths. Use i2c_update_timeout() from i2c-core to compute the timeout dynamically from the message length and the bus frequency, then read adap->timeout at each wait site. For FIFO, SE-DMA, and GPI single-descriptor paths the timeout is computed per message using that message's length. For the GPI multi-descriptor path, the maximum message length across the batch is used since one completion covers the entire BEI batch and the timeout must cover the longest individual message. A 10x safety margin over the theoretical wire time is applied, with a 300ms floor to account for I2C clock stretching and other situations where a slave may keep SCL asserted for an extended period, including faulty devices holding the bus. Both constants remain private to this driver. Signed-off-by: Aniket Randive Suggested-by: Dmitry Guzman --- drivers/i2c/busses/i2c-qcom-geni.c | 44 ++++++++++++++++++++++++++++++----= ---- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/drivers/i2c/busses/i2c-qcom-geni.c b/drivers/i2c/busses/i2c-qc= om-geni.c index 658636c1ee0e..c98d9e7b250c 100644 --- a/drivers/i2c/busses/i2c-qcom-geni.c +++ b/drivers/i2c/busses/i2c-qcom-geni.c @@ -79,9 +79,14 @@ enum geni_i2c_err_code { =20 #define ABORT_TIMEOUT HZ #define CANCEL_TIMEOUT HZ -#define XFER_TIMEOUT HZ #define RST_TIMEOUT HZ =20 +/* 9 bits per byte (8 data + 1 ACK), 10x safety margin */ +#define I2C_TIMEOUT_SAFETY_COEFFICIENT 10 + +/* 300ms floor: budget for clock stretching; slave may hold SCL low indefi= nitely */ +#define I2C_TIMEOUT_MIN_USEC 300000 + struct geni_i2c_desc { bool no_dma_support; unsigned int tx_fifo_depth; @@ -513,7 +518,9 @@ static int geni_i2c_rx_one_msg(struct geni_i2c_dev *gi2= c, struct i2c_msg *msg, } =20 cur =3D gi2c->cur; - time_left =3D wait_for_completion_timeout(&gi2c->done, XFER_TIMEOUT); + i2c_update_timeout(&gi2c->adap, gi2c->clk_freq_out, len, + I2C_TIMEOUT_SAFETY_COEFFICIENT, I2C_TIMEOUT_MIN_USEC); + time_left =3D wait_for_completion_timeout(&gi2c->done, gi2c->adap.timeout= ); if (!time_left || (gi2c->err && gi2c->err !=3D gi2c_log[ADDR_NACK].err)) geni_i2c_cancel_xfer(gi2c); =20 @@ -555,7 +562,9 @@ static int geni_i2c_tx_one_msg(struct geni_i2c_dev *gi2= c, struct i2c_msg *msg, writel_relaxed(1, se->base + SE_GENI_TX_WATERMARK_REG); =20 cur =3D gi2c->cur; - time_left =3D wait_for_completion_timeout(&gi2c->done, XFER_TIMEOUT); + i2c_update_timeout(&gi2c->adap, gi2c->clk_freq_out, len, + I2C_TIMEOUT_SAFETY_COEFFICIENT, I2C_TIMEOUT_MIN_USEC); + time_left =3D wait_for_completion_timeout(&gi2c->done, gi2c->adap.timeout= ); if (!time_left || (gi2c->err && gi2c->err !=3D gi2c_log[ADDR_NACK].err)) geni_i2c_cancel_xfer(gi2c); =20 @@ -633,7 +642,7 @@ static void geni_i2c_gpi_multi_desc_unmap(struct geni_i= 2c_dev *gi2c, struct i2c_ * geni_i2c_gpi_multi_xfer_timeout_handler() - Handles multi message trans= fer timeout * @dev: Pointer to the corresponding dev node * @multi_xfer: Pointer to the geni_i2c_gpi_multi_desc_xfer - * @transfer_timeout_msecs: Timeout value in milliseconds + * @timeout_jiffies: Per-message completion timeout in jiffies * @transfer_comp: Completion object of the transfer * * This function waits for the completion of each processed transfer messa= ges @@ -643,18 +652,18 @@ static void geni_i2c_gpi_multi_desc_unmap(struct geni= _i2c_dev *gi2c, struct i2c_ */ static int geni_i2c_gpi_multi_xfer_timeout_handler(struct device *dev, struct geni_i2c_gpi_multi_desc_xfer *multi_xfer, - u32 transfer_timeout_msecs, + unsigned long timeout_jiffies, struct completion *transfer_comp) { int i; - u32 time_left; + unsigned long time_left; =20 for (i =3D 0; i < multi_xfer->msg_idx_cnt - 1; i++) { reinit_completion(transfer_comp); =20 if (multi_xfer->msg_idx_cnt !=3D multi_xfer->irq_cnt) { time_left =3D wait_for_completion_timeout(transfer_comp, - transfer_timeout_msecs); + timeout_jiffies); if (!time_left) { dev_err(dev, "%s: Transfer timeout\n", __func__); return -ETIMEDOUT; @@ -778,8 +787,22 @@ static int geni_i2c_gpi(struct geni_i2c_dev *gi2c, str= uct i2c_msg msgs[], dma_async_issue_pending(gi2c->tx_c); =20 if ((msg_idx =3D=3D (gi2c->num_msgs - 1)) || flags & DMA_PREP_INTERRUPT)= { + size_t max_len =3D 0; + int j; + + /* + * Use the longest message as the timeout base: one completion + * covers the whole BEI batch, so the budget must fit the worst + * case single-message wire time. + */ + for (j =3D 0; j < gi2c->num_msgs; j++) + max_len =3D max_t(size_t, max_len, msgs[j].len); + i2c_update_timeout(&gi2c->adap, gi2c->clk_freq_out, max_len, + I2C_TIMEOUT_SAFETY_COEFFICIENT, + I2C_TIMEOUT_MIN_USEC); ret =3D geni_i2c_gpi_multi_xfer_timeout_handler(gi2c->se.dev, gi2c_gpi_= xfer, - XFER_TIMEOUT, &gi2c->done); + gi2c->adap.timeout, + &gi2c->done); if (ret) { dev_err(gi2c->se.dev, "I2C multi write msg transfer timeout: %d\n", @@ -899,7 +922,10 @@ static int geni_i2c_gpi_xfer(struct geni_i2c_dev *gi2c= , struct i2c_msg msgs[], i =20 if (!gi2c->is_tx_multi_desc_xfer) { dma_async_issue_pending(gi2c->tx_c); - time_left =3D wait_for_completion_timeout(&gi2c->done, XFER_TIMEOUT); + i2c_update_timeout(&gi2c->adap, gi2c->clk_freq_out, msgs[i].len, + I2C_TIMEOUT_SAFETY_COEFFICIENT, + I2C_TIMEOUT_MIN_USEC); + time_left =3D wait_for_completion_timeout(&gi2c->done, gi2c->adap.timeo= ut); if (!time_left) { dev_err(gi2c->se.dev, "%s:I2C timeout\n", __func__); gi2c->err =3D -ETIMEDOUT; --=20 2.34.1