[RFC PATCH 3/4] iio: Allow channels to share storage elements

From: Alexandru Ardelean
Date: Fri Apr 24 2020 - 01:18:17 EST


From: Lars-Peter Clausen <lars@xxxxxxxxxx>

Currently each IIO channel has it's own storage element in the data stream
each with its own unique scan index. This works for a lot of use-cases,
but in some is not good enough to represent the hardware accurately. On
those devices multiple separate pieces of information are stored within the
same storage element and the storage element can't be further broken down
into multiple storage elements (e.g. because the data is not aligned).

This can for example be status bits stored in unused data bits. E.g. a
14-bit ADC that stores its data in a 16-bit word and uses the two
additional bits to convey status information like for example whether a
overrange condition has happened. Currently this kind of extra status
information is usually ignored and can only be used by applications that
have special knowledge about the connected device and its data layout.

In addition to that some might also have data channels with less than 8
bits that get packed into the same storage element.

Allow two or more channels to use the same scan index, if they their
storage element does have the same size.

Signed-off-by: Lars-Peter Clausen <lars@xxxxxxxxxx>
Signed-off-by: Alexandru Ardelean <alexandru.ardelean@xxxxxxxxxx>
---
drivers/iio/industrialio-core.c | 27 ++++++++++++++++++++-------
1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index f4daf19f2a3b..cdf59a51c917 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -1651,6 +1651,16 @@ static const struct file_operations iio_buffer_fileops = {
.compat_ioctl = compat_ptr_ioctl,
};

+static bool iio_chan_same_size(const struct iio_chan_spec *a,
+ const struct iio_chan_spec *b)
+{
+ if (a->scan_type.storagebits != b->scan_type.storagebits)
+ return false;
+ if (a->scan_type.repeat != b->scan_type.repeat)
+ return false;
+ return true;
+}
+
static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
{
int i, j;
@@ -1662,13 +1672,16 @@ static int iio_check_unique_scan_index(struct iio_dev *indio_dev)
for (i = 0; i < indio_dev->num_channels - 1; i++) {
if (channels[i].scan_index < 0)
continue;
- for (j = i + 1; j < indio_dev->num_channels; j++)
- if (channels[i].scan_index == channels[j].scan_index) {
- dev_err(&indio_dev->dev,
- "Duplicate scan index %d\n",
- channels[i].scan_index);
- return -EINVAL;
- }
+ for (j = i + 1; j < indio_dev->num_channels; j++) {
+ if (channels[i].scan_index != channels[j].scan_index)
+ continue;
+ if (iio_chan_same_size(&channels[i], &channels[j]))
+ continue;
+ dev_err(&indio_dev->dev,
+ "Duplicate scan index %d\n",
+ channels[i].scan_index);
+ return -EINVAL;
+ }
}

return 0;
--
2.17.1