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

Configure Feed

Select the types of activity you want to include in your feed.

iio: adc: ad7606: rework available attributes for SW channels

For SW mode, the oversampling and scales attributes are always present.
So, they can be implemented via a 'read_avail' hook in iio_info.

For HW mode, it's a bit tricky, as these attributes get assigned based on
GPIO definitions.

So, for SW mode, we define a separate AD7606_SW_CHANNEL() macro, and use
that for the SW channels.
And 'ad7606_info_os_range_and_debug' can be renamed to
'ad7606_info_sw_mode' as it is only used for SW mode.

For the 'read_avail' hook, we'll need to allocate the SW scales, so that
they are just returned userspace without any extra processing.
The allocation will happen when then ad7606_state struct is allocated.
The oversampling available parameters don't need any extra processing; they
can just be passed back to userspace (as they are).

Signed-off-by: Alexandru Ardelean <aardelean@baylibre.com>
Link: https://patch.msgid.link/20240919130444.2100447-6-aardelean@baylibre.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Alexandru Ardelean and committed by
Jonathan Cameron
94aab7a0 bbd478f2

+77 -7
+49 -3
drivers/iio/adc/ad7606.c
··· 512 512 return 0; 513 513 } 514 514 515 + static int ad7606_read_avail(struct iio_dev *indio_dev, 516 + struct iio_chan_spec const *chan, 517 + const int **vals, int *type, int *length, 518 + long info) 519 + { 520 + struct ad7606_state *st = iio_priv(indio_dev); 521 + struct ad7606_chan_scale *cs; 522 + unsigned int ch = 0; 523 + 524 + switch (info) { 525 + case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 526 + *vals = st->oversampling_avail; 527 + *length = st->num_os_ratios; 528 + *type = IIO_VAL_INT; 529 + 530 + return IIO_AVAIL_LIST; 531 + 532 + case IIO_CHAN_INFO_SCALE: 533 + if (st->sw_mode_en) 534 + ch = chan->address; 535 + 536 + cs = &st->chan_scales[ch]; 537 + *vals = cs->scale_avail_show; 538 + *length = cs->num_scales * 2; 539 + *type = IIO_VAL_INT_PLUS_MICRO; 540 + 541 + return IIO_AVAIL_LIST; 542 + } 543 + return -EINVAL; 544 + } 545 + 515 546 static const struct iio_buffer_setup_ops ad7606_buffer_ops = { 516 547 .postenable = &ad7606_buffer_postenable, 517 548 .predisable = &ad7606_buffer_predisable, ··· 560 529 .validate_trigger = &ad7606_validate_trigger, 561 530 }; 562 531 563 - static const struct iio_info ad7606_info_os_range_and_debug = { 532 + static const struct iio_info ad7606_info_sw_mode = { 564 533 .read_raw = &ad7606_read_raw, 565 534 .write_raw = &ad7606_write_raw, 535 + .read_avail = &ad7606_read_avail, 566 536 .debugfs_reg_access = &ad7606_reg_access, 567 - .attrs = &ad7606_attribute_group_os_and_range, 568 537 .validate_trigger = &ad7606_validate_trigger, 569 538 }; 570 539 ··· 595 564 if (!st->sw_mode_en) 596 565 return 0; 597 566 598 - indio_dev->info = &ad7606_info_os_range_and_debug; 567 + indio_dev->info = &ad7606_info_sw_mode; 599 568 600 569 return st->bops->sw_mode_config(indio_dev); 601 570 } ··· 607 576 int ch, ret; 608 577 609 578 for (ch = 0; ch < num_channels; ch++) { 579 + struct ad7606_chan_scale *cs; 580 + int i; 581 + 610 582 ret = st->chip_info->scale_setup_cb(st, ch); 611 583 if (ret) 612 584 return ret; 585 + 586 + cs = &st->chan_scales[ch]; 587 + 588 + if (cs->num_scales * 2 > AD760X_MAX_SCALE_SHOW) 589 + return dev_err_probe(st->dev, -ERANGE, 590 + "Driver error: scale range too big"); 591 + 592 + /* Generate a scale_avail list for showing to userspace */ 593 + for (i = 0; i < cs->num_scales; i++) { 594 + cs->scale_avail_show[i * 2] = 0; 595 + cs->scale_avail_show[i * 2 + 1] = cs->scale_avail[i]; 596 + } 613 597 } 614 598 615 599 return 0;
+28 -4
drivers/iio/adc/ad7606.h
··· 27 27 }, \ 28 28 } 29 29 30 + #define AD7606_SW_CHANNEL(num, bits) { \ 31 + .type = IIO_VOLTAGE, \ 32 + .indexed = 1, \ 33 + .channel = num, \ 34 + .address = num, \ 35 + .info_mask_separate = \ 36 + BIT(IIO_CHAN_INFO_RAW) | \ 37 + BIT(IIO_CHAN_INFO_SCALE), \ 38 + .info_mask_separate_available = \ 39 + BIT(IIO_CHAN_INFO_SCALE), \ 40 + .info_mask_shared_by_all = \ 41 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ 42 + .info_mask_shared_by_all_available = \ 43 + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \ 44 + .scan_index = num, \ 45 + .scan_type = { \ 46 + .sign = 's', \ 47 + .realbits = (bits), \ 48 + .storagebits = (bits) > 16 ? 32 : 16, \ 49 + .endianness = IIO_CPU, \ 50 + }, \ 51 + } 52 + 30 53 #define AD7605_CHANNEL(num) \ 31 54 AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW), \ 32 55 BIT(IIO_CHAN_INFO_SCALE), 0, 16) ··· 58 35 AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW), \ 59 36 BIT(IIO_CHAN_INFO_SCALE), \ 60 37 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), bits) 61 - 62 - #define AD7606_SW_CHANNEL(num, bits) \ 63 - AD760X_CHANNEL(num, BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),\ 64 - 0, BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), bits) 65 38 66 39 #define AD7616_CHANNEL(num) AD7606_SW_CHANNEL(num, 16) 67 40 ··· 90 71 /** 91 72 * struct ad7606_chan_scale - channel scale configuration 92 73 * @scale_avail pointer to the array which stores the available scales 74 + * @scale_avail_show a duplicate of 'scale_avail' which is readily formatted 75 + * such that it can be read via the 'read_avail' hook 93 76 * @num_scales number of elements stored in the scale_avail array 94 77 * @range voltage range selection, selects which scale to apply 95 78 */ 96 79 struct ad7606_chan_scale { 80 + #define AD760X_MAX_SCALES 16 81 + #define AD760X_MAX_SCALE_SHOW (AD760X_MAX_SCALES * 2) 97 82 const unsigned int *scale_avail; 83 + int scale_avail_show[AD760X_MAX_SCALE_SHOW]; 98 84 unsigned int num_scales; 99 85 unsigned int range; 100 86 };
drivers/iio/adc/ad7606_spi.