iio: Add a logarithmic fractional value type
authorLars-Peter Clausen <lars@metafoo.de>
Tue, 16 Oct 2012 16:29:00 +0000 (17:29 +0100)
committerJonathan Cameron <jic23@kernel.org>
Fri, 19 Oct 2012 17:46:51 +0000 (18:46 +0100)
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>
drivers/iio/industrialio-core.c
drivers/iio/inkern.c
include/linux/iio/types.h

index 6eb24dbc081ed2f158d9c46b4ccb5ee75ff20272..37650a72b31f11fc75d0afb6123233c724ce8270 100644 (file)
@@ -397,6 +397,11 @@ static ssize_t iio_read_channel_info(struct device *dev,
                val2 = do_div(tmp, 1000000000LL);
                val = tmp;
                return sprintf(buf, "%d.%09u\n", val, val2);
+       case IIO_VAL_FRACTIONAL_LOG2:
+               tmp = (s64)val * 1000000000LL >> val2;
+               val2 = do_div(tmp, 1000000000LL);
+               val = tmp;
+               return sprintf(buf, "%d.%09u\n", val, val2);
        default:
                return 0;
        }
index 5230a33886c0c37e18bc1d742c4e4210a5f60495..b394621d362cb54da7b7e303499cb79bde339472 100644 (file)
@@ -314,6 +314,9 @@ static int iio_convert_raw_to_processed_unlocked(struct iio_channel *chan,
                *processed = div_s64(raw64 * (s64)scale_val * scale,
                                     scale_val2);
                break;
+       case IIO_VAL_FRACTIONAL_LOG2:
+               *processed = (raw64 * (s64)scale_val * scale) >> scale_val2;
+               break;
        default:
                return -EINVAL;
        }
index 5c647ecfd5ba9216810b031da17e26ed601364bf..87b196a2d69839ac5a5e2b2cf40d44877bb42fa4 100644 (file)
@@ -58,5 +58,6 @@ enum iio_modifier {
 #define IIO_VAL_INT_PLUS_NANO 3
 #define IIO_VAL_INT_PLUS_MICRO_DB 4
 #define IIO_VAL_FRACTIONAL 10
+#define IIO_VAL_FRACTIONAL_LOG2 11
 
 #endif /* _IIO_TYPES_H_ */
This page took 0.026813 seconds and 5 git commands to generate.