From patchwork Fri May 31 14:24:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Eichenberger X-Patchwork-Id: 13681757 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A0EC2C25B75 for ; Fri, 31 May 2024 14:25:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=rwdjoEqGQ9LDfp98Vkkrwivv2nGGVtR4UNVNWJLDGj4=; b=l6niHf8fE+7nLW WWx4ZK9pHvtzgXI28yfjt4TmAvbnpF5pOdCIqYdqHxO+MSYi81hNP+R0aL9JEAjF4mjfbBogM8df8 F9Xue/xaeTgh5KZnHbv0QUah9wLLMexDpWAbua4ThyME8xGI7R3nrXv4kFqSKazw/mQZ7BeBY995g /GS03f1rOofHozezAZ6IbiD4sPhVLXXe3DGhbxHbvQXVItYYwZKxfz135taEHRhCcUMa3ai/peMP8 AlZQr8pv80WIAKNgK6cMAv7006rBr8eFbUG7ODajiCgywfWr+4A1RPSSBI7C90w4D1ky4rCh1e/Bn yOedionzhVt7YKnPll0w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sD3Be-0000000AVB1-0Rtj; Fri, 31 May 2024 14:24:54 +0000 Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sD3BX-0000000AV8W-0WZn for linux-arm-kernel@lists.infradead.org; Fri, 31 May 2024 14:24:52 +0000 Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-420180b58c5so19731185e9.3 for ; Fri, 31 May 2024 07:24:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1717165484; x=1717770284; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=tvugHJuLmQUc6cOC5U1VQIb36XNwdMjbA+t0EJil/IA=; b=JoxHCZFNImVFpNbY+I2Ie9z/AOywHCl41ZNZWLyraZ9vHKBcvP4SZc7dfigSu76q7w N/qTeRa4AMz44G9pQ1g/mv4CCeiZNMor8hAWC+XXQyOka4FuCals80pWK00UzNH8vLFh Hh7U000LX4yIJz+OlrycUVWWGryV10vs8hO7AZ0QdrovdOFYbmJT+/cYVHL9F569gaoL drlfSWIfdEpYRWyRfwQnPb1muoDzSVRkUgKOZ9+PEcnJxTwRS+I/dOcqFkf2TzhNT5es TblJanl1hjgM8/IxegrUX3bPUj/8Cujw4aJJaBAexSwekcvEBkCOHnXRW34X2bXy3TEC tHbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717165484; x=1717770284; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=tvugHJuLmQUc6cOC5U1VQIb36XNwdMjbA+t0EJil/IA=; b=VYkmoVKSus46Uor5nSyvQIyFjFyDs3wTX67iyKPWowhR3OuMcr0Qahgfpu/QeVQOzf ABuYgXhG494L7x0vWnULT0OLeiGKZhJTlaaF3zIrX5QEoQQuTxXBWz8rTgpL5dRGUnfN 49EGWSSwQty/O78CvPUXo/xlCjuCmlG5F3aGObdKSCzHtUwbLcR2R2Af9DEO2zbJ1wAR qkiCIDGqLBSXw1NrHYJDl+oS6XV59sFytuf7wN6n2Amh7uRpf9kqKaFIcrMKJZZHuV8l Ns72/CEyjY3LYxS9xLhfbf7uO4TFF/zxxHaRRYTqigYOOE8reIUrPwulHbWIBWqKdFeW eneg== X-Forwarded-Encrypted: i=1; AJvYcCV8iwOvJ3R2oAdnq9AD3UAp+Z69BS6pXydv08UP1+kp9o1TG1wDRLxZ00FUNQx3W8Hvxp7nmxrxw1PSS9eVNDtQl+CVSjeYFftmE2y/V+pjm7hdpjg= X-Gm-Message-State: AOJu0YxcEFOdO2ftpRIvy12SLFGre5kabV6g84XbyJYaY1YYG8jW/XoU zt7hBQ/p6tBL4JNVZETEwszewh/xKgAVqeuOum9SC0blrQV1iWQo X-Google-Smtp-Source: AGHT+IGTYpztiA7U0O7D+ZJjrvnzu789lGae56lcoOL7cIRtDV3rFkXO1yh0FXSOa6ct61AKz8rl5w== X-Received: by 2002:a05:600c:45cb:b0:41f:fca0:8c04 with SMTP id 5b1f17b1804b1-4212e04998fmr16361265e9.11.1717165484201; Fri, 31 May 2024 07:24:44 -0700 (PDT) Received: from eichest-laptop.toradex.int ([2a02:168:af72:0:d7a5:3a0c:813f:54e]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-421292205ccsm48136685e9.4.2024.05.31.07.24.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 31 May 2024 07:24:43 -0700 (PDT) From: Stefan Eichenberger To: o.rempel@pengutronix.de, kernel@pengutronix.de, andi.shyti@kernel.org, shawnguo@kernel.org, s.hauer@pengutronix.de, festevam@gmail.com, jic23@kernel.org, lars@metafoo.de, nuno.sa@analog.com, andriy.shevchenko@linux.intel.com, u.kleine-koenig@pengutronix.de, marcelo.schmitt@analog.com, gnstark@salutedevices.com, francesco.dolcini@toradex.com Cc: linux-i2c@vger.kernel.org, imx@lists.linux.dev, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, eichest@gmail.com, Stefan Eichenberger Subject: [RFC PATCH] i2c: imx: avoid rescheduling when waiting for bus not busy Date: Fri, 31 May 2024 16:24:37 +0200 Message-Id: <20240531142437.74831-1-eichest@gmail.com> X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240531_072450_679805_CA9631B6 X-CRM114-Status: GOOD ( 21.39 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Stefan Eichenberger On our i.MX8M Mini based module we have an ADS1015 I2C ADC connected to the I2C bus. The ADS1015 I2C ADC will timeout after 25ms when the I2C bus is idle. The imx i2c driver will call schedule when waiting for the bus to become idle after switching to master mode. When the i2c controller switches to master mode it pulls SCL and SDA low, if the ADS1015 I2C ADC sees this for more than 25 ms without seeing SCL clocking, it will timeout and ignore all signals until the next start condition occurs (SCL and SDA low). This can occur when the system load is high and schedule returns after more than 25 ms. This rfc tries to solve the problem by using a udelay for the first 10 ms before calling schedule. This reduces the chance that we will reschedule. However, it is still theoretically possible for the problem to occur. To properly solve the problem, we would also need to disable interrupts during the transfer. After some internal discussion, we see three possible solutions: 1. Use udelay as shown in this rfc and also disable the interrupts during the transfer. This would solve the problem but disable the interrupts. Also, we would have to re-enable the interrupts if the timeout is longer than 1ms (TBD). 2. We use a retry mechanism in the ti-ads1015 driver. When we see a timeout, we try again. 3. We use the suggested solution and accept that there is an edge case where the timeout can happen. There may be a better way to do this, which is why this is an RFC. Signed-off-by: Stefan Eichenberger --- drivers/i2c/busses/i2c-imx.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c index 3842e527116b7..179f8367490a5 100644 --- a/drivers/i2c/busses/i2c-imx.c +++ b/drivers/i2c/busses/i2c-imx.c @@ -503,10 +503,18 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy, bool a "<%s> I2C bus is busy\n", __func__); return -ETIMEDOUT; } - if (atomic) + if (atomic) { udelay(100); - else - schedule(); + } else { + /* + * Avoid rescheduling in the first 10 ms to avoid + * timeouts for SMBus like devices + */ + if (time_before(jiffies, orig_jiffies + msecs_to_jiffies(10))) + udelay(10); + else + schedule(); + } } return 0;