From patchwork Thu Mar 13 07:18:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 14014520 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 E16D3C282DE for ; Thu, 13 Mar 2025 07:42:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=BFD+DcpWsMqfiQV+ZB7bRLNZX1LXyd6tXIeA6qREGzs=; b=FheKSmmFPJv0CKs8emRyiBHw3L TtX14IhZpd8SZQTlUhgWTjXtwQFvMBnKXt2RrjscMx+Hf7m1OlsjIdR2XnCfI3vGTW5lNsgEzB+Lx SYqKu0ZDp/Jog/lCoRSU2VjdeAvWQmQIyOwgy25tbwdayBtFcZEMQrh0wp5jSW0MkB7gqUSYOSctt OOYoQ7LgTXJgPAP1hIzNioLsA992lWM1hC+gPx70MhpZdW7iB0ZCcYY18TuJvZ1pfrL9IiqxG881E w4zX56LHNL3JPl9Tu3KeuSntAuSvp70KkC/rzbh12yXJNq4EJVs48GMxkGmg5qdMNYiIx1O6SF4Wv lImNXiDg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tsdCQ-0000000APPx-09uO; Thu, 13 Mar 2025 07:41:50 +0000 Received: from mail-lj1-x230.google.com ([2a00:1450:4864:20::230]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tscqH-0000000AL00-459P for linux-arm-kernel@lists.infradead.org; Thu, 13 Mar 2025 07:18:59 +0000 Received: by mail-lj1-x230.google.com with SMTP id 38308e7fff4ca-30c05fd126cso6057381fa.3 for ; Thu, 13 Mar 2025 00:18:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741850336; x=1742455136; darn=lists.infradead.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=BFD+DcpWsMqfiQV+ZB7bRLNZX1LXyd6tXIeA6qREGzs=; b=NVAgwM4dzx3qwSWzXa6jgBTEtHqJDLf3WJ6Cjvwk3WbVU6CnN67owCgVEDdXbN7obw 0hiYiEpGd9UTHOVDJleZJo3u/ikwjlP/PaF5p0hjnBDVGm7vm6bizYw3n3gGwX8jpusj bXa7zXvQZcLVDj6w8mQjQ9YkePZLvaCOkF0hwMxx4ZlQSY3Wwl2B+aSpXK4MVTLBnv/N vcWsK0yuLbDawNA7CkOOnN0UrkwY/5BLTD78cLtq+2Ml1mQPoWrqJ0lVFnQY6hvLbeo+ BAEGezBdv2AaOETj/KRBFUqod+fdrnimMFjWdWOLJpMDUAa02PhRyM+RQ8Jik7LwES3c onxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741850336; x=1742455136; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=BFD+DcpWsMqfiQV+ZB7bRLNZX1LXyd6tXIeA6qREGzs=; b=U/LgvukdDXTv8geOupPwOmtLgylafPQWNlvBl0+UeCK+PVpxLMwjxpYn/MnmAl0yKI 28qIQA5cvNvuJxZ1aosMdsCOz3/L/zKPqs3/wrgW1BY/BCppTOAw/VQeR7auuC5IyQXA h0VDWvjGI+xeOn3CWvlGMMEDZobrCJ4Z/VhSRIyE4avHwL4lp3tBK119VxqTMlfFJ9VZ X1daLjOKsWtczOTBfU0NkIu/zxcTdGkpQZgj+S5sHcsFrTmNjlUM9qCNuG+0HHyLCa6A oVDnMyTAJ0Z7CsxkcQUEHvvph7l4ToSIaxBjgRlNa1pKOm8ovPHUn+AkbS/MdBAdWeRw UJeQ== X-Forwarded-Encrypted: i=1; AJvYcCWlLB+y+o2ocFAqBz3w3GFMv0NiER09/bup+Da4uwLQIXgOiRTSKWyctTTRxtiugzJoyi32nLdhIizPcVvd3NWZ@lists.infradead.org X-Gm-Message-State: AOJu0YwFeIWgqmsRdjerDV6ohTxqyNLeIoI39xL+C9BwxkhH0cXwuaJM ohFgfW9D+aWOx8WywVg+b77IwFiSMSof2rbMIjZ9RPOVpIZVkWTw X-Gm-Gg: ASbGncs1L/VkmJSpDK5FnntWiLI+45qus7iSNx22QrvFCxGMxVpu9oPdNwUHNlGuc3i 1JkRlFCuLxRli6LaKHNzj/bE/bfXwpj3o/BtoV+vmQli2O85+TEgQmL7bWpCCGSsH1fKgtQ2O4r WDYoUFg0pmJ/NAHbT+JOvZYPTLXgd35VXAqREjEV8oREMs0hi3Y1GjTmUbV2G7kHYnwCn/kfH7w e9h/u3YDZlxZ45cED19KNH9tSvrBLmYPgwFm++kV2mEKfmG77N0bF3ZwhQCooVYkmWYOxtvr2He W/AmHWKKFBP2Gl/KOo55uoYeZ1gfPum1kmdNR6i5Az6rsfozXOxTEakgZMVeVQ== X-Google-Smtp-Source: AGHT+IHmLCwaOiAyND+HYla9YJ6Owuf2FrMyU+QZHQM+mLQ3MEh0Yx2JRunjCA8pnIPh1yD8DBMFSw== X-Received: by 2002:a2e:a543:0:b0:308:eb58:6591 with SMTP id 38308e7fff4ca-30bf460589fmr89888051fa.25.1741850335814; Thu, 13 Mar 2025 00:18:55 -0700 (PDT) Received: from mva-rohm ([2a10:a5c0:800d:dd00:8fdf:935a:2c85:d703]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-30c3f1ec0f4sm1170341fa.109.2025.03.13.00.18.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Mar 2025 00:18:54 -0700 (PDT) Date: Thu, 13 Mar 2025 09:18:49 +0200 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Nuno Sa , David Lechner , Javier Carrasco , Matti Vaittinen , Olivier Moysan , Guillaume Stols , Dumitru Ceclan , Trevor Gamblin , Matteo Martelli , Alisa-Dariana Roman , Andy Shevchenko , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev Subject: [PATCH v7 05/10] iio: adc: sun20i-gpadc: Use adc-helpers Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250313_001858_034796_32061C91 X-CRM114-Status: GOOD ( 22.72 ) 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 The new devm_iio_adc_device_alloc_chaninfo_se() -helper is intended to help drivers avoid open-coding the for_each_node -loop for getting the channel IDs. The helper provides standard way to detect the ADC channel nodes (by the node name), and a standard way to convert the "reg" -properties to channel identification numbers, used in the struct iio_chan_spec. Furthermore, the helper can optionally check the found channel IDs are smaller than given maximum. This is useful for callers which later use the IDs for example for indexing a channel data array. The original driver treated all found child nodes as channel nodes. The new helper requires channel nodes to be named channel[@N]. This should help avoid problems with devices which may contain also other but ADC child nodes. Quick grep from arch/* with the sun20i-gpadc's compatible string didn't reveal any in-tree .dts with channel nodes named otherwise. Also, same grep shows all the in-tree .dts seem to have channel IDs between 0..num of channels. Use the new helper. Signed-off-by: Matti Vaittinen --- Revision history: v6 => v7: - Fix function name in the commit message v5 => v6: - Commit message typofix v4 => v5: - Drop the diff-channel stuff from the commit message v3 => v4: - Adapt to 'drop diff-channel support' changes to ADC-helpers - select ADC helpers in the Kconfig v2 => v3: - New patch I picked the sun20i-gpadc in this series because it has a straightforward approach for populating the struct iio_chan_spec. Everything else except the .channel can use 'template'-data. This makes the sun20i-gpadc well suited to be an example user of this new helper. I hope this patch helps to evaluate whether these helpers are worth the hassle. The change is compile tested only!! Testing before applying is highly appreciated (as always!). Also, even though I tried to audit the dts files for the reg-properties in the channel nodes, use of references didn't make it easy. I can't guarantee I didn't miss anything. --- drivers/iio/adc/Kconfig | 1 + drivers/iio/adc/sun20i-gpadc-iio.c | 38 ++++++++++++------------------ 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index e4933de0c366..0993008a1586 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -1357,6 +1357,7 @@ config SUN4I_GPADC config SUN20I_GPADC tristate "Allwinner D1/T113s/T507/R329 and similar GPADCs driver" depends on ARCH_SUNXI || COMPILE_TEST + select IIO_ADC_HELPER help Say yes here to build support for Allwinner (D1, T113, T507 and R329) SoCs GPADC. This ADC provides up to 16 channels. diff --git a/drivers/iio/adc/sun20i-gpadc-iio.c b/drivers/iio/adc/sun20i-gpadc-iio.c index 136b8d9c294f..bf1db2a3de9b 100644 --- a/drivers/iio/adc/sun20i-gpadc-iio.c +++ b/drivers/iio/adc/sun20i-gpadc-iio.c @@ -15,6 +15,7 @@ #include #include +#include #include #define SUN20I_GPADC_DRIVER_NAME "sun20i-gpadc" @@ -149,37 +150,27 @@ static void sun20i_gpadc_reset_assert(void *data) reset_control_assert(rst); } +static const struct iio_chan_spec sun20i_gpadc_chan_template = { + .type = IIO_VOLTAGE, + .indexed = 1, + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), +}; + static int sun20i_gpadc_alloc_channels(struct iio_dev *indio_dev, struct device *dev) { - unsigned int channel; - int num_channels, i, ret; + int num_channels; struct iio_chan_spec *channels; - num_channels = device_get_child_node_count(dev); + num_channels = devm_iio_adc_device_alloc_chaninfo_se(dev, + &sun20i_gpadc_chan_template, -1, &channels); + if (num_channels < 0) + return num_channels; + if (num_channels == 0) return dev_err_probe(dev, -ENODEV, "no channel children\n"); - channels = devm_kcalloc(dev, num_channels, sizeof(*channels), - GFP_KERNEL); - if (!channels) - return -ENOMEM; - - i = 0; - device_for_each_child_node_scoped(dev, node) { - ret = fwnode_property_read_u32(node, "reg", &channel); - if (ret) - return dev_err_probe(dev, ret, "invalid channel number\n"); - - channels[i].type = IIO_VOLTAGE; - channels[i].indexed = 1; - channels[i].channel = channel; - channels[i].info_mask_separate = BIT(IIO_CHAN_INFO_RAW); - channels[i].info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE); - - i++; - } - indio_dev->channels = channels; indio_dev->num_channels = num_channels; @@ -271,3 +262,4 @@ module_platform_driver(sun20i_gpadc_driver); MODULE_DESCRIPTION("ADC driver for sunxi platforms"); MODULE_AUTHOR("Maksim Kiselev "); MODULE_LICENSE("GPL"); +MODULE_IMPORT_NS("IIO_DRIVER");