From patchwork Thu Aug 27 12:27:50 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aniket Randive X-Patchwork-Id: 2293331 X-Patchwork-Delegate: andi.shyti@kernel.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=pjz7+UmK; dkim=pass (2048-bit key; unprotected) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b=SGK2OV2W; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2600:3c04:e001:36c::12fc:5321; helo=tor.lore.kernel.org; envelope-from=linux-i2c+bounces-20251-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from tor.lore.kernel.org (tor.lore.kernel.org [IPv6:2600:3c04:e001:36c::12fc:5321]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4hW15m2428z1xpl for ; Thu, 27 Aug 2026 22:28:44 +1000 (AEST) Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1]) by tor.lore.kernel.org (Postfix) with ESMTP id 8C51530665E5 for ; Thu, 27 Aug 2026 12:28:09 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 121C736D503; Thu, 27 Aug 2026 12:28:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="pjz7+UmK"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="SGK2OV2W" X-Original-To: linux-i2c@vger.kernel.org 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 2A6C544684B for ; Thu, 27 Aug 2026 12:28:04 +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=1787833685; cv=none; b=bToUfOyCLAdV0hawqFk9vODNT68LLxR6IQ30jjAUtuxstvSsj+fmlAEDtaXIwQjFceJ0VEsp69fbEC29HOue7IgbiiK1p8R/qwn9NwT519VJPyDL03d57EyDTBy16rboTWoFc1eUokjk1kCC2JtaALCBydTFMO0B2UXHGGXNRnQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787833685; c=relaxed/simple; bh=9m66N446ktBYi/cdvd0Z4neSxdSkEViT/4d7JnekEwU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pht+kCzQicam2FwNtYSAAtn+Jf1TjfU1RGvkYM4pFCKvg6iXv0VvzW44W+rfB4aJ7+x5zqMre/1eJU2xku/KS51Y21ww1rXK0SeDl8hlgmCVjZvNS7aOftKbazLpmO92S5oMWK0rp3VlV9k4Ivu9cvX+rSUA44WrRtzPxFdnWhA= 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=pjz7+UmK; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=SGK2OV2W; 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 Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 67RCBpHY4038865 for ; Thu, 27 Aug 2026 12:28:03 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= qLimRGLDan9aHguGi6zUxwrS7BGYZe1VlRKyaqbqyow=; b=pjz7+UmKq8qz83qw /xbIEoPp1SpnHkejDZdM939++6Ux+BzbbyEi8C97Gq4jscNAGC1kKxvOPZn1q58M gDXm138KGKEOeZcyCtLbzvcUZLsDZxijYDuP8PKnSCf+YIXVTV7yRs+ef/mBhMkY FVa9xRD2amgvNDVNvjtAWCwsVJ1lFoha8ILrNRHTNo0Hqb84phusoxsRwfGm2UiU iatvIIefFcpQRiYUjMdZxtP1hCRhrW4clfVY7CI1DOWhu/axfZLdwozgWtMVUy5X UFLa7xR9sped2sqYvAfZ7KHM1SVqbI1z8AEtsGVxGfZ9cle87qaBMG5COenZqWet 2J/JEw== Received: from mail-pj1-f69.google.com (mail-pj1-f69.google.com [209.85.216.69]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4gam6fg81v-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Thu, 27 Aug 2026 12:28:03 +0000 (GMT) Received: by mail-pj1-f69.google.com with SMTP id 98e67ed59e1d1-38dc085b0a7so4245422a91.2 for ; Thu, 27 Aug 2026 05:28:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1787833682; x=1788438482; 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=qLimRGLDan9aHguGi6zUxwrS7BGYZe1VlRKyaqbqyow=; b=SGK2OV2WLZyCev22H+4EUQFys1cXmlvpNzFg7SSZizk8aj15LvlT5V5X3Nc3m98o2e CIIfaiwDIqOxLJamGvY279D+AMDWojUZ1yJbaAL0YLUYaHJ2QgJ5PnLCn3WPFF87yfU0 Fa1t3gdi5kx9t573Uy2nOqhpAZxx/BGEkme8Mpy3aoigmehaSWt/3g3ox7wss1FA31vY /El+eb1K4JyKJtnu7pl+JYD8p+Ln26YcZJBXJL+Av5F5q5z8itn/zxGzbm/Eiwsen6ju oyAGdmSsA2m1X4SqqhIOrajCW8LmjgwnAgaS72X+q7Wkb8czWLGjNdC+B9h9MMFgn2RI WbVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787833682; x=1788438482; 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=qLimRGLDan9aHguGi6zUxwrS7BGYZe1VlRKyaqbqyow=; b=fk4xyk96WYoCPqDtMEOd6f9vQkXmZAaZ6ucYDZkiMZoc8fU4WhVART0Vgs/cGHTAao 8CeWW0oWqKMT3EQv/uCEKS7uvpsf1/2H9g1GNv7kHZVjKH+SrZM9Ps3Z0MDAZoODk6BT cpdPIIBa0wl8zLWPwgvTJczQAy1+2TLAQETXTR9EqzLrGNwyjDfxOOwWFVsLEP/b0i3Q 4cYYpSfsnkU9rNKEM9kSR9GaTUI6wBcaTWGEMbbZljJ7qXW419dC5jJp7FH39Q8lGX8y nFB9foqoPsLmVJ0vXQWfLZw5VgUJygFv+D3QGJfisI2Pm982wj0MVJ3OpUP0KJEpvU4c b/SA== X-Forwarded-Encrypted: i=1; AHgh+Rq3EjTzJxVtR8bmcLUvTre1yMN55SW36iJvt9UlZ+hTcaKITRfb6Dz4B5LPOaT8RNAgqrXjCYYwOp0=@vger.kernel.org X-Gm-Message-State: AFuF++lSdb0wJp1ELoxqyWnwONn0mieoSXniL9JJ1gPcPZhFSzFNi0A+ xazeSmc5kbI9T9urBfJ6Ae6GQVVHn4pvQNSlb2DA5T3WUGBr5jkstGwzA73vcizopm5C6xcGR4w JOoMU1s/nJRG3KPHulq/M74WJPtqq720ib5P1pY5dAT96vy+dZ4rfsI1u1Yhie0c0ACX8v1w= X-Gm-Gg: AR+sD11fj42UwaTkbJvFw6wlQfIIy+MFxu3TH0t+hA0nvFCM6oukLRBL8Jl2atW38ki P+vQy8Zz5Cbg8sjtiK2x2whgUYiwX8nTDhfmgHfWF2SEqaWV76N0CQRECjc5fMdUrGvNch7rycN rAmjqLBPNTzxkE/dK9330Y9htL5jRYyL7hnal14CxGkcAzOQ+tHzwvxxBIUheni63/nRne33Vun l5/E6E7Ebz8MmYxgB4zjY3fEK7pf1I6jcPnv2caala5YbwiL/rEOESIX6ElIzkUAL2jQyQMwaJZ lnIdtbrGUCXSG0xtM67EoYUvz0GPjeUHu3rjQkrimN/2paHBb5y6PGmX5e6xRwPOHSSKFf6tIz3 TdURNQAo3DdChRLWHxG9ZLaZ9YmXUpknHScw= X-Received: by 2002:a17:90b:35c6:b0:38e:fea2:df53 with SMTP id 98e67ed59e1d1-3966d3bd49bmr30985552a91.4.1787833681872; Thu, 27 Aug 2026 05:28:01 -0700 (PDT) X-Received: by 2002:a17:90b:35c6:b0:38e:fea2:df53 with SMTP id 98e67ed59e1d1-3966d3bd49bmr30985422a91.4.1787833681284; Thu, 27 Aug 2026 05:28:01 -0700 (PDT) Received: from hu-arandive-hyd.qualcomm.com ([202.46.22.19]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-396b0fcfa3asm2707690a91.6.2026.08.27.05.27.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2026 05:28:00 -0700 (PDT) From: Aniket Randive Date: Thu, 27 Aug 2026 17:57:50 +0530 Subject: [PATCH v8 1/2] i2c: core: Add i2c_update_timeout() helper for dynamic transfer timeouts Precedence: bulk X-Mailing-List: linux-i2c@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20260827-master-v8-1-596bc81c5cf9@oss.qualcomm.com> References: <20260827-master-v8-0-596bc81c5cf9@oss.qualcomm.com> In-Reply-To: <20260827-master-v8-0-596bc81c5cf9@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=1787833673; l=6840; i=aniket.randive@oss.qualcomm.com; s=20260325; h=from:subject:message-id; bh=9m66N446ktBYi/cdvd0Z4neSxdSkEViT/4d7JnekEwU=; b=ANWpnw23fmiDnC6zx/pVAfYL06LIhEcpU5IykMT5Gccr1DUeaH4KEEFy2BpYqGHxP2uxZTq+4 zBPsQQSab8sAecQi9MM6tcSbu/3aNtnc2tp3t2pYIK1BoNagECHpFPe X-Developer-Key: i=aniket.randive@oss.qualcomm.com; a=ed25519; pk=4o37X1ZqGbCO/T2SR8kq/HnWvq0yb1RUlFuNukovbQk= X-Proofpoint-Spam-Info: AW1haW4tMjYwODI3MDEwNCBTYWx0ZWRfX8zhUsB3RdvpJ v9RPIMgcOJD+h+W6VUX4HfgS0iJuTRuYWZxwYGmpvFeIYbN5dsiMAJDGHVcEnQwHcxQphQP3bh7 Wnic/2nn+5Hh+53c3Bo8zQCpUfo3JPM= X-Proofpoint-GUID: JhsUyKpk-OeIA5g9YsNLKkI5n1jzONDn X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODI3MDEwNCBTYWx0ZWRfX+Fsvv1qQh6Cv iqqAjLAXWQJ0mbocbED5RJI3RphFsJiMp2CYmvVQlQCchDVv/Mr+2+F9gD76L3Ni4FvOwiDeNd2 MsG7MW/zaQgtUayd99/zch0brZ68pm5Kr/Nn5Xj6EjGhifYwmehT4eVDtjPTDDBXtpQvXfk1q1z TUjjFgE1IeSbGRgq7Mz/nltv0Qd9ZGQyNRZGAFdGtmpDJpogpm8CDB2jUt2WnlOnRUhRoL2m2pe cAjMzAX7BTkp6vRycqsW16lyK6JShZ5vgYh6x1d9qqN1gHbaAZ5Oicox9iR8NJizeVjZJPdVFtr W2V+UNXpL4lU3zdntjKJECXq8Q1tTiTE7A1IAwK8vCkOaxmjTxzEnTPcNXOe3ndFK7co17UVrLP vk+n/UjULHEd8K+pfgFeU0kgQPGk9VHx0BvLBssgb/vQ5tXfjkziVlyn4zxR9dcEe2quC6IlqFe 5RlXcMAHI6hxMzOGkrQ== X-Authority-Analysis: v=2.4 cv=eIgjSnp1 c=1 sm=1 tr=0 ts=6a902d53 cx=c_pps a=vVfyC5vLCtgYJKYeQD43oA==:117 a=fChuTYTh2wq5r3m49p7fHw==:17 a=IkcTkHD0fZMA:10 a=Sv0fKeRqtYgA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=gowsoOTTUOVcmtlkKump:22 a=EUspDBNiAAAA:8 a=Xw8hV2vT4XMzfsrjPA4A:9 a=QEXdDO2ut3YA:10 a=rl5im9kqc5Lf4LNbBjHf:22 X-Proofpoint-ORIG-GUID: JhsUyKpk-OeIA5g9YsNLKkI5n1jzONDn 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-27_05,2026-08-26_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 adultscore=0 clxscore=1015 impostorscore=0 malwarescore=0 bulkscore=0 phishscore=0 spamscore=0 priorityscore=1501 lowpriorityscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608270104 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. The ioctl keeps writing adap->timeout as well, so adapters that never call i2c_update_timeout() continue to honour it exactly as before. As i2c_update_timeout() is an exported helper, guard against a zero bus frequency from a misbehaving caller with WARN_ON_ONCE() and return early, leaving the existing timeout untouched as a safe fallback rather than dividing by zero. Signed-off-by: Aniket Randive Reviewed-by: Mukesh Kumar Savaliya --- drivers/i2c/Kconfig | 13 +++++++++++++ drivers/i2c/i2c-core-base.c | 43 +++++++++++++++++++++++++++++++++++++++++++ drivers/i2c/i2c-dev.c | 3 +++ include/linux/i2c.h | 13 +++++++++++++ 4 files changed, 72 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 endif +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..5f6a7370dc89 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,48 @@ void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_de } EXPORT_SYMBOL_GPL(i2c_parse_fw_timings); +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT +/** + * i2c_update_timeout - compute and set a dynamic transfer timeout on an adapter + * @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 policy + * 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, total_usec; + unsigned long jiffies_val; + + if (adap->user_timeout > 0) { + adap->timeout = adap->user_timeout; + return; + } + + if (WARN_ON_ONCE(!bus_freq_hz)) + return; + + bit_usec = mul_u64_u32_div(len * 9, USEC_PER_SEC, bus_freq_hz); + total_usec = bit_usec * safety_coeff + min_usec; + + jiffies_val = usecs_to_jiffies((unsigned int)min_t(u64, total_usec, UINT_MAX)); + adap->timeout = (int)min_t(unsigned long, jiffies_val, INT_MAX); +} +EXPORT_SYMBOL_GPL(i2c_update_timeout); +#endif /* CONFIG_I2C_DYNAMIC_TIMEOUT */ + /* ------------------------------------------------------------------------- */ 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..d219eb89f978 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -495,6 +495,9 @@ static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) return -EINVAL; client->adapter->timeout = msecs_to_jiffies(arg * 10); +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT + client->adapter->user_timeout = client->adapter->timeout; +#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..e4302a1bd8ea 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -742,6 +742,9 @@ struct i2c_adapter { struct rt_mutex mux_lock; int timeout; /* in jiffies */ +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT + int user_timeout; /* I2C_TIMEOUT ioctl value in jiffies */ +#endif int retries; struct device dev; /* the adapter device */ unsigned long locked_flags; /* owned by the I2C core */ @@ -913,6 +916,16 @@ unsigned int i2c_adapter_depth(struct i2c_adapter *adapter); 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_freq_hz, + size_t len, unsigned int safety_coeff, + unsigned int min_usec) {} +#endif + /* Return the functionality mask */ static inline u32 i2c_get_functionality(struct i2c_adapter *adap) { From patchwork Thu Aug 27 12:27:51 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aniket Randive X-Patchwork-Id: 2293332 X-Patchwork-Delegate: andi.shyti@kernel.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=UWu+isjq; dkim=pass (2048-bit key; unprotected) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.a=rsa-sha256 header.s=google header.b=TEHM0By6; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org (client-ip=2600:3c04:e001:36c::12fc:5321; helo=tor.lore.kernel.org; envelope-from=linux-i2c+bounces-20252-incoming=patchwork.ozlabs.org@vger.kernel.org; receiver=patchwork.ozlabs.org) Received: from tor.lore.kernel.org (tor.lore.kernel.org [IPv6:2600:3c04:e001:36c::12fc:5321]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4hW16B6pB1z1xpl for ; Thu, 27 Aug 2026 22:29:06 +1000 (AEST) Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.90.174.1]) by tor.lore.kernel.org (Postfix) with ESMTP id 02D15304A784 for ; Thu, 27 Aug 2026 12:28:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D3FC3448D01; Thu, 27 Aug 2026 12:28:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="UWu+isjq"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="TEHM0By6" X-Original-To: linux-i2c@vger.kernel.org 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 AEEB6447801 for ; Thu, 27 Aug 2026 12:28:07 +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=1787833689; cv=none; b=nGRon77qpT+SzkaHwz04WzpfZDtCUz65jSMLyDeWgM6W0aYAUnoz3r2HZdroM3Mt1MIaMKOjApA8JgIubMAcWqeCnnc0AKBklxoaLbeiAfK4Sxnq2bgZBWy16fXQ97er8e0tsFijOpQtF+mzm2/sRZ5eQTxiYArhOZCmiF4AzyI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787833689; c=relaxed/simple; bh=ZeptbrLh2OAqJST6zmSGFm03zkKujLOAAacfUDgoLFY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pxa9pwgsnbBpFcKaJs7obmDecEDgI3UR0vup3U1jbBl+Gr9DBfOyHJ/609+WzKoxE+oy139jcTTXP3uJn31DnFGEe9HjY06T8ZVzRH4h/2ehIJRE1miVx/hLDOGX+/iZyi9RfW9/BljRUrfq1fxkKkwP+zParpjMdboP0F++Lvc= 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=UWu+isjq; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=TEHM0By6; 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 Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 67RCC5tY4039093 for ; Thu, 27 Aug 2026 12:28:06 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= 1Z1YXiYze4iuUm3nc80GrxGrzMdPVj6ylSouQ8wpXYM=; b=UWu+isjq0r3IJa/i 2lJm9/uDXti5yQp25nAZsQwAschWsAd63j3uVO1CXaD/oMJK4yVfysbpOXv37kKm Dyfsamaq5o9AiBUwqnEAj3EUfqvoevWXSt1bQrvoovPhAj5HNLRifwLl8q5RKYUN 3hbKogaLTymE29ZnEVnMF9Tj1VAJjIEDQ46jkDacO9PFSrKvKkBlT7ivucwUbvU9 tO2n8K0tlJbBDyuDttqwo5LhFszXVZzVJR4eK47klLp03dtreukilbJfjfywDWCO WCngNJDBN6xs9FpoAgh0a5e95f3pY5MdkhR2DVSDaN9vumg+75bUN7wngCQ+09zE Oyfxqw== Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4gam6fg824-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Thu, 27 Aug 2026 12:28:06 +0000 (GMT) Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-38e475f83a2so1246679a91.1 for ; Thu, 27 Aug 2026 05:28:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1787833686; x=1788438486; 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=1Z1YXiYze4iuUm3nc80GrxGrzMdPVj6ylSouQ8wpXYM=; b=TEHM0By6sgPpknR7hS1YiH/3cNLd16s4bod+2Gw5EMSOg7xZMO6aieiz6kJtfmjRAC 6I4FdKw9niy1UAsbluY4iAO2pxS79R+yVY5q6U6DYRRXdsLgmHANCX1HvhHQeu6ffdmq S+iCHltx1CK2I5urxkvGUEB6LXZ3LOovQdqLhfInq4Jl0tRa7EmME+BSXvSNe0+Kt/jf TkWDu+5Wxvcaao6wVua19S4Tij1okSNYUYZIC/RwUg0pk3UNQOFMhYEFM5YJzjm7XK/z oVBY6JwZCUZrgJX4Hy5JDIKQeiImtZeMspOkRnSa2ITcncb2cn2qFttkATNSFYtvtB3D GOtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787833686; x=1788438486; 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=1Z1YXiYze4iuUm3nc80GrxGrzMdPVj6ylSouQ8wpXYM=; b=deLK23HcIVya+g/N5VbnBJCZOiS2OrXpwkg3WaT9go3WGjw/yWjyX6y/+MoA4nFY4O sNoQM1ONReNEHROKcU1ztdryQFReL/LyPGTMkv83EHauVLgHm6MVlLaUcIqPJz64pUBo G8+/G7RiA/Fcsg+SUHaZK021FsIiAEtj0Q5ddT333zOStHcKCUoIoburzTnQ0Smum5bb d/0Bbj2UpB5rBgHX9NbxJ1NKrSqDhx3ju/q4g1cFFa0UILMgsoRHz4xEjPojvIQ0MTHo RLrYl2lNN59ew6gKBYPn4DO9RQ7GAbJFriU3KPzPPpGUDhtTwRB/oy0FI3RbW1wdwxcN l9Yw== X-Forwarded-Encrypted: i=1; AHgh+Rq4cNVqBjI/tNc/eUXnTnnBtjFUhCgz5Qp/04CGZjfun3ljkwU2ejE0q8rdyAPK1lrqXp4KkVp7Ap4=@vger.kernel.org X-Gm-Message-State: AFuF++mDrZiiLGGTg06+DLaRFfgV7n/rMAWRZi3K5RDPPeSPqQcNgOXp IyMUR9YCSIbjO2ppKp4L29GlUUVY2HN9Hu0q7GZu0/bHOvZQ4a7au/DRSPzd3IR3TttaCoHX3t/ 64oLPNzM5aDLbxKpS3VEn9dr5KuOMGsWzSICNHs8Ru/hg6uAoZIvpzAAlO95hPaRE0Rh3U3g= X-Gm-Gg: AR+sD11MFHCaLqrWCa6sEgVW69dY0G7v2bKx9nFQrsSjrcfV5v1JhH2EkXMqDiynTqZ x4KhzEb8qRmSIEMTpDksNwe0HzP3F88rOV9J06rYyLbGAw3jaWnBOtb/ETPsgy2gBFyxFitdhJm zQPNKmK2N3woV/2RE/0FKN3DH57RqiYc0WUn5+bTAw/aG3lIPMd8GQh7KBnypf3HBZHZyezi7wj 8xVcbAqdTndXYEGM7nMqiRk9VxJJRIYLAzAGhXXqBuCeBJbp1CsXNipcwdNumMLAZEaiwNmT149 03BoNEy2y9x9Af4a0KppH27AHjeQT27KPAqwvw8BuEk1juU7KDSLmUoCyGDgDuaKf8ul/tqHYCa ef5yo9dC5uYg4J3rbheTUoS1YNDcBbXdZGjw= X-Received: by 2002:a17:90a:d2ce:b0:393:1d92:db5 with SMTP id 98e67ed59e1d1-3966d3dc5ecmr31793514a91.10.1787833685417; Thu, 27 Aug 2026 05:28:05 -0700 (PDT) X-Received: by 2002:a17:90a:d2ce:b0:393:1d92:db5 with SMTP id 98e67ed59e1d1-3966d3dc5ecmr31793376a91.10.1787833684918; Thu, 27 Aug 2026 05:28:04 -0700 (PDT) Received: from hu-arandive-hyd.qualcomm.com ([202.46.22.19]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-396b0fcfa3asm2707690a91.6.2026.08.27.05.28.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 27 Aug 2026 05:28:04 -0700 (PDT) From: Aniket Randive Date: Thu, 27 Aug 2026 17:57:51 +0530 Subject: [PATCH v8 2/2] i2c: qcom-geni: Add dynamic transfer timeout based on transfer length and frequency Precedence: bulk X-Mailing-List: linux-i2c@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-Id: <20260827-master-v8-2-596bc81c5cf9@oss.qualcomm.com> References: <20260827-master-v8-0-596bc81c5cf9@oss.qualcomm.com> In-Reply-To: <20260827-master-v8-0-596bc81c5cf9@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=1787833673; l=5805; i=aniket.randive@oss.qualcomm.com; s=20260325; h=from:subject:message-id; bh=ZeptbrLh2OAqJST6zmSGFm03zkKujLOAAacfUDgoLFY=; b=cun3mFMSgdZ9V4Zeqxn7Jif5Dm2wuXhB8pTyKVDbrK/2FCEGUctvr7pKC+4mTurIt9PErFxta 71yXrJrYb1pB4bWcWcrsVmzGVrwC28NVDklcd7QbclbyKsV+9k8dXGE X-Developer-Key: i=aniket.randive@oss.qualcomm.com; a=ed25519; pk=4o37X1ZqGbCO/T2SR8kq/HnWvq0yb1RUlFuNukovbQk= X-Proofpoint-Spam-Info: AW1haW4tMjYwODI3MDEwNCBTYWx0ZWRfX9CmsJk43nGtn PlVWsnlr1/FaifjKGXFaQsnAzzIHCNgu6YavyMbXa56yvk+1LIIpJkrJX3aEF35HWi1EhHjGN0s ZLk2PNtOmmb7ylACr93OaB0KyaTjy3w= X-Proofpoint-GUID: H0JGoOz8y8qJPrwG8cJMX-zg8mvK0XX1 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODI3MDEwNCBTYWx0ZWRfX4+t6UzxjS46T rbNL87f2xKhADMPcg8KNPh0/tEk5AWy+uCfu0opKv5mIHg2ALOU517SrFYK5H2amLMbG4XINpBy g3rQjq09u4AVMVfq5C1YMBEopyXVO4dQ297UcdiEsJ3ouo26y+HsDndyg2a6omzygvLJeE90NQJ H5BhhnmqcSmPe5dQHoUoVMCBzMnV4ssoSRQhrV82cU1/NAtqpHWoIho8IZVZ/kmVnKEJModrU3G ZSftfBdIPT7dLzs/CTjCEj3W0clzrvLZGfANdwTR5C33IGfW0/1ShjLsKoTOx9RJbln7qFMOYjh fLSJOg4nsz3S7fc0TKKrjykMzKJezfT5z5GdlxlOkiCYBKwHX4IdslYv0VYJ2UHPag01v/H1pqy YV8Ol+KkSQeiywnyugM6sAUtBAwFwG60Rj3WMQZe/22kXA2VOyn0+XK2oT0tUHTklFchY6+zo1h q1giL+5sjFVS4iHeY0Q== X-Authority-Analysis: v=2.4 cv=eIgjSnp1 c=1 sm=1 tr=0 ts=6a902d56 cx=c_pps a=UNFcQwm+pnOIJct1K4W+Mw==:117 a=fChuTYTh2wq5r3m49p7fHw==:17 a=IkcTkHD0fZMA:10 a=Sv0fKeRqtYgA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=gowsoOTTUOVcmtlkKump:22 a=EUspDBNiAAAA:8 a=ZP9fceawhsPimmF_JtQA:9 a=QEXdDO2ut3YA:10 a=uKXjsCUrEbL0IQVhDsJ9:22 X-Proofpoint-ORIG-GUID: H0JGoOz8y8qJPrwG8cJMX-zg8mvK0XX1 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-27_05,2026-08-26_02,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 adultscore=0 clxscore=1015 impostorscore=0 malwarescore=0 bulkscore=0 phishscore=0 spamscore=0 priorityscore=1501 lowpriorityscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608270104 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() API to calculate transfer timeouts dynamically based on message length and bus frequency. update the timeout per message for FIFO, SE-DMA and GPI single descriptor transfers. For GPI multi descriptor transfers, calculate the timeout from the combined length of all messages, as completion is reported only after the entire batch has finished. 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. Signed-off-by: Aniket Randive Reviewed-by: Mukesh Kumar Savaliya --- drivers/i2c/busses/i2c-qcom-geni.c | 48 +++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/drivers/i2c/busses/i2c-qcom-geni.c b/drivers/i2c/busses/i2c-qcom-geni.c index 658636c1ee0e..203f4d627ca1 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 { #define ABORT_TIMEOUT HZ #define CANCEL_TIMEOUT HZ -#define XFER_TIMEOUT HZ #define RST_TIMEOUT HZ +/* 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 indefinitely */ +#define I2C_TIMEOUT_MIN_USEC 300000 + struct geni_i2c_desc { bool no_dma_support; unsigned int tx_fifo_depth; @@ -513,7 +518,10 @@ static int geni_i2c_rx_one_msg(struct geni_i2c_dev *gi2c, struct i2c_msg *msg, } cur = gi2c->cur; - time_left = 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 = wait_for_completion_timeout(&gi2c->done, gi2c->adap.timeout); if (!time_left || (gi2c->err && gi2c->err != gi2c_log[ADDR_NACK].err)) geni_i2c_cancel_xfer(gi2c); @@ -555,7 +563,10 @@ static int geni_i2c_tx_one_msg(struct geni_i2c_dev *gi2c, struct i2c_msg *msg, writel_relaxed(1, se->base + SE_GENI_TX_WATERMARK_REG); cur = gi2c->cur; - time_left = 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 = wait_for_completion_timeout(&gi2c->done, gi2c->adap.timeout); if (!time_left || (gi2c->err && gi2c->err != gi2c_log[ADDR_NACK].err)) geni_i2c_cancel_xfer(gi2c); @@ -633,7 +644,7 @@ static void geni_i2c_gpi_multi_desc_unmap(struct geni_i2c_dev *gi2c, struct i2c_ * geni_i2c_gpi_multi_xfer_timeout_handler() - Handles multi message transfer 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 messages @@ -643,18 +654,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; for (i = 0; i < multi_xfer->msg_idx_cnt - 1; i++) { reinit_completion(transfer_comp); if (multi_xfer->msg_idx_cnt != multi_xfer->irq_cnt) { time_left = 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 +789,24 @@ static int geni_i2c_gpi(struct geni_i2c_dev *gi2c, struct i2c_msg msgs[], dma_async_issue_pending(gi2c->tx_c); if ((msg_idx == (gi2c->num_msgs - 1)) || flags & DMA_PREP_INTERRUPT) { + size_t total_len = 0; + int j; + + /* + * All TREs except the last carry the BEI bit, so a single + * completion interrupt fires only after the entire batch has + * drained on the wire. The timeout budget must therefore cover + * the combined wire time of every message in the batch. + */ + for (j = 0; j < gi2c->num_msgs; j++) + total_len += msgs[j].len; + + i2c_update_timeout(&gi2c->adap, gi2c->clk_freq_out, total_len, + I2C_TIMEOUT_SAFETY_COEFFICIENT, + I2C_TIMEOUT_MIN_USEC); ret = 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 +926,10 @@ static int geni_i2c_gpi_xfer(struct geni_i2c_dev *gi2c, struct i2c_msg msgs[], i if (!gi2c->is_tx_multi_desc_xfer) { dma_async_issue_pending(gi2c->tx_c); - time_left = 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 = wait_for_completion_timeout(&gi2c->done, gi2c->adap.timeout); if (!time_left) { dev_err(gi2c->se.dev, "%s:I2C timeout\n", __func__); gi2c->err = -ETIMEDOUT;