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

iio: adc: ina2xx: Actually align the loop with the conversion ready flag

Currently, the registers are read out once per conversion interval. If
the reading is delayed as the conversion has not yet finished, this extra
time is treated as being part of the readout, although it should delay
the start of the poll interval. This results in the interval starting
slightly earlier in each iteration, until all time between reads is
spent polling the status registers instead of sleeping.

To fix this, the delay has to account for the state of the conversion
ready flag. Whenever the conversion is already finished, schedule the next
read on the regular interval, otherwise schedule it one interval after the
flag bit has been set.

Split the work function in two functions, one for the status poll and one
for reading the values, to be able to note down the time when the flag
bit is raised.

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Stefan Brüns and committed by
Greg Kroah-Hartman
9273aa16 8c3a7b0a

+38 -19
+38 -19
drivers/iio/adc/ina2xx-adc.c
··· 699 699 IIO_CHAN_SOFT_TIMESTAMP(4), 700 700 }; 701 701 702 - static int ina2xx_work_buffer(struct iio_dev *indio_dev) 702 + static int ina2xx_conversion_ready(struct iio_dev *indio_dev) 703 703 { 704 704 struct ina2xx_chip_info *chip = iio_priv(indio_dev); 705 - /* data buffer needs space for channel data and timestap */ 706 - unsigned short data[4 + sizeof(s64)/sizeof(short)]; 707 - int bit, ret, i = 0; 708 - s64 time; 705 + int ret; 709 706 unsigned int alert; 710 707 711 708 /* ··· 716 719 * For now, we do an extra read of the MASK_ENABLE register (INA226) 717 720 * resp. the BUS_VOLTAGE register (INA219). 718 721 */ 719 - if (!chip->allow_async_readout) 720 - do { 721 - if (chip->config->chip_id == ina226) { 722 - ret = regmap_read(chip->regmap, 723 - INA226_MASK_ENABLE, &alert); 724 - alert &= INA226_CVRF; 725 - } else { 726 - ret = regmap_read(chip->regmap, 727 - INA2XX_BUS_VOLTAGE, &alert); 728 - alert &= INA219_CNVR; 729 - } 722 + if (chip->config->chip_id == ina226) { 723 + ret = regmap_read(chip->regmap, 724 + INA226_MASK_ENABLE, &alert); 725 + alert &= INA226_CVRF; 726 + } else { 727 + ret = regmap_read(chip->regmap, 728 + INA2XX_BUS_VOLTAGE, &alert); 729 + alert &= INA219_CNVR; 730 + } 730 731 731 - if (ret < 0) 732 - return ret; 732 + if (ret < 0) 733 + return ret; 733 734 734 - } while (!alert); 735 + return !!alert; 736 + } 737 + 738 + static int ina2xx_work_buffer(struct iio_dev *indio_dev) 739 + { 740 + struct ina2xx_chip_info *chip = iio_priv(indio_dev); 741 + /* data buffer needs space for channel data and timestap */ 742 + unsigned short data[4 + sizeof(s64)/sizeof(short)]; 743 + int bit, ret, i = 0; 744 + s64 time; 735 745 736 746 time = iio_get_time_ns(indio_dev); 737 747 ··· 782 778 ktime_get_ts64(&next); 783 779 784 780 do { 781 + while (!chip->allow_async_readout) { 782 + ret = ina2xx_conversion_ready(indio_dev); 783 + if (ret < 0) 784 + return ret; 785 + 786 + /* 787 + * If the conversion was not yet finished, 788 + * reset the reference timestamp. 789 + */ 790 + if (ret == 0) 791 + ktime_get_ts64(&next); 792 + else 793 + break; 794 + } 795 + 785 796 ret = ina2xx_work_buffer(indio_dev); 786 797 if (ret < 0) 787 798 return ret;