From patchwork Fri Jan 19 07:22:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alexander Stein X-Patchwork-Id: 13523379 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 D2E42C4725D for ; Fri, 19 Jan 2024 07:23:04 +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=WNTUvYu3jjMYcpTAOHzF70W/pdEEgdLuq6BlvVkPz3U=; b=p8qePh2upDnJlG ExnO4l0BdLwstXig8zrPZyWMp697L1SYuZN5hXdhmdd4kOTYvI+xuIcXkaOx9LPaX+iuO9hC4AGsd yVUVGGcendlx8mUVc1mSlFGKuBGm9kI2JzkgwQCttFR2NJibTkbSOY87/LyGYv7lOiPh3oFCf5jq9 IhPTQwwiDqLUTIPeJ4B4v9vyCh6bxG5YLk8jM0+n9LPXmk9tR+Q8RNHRj4vzjcNRY7x7snPx5jn7G j5DN6vhxhaG5UgoT4+hTtLkjkNcHMQKyPFaRZ7qmRNGUNq+12RdzyqswRPnEbArGNycHdjZSjNfgR Dofa6Fa+stKAT7430XJQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1rQjD2-004gkv-2f; Fri, 19 Jan 2024 07:22:36 +0000 Received: from mx1.tq-group.com ([93.104.207.81]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1rQjCz-004gjE-1W for linux-arm-kernel@lists.infradead.org; Fri, 19 Jan 2024 07:22:35 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tq-group.com; i=@tq-group.com; q=dns/txt; s=key1; t=1705648953; x=1737184953; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=ToCD64IZyp9R5KDsXQDCPabTKt3AQPT7ipiSDB71oLo=; b=eML6ZHKzUxDnkUCAEaj5WE6LDzWN+4GlAMdv6aOzulH1DOz8scLLiZMv sJcWnKg5v8Jg7NlvVx1fbPSlE8gZc91ZH+HgJC5iMzs0S4cJdZqL9jaz8 Ctap2cGvPsc+gqOsYAW1+WS/1OezfNcGkiplM9Ll18/D8+oGAzA7lJ4c8 RpCz7VtYMw+7dQAK6T9rPMFDpcvPyVqLCX6lXjl19bboYPWJaKo+s9tzE iLOpwaxlH4Y9K7flucU5Z5RDI6zZ0Y3OyG+mVKeRolIjOrviGbuV7mFJ+ KpvP/x4c68ijgMht7fqn1pEts1Skr7X1bHG4v5xorTWPNgMjQQhodDo2n A==; X-IronPort-AV: E=Sophos;i="6.05,204,1701126000"; d="scan'208";a="34974265" Received: from vtuxmail01.tq-net.de ([10.115.0.20]) by mx1.tq-group.com with ESMTP; 19 Jan 2024 08:22:29 +0100 Received: from steina-w.tq-net.de (steina-w.tq-net.de [10.123.53.25]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by vtuxmail01.tq-net.de (Postfix) with ESMTPSA id 13478280075; Fri, 19 Jan 2024 08:22:29 +0100 (CET) From: Alexander Stein To: Dong Aisheng , Andi Shyti , Shawn Guo , Sascha Hauer , Fabio Estevam Cc: Alexander Stein , Pengutronix Kernel Team , NXP Linux Team , linux-i2c@vger.kernel.org, linux-arm-kernel@lists.infradead.org, =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= Subject: [PATCH v2 1/1] i2c: lpi2c: Avoid calling clk_get_rate during transfer Date: Fri, 19 Jan 2024 08:22:23 +0100 Message-Id: <20240119072223.3986183-1-alexander.stein@ew.tq-group.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240118_232233_885775_2CEDA864 X-CRM114-Status: GOOD ( 19.40 ) 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 Instead of repeatedly calling clk_get_rate for each transfer, lock the clock rate and cache the value. A deadlock has been observed while adding tlv320aic32x4 audio codec to the system. When this clock provider adds its clock, the clk mutex is locked already, it needs to access i2c, which in return needs the mutex for clk_get_rate as well. Signed-off-by: Alexander Stein Reviewed-by: Uwe Kleine-König Reviewed-by: Andi Shyti --- This is an alternative, lightweight approach replacing the patch [1] and depends on [2]. The issue to address is still removing the call to clk_get_rate() during each transfer, which might reuslt in a deadlock. lockdep also complains about this call chain. Instead of adding a clock notifier, lock the peripheral clock rate and cache the peripheral clock rate. Currently LPI2C is available in the following SoC: * i.MX7ULP * i.MX8ULP * i.MX8DXL * i.MX8X * i.MX8 * i.MX93 Additionally I expect both i.MX91 and i.MX95 to also use this driver. This patch assumes the parent clock rate never changes. This is apparently true for i.MX93 as each I2C has it's own lpi2c*_root clock. On i.MX8 and i.MX8X clocks are managed by SCU with it's own dedicated firmware. I can't say if the clock never changes though. I have no idea about the other SoC. Changes in v2: * Removed redundent clk_rate check in lpi2c_imx_config (I opted to keep the local variable to not extent the calculation code lines) * Collected R-b Best regards, Alexander [1] https://lore.kernel.org/all/20240110120556.519800-1-alexander.stein@ew.tq-group.com/ [2] https://lore.kernel.org/all/20240104225512.1124519-2-u.kleine-koenig@pengutronix.de/ drivers/i2c/busses/i2c-imx-lpi2c.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/drivers/i2c/busses/i2c-imx-lpi2c.c b/drivers/i2c/busses/i2c-imx-lpi2c.c index 678b30e90492a..14473c223cfa7 100644 --- a/drivers/i2c/busses/i2c-imx-lpi2c.c +++ b/drivers/i2c/busses/i2c-imx-lpi2c.c @@ -99,6 +99,7 @@ struct lpi2c_imx_struct { __u8 *rx_buf; __u8 *tx_buf; struct completion complete; + unsigned long rate_per; unsigned int msglen; unsigned int delivered; unsigned int block_data; @@ -207,9 +208,7 @@ static int lpi2c_imx_config(struct lpi2c_imx_struct *lpi2c_imx) lpi2c_imx_set_mode(lpi2c_imx); - clk_rate = clk_get_rate(lpi2c_imx->clks[0].clk); - if (!clk_rate) - return -EINVAL; + clk_rate = lpi2c_imx->rate_per; if (lpi2c_imx->mode == HS || lpi2c_imx->mode == ULTRA_FAST) filt = 0; @@ -590,6 +589,20 @@ static int lpi2c_imx_probe(struct platform_device *pdev) if (ret) return ret; + /* + * Lock the parent clock rate to avoid getting parent clock upon + * each transfer + */ + ret = devm_clk_rate_exclusive_get(&pdev->dev, lpi2c_imx->clks[0].clk); + if (ret) + return dev_err_probe(&pdev->dev, ret, + "can't lock I2C peripheral clock rate\n"); + + lpi2c_imx->rate_per = clk_get_rate(lpi2c_imx->clks[0].clk); + if (!lpi2c_imx->rate_per) + return dev_err_probe(&pdev->dev, -EINVAL, + "can't get I2C peripheral clock rate\n"); + pm_runtime_set_autosuspend_delay(&pdev->dev, I2C_PM_TIMEOUT); pm_runtime_use_autosuspend(&pdev->dev); pm_runtime_get_noresume(&pdev->dev);