mbox series

[v11,00/15] iio: afe: add temperature rescaling support

Message ID 20211222034646.222189-1-liambeguin@gmail.com (mailing list archive)
Headers show
Series iio: afe: add temperature rescaling support | expand

Message

Liam Beguin Dec. 22, 2021, 3:46 a.m. UTC
Jonathan, Peter, Andy,

I left out IIO_VAL_INT overflows for now, so that I can focus on getting
the rest of these changes pulled in, but I don't mind adding a patch for
that later on.

This series focuses on adding temperature rescaling support to the IIO
Analog Front End (AFE) driver.

The first few patches address minor bugs in IIO inkernel functions, and
prepare the AFE driver for the additional features.

The main changes to the AFE driver include an initial Kunit test suite,
support for IIO_VAL_INT_PLUS_{NANO,MICRO} scales, and support for RTDs
and temperature transducer sensors.

I applied Peter's Reviewed-by only on the unchanged commits, I wasn't
sure it would be okay to do so on the whole series with the few
changes in v11.

Thanks for your time,
Liam

Changes since v10:
- apply Andy's suggestion for offset calculations
- make use of units.h more consistently

Changes since v9:
- make use of linux/units.h
- reorder commits, fix fract_log2 before merging fract
- keep fractional representation when not overflowing

Changes since v8:
- reword comment
- fix erroneous 64-bit division
- optimize and use 32-bit divisions when values are know to not overflow
- keep IIO_VAL_FRACTIONAL scale when possible, if not default to fixed
  point
- add test cases
- use nano precision in test cases
- simplify offset calculation in rtd_props()

Changes since v7:
- drop gcd() logic in rescale_process_scale()
- use div_s64() instead of do_div() for signed 64-bit divisions
- combine IIO_VAL_FRACTIONAL and IIO_VAL_FRACTIONAL_LOG2 scale cases
- switch to INT_PLUS_NANO when accuracy is lost with FRACTIONAL scales
- rework test logic to allow for small relative error
- rename test variables to align error output messages

Changes since v6:
- rework IIO_VAL_INT_PLUS_{NANO,MICRO} based on Peter's suggestion
- combine IIO_VAL_INT_PLUS_{NANO,MICRO} cases
- add test cases for negative IIO_VAL_INT_PLUS_{NANO,MICRO} corner cases
- force use of positive integers with gcd()
- reduce risk of integer overflow in IIO_VAL_FRACTIONAL_LOG2
- fix duplicate symbol build error
- apply Reviewed-by

Changes since v5:
- add include/linux/iio/afe/rescale.h
- expose functions use to process scale and offset
- add basic iio-rescale kunit test cases
- fix integer overflow case
- improve precision for IIO_VAL_FRACTIONAL_LOG2

Changes since v4:
- only use gcd() when necessary in overflow mitigation
- fix INT_PLUS_{MICRO,NANO} support
- apply Reviewed-by
- fix temperature-transducer bindings

Changes since v3:
- drop unnecessary fallthrough statements
- drop redundant local variables in some calculations
- fix s64 divisions on 32bit platforms by using do_div
- add comment describing iio-rescaler offset calculation
- drop unnecessary MAINTAINERS entry

Changes since v2:
- don't break implicit offset truncations
- make a best effort to get a valid value for fractional types
- drop return value change in iio_convert_raw_to_processed_unlocked()
- don't rely on processed value for offset calculation
- add INT_PLUS_{MICRO,NANO} support in iio-rescale
- revert generic implementation in favor of temperature-sense-rtd and
  temperature-transducer
- add separate section to MAINTAINERS file

Changes since v1:
- rebase on latest iio `testing` branch
- also apply consumer scale on integer channel scale types
- don't break implicit truncation in processed channel offset
  calculation
- drop temperature AFE flavors in favor of a simpler generic
  implementation

Liam Beguin (15):
  iio: inkern: apply consumer scale on IIO_VAL_INT cases
  iio: inkern: apply consumer scale when no channel scale is available
  iio: inkern: make a best effort on offset calculation
  iio: afe: rescale: expose scale processing function
  iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support
  iio: afe: rescale: add offset support
  iio: afe: rescale: use s64 for temporary scale calculations
  iio: afe: rescale: fix accuracy for small fractional scales
  iio: afe: rescale: reduce risk of integer overflow
  iio: afe: rescale: make use of units.h
  iio: test: add basic tests for the iio-rescale driver
  iio: afe: rescale: add RTD temperature sensor support
  iio: afe: rescale: add temperature transducers
  dt-bindings: iio: afe: add bindings for temperature-sense-rtd
  dt-bindings: iio: afe: add bindings for temperature transducers

 .../iio/afe/temperature-sense-rtd.yaml        | 101 +++
 .../iio/afe/temperature-transducer.yaml       | 114 +++
 drivers/iio/afe/iio-rescale.c                 | 291 +++++++-
 drivers/iio/inkern.c                          |  40 +-
 drivers/iio/test/Kconfig                      |  10 +
 drivers/iio/test/Makefile                     |   1 +
 drivers/iio/test/iio-test-rescale.c           | 705 ++++++++++++++++++
 include/linux/iio/afe/rescale.h               |  34 +
 8 files changed, 1248 insertions(+), 48 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/iio/afe/temperature-sense-rtd.yaml
 create mode 100644 Documentation/devicetree/bindings/iio/afe/temperature-transducer.yaml
 create mode 100644 drivers/iio/test/iio-test-rescale.c
 create mode 100644 include/linux/iio/afe/rescale.h

