From patchwork Mon Mar 3 11:33:02 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13998730 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 7D94FC282C6 for ; Mon, 3 Mar 2025 12:20:39 +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=jr7uWyzHUY99eCAGXzsJFETrGHISY9L/T3cghzdv3dw=; b=X4wD5EQKv9h4aJOalWxppcWd9Y WdjwnKOptzptJ5ciicjhhMInjmMGI83Ci0Ww0b3D1rvcWob1E9M3mmj/ioSQ+ZEX9/iWgvwciBPEH x+GwmZF0ey64K6pEoI2tValPISA4vvm6jrBOw3pvd6srVvQ6FwWMwD4RoEI2TbsCwkLf/UuCnDMQE ubEjgMzhtxl0sN85ggRDsNqnbt+AeZNGFci8esBIOlCpGg8f/gHB0VnD3QdUJsHkwR55F0W/VaklT G+xnLaayDotgDxorh1nlJ/S9cUnJh931zLOYTaiPgMKQuwPzBt86n80EIeHPfexvj50qlsDai7uvU Q/wzTuXA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tp4mW-00000000iwx-2jNY; Mon, 03 Mar 2025 12:20:24 +0000 Received: from mail-lj1-x234.google.com ([2a00:1450:4864:20::234]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tp42p-00000000a0m-0Z33 for linux-arm-kernel@lists.infradead.org; Mon, 03 Mar 2025 11:33:12 +0000 Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-3072f8dc069so47325841fa.3 for ; Mon, 03 Mar 2025 03:33:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741001589; x=1741606389; 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=jr7uWyzHUY99eCAGXzsJFETrGHISY9L/T3cghzdv3dw=; b=NFUcSKQFupoEkVzZetO1vg1yL57PKxVOldel+6csI494xkxtYL8+L21Jja2dSoArg0 xghDKJjW1ZSAWyDq62hpO9DCPdwNUGpCYT83qRlMkB+GDfrqof1eqP+6ewjuXeXv2ZRf Q9gUgiXuY38uJnHMo61ex8Mla6s2ukSmRnX91bU/H/amu/cmRCSAo4ozPq8ge+IaJgtj Z1RAxsySuNQoIjYxkhP5ICLf1RVKKjQf8BPQIYBhZBW0tr+2zN6bgX97sqXDQ4sx/Z/E rnuciTAoCGjCfC7fdDZfcdmQEtPEj0LwpkPJcVoSeChZgfXBNq1Gb+3+rWmOwATmhe03 i5TQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741001589; x=1741606389; 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=jr7uWyzHUY99eCAGXzsJFETrGHISY9L/T3cghzdv3dw=; b=peAxfR9PzXTb9dMW3hnnfLBxcugSMbsdBUTxO8DcIY+jtzYcKoM/NR1PspU8Yf6GF3 tuj8yqJtKdGgAGKkdMslrUg07YSRif3dEpjIitCNEi0EGV2uczznU2H0CU1QqkEFDX9a vOuol1TlPQWAJn+pq4S8d4UNkJWYAA/Ub/YDTsPC5vaoMWbTiDgfnY0SCcKeYk58Suq+ 9BPUYzSRPY39cvp3q5QEYeNHjpfHtdUfx2EQsT4iYvmZL0q6Q9FA6s262n/9SRdATNjg ZsLjoet+ndEaty7zuFSV0Q6/QH+ilGz3XHvBO5ck+06rB1jPbu0lkxo/mcJJZ3wMM4xe /Ggg== X-Forwarded-Encrypted: i=1; AJvYcCV0tplCORKVA8qf8AFDEOR6GUy/1QXqGjsTyJpOkPM1dLHuyXnZJiQLw7eD8iP58FJSmAsYCOEPR1K3HtqBVQa2@lists.infradead.org X-Gm-Message-State: AOJu0YyxGMrOMYSiJHn06912jz12ctxJC6AOiJgOEix0vb8z2zKd8OkO FqttZYL2rk4u8/dRzTg9oQYPfwdO7FhECdak6kdHPiyvlUWUTa8g X-Gm-Gg: ASbGncur+bE/kaOBlgV9p2cfw0T0ox5fOeFLUoh5sjDO6IeVJd1I8/3v2/FjOKiffcV WkJ3sM6hX39jn3kXQh63bG4fqfhVjc/K1XafldP37wml5e2uRxBdDyrYKXPL15n8wrMxNtiJfAi MHCGQh57A7E/3+HDRuWfmrjm5MOhFnoHJFrm8yL04+cuQvs1sIWku23Gk0rpvDNRFYqui2VlwJp NqE+XCsY3TNQRZOQWJ4Y0ogDQf/rAvl+RY8kCVE49n0Ydc8/CslgdM79Ns7AGuUlbbMrp/ctyCx eRX3uefydN9VZZJvjg64ADXKjoe0L6l2Amvfmo5v7DXrKjBJN4h3DnjC4DAsFoMs2868MdKDzBQ Dr4yMRdaUvd8= X-Google-Smtp-Source: AGHT+IGzOR5aUmDt+a/MXoTy+5OLlJ65L1UVO+lQK86VsDtTaAuu0JJFF79k+4B/QbjhjBDZ83bKrA== X-Received: by 2002:a05:6512:398e:b0:545:3031:40aa with SMTP id 2adb3069b0e04-5494c129d52mr4959258e87.9.1741001588885; Mon, 03 Mar 2025 03:33:08 -0800 (PST) Received: from mva-rohm (mobile-access-2e8451-125.dhcp.inet.fi. [46.132.81.125]) by smtp.gmail.com with ESMTPSA id 2adb3069b0e04-5494ab017d0sm1112555e87.84.2025.03.03.03.33.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Mar 2025 03:33:07 -0800 (PST) Date: Mon, 3 Mar 2025 13:33:02 +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 v5 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-20250303_033311_173742_FAA02318 X-CRM114-Status: GOOD ( 22.54 ) 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() -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" -propereties 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: 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");