From nobody Thu Apr 25 21:28:51 2024 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9A2A2C7EE2D for ; Fri, 26 May 2023 07:07:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242093AbjEZHHY (ORCPT ); Fri, 26 May 2023 03:07:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229928AbjEZHHV (ORCPT ); Fri, 26 May 2023 03:07:21 -0400 Received: from hust.edu.cn (unknown [202.114.0.240]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 372E8F7; Fri, 26 May 2023 00:07:19 -0700 (PDT) Received: from van1shing-pc.localdomain ([10.12.182.0]) (user=silver_code@hust.edu.cn mech=LOGIN bits=0) by mx1.hust.edu.cn with ESMTP id 34Q75tik000939-34Q75til000939 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Fri, 26 May 2023 15:05:57 +0800 From: Wang Zhang To: Peter Korsgaard , Andrew Lunn , Wolfram Sang , Andreas Larsson Cc: hust-os-kernel-patches@googlegroups.com, Wang Zhang , Peter Korsgaard , linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4] i2c: ocores: use devm_ managed clks Date: Fri, 26 May 2023 15:05:33 +0800 Message-Id: <20230526070534.76112-1-silver_code@hust.edu.cn> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FEAS-AUTH-USER: silver_code@hust.edu.cn Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Smatch complains that: drivers/i2c/busses/i2c-ocores.c:704 ocores_i2c_probe() warn: missing unwind goto? If any wrong occurs in ocores_i2c_of_probe, the i2c->clk needs to be released. But the function returns directly without freeing the clock. Fix this by updating the code to use devm_clk_get_optional_enabled() instead. Use dev_err_probe() where appropriate as well since we are changing those statements. Fixes: f5f35a92e44a ("i2c: ocores: Add irq support for sparc") Signed-off-by: Wang Zhang Reviewed-by: Andi Shyti =20 --- v3->v4: use `dev_err_probe` to compact the code and add a fixes tag v2->v3: use `devm_clk_get_optional_enabled()` to manage clks v1->v2: change `ocores_i2c_of_probe` to use `devm_clk_get_enabled()` --- drivers/i2c/busses/i2c-ocores.c | 64 +++++++++++---------------------- 1 file changed, 21 insertions(+), 43 deletions(-) diff --git a/drivers/i2c/busses/i2c-ocores.c b/drivers/i2c/busses/i2c-ocore= s.c index 2e575856c5cd..e30df2b78fdf 100644 --- a/drivers/i2c/busses/i2c-ocores.c +++ b/drivers/i2c/busses/i2c-ocores.c @@ -552,28 +552,20 @@ static int ocores_i2c_of_probe(struct platform_device= *pdev, &clock_frequency); i2c->bus_clock_khz =3D 100; =20 - i2c->clk =3D devm_clk_get(&pdev->dev, NULL); - - if (!IS_ERR(i2c->clk)) { - int ret =3D clk_prepare_enable(i2c->clk); - - if (ret) { - dev_err(&pdev->dev, - "clk_prepare_enable failed: %d\n", ret); - return ret; - } - i2c->ip_clock_khz =3D clk_get_rate(i2c->clk) / 1000; - if (clock_frequency_present) - i2c->bus_clock_khz =3D clock_frequency / 1000; - } - + i2c->clk =3D devm_clk_get_optional_enabled(&pdev->dev, NULL); + if (IS_ERR(i2c->clk)) + return dev_err_probe(&pdev->dev, PTR_ERR(i2c->clk), + "devm_clk_get_optional_enabled failed\n"); + + i2c->ip_clock_khz =3D clk_get_rate(i2c->clk) / 1000; + if (clock_frequency_present) + i2c->bus_clock_khz =3D clock_frequency / 1000; if (i2c->ip_clock_khz =3D=3D 0) { if (of_property_read_u32(np, "opencores,ip-clock-frequency", &val)) { if (!clock_frequency_present) { dev_err(&pdev->dev, "Missing required parameter 'opencores,ip-clock-frequency'\n"); - clk_disable_unprepare(i2c->clk); return -ENODEV; } i2c->ip_clock_khz =3D clock_frequency / 1000; @@ -678,8 +670,7 @@ static int ocores_i2c_probe(struct platform_device *pde= v) default: dev_err(&pdev->dev, "Unsupported I/O width (%d)\n", i2c->reg_io_width); - ret =3D -EINVAL; - goto err_clk; + return -EINVAL; } } =20 @@ -710,13 +701,13 @@ static int ocores_i2c_probe(struct platform_device *p= dev) pdev->name, i2c); if (ret) { dev_err(&pdev->dev, "Cannot claim IRQ\n"); - goto err_clk; + return ret; } } =20 ret =3D ocores_init(&pdev->dev, i2c); if (ret) - goto err_clk; + return ret; =20 /* hook up driver to tree */ platform_set_drvdata(pdev, i2c); @@ -728,7 +719,7 @@ static int ocores_i2c_probe(struct platform_device *pde= v) /* add i2c adapter to i2c tree */ ret =3D i2c_add_adapter(&i2c->adap); if (ret) - goto err_clk; + return ret; =20 /* add in known devices to the bus */ if (pdata) { @@ -737,10 +728,6 @@ static int ocores_i2c_probe(struct platform_device *pd= ev) } =20 return 0; - -err_clk: - clk_disable_unprepare(i2c->clk); - return ret; } =20 static int ocores_i2c_remove(struct platform_device *pdev) @@ -755,9 +742,6 @@ static int ocores_i2c_remove(struct platform_device *pd= ev) /* remove adapter & data */ i2c_del_adapter(&i2c->adap); =20 - if (!IS_ERR(i2c->clk)) - clk_disable_unprepare(i2c->clk); - return 0; } =20 @@ -771,28 +755,22 @@ static int ocores_i2c_suspend(struct device *dev) ctrl &=3D ~(OCI2C_CTRL_EN | OCI2C_CTRL_IEN); oc_setreg(i2c, OCI2C_CONTROL, ctrl); =20 - if (!IS_ERR(i2c->clk)) - clk_disable_unprepare(i2c->clk); + clk_disable_unprepare(i2c->clk); return 0; } =20 static int ocores_i2c_resume(struct device *dev) { struct ocores_i2c *i2c =3D dev_get_drvdata(dev); + unsigned long rate; + int ret; =20 - if (!IS_ERR(i2c->clk)) { - unsigned long rate; - int ret =3D clk_prepare_enable(i2c->clk); - - if (ret) { - dev_err(dev, - "clk_prepare_enable failed: %d\n", ret); - return ret; - } - rate =3D clk_get_rate(i2c->clk) / 1000; - if (rate) - i2c->ip_clock_khz =3D rate; - } + ret =3D clk_prepare_enable(i2c->clk); + if (ret) + return dev_err_probe(dev, ret, "clk_prepare_enable failed\n"); + rate =3D clk_get_rate(i2c->clk) / 1000; + if (rate) + i2c->ip_clock_khz =3D rate; return ocores_init(dev, i2c); } =20 --=20 2.34.1