iio: inkern: make a best effort on offset calculation
commit ca85123354e1a65a22170286387b4791997fe864 upstream.
iio_convert_raw_to_processed_unlocked() assumes the offset is an
integer. Make a best effort to get a valid offset value for fractional
cases without breaking implicit truncations.
Fixes: 48e44ce0f8
("iio:inkern: Add function to read the processed value")
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/20220108205319.2046348-4-liambeguin@gmail.com
Cc: <Stable@vger.kernel.org>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:

committed by
Greg Kroah-Hartman

parent
19e533452f
commit
9f0cd81174
@@ -561,13 +561,35 @@ EXPORT_SYMBOL_GPL(iio_read_channel_average_raw);
|
|||||||
static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
|
static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
|
||||||
int raw, int *processed, unsigned int scale)
|
int raw, int *processed, unsigned int scale)
|
||||||
{
|
{
|
||||||
int scale_type, scale_val, scale_val2, offset;
|
int scale_type, scale_val, scale_val2;
|
||||||
|
int offset_type, offset_val, offset_val2;
|
||||||
s64 raw64 = raw;
|
s64 raw64 = raw;
|
||||||
int ret;
|
|
||||||
|
|
||||||
ret = iio_channel_read(chan, &offset, NULL, IIO_CHAN_INFO_OFFSET);
|
offset_type = iio_channel_read(chan, &offset_val, &offset_val2,
|
||||||
if (ret >= 0)
|
IIO_CHAN_INFO_OFFSET);
|
||||||
raw64 += offset;
|
if (offset_type >= 0) {
|
||||||
|
switch (offset_type) {
|
||||||
|
case IIO_VAL_INT:
|
||||||
|
break;
|
||||||
|
case IIO_VAL_INT_PLUS_MICRO:
|
||||||
|
case IIO_VAL_INT_PLUS_NANO:
|
||||||
|
/*
|
||||||
|
* Both IIO_VAL_INT_PLUS_MICRO and IIO_VAL_INT_PLUS_NANO
|
||||||
|
* implicitely truncate the offset to it's integer form.
|
||||||
|
*/
|
||||||
|
break;
|
||||||
|
case IIO_VAL_FRACTIONAL:
|
||||||
|
offset_val /= offset_val2;
|
||||||
|
break;
|
||||||
|
case IIO_VAL_FRACTIONAL_LOG2:
|
||||||
|
offset_val >>= offset_val2;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
raw64 += offset_val;
|
||||||
|
}
|
||||||
|
|
||||||
scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
|
scale_type = iio_channel_read(chan, &scale_val, &scale_val2,
|
||||||
IIO_CHAN_INFO_SCALE);
|
IIO_CHAN_INFO_SCALE);
|
||||||
|
Reference in New Issue
Block a user