Skip to content

Commit d4adb69

Browse files
dlechnunojsa
authored andcommitted
iio: buffer: use struct iio_scan_type to simplify code
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>
1 parent b96f552 commit d4adb69

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

drivers/iio/industrialio-buffer.c

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ static ssize_t iio_show_fixed_type(struct device *dev,
400400
char *buf)
401401
{
402402
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
403-
u8 type = this_attr->c->scan_type.endianness;
403+
const struct iio_scan_type *scan_type = &this_attr->c->scan_type;
404+
u8 type = scan_type->endianness;
404405

405406
if (type == IIO_CPU) {
406407
#ifdef __LITTLE_ENDIAN
@@ -409,21 +410,21 @@ static ssize_t iio_show_fixed_type(struct device *dev,
409410
type = IIO_BE;
410411
#endif
411412
}
412-
if (this_attr->c->scan_type.repeat > 1)
413+
if (scan_type->repeat > 1)
413414
return sysfs_emit(buf, "%s:%c%d/%dX%d>>%u\n",
414415
iio_endian_prefix[type],
415-
this_attr->c->scan_type.sign,
416-
this_attr->c->scan_type.realbits,
417-
this_attr->c->scan_type.storagebits,
418-
this_attr->c->scan_type.repeat,
419-
this_attr->c->scan_type.shift);
416+
scan_type->sign,
417+
scan_type->realbits,
418+
scan_type->storagebits,
419+
scan_type->repeat,
420+
scan_type->shift);
420421
else
421422
return sysfs_emit(buf, "%s:%c%d/%d>>%u\n",
422423
iio_endian_prefix[type],
423-
this_attr->c->scan_type.sign,
424-
this_attr->c->scan_type.realbits,
425-
this_attr->c->scan_type.storagebits,
426-
this_attr->c->scan_type.shift);
424+
scan_type->sign,
425+
scan_type->realbits,
426+
scan_type->storagebits,
427+
scan_type->shift);
427428
}
428429

429430
static ssize_t iio_scan_el_show(struct device *dev,
@@ -735,12 +736,16 @@ static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev,
735736
unsigned int scan_index)
736737
{
737738
const struct iio_chan_spec *ch;
739+
const struct iio_scan_type *scan_type;
738740
unsigned int bytes;
739741

740742
ch = iio_find_channel_from_si(indio_dev, scan_index);
741-
bytes = ch->scan_type.storagebits / 8;
742-
if (ch->scan_type.repeat > 1)
743-
bytes *= ch->scan_type.repeat;
743+
scan_type = &ch->scan_type;
744+
bytes = scan_type->storagebits / 8;
745+
746+
if (scan_type->repeat > 1)
747+
bytes *= scan_type->repeat;
748+
744749
return bytes;
745750
}
746751

@@ -1889,18 +1894,21 @@ static int __iio_buffer_alloc_sysfs_and_mask(struct iio_buffer *buffer,
18891894
if (channels) {
18901895
/* new magic */
18911896
for (i = 0; i < indio_dev->num_channels; i++) {
1897+
const struct iio_scan_type *scan_type;
1898+
18921899
if (channels[i].scan_index < 0)
18931900
continue;
18941901

1902+
scan_type = &channels[i].scan_type;
1903+
18951904
/* Verify that sample bits fit into storage */
1896-
if (channels[i].scan_type.storagebits <
1897-
channels[i].scan_type.realbits +
1898-
channels[i].scan_type.shift) {
1905+
if (scan_type->storagebits <
1906+
scan_type->realbits + scan_type->shift) {
18991907
dev_err(&indio_dev->dev,
19001908
"Channel %d storagebits (%d) < shifted realbits (%d + %d)\n",
1901-
i, channels[i].scan_type.storagebits,
1902-
channels[i].scan_type.realbits,
1903-
channels[i].scan_type.shift);
1909+
i, scan_type->storagebits,
1910+
scan_type->realbits,
1911+
scan_type->shift);
19041912
ret = -EINVAL;
19051913
goto error_cleanup_dynamic;
19061914
}

0 commit comments

Comments
 (0)