Range-diff against v10:
 -:  ------------ >  1:  ae3cc93baee6 iio: inkern: apply consumer scale on IIO_VAL_INT cases
 -:  ------------ >  2:  06f66e7f7403 iio: inkern: apply consumer scale when no channel scale is available
 1:  2dbf6b3bbaeb !  3:  1717b82460c0 iio: inkern: make a best effort on offset calculation
    @@ drivers/iio/inkern.c: EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
     +			offset_val /= offset_val2;
     +			break;
     +		case IIO_VAL_FRACTIONAL_LOG2:
    -+			offset_val /= (1 << offset_val2);
    ++			offset_val >>= offset_val2;
     +			break;
     +		default:
     +			return -EINVAL;
 2:  b083cf307268 =  4:  6fc26588f651 iio: afe: rescale: expose scale processing function
 3:  f46b59690e46 =  5:  8e63c4036157 iio: afe: rescale: add INT_PLUS_{MICRO,NANO} support
 4:  80701b87cdf4 !  6:  eea57faec241 iio: afe: rescale: add offset support
    @@ drivers/iio/afe/iio-rescale.c: int rescale_process_scale(struct rescale *rescale
     +		*val = div_s64(tmp, scale) + schan_off;
     +		return IIO_VAL_INT;
     +	case IIO_VAL_INT_PLUS_NANO:
    -+		tmp = (s64)rescale->offset * 1000000000;
    -+		tmp2 = ((s64)scale * 1000000000L) + scale2;
    ++		tmp = (s64)rescale->offset * NANO;
    ++		tmp2 = ((s64)scale * NANO) + scale2;
     +		*val = div64_s64(tmp, tmp2) + schan_off;
     +		return IIO_VAL_INT;
     +	case IIO_VAL_INT_PLUS_MICRO:
    -+		tmp = (s64)rescale->offset * 1000000L;
    -+		tmp2 = ((s64)scale * 1000000L) + scale2;
    ++		tmp = (s64)rescale->offset * MICRO;
    ++		tmp2 = ((s64)scale * MICRO) + scale2;
     +		*val = div64_s64(tmp, tmp2) + schan_off;
     +		return IIO_VAL_INT;
     +	default:
 5:  a3d8fb812678 =  7:  6bc5dd8c92ac iio: afe: rescale: use s64 for temporary scale calculations
 6:  b83947d96676 =  8:  7d426d67a7fd iio: afe: rescale: fix accuracy for small fractional scales
 7:  c42c8121bcdf =  9:  dbea6ae8fec2 iio: afe: rescale: reduce risk of integer overflow
 -:  ------------ > 10:  9ab1138449d3 iio: afe: rescale: make use of units.h
 8:  6c87d491a275 = 11:  3006151cd193 iio: test: add basic tests for the iio-rescale driver
 9:  3f3d1939b17c ! 12:  d4229e8d7f24 iio: afe: rescale: add RTD temperature sensor support
    @@ drivers/iio/afe/iio-rescale.c: static int rescale_voltage_divider_props(struct d
     +		return ret;
     +	}
     +
    -+	tmp = r0 * iexc * alpha / 1000000;
    -+	factor = gcd(tmp, 1000000);
    -+	rescale->numerator = 1000000 / factor;
    ++	tmp = r0 * iexc * alpha / MICRO;
    ++	factor = gcd(tmp, MICRO);
    ++	rescale->numerator = MICRO / factor;
     +	rescale->denominator = tmp / factor;
     +
    -+	rescale->offset = -1 * ((r0 * iexc) / 1000);
    ++	rescale->offset = -1 * ((r0 * iexc) / MICRO * MILLI);
     +
     +	return 0;
     +}
10:  0c83d15f3b92 ! 13:  b93e303c5e60 iio: afe: rescale: add temperature transducers
    @@ drivers/iio/afe/iio-rescale.c: static int rescale_temp_sense_rtd_props(struct de
     +		return ret;
     +	}
     +
    -+	rescale->numerator = 1000000;
    ++	rescale->numerator = MICRO;
     +	rescale->denominator = alpha * sense;
     +
     +	rescale->offset = div_s64((s64)offset * rescale->denominator,
11:  55af6c9391f8 = 14:  7bdd57435c5c dt-bindings: iio: afe: add bindings for temperature-sense-rtd
12:  8f9cd46c702e = 15:  604ef3e42c07 dt-bindings: iio: afe: add bindings for temperature transducers

base-commit: 2b6bff0b122785f09cfbdc34b1aa9edceea6e4c1