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

iio:adxl372: Add sampling frequency support

This patch adds the option for the user to select the sampling frequency.
Also, the user can read the available frequencies and read the currently
set frequency via the read_raw function. The frequency can be set via the
write_raw function.

When the frequency is set, the bandwidth is also checked and ensured
that it is constrained to at most half of the sampling frequency. Also, the
activity and inactivity timers have to be updated because they depend on
the selected ODR.

Signed-off-by: Stefan Popa <stefan.popa@analog.com>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Stefan Popa and committed by
Jonathan Cameron
5e605a4d 1c412a32

+73 -1
+73 -1
drivers/iio/accel/adxl372.c
··· 223 223 .modified = 1, \ 224 224 .channel2 = IIO_MOD_##axis, \ 225 225 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 226 - .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 226 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \ 227 + BIT(IIO_CHAN_INFO_SAMP_FREQ), \ 227 228 .scan_index = index, \ 228 229 .scan_type = { \ 229 230 .sign = 's', \ ··· 310 309 st->odr = odr; 311 310 312 311 return ret; 312 + } 313 + 314 + static int adxl372_find_closest_match(const int *array, 315 + unsigned int size, int val) 316 + { 317 + int i; 318 + 319 + for (i = 0; i < size; i++) { 320 + if (val <= array[i]) 321 + return i; 322 + } 323 + 324 + return size - 1; 313 325 } 314 326 315 327 static int adxl372_set_bandwidth(struct adxl372_state *st, ··· 645 631 *val = 0; 646 632 *val2 = ADXL372_USCALE; 647 633 return IIO_VAL_INT_PLUS_MICRO; 634 + case IIO_CHAN_INFO_SAMP_FREQ: 635 + *val = adxl372_samp_freq_tbl[st->odr]; 636 + return IIO_VAL_INT; 637 + } 638 + 639 + return -EINVAL; 640 + } 641 + 642 + static int adxl372_write_raw(struct iio_dev *indio_dev, 643 + struct iio_chan_spec const *chan, 644 + int val, int val2, long info) 645 + { 646 + struct adxl372_state *st = iio_priv(indio_dev); 647 + int odr_index, ret; 648 + 649 + switch (info) { 650 + case IIO_CHAN_INFO_SAMP_FREQ: 651 + odr_index = adxl372_find_closest_match(adxl372_samp_freq_tbl, 652 + ARRAY_SIZE(adxl372_samp_freq_tbl), 653 + val); 654 + ret = adxl372_set_odr(st, odr_index); 655 + if (ret < 0) 656 + return ret; 657 + /* 658 + * The timer period depends on the ODR selected. 659 + * At 3200 Hz and below, it is 6.6 ms; at 6400 Hz, it is 3.3 ms 660 + */ 661 + ret = adxl372_set_activity_time_ms(st, st->act_time_ms); 662 + if (ret < 0) 663 + return ret; 664 + /* 665 + * The timer period depends on the ODR selected. 666 + * At 3200 Hz and below, it is 26 ms; at 6400 Hz, it is 13 ms 667 + */ 668 + ret = adxl372_set_inactivity_time_ms(st, st->inact_time_ms); 669 + if (ret < 0) 670 + return ret; 671 + /* 672 + * The maximum bandwidth is constrained to at most half of 673 + * the ODR to ensure that the Nyquist criteria is not violated 674 + */ 675 + if (st->bw > odr_index) 676 + ret = adxl372_set_bandwidth(st, odr_index); 677 + 678 + return ret; 648 679 default: 649 680 return -EINVAL; 650 681 } ··· 837 778 .set_trigger_state = adxl372_dready_trig_set_state, 838 779 }; 839 780 781 + static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("400 800 1600 3200 6400"); 782 + 783 + static struct attribute *adxl372_attributes[] = { 784 + &iio_const_attr_sampling_frequency_available.dev_attr.attr, 785 + NULL, 786 + }; 787 + 788 + static const struct attribute_group adxl372_attrs_group = { 789 + .attrs = adxl372_attributes, 790 + }; 791 + 840 792 static const struct iio_info adxl372_info = { 841 793 .validate_trigger = &adxl372_validate_trigger, 794 + .attrs = &adxl372_attrs_group, 842 795 .read_raw = adxl372_read_raw, 796 + .write_raw = adxl372_write_raw, 843 797 .debugfs_reg_access = &adxl372_reg_access, 844 798 .hwfifo_set_watermark = adxl372_set_watermark, 845 799 };