Skip to content

Commit 7985e7c

Browse files
larsclausenjic23
authored andcommitted
iio: Introduce a new fractional value type
Currently IIO uses a decimal fixed point representations for real type numbers. This patch introduces a new representation for rational type numbers. The number will be expressed by specifying a numerator and denominator. For converting a raw value to a processed value multiply it by the numerator and divide it by the denominator. The reasoning for introducing this new type is that for a lot of devices the scale can be represented easily by a fractional number, but it is not possible to represent it as fixed point number without rounding. E.g. for a simple DAC the scale is often the reference voltage divided by the number of possible values (Usually 2**n_bits - 1). Each driver currently implements the conversion of this fraction to a fixed point number on its own. Also when it comes to the in-kernel interface this allows to directly use the fractional factors to convert a raw value to a processed value. This should on one hand require less instructions and on the other hand increase the precision. Signed-off-by: Lars-Peter Clausen <lars@metafoo.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
1 parent ca7d1b3 commit 7985e7c

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

‎drivers/iio/industrialio-core.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ static ssize_t iio_read_channel_info(struct device *dev,
366366
{
367367
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
368368
struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
369+
unsigned long long tmp;
369370
int val, val2;
370371
bool scale_db = false;
371372
int ret = indio_dev->info->read_raw(indio_dev, this_attr->c,
@@ -391,6 +392,11 @@ static ssize_t iio_read_channel_info(struct device *dev,
391392
return sprintf(buf, "-%d.%09u\n", val, -val2);
392393
else
393394
return sprintf(buf, "%d.%09u\n", val, val2);
395+
case IIO_VAL_FRACTIONAL:
396+
tmp = div_s64((s64)val * 1000000000LL, val2);
397+
val2 = do_div(tmp, 1000000000LL);
398+
val = tmp;
399+
return sprintf(buf, "%d.%09u\n", val, val2);
394400
default:
395401
return 0;
396402
}

‎include/linux/iio/types.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,6 @@ enum iio_modifier {
5757
#define IIO_VAL_INT_PLUS_MICRO 2
5858
#define IIO_VAL_INT_PLUS_NANO 3
5959
#define IIO_VAL_INT_PLUS_MICRO_DB 4
60+
#define IIO_VAL_FRACTIONAL 10
6061

6162
#endif /* _IIO_TYPES_H_ */

0 commit comments

Comments
 (0)