Skip to content

Commit 8d0e8e7

Browse files
mitajic23
authored andcommitted
iio: adc: ti-ads1015: fix scale information for ADS1115
The ti-ads1015 driver supports ADS1015 and ADS1115 devices. The same scale information is used for both devices in this driver, however they have actually different values and the ADS1115's one is not correct. These devices have the same full-scale input voltage range for each PGA selection. So instead of adding another hardcoded scale information, compute a correct scale on demand from each device's resolution. Cc: Daniel Baluta <[email protected]> Signed-off-by: Akinobu Mita <[email protected]> Cc: <[email protected]> Signed-off-by: Jonathan Cameron <[email protected]>
1 parent 0d106b7 commit 8d0e8e7

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

drivers/iio/adc/ti-ads1015.c

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,12 @@ static const unsigned int ads1115_data_rate[] = {
8181
8, 16, 32, 64, 128, 250, 475, 860
8282
};
8383

84-
static const struct {
85-
int scale;
86-
int uscale;
87-
} ads1015_scale[] = {
88-
{3, 0},
89-
{2, 0},
90-
{1, 0},
91-
{0, 500000},
92-
{0, 250000},
93-
{0, 125000},
94-
{0, 125000},
95-
{0, 125000},
84+
/*
85+
* Translation from PGA bits to full-scale positive and negative input voltage
86+
* range in mV
87+
*/
88+
static int ads1015_fullscale_range[] = {
89+
6144, 4096, 2048, 1024, 512, 256, 256, 256
9690
};
9791

9892
#define ADS1015_V_CHAN(_chan, _addr) { \
@@ -300,17 +294,20 @@ static irqreturn_t ads1015_trigger_handler(int irq, void *p)
300294
return IRQ_HANDLED;
301295
}
302296

303-
static int ads1015_set_scale(struct ads1015_data *data, int chan,
297+
static int ads1015_set_scale(struct ads1015_data *data,
298+
struct iio_chan_spec const *chan,
304299
int scale, int uscale)
305300
{
306301
int i, ret, rindex = -1;
302+
int fullscale = div_s64((scale * 1000000LL + uscale) <<
303+
(chan->scan_type.realbits - 1), 1000000);
307304

308-
for (i = 0; i < ARRAY_SIZE(ads1015_scale); i++)
309-
if (ads1015_scale[i].scale == scale &&
310-
ads1015_scale[i].uscale == uscale) {
305+
for (i = 0; i < ARRAY_SIZE(ads1015_fullscale_range); i++) {
306+
if (ads1015_fullscale_range[i] == fullscale) {
311307
rindex = i;
312308
break;
313309
}
310+
}
314311
if (rindex < 0)
315312
return -EINVAL;
316313

@@ -320,7 +317,7 @@ static int ads1015_set_scale(struct ads1015_data *data, int chan,
320317
if (ret < 0)
321318
return ret;
322319

323-
data->channel_data[chan].pga = rindex;
320+
data->channel_data[chan->address].pga = rindex;
324321

325322
return 0;
326323
}
@@ -378,9 +375,9 @@ static int ads1015_read_raw(struct iio_dev *indio_dev,
378375
}
379376
case IIO_CHAN_INFO_SCALE:
380377
idx = data->channel_data[chan->address].pga;
381-
*val = ads1015_scale[idx].scale;
382-
*val2 = ads1015_scale[idx].uscale;
383-
ret = IIO_VAL_INT_PLUS_MICRO;
378+
*val = ads1015_fullscale_range[idx];
379+
*val2 = chan->scan_type.realbits - 1;
380+
ret = IIO_VAL_FRACTIONAL_LOG2;
384381
break;
385382
case IIO_CHAN_INFO_SAMP_FREQ:
386383
idx = data->channel_data[chan->address].data_rate;
@@ -407,7 +404,7 @@ static int ads1015_write_raw(struct iio_dev *indio_dev,
407404
mutex_lock(&data->lock);
408405
switch (mask) {
409406
case IIO_CHAN_INFO_SCALE:
410-
ret = ads1015_set_scale(data, chan->address, val, val2);
407+
ret = ads1015_set_scale(data, chan, val, val2);
411408
break;
412409
case IIO_CHAN_INFO_SAMP_FREQ:
413410
ret = ads1015_set_data_rate(data, chan->address, val);
@@ -439,15 +436,18 @@ static const struct iio_buffer_setup_ops ads1015_buffer_setup_ops = {
439436
.validate_scan_mask = &iio_validate_scan_mask_onehot,
440437
};
441438

442-
static IIO_CONST_ATTR(scale_available, "3 2 1 0.5 0.25 0.125");
439+
static IIO_CONST_ATTR_NAMED(ads1015_scale_available, scale_available,
440+
"3 2 1 0.5 0.25 0.125");
441+
static IIO_CONST_ATTR_NAMED(ads1115_scale_available, scale_available,
442+
"0.1875 0.125 0.0625 0.03125 0.015625 0.007813");
443443

444444
static IIO_CONST_ATTR_NAMED(ads1015_sampling_frequency_available,
445445
sampling_frequency_available, "128 250 490 920 1600 2400 3300");
446446
static IIO_CONST_ATTR_NAMED(ads1115_sampling_frequency_available,
447447
sampling_frequency_available, "8 16 32 64 128 250 475 860");
448448

449449
static struct attribute *ads1015_attributes[] = {
450-
&iio_const_attr_scale_available.dev_attr.attr,
450+
&iio_const_attr_ads1015_scale_available.dev_attr.attr,
451451
&iio_const_attr_ads1015_sampling_frequency_available.dev_attr.attr,
452452
NULL,
453453
};
@@ -457,7 +457,7 @@ static const struct attribute_group ads1015_attribute_group = {
457457
};
458458

459459
static struct attribute *ads1115_attributes[] = {
460-
&iio_const_attr_scale_available.dev_attr.attr,
460+
&iio_const_attr_ads1115_scale_available.dev_attr.attr,
461461
&iio_const_attr_ads1115_sampling_frequency_available.dev_attr.attr,
462462
NULL,
463463
};

0 commit comments

Comments
 (0)