iio: buffer: use struct iio_scan_type to simplify code
authorDavid Lechner <dlechner@baylibre.com>
Thu, 30 May 2024 15:14:09 +0000 (10:14 -0500)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Tue, 4 Jun 2024 18:53:08 +0000 (19:53 +0100)
By using struct iio_scan_type, we can simplify the code by removing
lots of duplicate pointer dereferences. This make the code a bit easier
to read.

This also prepares for a future where channels may have more than one
scan_type.

Signed-off-by: David Lechner <dlechner@baylibre.com>
Reviewed-by: Nuno Sa <nuno.sa@analog.com>
Link: https://lore.kernel.org/r/20240530-iio-add-support-for-multiple-scan-types-v3-2-cbc4acea2cfa@baylibre.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/industrialio-buffer.c

index cec58a604d73a93b46dd4e2c5a64c92d1d4f31ab..08103a9e77f72a9566c4e140910cd3fafbae07c0 100644 (file)
@@ -366,7 +366,8 @@ static ssize_t iio_show_fixed_type(struct device *dev,
                                   char *buf)
 {
        struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
-       u8 type = this_attr->c->scan_type.endianness;
+       const struct iio_scan_type *scan_type = &this_attr->c->scan_type;
+       u8 type = scan_type->endianness;
 
        if (type == IIO_CPU) {
 #ifdef __LITTLE_ENDIAN
@@ -375,21 +376,21 @@ static ssize_t iio_show_fixed_type(struct device *dev,
                type = IIO_BE;
 #endif
        }
-       if (this_attr->c->scan_type.repeat > 1)
+       if (scan_type->repeat > 1)
                return sysfs_emit(buf, "%s:%c%d/%dX%d>>%u\n",
                       iio_endian_prefix[type],
-                      this_attr->c->scan_type.sign,
-                      this_attr->c->scan_type.realbits,
-                      this_attr->c->scan_type.storagebits,
-                      this_attr->c->scan_type.repeat,
-                      this_attr->c->scan_type.shift);
+                      scan_type->sign,
+                      scan_type->realbits,
+                      scan_type->storagebits,
+                      scan_type->repeat,
+                      scan_type->shift);
        else
                return sysfs_emit(buf, "%s:%c%d/%d>>%u\n",
                       iio_endian_prefix[type],
-                      this_attr->c->scan_type.sign,
-                      this_attr->c->scan_type.realbits,
-                      this_attr->c->scan_type.storagebits,
-                      this_attr->c->scan_type.shift);
+                      scan_type->sign,
+                      scan_type->realbits,
+                      scan_type->storagebits,
+                      scan_type->shift);
 }
 
 static ssize_t iio_scan_el_show(struct device *dev,
@@ -694,12 +695,16 @@ static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev,
                                             unsigned int scan_index)
 {
        const struct iio_chan_spec *ch;
+       const struct iio_scan_type *scan_type;
        unsigned int bytes;
 
        ch = iio_find_channel_from_si(indio_dev, scan_index);
-       bytes = ch->scan_type.storagebits / 8;
-       if (ch->scan_type.repeat > 1)
-               bytes *= ch->scan_type.repeat;
+       scan_type = &ch->scan_type;
+       bytes = scan_type->storagebits / 8;
+
+       if (scan_type->repeat > 1)
+               bytes *= scan_type->repeat;
+
        return bytes;
 }
 
@@ -1616,18 +1621,21 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
        if (channels) {
                /* new magic */
                for (i = 0; i < indio_dev->num_channels; i++) {
+                       const struct iio_scan_type *scan_type;
+
                        if (channels[i].scan_index < 0)
                                continue;
 
+                       scan_type = &channels[i].scan_type;
+
                        /* Verify that sample bits fit into storage */
-                       if (channels[i].scan_type.storagebits <
-                           channels[i].scan_type.realbits +
-                           channels[i].scan_type.shift) {
+                       if (scan_type->storagebits <
+                           scan_type->realbits + scan_type->shift) {
                                dev_err(&indio_dev->dev,
                                        "Channel %d storagebits (%d) < shifted realbits (%d + %d)\n",
-                                       i, channels[i].scan_type.storagebits,
-                                       channels[i].scan_type.realbits,
-                                       channels[i].scan_type.shift);
+                                       i, scan_type->storagebits,
+                                       scan_type->realbits,
+                                       scan_type->shift);
                                ret = -EINVAL;
                                goto error_cleanup_dynamic;
                        }