Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: Add a logarithmic fractional value type

For ADCs or DACs the denominator for fractional types often is a power of two.
In this case we can use a shift operation instead of the rather expensive 64 bit
division. This patch adds a new fractional type which expects the denominator to
be specified as the log2 of the actual denominator. E.g. for ADCs and DACs this
will usually be the number of significant bits.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>

authored by

Lars-Peter Clausen and committed by
Jonathan Cameron
103d9fb9 948ad205

+9
+5
drivers/iio/industrialio-core.c
··· 397 397 val2 = do_div(tmp, 1000000000LL); 398 398 val = tmp; 399 399 return sprintf(buf, "%d.%09u\n", val, val2); 400 + case IIO_VAL_FRACTIONAL_LOG2: 401 + tmp = (s64)val * 1000000000LL >> val2; 402 + val2 = do_div(tmp, 1000000000LL); 403 + val = tmp; 404 + return sprintf(buf, "%d.%09u\n", val, val2); 400 405 default: 401 406 return 0; 402 407 }
+3
drivers/iio/inkern.c
··· 314 314 *processed = div_s64(raw64 * (s64)scale_val * scale, 315 315 scale_val2); 316 316 break; 317 + case IIO_VAL_FRACTIONAL_LOG2: 318 + *processed = (raw64 * (s64)scale_val * scale) >> scale_val2; 319 + break; 317 320 default: 318 321 return -EINVAL; 319 322 }
+1
include/linux/iio/types.h
··· 58 58 #define IIO_VAL_INT_PLUS_NANO 3 59 59 #define IIO_VAL_INT_PLUS_MICRO_DB 4 60 60 #define IIO_VAL_FRACTIONAL 10 61 + #define IIO_VAL_FRACTIONAL_LOG2 11 61 62 62 63 #endif /* _IIO_TYPES_H_ */