Message ID | 20210820191714.69898-6-liambeguin@gmail.com (mailing list archive) |
---|---|
State | Changes Requested |
Headers | show |
Series | iio: afe: add temperature rescaling support | expand |
On 2021-08-20 21:17, Liam Beguin wrote: > From: Liam Beguin <lvb@xiphos.com> > > 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 <lvb@xiphos.com> > --- > drivers/iio/afe/iio-rescale.c | 35 +++++++++++++++++++++++++++++++++++ > 1 file changed, 35 insertions(+) > > diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c > index d0669fd8eac5..8488f1d83527 100644 > --- a/drivers/iio/afe/iio-rescale.c > +++ b/drivers/iio/afe/iio-rescale.c > @@ -22,6 +22,9 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, > int *val, int *val2) > { > unsigned long long tmp; > + s32 rem; > + u32 mult; > + u32 neg; > > switch (scale_type) { > case IIO_VAL_FRACTIONAL: > @@ -40,6 +43,38 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, > tmp *= rescale->numerator; > do_div(tmp, 1000000000LL); > *val = tmp; > + return scale_type; > + case IIO_VAL_INT_PLUS_NANO: > + case IIO_VAL_INT_PLUS_MICRO: > + if (scale_type == IIO_VAL_INT_PLUS_NANO) > + mult = 1000000000LL; > + else > + mult = 1000000LL; > + /* > + * For IIO_VAL_INT_PLUS_{MICRO,NANO} scale types if *val OR > + * *val2 is negative the schan scale is negative The last line doesn't parse for me. It doesn't end with a period either, so it looks like you moved on before you finished it? Cheers, Peter > + */ > + 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; >
On Thu, Aug 26, 2021 at 10:11:18AM +0200, Peter Rosin wrote: > On 2021-08-20 21:17, Liam Beguin wrote: > > From: Liam Beguin <lvb@xiphos.com> > > > > 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 <lvb@xiphos.com> > > --- > > drivers/iio/afe/iio-rescale.c | 35 +++++++++++++++++++++++++++++++++++ > > 1 file changed, 35 insertions(+) > > > > diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c > > index d0669fd8eac5..8488f1d83527 100644 > > --- a/drivers/iio/afe/iio-rescale.c > > +++ b/drivers/iio/afe/iio-rescale.c > > @@ -22,6 +22,9 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, > > int *val, int *val2) > > { > > unsigned long long tmp; > > + s32 rem; > > + u32 mult; > > + u32 neg; > > > > switch (scale_type) { > > case IIO_VAL_FRACTIONAL: > > @@ -40,6 +43,38 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, > > tmp *= rescale->numerator; > > do_div(tmp, 1000000000LL); > > *val = tmp; > > + return scale_type; > > + case IIO_VAL_INT_PLUS_NANO: > > + case IIO_VAL_INT_PLUS_MICRO: > > + if (scale_type == IIO_VAL_INT_PLUS_NANO) > > + mult = 1000000000LL; > > + else > > + mult = 1000000LL; > > + /* > > + * For IIO_VAL_INT_PLUS_{MICRO,NANO} scale types if *val OR > > + * *val2 is negative the schan scale is negative > > The last line doesn't parse for me. It doesn't end with a period either, so > it looks like you moved on before you finished it? I meant to warn that IIO_VAL_INT_PLUS_{MICRO,NANO} are a little odd, and that if either one *val or *val2 is negative, the result will be negative. i.e. *val = 1 and *val2 = -0.5 gives -1.5 and not 0.5. I'll give it another try and will add the period. Thanks, Liam > > Cheers, > Peter > > > + */ > > + 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; > >
diff --git a/drivers/iio/afe/iio-rescale.c b/drivers/iio/afe/iio-rescale.c index d0669fd8eac5..8488f1d83527 100644 --- a/drivers/iio/afe/iio-rescale.c +++ b/drivers/iio/afe/iio-rescale.c @@ -22,6 +22,9 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, int *val, int *val2) { unsigned long long tmp; + s32 rem; + u32 mult; + u32 neg; switch (scale_type) { case IIO_VAL_FRACTIONAL: @@ -40,6 +43,38 @@ int rescale_process_scale(struct rescale *rescale, int scale_type, tmp *= rescale->numerator; do_div(tmp, 1000000000LL); *val = tmp; + return scale_type; + case IIO_VAL_INT_PLUS_NANO: + case IIO_VAL_INT_PLUS_MICRO: + if (scale_type == IIO_VAL_INT_PLUS_NANO) + mult = 1000000000LL; + else + mult = 1000000LL; + /* + * For IIO_VAL_INT_PLUS_{MICRO,NANO} scale types if *val OR + * *val2 is negative the schan scale is negative + */ + 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;