aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLiam Beguin <liambeguin@gmail.com>2022-02-12 21:57:31 -0500
committerJonathan Cameron <Jonathan.Cameron@huawei.com>2022-02-27 13:38:05 +0000
commit701ee14da95dcd25c1bf20dea7c8335b70d73124 (patch)
treef74fb1a4cf79da94265f03cc191ea92912b34e6e
parentbc437f7515f5e14aec9f2801412d9ea48116a97d (diff)
downloadiio-701ee14da95dcd25c1bf20dea7c8335b70d73124.tar.gz
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>
-rw-r--r--drivers/iio/afe/iio-rescale.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c
index 65832dd09249d..e67d9a9e6135c 100644
--- a/drivers/iio/afe/iio-rescale.c
+++ b/drivers/iio/afe/iio-rescale.c
@@ -23,6 +23,9 @@ int rescale_process_scale(struct rescale *rescale, int scale_type,
int *val, int *val2)
{
s64 tmp;
+ s32 rem;
+ u32 mult;
+ u32 neg;
switch (scale_type) {
case IIO_VAL_FRACTIONAL:
@@ -42,6 +45,37 @@ int rescale_process_scale(struct rescale *rescale, int scale_type,
tmp = div_s64(tmp, 1000000000LL);
*val = tmp;
return scale_type;
+ case IIO_VAL_INT_PLUS_NANO:
+ case IIO_VAL_INT_PLUS_MICRO:
+ mult = scale_type == IIO_VAL_INT_PLUS_NANO ? 1000000000L : 1000000L;
+
+ /*
+ * For IIO_VAL_INT_PLUS_{MICRO,NANO} scale types if either *val
+ * OR *val2 is negative the schan scale is negative, i.e.
+ * *val = 1 and *val2 = -0.5 yields -1.5 not -0.5.
+ */
+ neg = *val < 0 || *val2 < 0;
+
+ tmp = (s64)abs(*val) * abs(rescale->numerator);
+ *val = div_s64_rem(tmp, abs(rescale->denominator), &rem);
+
+ tmp = (s64)rem * mult + (s64)abs(*val2) * abs(rescale->numerator);
+ tmp = div_s64(tmp, abs(rescale->denominator));
+
+ *val += div_s64_rem(tmp, mult, val2);
+
+ /*
+ * If only one of the rescaler elements or the schan scale is
+ * negative, the combined scale is negative.
+ */
+ if (neg ^ ((rescale->numerator < 0) ^ (rescale->denominator < 0))) {
+ if (*val)
+ *val = -*val;
+ else
+ *val2 = -*val2;
+ }
+
+ return scale_type;
default:
return -EOPNOTSUPP;
}