From patchwork Mon Mar 3 11:32:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13998729 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 386F7C282C5 for ; Mon, 3 Mar 2025 12:17:25 +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=jtBChGL3VlgSfzXhFjLcRU34BzOSwfeij0YOo0Xrd7Y=; b=OpFp91C890GkzIQkPUnzUcwtyT e3HM1GxVvnT5BuP+CARSHYLuOleKV8V6+wDLjCJz72yaijD7j6+MiMpzbsvjycQDXEUyZjxiXffMy q69oI6sE8ky/c8kSPFssf+8n0pn/gRsCQ2xMapynTXEin4zP072nt6QUedbYBvacfz/bIqmpHJNhO /kahA4yiL5hN8Rn3DzlsUTUFe+KhvRXeNTQIZ4sUg7+zlkXMLMRMkDPXr3w9hbaq6/hhNfhLC47kE 3bl/lJnrpEZdK7nPox7Ud5GKkiJxQMdGU+tbEy13tnzdZCS4gwSQ2VwNEAlqvULc7xqrU1Gp9Uw05 a1kk4Hnw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tp4jS-00000000iXN-3V1X; Mon, 03 Mar 2025 12:17:14 +0000 Received: from mail-lj1-x233.google.com ([2a00:1450:4864:20::233]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tp421-00000000ZiH-3tYd for linux-arm-kernel@lists.infradead.org; Mon, 03 Mar 2025 11:32:23 +0000 Received: by mail-lj1-x233.google.com with SMTP id 38308e7fff4ca-30615661f98so44993701fa.2 for ; Mon, 03 Mar 2025 03:32:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741001540; x=1741606340; 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=jtBChGL3VlgSfzXhFjLcRU34BzOSwfeij0YOo0Xrd7Y=; b=AbeMgeZOUNnBglMhmi6bSdjI59/jXAtT6QZ1rHLy9yR8NrnFyaLJhnaxPcDoKhRw/E 84s1gSzNFyLQiB1tv7tF8Cbw/1c321MLm/FARikFwCvEdCx9cUaPL6T0yXws3CN0qFYf iIEoPpSfZw2MEdCIZ3/RGpJMZ+ZoNvJ6CRf2sTbOZLTa9Vxh8B4ikVbqSfwXwfxRFGnz JurvGTt8lZmDZkPmfG6lVXQvjb8pA39V3ohK4drmUBN4QgdDi7wN9McBqwAMkeW2Dkw9 vGaDgx+fOi6RPbg2ESqRWk1LRbYo44TocUq3prlv3X3wa3jWDS0TAREPiZoQO9qPZkZX ATOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741001540; x=1741606340; 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=jtBChGL3VlgSfzXhFjLcRU34BzOSwfeij0YOo0Xrd7Y=; b=W149ERLT91WuEkzLUsu6z2tg9KosDXbC+pmPnWLpH+hTYHRPR7YJzs9Vfu42ur2Nw6 sl90qKx+flkLJHUrF1jwwd5oNHCh5k+BADdrO5VXD1QY4IuGqozAJQKBWC0DxObjBfo+ 9QGlpVRwsxQswLx/gRiMGjamtekCUdV8ZM6+v4jUsaYdx1qabPSYr9n5QlRmEAooU6he k3yPUFxUnc+9w3vkF+rjUWh7fXE7dMjtDPCRqxxGVHuVamlb7lg8YAIIIOgOhg/nJ2kt dT0lcQIKD9AJYrRd8+X/tVHPXt+KJh8iv2vJ39uEYrwDAuxZ65wxba7EHClsKK4USMOO 3NZA== X-Forwarded-Encrypted: i=1; AJvYcCUISXnYgyQMJZ2wB6udC534kuIzqhqVDs1BEkQ4tSrb6QF7+9hk/MHEe9BCnuXAo5h1IGgTmdiJkR07n0rUItAx@lists.infradead.org X-Gm-Message-State: AOJu0YzHCiP81tRCSQMhPThBJ3TGicm2dBNzF+sFRmlWrPeHLp+iVyxx DpyYvA3L1YjHVFrjo/cAnz1n9GVvWPjNJiQjub0q/RQWuZ2U9iN1 X-Gm-Gg: ASbGncvnmDzfGH4Z6w3CDLW/IEDWELTQkcoNb3OQtvZJiTU/jC/4S4ZQhWnnnV1cc7f L4pNHUDXDIHXLDGjB85g8uk5+tk63YIO9ec8fRWiyZeOjUQZsxkD68yKGYj3YqkPiQLdb0iP13m a7Fzystv1WgghhAbItNpRZZmFAAY15dczkL2BxW2jbxV382GuMAIikLlZqDdlAafPiZHQgI5/BZ w6ot3Na3s9qgw14EjIKDF6CDgjRl2PYMVBOChCdG+usgn6ca+LwHyTGoOJQxO3ZAEN+QSoASZCx Q0liQZoew0EOBrCyn1XLlapHUMrXLmA2RGEaiDjC9sn294Rz5dzhqELH9k7yMLHwAJ42hooCwO0 aMiVylgwvH78= X-Google-Smtp-Source: AGHT+IFLEDrP5BMqyi/QYjEYMZ1pICg9oq6il8b+Tt5IsjAulwVVVhkiZFRC9rVJrJAglId4UhYVLA== X-Received: by 2002:a2e:bc23:0:b0:30b:bf18:91b2 with SMTP id 38308e7fff4ca-30bbf18952cmr9389191fa.3.1741001539821; Mon, 03 Mar 2025 03:32:19 -0800 (PST) Received: from mva-rohm (mobile-access-2e8451-125.dhcp.inet.fi. [46.132.81.125]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-30bbb7f2b69sm3425911fa.29.2025.03.03.03.32.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Mar 2025 03:32:18 -0800 (PST) Date: Mon, 3 Mar 2025 13:32:12 +0200 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , Andy Shevchenko , Matti Vaittinen , Lad Prabhakar , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Hugo Villeneuve , Nuno Sa , David Lechner , Javier Carrasco , Guillaume Stols , Dumitru Ceclan , Trevor Gamblin , Matteo Martelli , Alisa-Dariana Roman , Ramona Alexandra Nechita , AngeloGioacchino Del Regno , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, linux-renesas-soc@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev Subject: [PATCH v5 03/10] iio: adc: add helpers for parsing ADC nodes 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_033221_972493_D29ED61B X-CRM114-Status: GOOD ( 28.87 ) 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 There are ADC ICs which may have some of the AIN pins usable for other functions. These ICs may have some of the AIN pins wired so that they should not be used for ADC. (Preferred?) way for marking pins which can be used as ADC inputs is to add corresponding channels@N nodes in the device tree as described in the ADC binding yaml. Add couple of helper functions which can be used to retrieve the channel information from the device node. Signed-off-by: Matti Vaittinen --- Revision history: v4 => v5: - Inline iio_adc_device_num_channels() - Fix Indenting function parameters - Combine the max channel ID checks. v3 => v4: - Drop diff-channel support - Drop iio_adc_device_channels_by_property() - Add IIO_DEVICE namespace - Move industrialio-adc.o to top of the Makefile - Some styling as suggested by Andy - Re-consider included headers v2 => v3: Mostly based on review comments by Jonathan - Support differential and single-ended channels - Rename iio_adc_device_get_channels() as iio_adc_device_channels_by_property() - Improve spelling - Drop support for cases where DT comes from parent device's node - Decrease loop indent by reverting node name check conditions - Don't set 'chan->indexed' by number of channels to keep the interface consistent no matter how many channels are connected. - Fix ID range check and related comment RFC v1 => v2: - New patch --- drivers/iio/adc/Kconfig | 3 ++ drivers/iio/adc/Makefile | 2 + drivers/iio/adc/industrialio-adc.c | 82 ++++++++++++++++++++++++++++++ include/linux/iio/adc-helpers.h | 27 ++++++++++ 4 files changed, 114 insertions(+) create mode 100644 drivers/iio/adc/industrialio-adc.c create mode 100644 include/linux/iio/adc-helpers.h diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig index 849c90203071..37b70a65da6f 100644 --- a/drivers/iio/adc/Kconfig +++ b/drivers/iio/adc/Kconfig @@ -6,6 +6,9 @@ menu "Analog to digital converters" +config IIO_ADC_HELPER + tristate + config AB8500_GPADC bool "ST-Ericsson AB8500 GPADC driver" depends on AB8500_CORE && REGULATOR_AB8500 diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile index ee19afba62b7..1c410f483029 100644 --- a/drivers/iio/adc/Makefile +++ b/drivers/iio/adc/Makefile @@ -3,6 +3,8 @@ # Makefile for IIO ADC drivers # +obj-$(CONFIG_IIO_ADC_HELPER) += industrialio-adc.o + # When adding new entries keep the list in alphabetical order obj-$(CONFIG_AB8500_GPADC) += ab8500-gpadc.o obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o diff --git a/drivers/iio/adc/industrialio-adc.c b/drivers/iio/adc/industrialio-adc.c new file mode 100644 index 000000000000..7bdae5330224 --- /dev/null +++ b/drivers/iio/adc/industrialio-adc.c @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Helpers for parsing common ADC information from a firmware node. + * + * Copyright (c) 2025 Matti Vaittinen + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +/** + * devm_iio_adc_device_alloc_chaninfo_se - allocate and fill iio_chan_spec for ADC + * + * Scan the device node for single-ended ADC channel information. Channel ID is + * expected to be found from the "reg" property. Allocate and populate the + * iio_chan_spec structure corresponding to channels that are found. The memory + * for iio_chan_spec structure will be freed upon device detach. + * + * @dev: Pointer to the ADC device. + * @template: Template iio_chan_spec from which the fields of all + * found and allocated channels are initialized. + * @max_chan_id: Maximum value of a channel ID. Use -1 if no checking + * is required. + * @cs: Location where pointer to allocated iio_chan_spec + * should be stored. + * + * Return: Number of found channels on succes. Negative value to indicate + * failure. + */ +int devm_iio_adc_device_alloc_chaninfo_se(struct device *dev, + const struct iio_chan_spec *template, + int max_chan_id, + struct iio_chan_spec **cs) +{ + struct iio_chan_spec *chan_array, *chan; + int num_chan = 0, ret; + + num_chan = iio_adc_device_num_channels(dev); + if (num_chan < 1) + return num_chan; + + chan_array = devm_kcalloc(dev, num_chan, sizeof(*chan_array), + GFP_KERNEL); + if (!chan_array) + return -ENOMEM; + + chan = &chan_array[0]; + + device_for_each_child_node_scoped(dev, child) { + u32 ch; + + if (!fwnode_name_eq(child, "channel")) + continue; + + ret = fwnode_property_read_u32(child, "reg", &ch); + if (ret) + return ret; + + if (max_chan_id != -1 && ch > max_chan_id) + return -ERANGE; + + *chan = *template; + chan->channel = ch; + chan++; + } + + *cs = chan_array; + + return num_chan; +} +EXPORT_SYMBOL_NS_GPL(devm_iio_adc_device_alloc_chaninfo_se, "IIO_DRIVER"); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Matti Vaittinen "); +MODULE_DESCRIPTION("IIO ADC fwnode parsing helpers"); diff --git a/include/linux/iio/adc-helpers.h b/include/linux/iio/adc-helpers.h new file mode 100644 index 000000000000..403a70b109ec --- /dev/null +++ b/include/linux/iio/adc-helpers.h @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +/* + * The industrial I/O ADC firmware property parsing helpers + * + * Copyright (c) 2025 Matti Vaittinen + */ + +#ifndef _INDUSTRIAL_IO_ADC_HELPERS_H_ +#define _INDUSTRIAL_IO_ADC_HELPERS_H_ + +#include + +struct device; +struct iio_chan_spec; + +static inline int iio_adc_device_num_channels(struct device *dev) +{ + return device_get_child_node_count_named(dev, "channel"); +} + +int devm_iio_adc_device_alloc_chaninfo_se(struct device *dev, + const struct iio_chan_spec *template, + int max_chan_id, + struct iio_chan_spec **cs); + +#endif /* _INDUSTRIAL_IO_ADC_HELPERS_H_ */ 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"); From patchwork Mon Mar 3 11:34:06 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13998731 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 D5ECEC282C5 for ; Mon, 3 Mar 2025 12:22:08 +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=jKyByvqg+jAL9PKNdpeqdSWpvScxjw4N93b4UfEb/Uo=; b=4g9Olmn5Ur3AGcXb1DiTsmxFPe WtmojX66+G/UD2QFzn6hWXChtUWZDdweCvMdfzR5XDAhGAP4OzZG0toFEpKXxHx0G9cjn9A1kbyx5 NsuqFPwH8lRVRFZHLzqOjwcJea5F8rKLuj1cE0VwdTBimRsXJJfWwX7TfIhxsh5NEnEoWqveaguwP dCrLnFx0c/jyARnnHwduIvxHIB3w/8RIWD5KTHiQA9dHmYwxNdJv4CXdxG+BPTQOSQhP6jVyEmYcC CO5YzkZbeqJfS74uU0DAm8QF+oERGeydVbviStyfRiIDZbNnGhZZtb4TRBQyVN+lg3KMAPBSeIFGE a36C0lcA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1tp4o4-00000000jIe-380X; Mon, 03 Mar 2025 12:22:00 +0000 Received: from mail-lj1-x22c.google.com ([2a00:1450:4864:20::22c]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1tp43r-00000000aD1-1HuT for linux-arm-kernel@lists.infradead.org; Mon, 03 Mar 2025 11:34:16 +0000 Received: by mail-lj1-x22c.google.com with SMTP id 38308e7fff4ca-30bbbd0ea7eso7923671fa.1 for ; Mon, 03 Mar 2025 03:34:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741001653; x=1741606453; 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=jKyByvqg+jAL9PKNdpeqdSWpvScxjw4N93b4UfEb/Uo=; b=QFn551rPnrsKVlpyfXrUUtPNRDBIhaeZ3i3TMwFF06yUMFQFU/vomedLYStICqDUWd k7I12bygmJIed0U1uxOIjAuicf8RwWYO5lraThIZxfQGSyMD69yFm/8y2b9aAlbafIix 9ETNyjGeFhhXMtBeTEiZQeoj/69IvjBQ9wJpbopgK9NwNYKXU2jli3ifS1sdWLBAUwNe BlTVql4lec70w3qVAKM/xjVefWyfQJ4ADxvHD3isWGfzIgbcSaquOtJg5SIn9L4iSsct syaEnOFVcN6QFrnMvZ3zyaNu7wy/7Mp4MM6uDHeHlI5jrp+1mDdQ171++v3n9uzyDjcq OGtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741001653; x=1741606453; 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=jKyByvqg+jAL9PKNdpeqdSWpvScxjw4N93b4UfEb/Uo=; b=T+wHwbBD7P1Ek4rG/xdOs3nikCyJQTMTTYfHAS+pD5RvBD2lPaOGt2u5jaVajOLbZA Lu6cmcXntrtTJoYaWMqq6c4jCykpDopWKmHNTfMu3wMRjpxqR4XCWd6aNe6ADN12wxT1 o/oAqnhfJwdi7q0UCj6Ak/sXgFDggYVnwqchvDdm5mjwncJAz1pbQd31GzIC8nggV8Ff 7UP4y/zyi61fJ+7dNLV7STD5MuvjrLbgXaKtXFffqtt0ii8QKQzhv69IuUre/Di7zrjK SkOcWm7hzOVDdqaqGrZABd7IofVhzH/NfseczFLXtDcRG1Pk5nV/3Ahr6kts+2GNYIQi lWrw== X-Forwarded-Encrypted: i=1; AJvYcCUhWSsi7MwabwGj75df8BR8vFl7zfGUAC4/JKsRy0ArBf7uOhhyfawjRXTD1kTpucVQ0Is7VvZSWm/dCMgNGxCE@lists.infradead.org X-Gm-Message-State: AOJu0Yxi++n/BKWJQ5OMsH7wtxvUJ/7s63by4TvUzsOOnutEfhsIc3Xb tOVyNjTeNmRYaJYP33/Rrww7xjYMKgHT0549QjDtrSsPLQS0/6JB X-Gm-Gg: ASbGncuqJoxQI7P+BZWrZlWIG4YfSYOckNTA88V4OQ38/2uFolGIX4mMqi9cI68aRv+ MDGwwws+foDp0JZ8QqDiqZczJR6XCcYpx6XPImQ0ZlfKslwBridhsuw1Oaw8UJtRuqZtVt4tbeJ mJ6+uuw8nHqlJFxEtjt/K2PwMd8vWoQu1VhOW0kzM8ed6m9sU5kaYd/ZJ0M5MxBTfzluKM+VVOe Buom+Q3E31DCkVClT1Y0nhRLSFSt1UrRvvnmp6voBp53bnBQDbQrrdn+bSGHVR+R3GY+mtUC+49 DZV29tCrQXjBUYhczR/03ME8acNnR4PQ0kLsFh1Dzr+Be2Yz/chu4VBy0VW8kOdU10lU8Oa+KTI tMPeKuKe6WqA= X-Google-Smtp-Source: AGHT+IH2q/EQ50OKDdOvQmK3DgcD8QV9qiZg9HjaiRlsRxpoAX3Icmu9TOCjXHgkc5dFCtxPesT+sQ== X-Received: by 2002:a2e:9515:0:b0:307:dc1f:e465 with SMTP id 38308e7fff4ca-30b9326b162mr42530121fa.22.1741001653116; Mon, 03 Mar 2025 03:34:13 -0800 (PST) Received: from mva-rohm (mobile-access-2e8451-125.dhcp.inet.fi. [46.132.81.125]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-30b86875b76sm14035841fa.91.2025.03.03.03.34.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Mar 2025 03:34:12 -0800 (PST) Date: Mon, 3 Mar 2025 13:34:06 +0200 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , Andy Shevchenko , Matti Vaittinen , Lad Prabhakar , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Hugo Villeneuve , Nuno Sa , David Lechner , Javier Carrasco , Guillaume Stols , Dumitru Ceclan , Trevor Gamblin , Matteo Martelli , Alisa-Dariana Roman , Ramona Alexandra Nechita , AngeloGioacchino Del Regno , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-acpi@vger.kernel.org, linux-renesas-soc@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev Subject: [PATCH v5 08/10] MAINTAINERS: Add IIO ADC helpers Message-ID: <78c18b224cd18b1bd83eea51c7e52dcea28e3d50.1740993491.git.mazziesaccount@gmail.com> 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_033415_345490_2FCE9B15 X-CRM114-Status: GOOD ( 10.98 ) 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 Add undersigned as a maintainer for the IIO ADC helpers. Signed-off-by: Matti Vaittinen --- Revision history: RFC v1 => v2: - New patch --- MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index 8e0736dc2ee0..5b96fb864227 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -11208,6 +11208,13 @@ L: linux-media@vger.kernel.org S: Maintained F: drivers/media/rc/iguanair.c +IIO ADC HELPERS +M: Matti Vaittinen +L: linux-iio@vger.kernel.org +S: Maintained +F: drivers/iio/adc/industrialio-adc.c +F: include/linux/iio/adc-helpers.h + IIO BACKEND FRAMEWORK M: Nuno Sa R: Olivier Moysan