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: afe: rescale: add INT_PLUS_{MICRO,NANO} support

Some ADCs use IIO_VAL_INT_PLUS_{NANO,MICRO} scale types.
Add support for these to allow using the iio-rescaler with them.

Signed-off-by: Liam Beguin <liambeguin@gmail.com>
Reviewed-by: Peter Rosin <peda@axentia.se>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Link: https://lore.kernel.org/r/20220213025739.2561834-3-liambeguin@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Liam Beguin and committed by
Jonathan Cameron
701ee14d bc437f75

+34
+34
drivers/iio/afe/iio-rescale.c
··· 23 23 int *val, int *val2) 24 24 { 25 25 s64 tmp; 26 + s32 rem; 27 + u32 mult; 28 + u32 neg; 26 29 27 30 switch (scale_type) { 28 31 case IIO_VAL_FRACTIONAL: ··· 44 41 tmp *= rescale->numerator; 45 42 tmp = div_s64(tmp, 1000000000LL); 46 43 *val = tmp; 44 + return scale_type; 45 + case IIO_VAL_INT_PLUS_NANO: 46 + case IIO_VAL_INT_PLUS_MICRO: 47 + mult = scale_type == IIO_VAL_INT_PLUS_NANO ? 1000000000L : 1000000L; 48 + 49 + /* 50 + * For IIO_VAL_INT_PLUS_{MICRO,NANO} scale types if either *val 51 + * OR *val2 is negative the schan scale is negative, i.e. 52 + * *val = 1 and *val2 = -0.5 yields -1.5 not -0.5. 53 + */ 54 + neg = *val < 0 || *val2 < 0; 55 + 56 + tmp = (s64)abs(*val) * abs(rescale->numerator); 57 + *val = div_s64_rem(tmp, abs(rescale->denominator), &rem); 58 + 59 + tmp = (s64)rem * mult + (s64)abs(*val2) * abs(rescale->numerator); 60 + tmp = div_s64(tmp, abs(rescale->denominator)); 61 + 62 + *val += div_s64_rem(tmp, mult, val2); 63 + 64 + /* 65 + * If only one of the rescaler elements or the schan scale is 66 + * negative, the combined scale is negative. 67 + */ 68 + if (neg ^ ((rescale->numerator < 0) ^ (rescale->denominator < 0))) { 69 + if (*val) 70 + *val = -*val; 71 + else 72 + *val2 = -*val2; 73 + } 74 + 47 75 return scale_type; 48 76 default: 49 77 return -EOPNOTSUPP;