From patchwork Sat May 8 18:23:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 12246197 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C2DCC433B4 for ; Sat, 8 May 2021 18:24:36 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6D5A261249 for ; Sat, 8 May 2021 18:24:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229649AbhEHSZh (ORCPT ); Sat, 8 May 2021 14:25:37 -0400 Received: from mail.kernel.org ([198.145.29.99]:33996 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229523AbhEHSZg (ORCPT ); Sat, 8 May 2021 14:25:36 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7B04C611BE; Sat, 8 May 2021 18:24:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1620498275; bh=2YIUztgQys0rC77/fi0wyJ5hjTZ0q0AGYDBVLa4bD8k=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SqwVbjdi/EC/tI/OG0ns2l+32cqzk2QYTSQxBuAJoICVKDi5i0uuZzMKxkNMDVrSV kOoyi6nBu+QDRZneM3Rx+njPMU6JQNf0g/Y8W1ikLX1nPE51Xqyimz2qSQibKZx3iX l5ZGUYEvG8MTaPIl96/T1+ZKZpui3VBueWUFt6PRdz/Et6MwLPyY6H8OUskm3nnfca 0p3R6fl+PjL7qFR4et2jQUw3lnwgaPXhcQQg0yY/K2cAFSmiMH/11UXSZc/l1VpFaT OzACY11HMbkEWiBYJZy1VA7JP58ej3KeqXZEZKTNfaACzySP/xrp7YtYQEbCSdm0cF uiSyeVa3trWhw== From: Jonathan Cameron To: linux-iio@vger.kernel.org Cc: Lars-Peter Clausen , alexandru.tachici@analog.com, Alexandru Ardelean , Jonathan Cameron Subject: [PATCH 1/3] iio: adc: ad7124: Fix missbalanced regulator enable / disable on error. Date: Sat, 8 May 2021 19:23:17 +0100 Message-Id: <20210508182319.488551-2-jic23@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210508182319.488551-1-jic23@kernel.org> References: <20210508182319.488551-1-jic23@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Jonathan Cameron If the devm_regulator_get() call succeeded but not the regulator_enable() then regulator_disable() would be called on a regulator that was not enabled. Fix this by moving regulator enabling / disabling over to devm_ management via devm_add_action_or_reset. Fixes: b3af341bbd96 ("iio: adc: Add ad7124 support") Signed-off-by: Jonathan Cameron Cc: Alexandru Ardelean Reviewed-by: Alexandru Ardelean --- drivers/iio/adc/ad7124.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c index 766c73333604..c0d0870a29ff 100644 --- a/drivers/iio/adc/ad7124.c +++ b/drivers/iio/adc/ad7124.c @@ -707,6 +707,11 @@ static int ad7124_setup(struct ad7124_state *st) return ret; } +static void ad7124_reg_disable(void *r) +{ + regulator_disable(r); +} + static int ad7124_probe(struct spi_device *spi) { const struct ad7124_chip_info *info; @@ -752,17 +757,20 @@ static int ad7124_probe(struct spi_device *spi) ret = regulator_enable(st->vref[i]); if (ret) return ret; + + ret = devm_add_action_or_reset(&spi->dev, ad7124_reg_disable, + st->vref[i]); + if (ret) + return ret; } st->mclk = devm_clk_get(&spi->dev, "mclk"); - if (IS_ERR(st->mclk)) { - ret = PTR_ERR(st->mclk); - goto error_regulator_disable; - } + if (IS_ERR(st->mclk)) + return PTR_ERR(st->mclk); ret = clk_prepare_enable(st->mclk); if (ret < 0) - goto error_regulator_disable; + return ret; ret = ad7124_soft_reset(st); if (ret < 0) @@ -792,11 +800,6 @@ static int ad7124_probe(struct spi_device *spi) ad_sd_cleanup_buffer_and_trigger(indio_dev); error_clk_disable_unprepare: clk_disable_unprepare(st->mclk); -error_regulator_disable: - for (i = ARRAY_SIZE(st->vref) - 1; i >= 0; i--) { - if (!IS_ERR_OR_NULL(st->vref[i])) - regulator_disable(st->vref[i]); - } return ret; } @@ -805,17 +808,11 @@ static int ad7124_remove(struct spi_device *spi) { struct iio_dev *indio_dev = spi_get_drvdata(spi); struct ad7124_state *st = iio_priv(indio_dev); - int i; iio_device_unregister(indio_dev); ad_sd_cleanup_buffer_and_trigger(indio_dev); clk_disable_unprepare(st->mclk); - for (i = ARRAY_SIZE(st->vref) - 1; i >= 0; i--) { - if (!IS_ERR_OR_NULL(st->vref[i])) - regulator_disable(st->vref[i]); - } - return 0; } From patchwork Sat May 8 18:23:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 12246199 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 821E7C43461 for ; Sat, 8 May 2021 18:24:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4AF7561249 for ; Sat, 8 May 2021 18:24:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229650AbhEHSZj (ORCPT ); Sat, 8 May 2021 14:25:39 -0400 Received: from mail.kernel.org ([198.145.29.99]:34026 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229523AbhEHSZi (ORCPT ); Sat, 8 May 2021 14:25:38 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8901D611F1; Sat, 8 May 2021 18:24:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1620498277; bh=YFu5qLBoHuMq0a+gUKgwrvIxd0Z4E24hZmQnyzuOkzw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Bu4Ci8cdREZQqXGiJH4soCNpq/PbbfpAr4KVAeH5Ss+MQ6Gxi2QzeeShSiqCaIW4H vbGjWsJindzTSGmv/fGuwYnsugriRbwXKNqgrt/VDKkvIQNRQTFv4wqFbegHHq//HG 3uF21WupJYw5GWAaCZYihIYmoUmEl0hdL1EOhwnUft22fRMnC2XF95UdYtpY7jxplv FwrKp6U4GBSMesb7eRd5AsbVQMgDo3Otjfb+EgLsZWkQ6WUUP9W6fedLniCTVM7FFD dkhyTT/v2U/YiNIaV5oW7iJClHHgEDX6/L6UXt7zJpOm48Vf0YQzTnUW7ePIwYuQV4 UwGO036dYO9Og== From: Jonathan Cameron To: linux-iio@vger.kernel.org Cc: Lars-Peter Clausen , alexandru.tachici@analog.com, Alexandru Ardelean , Jonathan Cameron Subject: [PATCH 2/3] iio: adc: ad7124: Fix potential overflow due to non sequential channel numbers Date: Sat, 8 May 2021 19:23:18 +0100 Message-Id: <20210508182319.488551-3-jic23@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210508182319.488551-1-jic23@kernel.org> References: <20210508182319.488551-1-jic23@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Jonathan Cameron Channel numbering must start at 0 and then not have any holes, or it is possible to overflow the available storage. Note this bug was introduced as part of a fix to ensure we didn't rely on the ordering of child nodes. So we need to support arbitrary ordering but they all need to be there somewhere. Note I hit this when using qemu to test the rest of this series. Arguably this isn't the best fix, but it is probably the most minimal option for backporting etc. Fixes: d7857e4ee1ba6 ("iio: adc: ad7124: Fix DT channel configuration") Signed-off-by: Jonathan Cameron Reviewed-by: Alexandru Ardelean --- drivers/iio/adc/ad7124.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c index c0d0870a29ff..9c2401c5848e 100644 --- a/drivers/iio/adc/ad7124.c +++ b/drivers/iio/adc/ad7124.c @@ -616,6 +616,13 @@ static int ad7124_of_parse_channel_config(struct iio_dev *indio_dev, if (ret) goto err; + if (channel >= indio_dev->num_channels) { + dev_err(indio_dev->dev.parent, + "Channel index >= number of channels\n"); + ret = -EINVAL; + goto err; + } + ret = of_property_read_u32_array(child, "diff-channels", ain, 2); if (ret) From patchwork Sat May 8 18:23:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 12246201 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6B040C43460 for ; Sat, 8 May 2021 18:24:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 40708611CE for ; Sat, 8 May 2021 18:24:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229523AbhEHSZl (ORCPT ); Sat, 8 May 2021 14:25:41 -0400 Received: from mail.kernel.org ([198.145.29.99]:34048 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229586AbhEHSZl (ORCPT ); Sat, 8 May 2021 14:25:41 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 925AA6140F; Sat, 8 May 2021 18:24:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1620498279; bh=lauP138cIHeiwsPYhU9Sowp8TxNDkBL7ETNWKJD5jIw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=H130zbrLH/mFCQfPdWd9IpYPVbjIqUeySGkIEoIU/DmRNNXpv1bkPCsk/g+U6HpJN 1GSkNCwUyakb11UxbtaRA84Sre9+Mwiu2W6K7sfaugiHWoVgtwRWc7ETR0kvZrVVUR y6zx9xNdDE1nA6gJTYdCkP2I5XRYOSsSM7yqFZhCjXTOz0UZX7rLtQKcV3qJPZmHed dY0U4lDHFTpBot1KjiQ0gofzU98wO9vaM4UZL7sQUuxTI9CsVvGAdnrJowyzzrY7cU Fqg0NYsgKL6sM8DM7XC0acjEiuAhTCmQmgjqWpC/AJZ1kc64IjPPMOen7XhbYJGlHu CX85YR0cI1BDw== From: Jonathan Cameron To: linux-iio@vger.kernel.org Cc: Lars-Peter Clausen , alexandru.tachici@analog.com, Alexandru Ardelean , Jonathan Cameron Subject: [PATCH 3/3] iio: adc: ad7124: Use devm_ managed calls for all of probe() + drop remove() Date: Sat, 8 May 2021 19:23:19 +0100 Message-Id: <20210508182319.488551-4-jic23@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210508182319.488551-1-jic23@kernel.org> References: <20210508182319.488551-1-jic23@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Jonathan Cameron As not many steps were not already devm_ managed, use devm_add_action_or_reset() to handle the rest. Signed-off-by: Jonathan Cameron Cc: Cc: Alexandru Ardelean Reviewed-by: Alexandru Ardelean --- drivers/iio/adc/ad7124.c | 53 ++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/drivers/iio/adc/ad7124.c b/drivers/iio/adc/ad7124.c index 9c2401c5848e..deb166d2b645 100644 --- a/drivers/iio/adc/ad7124.c +++ b/drivers/iio/adc/ad7124.c @@ -719,6 +719,16 @@ static void ad7124_reg_disable(void *r) regulator_disable(r); } +static void ad7124_clk_disable(void *c) +{ + clk_disable_unprepare(c); +} + +static void ad7124_buffer_cleanup(void *idev) +{ + ad_sd_cleanup_buffer_and_trigger(idev); +} + static int ad7124_probe(struct spi_device *spi) { const struct ad7124_chip_info *info; @@ -740,8 +750,6 @@ static int ad7124_probe(struct spi_device *spi) ad_sd_init(&st->sd, indio_dev, spi, &ad7124_sigma_delta_info); - spi_set_drvdata(spi, indio_dev); - indio_dev->name = st->chip_info->name; indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->info = &ad7124_info; @@ -779,48 +787,36 @@ static int ad7124_probe(struct spi_device *spi) if (ret < 0) return ret; + ret = devm_add_action_or_reset(&spi->dev, ad7124_clk_disable, st->mclk); + if (ret) + return ret; + ret = ad7124_soft_reset(st); if (ret < 0) - goto error_clk_disable_unprepare; + return ret; ret = ad7124_check_chip_id(st); if (ret) - goto error_clk_disable_unprepare; + return ret; ret = ad7124_setup(st); if (ret < 0) - goto error_clk_disable_unprepare; + return ret; ret = ad_sd_setup_buffer_and_trigger(indio_dev); if (ret < 0) - goto error_clk_disable_unprepare; - - ret = iio_device_register(indio_dev); - if (ret < 0) { - dev_err(&spi->dev, "Failed to register iio device\n"); - goto error_remove_trigger; - } + return ret; - return 0; + ret = devm_add_action_or_reset(&spi->dev, ad7124_buffer_cleanup, indio_dev); + if (ret) + return ret; -error_remove_trigger: - ad_sd_cleanup_buffer_and_trigger(indio_dev); -error_clk_disable_unprepare: - clk_disable_unprepare(st->mclk); + ret = devm_iio_device_register(&spi->dev, indio_dev); + if (ret) + dev_err(&spi->dev, "Failed to register iio device\n"); return ret; -} -static int ad7124_remove(struct spi_device *spi) -{ - struct iio_dev *indio_dev = spi_get_drvdata(spi); - struct ad7124_state *st = iio_priv(indio_dev); - - iio_device_unregister(indio_dev); - ad_sd_cleanup_buffer_and_trigger(indio_dev); - clk_disable_unprepare(st->mclk); - - return 0; } static const struct of_device_id ad7124_of_match[] = { @@ -838,7 +834,6 @@ static struct spi_driver ad71124_driver = { .of_match_table = ad7124_of_match, }, .probe = ad7124_probe, - .remove = ad7124_remove, }; module_spi_driver(ad71124_driver);