Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: adc: add helpers for parsing ADC nodes

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.

A common way of marking pins that can be used as ADC inputs is to add
corresponding channel@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 <mazziesaccount@gmail.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Marcelo Schmitt <marcelo.schmitt1@gmail.com>
Link: https://patch.msgid.link/f1d8b3e15237947738912c0d297b3e1e21d8b03e.1742560649.git.mazziesaccount@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Matti Vaittinen and committed by
Jonathan Cameron
f3a8f870 f2430363

+114
+3
drivers/iio/adc/Kconfig
··· 6 6 7 7 menu "Analog to digital converters" 8 8 9 + config IIO_ADC_HELPER 10 + tristate 11 + 9 12 config AB8500_GPADC 10 13 bool "ST-Ericsson AB8500 GPADC driver" 11 14 depends on AB8500_CORE && REGULATOR_AB8500
+2
drivers/iio/adc/Makefile
··· 3 3 # Makefile for IIO ADC drivers 4 4 # 5 5 6 + obj-$(CONFIG_IIO_ADC_HELPER) += industrialio-adc.o 7 + 6 8 # When adding new entries keep the list in alphabetical order 7 9 obj-$(CONFIG_AB8500_GPADC) += ab8500-gpadc.o 8 10 obj-$(CONFIG_AD_SIGMA_DELTA) += ad_sigma_delta.o
+82
drivers/iio/adc/industrialio-adc.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Helpers for parsing common ADC information from a firmware node. 4 + * 5 + * Copyright (c) 2025 Matti Vaittinen <mazziesaccount@gmail.com> 6 + */ 7 + 8 + #include <linux/device.h> 9 + #include <linux/errno.h> 10 + #include <linux/export.h> 11 + #include <linux/module.h> 12 + #include <linux/property.h> 13 + #include <linux/types.h> 14 + 15 + #include <linux/iio/adc-helpers.h> 16 + #include <linux/iio/iio.h> 17 + 18 + /** 19 + * devm_iio_adc_device_alloc_chaninfo_se - allocate and fill iio_chan_spec for ADC 20 + * 21 + * Scan the device node for single-ended ADC channel information. Channel ID is 22 + * expected to be found from the "reg" property. Allocate and populate the 23 + * iio_chan_spec structure corresponding to channels that are found. The memory 24 + * for iio_chan_spec structure will be freed upon device detach. 25 + * 26 + * @dev: Pointer to the ADC device. 27 + * @template: Template iio_chan_spec from which the fields of all 28 + * found and allocated channels are initialized. 29 + * @max_chan_id: Maximum value of a channel ID. Use negative value if no 30 + * checking is required. 31 + * @cs: Location where pointer to allocated iio_chan_spec 32 + * should be stored. 33 + * 34 + * Return: Number of found channels on success. Negative value to indicate 35 + * failure. Specifically, -ENOENT if no channel nodes were found. 36 + */ 37 + int devm_iio_adc_device_alloc_chaninfo_se(struct device *dev, 38 + const struct iio_chan_spec *template, 39 + int max_chan_id, 40 + struct iio_chan_spec **cs) 41 + { 42 + struct iio_chan_spec *chan_array, *chan; 43 + int num_chan, ret; 44 + 45 + num_chan = iio_adc_device_num_channels(dev); 46 + if (num_chan < 0) 47 + return num_chan; 48 + 49 + if (!num_chan) 50 + return -ENOENT; 51 + 52 + chan_array = devm_kcalloc(dev, num_chan, sizeof(*chan_array), 53 + GFP_KERNEL); 54 + if (!chan_array) 55 + return -ENOMEM; 56 + 57 + chan = &chan_array[0]; 58 + 59 + device_for_each_named_child_node_scoped(dev, child, "channel") { 60 + u32 ch; 61 + 62 + ret = fwnode_property_read_u32(child, "reg", &ch); 63 + if (ret) 64 + return ret; 65 + 66 + if (max_chan_id >= 0 && ch > max_chan_id) 67 + return -ERANGE; 68 + 69 + *chan = *template; 70 + chan->channel = ch; 71 + chan++; 72 + } 73 + 74 + *cs = chan_array; 75 + 76 + return num_chan; 77 + } 78 + EXPORT_SYMBOL_NS_GPL(devm_iio_adc_device_alloc_chaninfo_se, "IIO_DRIVER"); 79 + 80 + MODULE_LICENSE("GPL"); 81 + MODULE_AUTHOR("Matti Vaittinen <mazziesaccount@gmail.com>"); 82 + MODULE_DESCRIPTION("IIO ADC fwnode parsing helpers");
+27
include/linux/iio/adc-helpers.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-only */ 2 + 3 + /* 4 + * The industrial I/O ADC firmware property parsing helpers 5 + * 6 + * Copyright (c) 2025 Matti Vaittinen <mazziesaccount@gmail.com> 7 + */ 8 + 9 + #ifndef _INDUSTRIAL_IO_ADC_HELPERS_H_ 10 + #define _INDUSTRIAL_IO_ADC_HELPERS_H_ 11 + 12 + #include <linux/property.h> 13 + 14 + struct device; 15 + struct iio_chan_spec; 16 + 17 + static inline int iio_adc_device_num_channels(struct device *dev) 18 + { 19 + return device_get_named_child_node_count(dev, "channel"); 20 + } 21 + 22 + int devm_iio_adc_device_alloc_chaninfo_se(struct device *dev, 23 + const struct iio_chan_spec *template, 24 + int max_chan_id, 25 + struct iio_chan_spec **cs); 26 + 27 + #endif /* _INDUSTRIAL_IO_ADC_HELPERS_H_ */