From patchwork Mon Nov 2 14:22:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 11874037 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4FAF21130 for ; Mon, 2 Nov 2020 14:26:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 27E5F206F8 for ; Mon, 2 Nov 2020 14:26:19 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="htAF5Ujg" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726103AbgKBO0S (ORCPT ); Mon, 2 Nov 2020 09:26:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726070AbgKBOWg (ORCPT ); Mon, 2 Nov 2020 09:22:36 -0500 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AEA03C0617A6 for ; Mon, 2 Nov 2020 06:22:34 -0800 (PST) Received: by mail-wr1-x441.google.com with SMTP id k10so13478554wrw.13 for ; Mon, 02 Nov 2020 06:22:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Jw3oq9QTE7nFTaO8GB8D/I4qCpQ+Kq8IawJXBfxGxQ8=; b=htAF5Ujg6yCl9eqYEVMVTYCl4kGp3ZTnkUV9UbXU9g8dU+97BuZIfMHctZHTz0z0iw 3eeHUCRmY8CY6eXn1cDdvLdt+IN29epMqgLCrzN+vh79zTaxv2xPgxDqSO0vH5XTJUcT uUZiGkBOEP+VgLPnP9/BvaBT0yvVKQWomOnkUC7OZJUyWRpMj8ozdkZdC++M0csjtDhg G+hSVlYLvT3l0jnZaioFiFNdMmwIUzI/gRoZgrDigurfpQRk25DlBetOiCQtEgLwlH1I EBRWmy2aF7gaNgRBK0IcpKqT8Seoyx0RZc5wln1sAOFpvOfIYtdWwsU+GUDBNT8syjpt S6UQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Jw3oq9QTE7nFTaO8GB8D/I4qCpQ+Kq8IawJXBfxGxQ8=; b=GOmmLzHOdsm4jWfgs+0p6orNrwxvMX2DG1tdH3Z373bHwSKWH1ioUqQk78OLKEbY0q bDLpiYR0dGpE8p29JBKW/pjBLdm5RRCGRgkYQT82jXV1V04CBCMPFqDgQ403fmfQ2F0l iRI/Ub4ORLyKFd0AdqoFYL+JukD36p0O6m3BqAeQpRYfXfmgbGB/mPzGf6h032IICgye tyqwwI0hROh2k1aBibhlpkrFdZAbmlA9Uxe1+azLSB0JiIykn5Ey/ZYSuC+LKORFh7WV ouDSsDPO0K44jGGspb0Hv6u1rttvrBBvy0CO+lAns7qC1BtBlgTKVKvPGncItewXQZ7+ T1VA== X-Gm-Message-State: AOAM532bfv6c0SaSKLszz+xRUVRX5N6sr52RGdC/5m2RdfEMaCclG42Z 8q5NbESNWA5savBGti3RgxQp6Q== X-Google-Smtp-Source: ABdhPJzSQGvgcpKNMN8/+kAWDq0KWVIo0vIEPBTqaI3e1RKL5IFUmYi3Hy6oECVzYa5AsZyknYP3ww== X-Received: by 2002:a05:6000:142:: with SMTP id r2mr14427807wrx.155.1604326953520; Mon, 02 Nov 2020 06:22:33 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id 3sm16182987wmd.19.2020.11.02.06.22.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Nov 2020 06:22:32 -0800 (PST) From: Bartosz Golaszewski To: Jonathan Cameron , Lars-Peter Clausen , Peter Meerwald-Stadler , Michal Simek Cc: linux-iio@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 1/4] device: provide devm_krealloc_array() Date: Mon, 2 Nov 2020 15:22:25 +0100 Message-Id: <20201102142228.14949-2-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201102142228.14949-1-brgl@bgdev.pl> References: <20201102142228.14949-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Bartosz Golaszewski When allocating an array of elements, users should check for multiplication overflow or preferably use one of the provided helpers like: devm_kmalloc_array(). This provides devm_krealloc_array() for users who want to reallocate managed arrays. Signed-off-by: Bartosz Golaszewski --- include/linux/device.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/linux/device.h b/include/linux/device.h index 5ed101be7b2e..e77203faea55 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -226,6 +226,17 @@ static inline void *devm_kmalloc_array(struct device *dev, return devm_kmalloc(dev, bytes, flags); } +static inline void * +devm_krealloc_array(struct device *dev, void *ptr, size_t new_n, + size_t new_size, gfp_t gfp) +{ + size_t bytes; + + if (unlikely(check_mul_overflow(new_n, new_size, &bytes))) + return NULL; + + return devm_krealloc(dev, ptr, bytes, gfp); +} static inline void *devm_kcalloc(struct device *dev, size_t n, size_t size, gfp_t flags) { From patchwork Mon Nov 2 14:22:26 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 11874033 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3A04E1130 for ; Mon, 2 Nov 2020 14:26:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0FB66206F8 for ; Mon, 2 Nov 2020 14:26:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="EcVmzz9j" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726083AbgKBO0N (ORCPT ); Mon, 2 Nov 2020 09:26:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726080AbgKBOWh (ORCPT ); Mon, 2 Nov 2020 09:22:37 -0500 Received: from mail-wm1-x344.google.com (mail-wm1-x344.google.com [IPv6:2a00:1450:4864:20::344]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C7A64C061A47 for ; Mon, 2 Nov 2020 06:22:35 -0800 (PST) Received: by mail-wm1-x344.google.com with SMTP id 23so1148950wmg.1 for ; Mon, 02 Nov 2020 06:22:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oc+CRyq0MP1rGPx6qu4fJIiuXgNVzq5WpNvySKjLu6M=; b=EcVmzz9jth/73LQEedUdNCEyD5xs5p5MrEHBHb5vnk+/Fhd6bkVFftxytaQ98ipoi2 E7o8OHb6Yj9gmE5x5+rfThOvj9oZYoEpsZhKZDXRdJ5C7T4tzde8EdYcpT4DohOb/0OB QBIQIFx+0s4OJ0KSlHkLKm4zQbUyVL3yPrVzN8k9IMQ5pyfaqyyhRV9EROX94IGpZcW7 nKaCjKX5gcLcDQBG9z07e0FrG0CD9kLjyMyiz6vGUnt5h2e6UNSqKH//ptHRGgLbY33z HGY7ZpNcovsFzKYMww8DIz6Q0V/t2wrYTpNpbgocFjaF+44Bjjnlp8fPlq616UdRR6KQ jNfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oc+CRyq0MP1rGPx6qu4fJIiuXgNVzq5WpNvySKjLu6M=; b=pRHuEXgXVAt1QZVRFQXrIppcMyGh6eIl+94k+AL089NwPVcXi58xwJWJpbfNzNs9G1 3A5CGaUdmD8KOl8RY++iB5MtaZuUHxV5ZLFuukE5qkE0zAwAYBSKT9mn2/R5d64WPAna /f8qaIBzeZgW+MfqSobH3aw7413wXTzZFZYLkDuMsLwCMjMjEWeLy2G5C+/AKrA6yze9 2wHCKj0oFBy9qtB2yfXujMcMSJf06RCVNpq7R64nbDqod5yZtV9K2wgAPBbclRXEDojQ yGRzyKXMAqRdHgWtxgBpzPVWNwi0OxIAShhXfdMOtTa30ht795JkNmukdZiJwTcJe6m5 VZqQ== X-Gm-Message-State: AOAM533nqr1N/uG+klsx2+gagUcr9JKXKckF9CS4XJ+1vSf7QU+D3JGK d9CxQnR18HK/pCX6o1E/bqH5iA== X-Google-Smtp-Source: ABdhPJy4aiDk0PjW2NDVFwcRQ7QvYaGh//Ek/ANQ8H+KnrDn3+7QwhU39uVvCEUSktsWeSUAcRlx5A== X-Received: by 2002:a1c:6405:: with SMTP id y5mr7346371wmb.150.1604326954564; Mon, 02 Nov 2020 06:22:34 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id 3sm16182987wmd.19.2020.11.02.06.22.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Nov 2020 06:22:34 -0800 (PST) From: Bartosz Golaszewski To: Jonathan Cameron , Lars-Peter Clausen , Peter Meerwald-Stadler , Michal Simek Cc: linux-iio@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 2/4] iio: adc: xilinx: use helper variable for &pdev->dev Date: Mon, 2 Nov 2020 15:22:26 +0100 Message-Id: <20201102142228.14949-3-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201102142228.14949-1-brgl@bgdev.pl> References: <20201102142228.14949-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Bartosz Golaszewski It's more elegant to use a helper local variable to store the address of the underlying struct device than to dereference pdev everywhere. Signed-off-by: Bartosz Golaszewski --- drivers/iio/adc/xilinx-xadc-core.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/drivers/iio/adc/xilinx-xadc-core.c b/drivers/iio/adc/xilinx-xadc-core.c index f93c34fe5873..8494eb424b33 100644 --- a/drivers/iio/adc/xilinx-xadc-core.c +++ b/drivers/iio/adc/xilinx-xadc-core.c @@ -1186,6 +1186,7 @@ static int xadc_parse_dt(struct iio_dev *indio_dev, struct device_node *np, static int xadc_probe(struct platform_device *pdev) { + struct device *dev = &pdev->dev; const struct of_device_id *id; struct iio_dev *indio_dev; unsigned int bipolar_mask; @@ -1195,10 +1196,10 @@ static int xadc_probe(struct platform_device *pdev) int irq; int i; - if (!pdev->dev.of_node) + if (!dev->of_node) return -ENODEV; - id = of_match_node(xadc_of_match_table, pdev->dev.of_node); + id = of_match_node(xadc_of_match_table, dev->of_node); if (!id) return -EINVAL; @@ -1206,7 +1207,7 @@ static int xadc_probe(struct platform_device *pdev) if (irq <= 0) return -ENXIO; - indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*xadc)); + indio_dev = devm_iio_device_alloc(dev, sizeof(*xadc)); if (!indio_dev) return -ENOMEM; @@ -1226,7 +1227,7 @@ static int xadc_probe(struct platform_device *pdev) indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->info = &xadc_info; - ret = xadc_parse_dt(indio_dev, pdev->dev.of_node, &conf0); + ret = xadc_parse_dt(indio_dev, dev->of_node, &conf0); if (ret) return ret; @@ -1250,7 +1251,7 @@ static int xadc_probe(struct platform_device *pdev) } } - xadc->clk = devm_clk_get(&pdev->dev, NULL); + xadc->clk = devm_clk_get(dev, NULL); if (IS_ERR(xadc->clk)) { ret = PTR_ERR(xadc->clk); goto err_free_samplerate_trigger; @@ -1276,7 +1277,7 @@ static int xadc_probe(struct platform_device *pdev) } ret = request_irq(xadc->irq, xadc->ops->interrupt_handler, 0, - dev_name(&pdev->dev), indio_dev); + dev_name(dev), indio_dev); if (ret) goto err_clk_disable_unprepare; From patchwork Mon Nov 2 14:22:27 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 11874035 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BFE0B92C for ; Mon, 2 Nov 2020 14:26:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9C140206F8 for ; Mon, 2 Nov 2020 14:26:14 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="Iz+6HeRy" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726109AbgKBO0N (ORCPT ); Mon, 2 Nov 2020 09:26:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726076AbgKBOWh (ORCPT ); Mon, 2 Nov 2020 09:22:37 -0500 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1032BC061A04 for ; Mon, 2 Nov 2020 06:22:37 -0800 (PST) Received: by mail-wr1-x441.google.com with SMTP id n15so14860620wrq.2 for ; Mon, 02 Nov 2020 06:22:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9A0YZTtyChgf0u6PFbkMOtS/8IXdoUAvGcveYosNg8o=; b=Iz+6HeRyJ0MzqEaZdma++F+Cl26ft+hc4SvyuEOwHNxpA9O2d9Ccrt//8K9HHCIGeZ PN3iov4vxUF8uIwsXYRP/xOxjR9AG1Icc1aQarzzAZ9ds+u7ghaZnJwLeuYXdoqYJprL xB8zPvuA1ahdRu3rRVh598nguAmJqCg2KA4KTNPDisAigjNyFkA8Z/yh0W4W40YktLVg bs9JczBvspN3PnSZ738KfquAPGW2rtBneSeDWY2SSINssmt+xct8YHlDqWDp7jPOL6iz HGjKojUFxD2tr4fe/2JUXl6sZ7XjLoHEzurMV9I0JIaQxwP7qOFQl8Ps9bd6R/LsA4XY CUHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9A0YZTtyChgf0u6PFbkMOtS/8IXdoUAvGcveYosNg8o=; b=eFYCGUX7vazR729ldll0h70LymKMpuN2XDIw+ZHrGUSa6mu0eIty5z9KtgiqJrL9FC dLPG6Tr72PiNK7tbA4PmFooRRKDTCCqkKq1GfQqX6SyKfd4T0opOWyCXhELN5wwFkE3/ qBuQdKvgNJDoKuC5KBDkCDXjH29LxU13guHX19qvUvCKUF3QAxIm1jMfE5dRv6xtQ/AS onhmpVWmDw67lA/RrelnhKgelfbn1TijlOYpNrEeeRy0H5hYgyl3rrBBhFboQb6oaqv+ C1fwcWQC3Sn/UeKibdt13CEnypG4lTQ27gsZ2P3yaXt+F4DZ7ZqS7cjmfb5dyq1KrtjZ FBjA== X-Gm-Message-State: AOAM5308nIAGFtCisl5LPmWBrVSJ5t/rrIOFHeam5EwsYLfXEwkjQYP5 gpZNn3ZCepe7fzLhmHtn3Yu6mw== X-Google-Smtp-Source: ABdhPJzEMMHZfL1mdwXFbBpQAWTyd9NcamKKsSu8d3RLFB6kvwNmlyzoTY0otzx1aVegApUhQc86jQ== X-Received: by 2002:adf:ea11:: with SMTP id q17mr21799947wrm.251.1604326955882; Mon, 02 Nov 2020 06:22:35 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id 3sm16182987wmd.19.2020.11.02.06.22.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Nov 2020 06:22:35 -0800 (PST) From: Bartosz Golaszewski To: Jonathan Cameron , Lars-Peter Clausen , Peter Meerwald-Stadler , Michal Simek Cc: linux-iio@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 3/4] iio: adc: xilinx: use devm_krealloc_array() instead of kfree() + kcalloc() Date: Mon, 2 Nov 2020 15:22:27 +0100 Message-Id: <20201102142228.14949-4-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201102142228.14949-1-brgl@bgdev.pl> References: <20201102142228.14949-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Bartosz Golaszewski We now have devm_krealloc_array() in the kernel Use it indstead of calling kfree() and kcalloc() separately. Signed-off-by: Bartosz Golaszewski --- drivers/iio/adc/xilinx-xadc-core.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/iio/adc/xilinx-xadc-core.c b/drivers/iio/adc/xilinx-xadc-core.c index 8494eb424b33..7792aa4cf9cb 100644 --- a/drivers/iio/adc/xilinx-xadc-core.c +++ b/drivers/iio/adc/xilinx-xadc-core.c @@ -586,14 +586,18 @@ static int xadc_update_scan_mode(struct iio_dev *indio_dev, { struct xadc *xadc = iio_priv(indio_dev); unsigned int n; + void *data; n = bitmap_weight(mask, indio_dev->masklength); - kfree(xadc->data); - xadc->data = kcalloc(n, sizeof(*xadc->data), GFP_KERNEL); - if (!xadc->data) + data = devm_krealloc_array(indio_dev->dev.parent, xadc->data, n, + sizeof(*xadc->data), GFP_KERNEL); + if (!data) return -ENOMEM; + memset(data, 0, n * sizeof(*xadc->data)); + xadc->data = data; + return 0; } @@ -1372,7 +1376,6 @@ static int xadc_remove(struct platform_device *pdev) free_irq(xadc->irq, indio_dev); cancel_delayed_work_sync(&xadc->zynq_unmask_work); clk_disable_unprepare(xadc->clk); - kfree(xadc->data); return 0; } From patchwork Mon Nov 2 14:22:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bartosz Golaszewski X-Patchwork-Id: 11874031 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4B1B714B2 for ; Mon, 2 Nov 2020 14:26:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1AF762072C for ; Mon, 2 Nov 2020 14:26:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=bgdev-pl.20150623.gappssmtp.com header.i=@bgdev-pl.20150623.gappssmtp.com header.b="SFVRkrzI" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1725982AbgKBO0B (ORCPT ); Mon, 2 Nov 2020 09:26:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726090AbgKBOWk (ORCPT ); Mon, 2 Nov 2020 09:22:40 -0500 Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4662FC061A04 for ; Mon, 2 Nov 2020 06:22:38 -0800 (PST) Received: by mail-wr1-x443.google.com with SMTP id k10so13478793wrw.13 for ; Mon, 02 Nov 2020 06:22:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vLbBt6T4zV/xw9PURuQlOM8wVnilFKG9GdBusjjh0Mo=; b=SFVRkrzIcWH8EfsDxR6Pfi92y1MXVYva5l0WcguIBbfiE6IhQzVSHxF9UsbfKH4vFQ /61PTFnjWKt2Ktu6jXxhzFS3RDl7WTTD9S3vsW+B4e7Sg6e/XsaQ2vDhYcUZgEzWkrnR AHPJOKqdTpNTijRvRzHgu4GmCzVhCVMucqJ2YYGxvb+fZLBWTVeTxe49bniLiXylqHLl IBftQ1vXwTIf/WVsV43Cl2yXb506SQEBnc91/oWiwSps/zB8FBL2gSYJ8mO3LGbm2/OJ sm3hZ7NMOKpMqAFEXP6N3uptECOZQEhE4YxhwTsGRe0a1doWV3b/H1Hv/j1+7CIuRcPb BiLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=vLbBt6T4zV/xw9PURuQlOM8wVnilFKG9GdBusjjh0Mo=; b=biZjlC192itI8/nmIFXAkOPmyytwhj+JU2ySeaA7VfHfSCvUdmzTG8OM+YTNonE3pd v3TR2QsQUALerS6EtiGnbm+bre1IZ5pxLWIiMAHpMmo4LV/zVC4003CRferZQjf3P00N wM5genoHJMoZkHdekJRUKST/p9rwCgAVMuFkqmdVuzye59p4EzXbPQVk8bZ3hwdRP9pX Y4GJ8URBxZxk4NtR7BpXKlhSqnmOl1br8vqrTPx4S14pp5KkhdsPywZr7eGq1sV6Comz JVQlLZpb2W7q/lMyIN44pyzwa5mbcMKiYLJ0/dDWrAsR1h4LP/aHHN+1UZ0FFL5EcSWz WHaQ== X-Gm-Message-State: AOAM5318zTcmivrbfM4EwX09jk3HwmclNBHZMoF8LBnOsbFXMNBD+Go+ xEX3IYsYZPASbDz58jIchQ7Hrg== X-Google-Smtp-Source: ABdhPJw416wPtB82zogsu5olmJkH1UCsYtbiT23TJOgqrvgg/jImFRTpDsKYOmvyA335IzD2wpSLyg== X-Received: by 2002:a5d:4612:: with SMTP id t18mr20763254wrq.307.1604326957059; Mon, 02 Nov 2020 06:22:37 -0800 (PST) Received: from debian-brgl.home (amarseille-656-1-4-167.w90-8.abo.wanadoo.fr. [90.8.158.167]) by smtp.gmail.com with ESMTPSA id 3sm16182987wmd.19.2020.11.02.06.22.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Nov 2020 06:22:36 -0800 (PST) From: Bartosz Golaszewski To: Jonathan Cameron , Lars-Peter Clausen , Peter Meerwald-Stadler , Michal Simek Cc: linux-iio@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH v2 4/4] iio: adc: xilinx: use more devres helpers and remove remove() Date: Mon, 2 Nov 2020 15:22:28 +0100 Message-Id: <20201102142228.14949-5-brgl@bgdev.pl> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201102142228.14949-1-brgl@bgdev.pl> References: <20201102142228.14949-1-brgl@bgdev.pl> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Bartosz Golaszewski In order to simplify resource management and error paths in probe() and entirely drop the remove() callback - use devres helpers wherever possible. Define devm actions for cancelling the delayed work and disabling the clock. Signed-off-by: Bartosz Golaszewski --- drivers/iio/adc/xilinx-xadc-core.c | 129 +++++++++++++---------------- 1 file changed, 56 insertions(+), 73 deletions(-) diff --git a/drivers/iio/adc/xilinx-xadc-core.c b/drivers/iio/adc/xilinx-xadc-core.c index 7792aa4cf9cb..6ce1745b4995 100644 --- a/drivers/iio/adc/xilinx-xadc-core.c +++ b/drivers/iio/adc/xilinx-xadc-core.c @@ -709,11 +709,12 @@ static const struct iio_trigger_ops xadc_trigger_ops = { static struct iio_trigger *xadc_alloc_trigger(struct iio_dev *indio_dev, const char *name) { + struct device *dev = indio_dev->dev.parent; struct iio_trigger *trig; int ret; - trig = iio_trigger_alloc("%s%d-%s", indio_dev->name, - indio_dev->id, name); + trig = devm_iio_trigger_alloc(dev, "%s%d-%s", indio_dev->name, + indio_dev->id, name); if (trig == NULL) return ERR_PTR(-ENOMEM); @@ -721,15 +722,11 @@ static struct iio_trigger *xadc_alloc_trigger(struct iio_dev *indio_dev, trig->ops = &xadc_trigger_ops; iio_trigger_set_drvdata(trig, iio_priv(indio_dev)); - ret = iio_trigger_register(trig); + ret = devm_iio_trigger_register(dev, trig); if (ret) - goto error_free_trig; + return ERR_PTR(ret); return trig; - -error_free_trig: - iio_trigger_free(trig); - return ERR_PTR(ret); } static int xadc_power_adc_b(struct xadc *xadc, unsigned int seq_mode) @@ -1188,6 +1185,20 @@ static int xadc_parse_dt(struct iio_dev *indio_dev, struct device_node *np, return 0; } +static void xadc_clk_disable_unprepare(void *data) +{ + struct clk *clk = data; + + clk_disable_unprepare(clk); +} + +static void xadc_cancel_delayed_work(void *data) +{ + struct delayed_work *work = data; + + cancel_delayed_work_sync(work); +} + static int xadc_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; @@ -1236,34 +1247,35 @@ static int xadc_probe(struct platform_device *pdev) return ret; if (xadc->ops->flags & XADC_FLAGS_BUFFERED) { - ret = iio_triggered_buffer_setup(indio_dev, - &iio_pollfunc_store_time, &xadc_trigger_handler, - &xadc_buffer_ops); + ret = devm_iio_triggered_buffer_setup(dev, indio_dev, + &iio_pollfunc_store_time, + &xadc_trigger_handler, + &xadc_buffer_ops); if (ret) return ret; xadc->convst_trigger = xadc_alloc_trigger(indio_dev, "convst"); - if (IS_ERR(xadc->convst_trigger)) { - ret = PTR_ERR(xadc->convst_trigger); - goto err_triggered_buffer_cleanup; - } + if (IS_ERR(xadc->convst_trigger)) + return PTR_ERR(xadc->convst_trigger); + xadc->samplerate_trigger = xadc_alloc_trigger(indio_dev, "samplerate"); - if (IS_ERR(xadc->samplerate_trigger)) { - ret = PTR_ERR(xadc->samplerate_trigger); - goto err_free_convst_trigger; - } + if (IS_ERR(xadc->samplerate_trigger)) + return PTR_ERR(xadc->samplerate_trigger); } xadc->clk = devm_clk_get(dev, NULL); - if (IS_ERR(xadc->clk)) { - ret = PTR_ERR(xadc->clk); - goto err_free_samplerate_trigger; - } + if (IS_ERR(xadc->clk)) + return PTR_ERR(xadc->clk); ret = clk_prepare_enable(xadc->clk); if (ret) - goto err_free_samplerate_trigger; + return ret; + + ret = devm_add_action_or_reset(dev, + xadc_clk_disable_unprepare, xadc->clk); + if (ret) + return ret; /* * Make sure not to exceed the maximum samplerate since otherwise the @@ -1272,22 +1284,28 @@ static int xadc_probe(struct platform_device *pdev) if (xadc->ops->flags & XADC_FLAGS_BUFFERED) { ret = xadc_read_samplerate(xadc); if (ret < 0) - goto err_free_samplerate_trigger; + return ret; + if (ret > XADC_MAX_SAMPLERATE) { ret = xadc_write_samplerate(xadc, XADC_MAX_SAMPLERATE); if (ret < 0) - goto err_free_samplerate_trigger; + return ret; } } - ret = request_irq(xadc->irq, xadc->ops->interrupt_handler, 0, - dev_name(dev), indio_dev); + ret = devm_request_irq(dev, xadc->irq, xadc->ops->interrupt_handler, 0, + dev_name(dev), indio_dev); + if (ret) + return ret; + + ret = devm_add_action_or_reset(dev, xadc_cancel_delayed_work, + &xadc->zynq_unmask_work); if (ret) - goto err_clk_disable_unprepare; + return ret; ret = xadc->ops->setup(pdev, indio_dev, xadc->irq); if (ret) - goto err_free_irq; + return ret; for (i = 0; i < 16; i++) xadc_read_adc_reg(xadc, XADC_REG_THRESHOLD(i), @@ -1295,7 +1313,7 @@ static int xadc_probe(struct platform_device *pdev) ret = xadc_write_adc_reg(xadc, XADC_REG_CONF0, conf0); if (ret) - goto err_free_irq; + return ret; bipolar_mask = 0; for (i = 0; i < indio_dev->num_channels; i++) { @@ -1305,17 +1323,18 @@ static int xadc_probe(struct platform_device *pdev) ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(0), bipolar_mask); if (ret) - goto err_free_irq; + return ret; + ret = xadc_write_adc_reg(xadc, XADC_REG_INPUT_MODE(1), bipolar_mask >> 16); if (ret) - goto err_free_irq; + return ret; /* Disable all alarms */ ret = xadc_update_adc_reg(xadc, XADC_REG_CONF1, XADC_CONF1_ALARM_MASK, XADC_CONF1_ALARM_MASK); if (ret) - goto err_free_irq; + return ret; /* Set thresholds to min/max */ for (i = 0; i < 16; i++) { @@ -1330,59 +1349,23 @@ static int xadc_probe(struct platform_device *pdev) ret = xadc_write_adc_reg(xadc, XADC_REG_THRESHOLD(i), xadc->threshold[i]); if (ret) - goto err_free_irq; + return ret; } /* Go to non-buffered mode */ xadc_postdisable(indio_dev); - ret = iio_device_register(indio_dev); + ret = devm_iio_device_register(dev, indio_dev); if (ret) - goto err_free_irq; + return ret; platform_set_drvdata(pdev, indio_dev); - return 0; - -err_free_irq: - free_irq(xadc->irq, indio_dev); - cancel_delayed_work_sync(&xadc->zynq_unmask_work); -err_clk_disable_unprepare: - clk_disable_unprepare(xadc->clk); -err_free_samplerate_trigger: - if (xadc->ops->flags & XADC_FLAGS_BUFFERED) - iio_trigger_free(xadc->samplerate_trigger); -err_free_convst_trigger: - if (xadc->ops->flags & XADC_FLAGS_BUFFERED) - iio_trigger_free(xadc->convst_trigger); -err_triggered_buffer_cleanup: - if (xadc->ops->flags & XADC_FLAGS_BUFFERED) - iio_triggered_buffer_cleanup(indio_dev); - - return ret; -} - -static int xadc_remove(struct platform_device *pdev) -{ - struct iio_dev *indio_dev = platform_get_drvdata(pdev); - struct xadc *xadc = iio_priv(indio_dev); - - iio_device_unregister(indio_dev); - if (xadc->ops->flags & XADC_FLAGS_BUFFERED) { - iio_trigger_free(xadc->samplerate_trigger); - iio_trigger_free(xadc->convst_trigger); - iio_triggered_buffer_cleanup(indio_dev); - } - free_irq(xadc->irq, indio_dev); - cancel_delayed_work_sync(&xadc->zynq_unmask_work); - clk_disable_unprepare(xadc->clk); - return 0; } static struct platform_driver xadc_driver = { .probe = xadc_probe, - .remove = xadc_remove, .driver = { .name = "xadc", .of_match_table = xadc_of_match_table,