Skip to content

Commit 6751d92

Browse files
PIoandannunojsa
authored andcommitted
iio:adc:ad7768: Display scale factor more precisely
According to the calculations, we get the scale value: (2*4.096)/(2^24) = 488.28125nV. With nano precision doing voltage_raw * voltage_scale we get 476nV. The scale value is not precise enough,so this has been fixed. Signed-off-by: Ioan-daniel Pop <Pop.Ioan-daniel@analog.com>
1 parent 17e3339 commit 6751d92

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

drivers/iio/adc/ad7768.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include <linux/clk.h>
99
#include <linux/delay.h>
1010
#include <linux/module.h>
11+
#include <linux/math64.h>
12+
#include <linux/units.h>
1113
#include <linux/spi/spi.h>
1214
#include <linux/dma-mapping.h>
1315
#include <linux/dmaengine.h>
@@ -464,22 +466,36 @@ static ssize_t ad7768_attr_sampl_freq_avail(struct device *dev,
464466

465467
static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(ad7768_attr_sampl_freq_avail);
466468

469+
static ssize_t ad7768_scale(struct device *dev,
470+
struct device_attribute *attr,
471+
char *buf)
472+
{
473+
struct iio_dev *indio_dev = dev_to_iio_dev(dev);
474+
const struct iio_chan_spec *chan = &indio_dev->channels[0];
475+
struct ad7768_state *st = ad7768_get_data(indio_dev);
476+
u64 vref_nv;
477+
u64 scale;
478+
int ret;
479+
480+
ret = regulator_get_voltage(st->vref);
481+
if (ret < 0)
482+
return ret;
483+
484+
vref_nv = (u64)ret * NANO * 2;
485+
scale = div64_ul(vref_nv, BIT(chan->scan_type.realbits));
486+
487+
return scnprintf(buf, PAGE_SIZE, "0.%012llu\n", scale);
488+
}
489+
490+
static IIO_DEVICE_ATTR(in_voltage_scale, 0644, ad7768_scale, NULL, 0);
491+
467492
static int ad7768_read_raw(struct iio_dev *indio_dev,
468493
const struct iio_chan_spec *chan,
469494
int *val, int *val2, long info)
470495
{
471496
struct ad7768_state *st = ad7768_get_data(indio_dev);
472-
int ret;
473497

474498
switch (info) {
475-
case IIO_CHAN_INFO_SCALE:
476-
ret = regulator_get_voltage(st->vref);
477-
if (ret < 0)
478-
return ret;
479-
480-
*val = 2 * (ret / 1000000);
481-
*val2 = chan->scan_type.realbits;
482-
return IIO_VAL_FRACTIONAL_LOG2;
483499
case IIO_CHAN_INFO_SAMP_FREQ:
484500
*val = st->sampling_freq;
485501
return IIO_VAL_INT;
@@ -519,6 +535,7 @@ static struct iio_chan_spec_ext_info ad7768_ext_info[] = {
519535

520536
static struct attribute *ad7768_attributes[] = {
521537
&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
538+
&iio_dev_attr_in_voltage_scale.dev_attr.attr,
522539
NULL
523540
};
524541

@@ -536,7 +553,6 @@ static const struct iio_info ad7768_info = {
536553
#define AD7768_CHAN(index) \
537554
{ \
538555
.type = IIO_VOLTAGE, \
539-
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
540556
.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
541557
.address = index, \
542558
.indexed = 1, \

0 commit comments

Comments
 (0)