Skip to content

Commit 2d7b60f

Browse files
dlechjic23
authored andcommitted
iio: adc: ad7380: fix event threshold shift
Add required bit shift to the event threshold read function to get correct scaling. When alert support was added, the write function correctly included the required shift needed to convert the threshold register value to the same scale as the raw ADC value. However, the shift got missed in the read function. Fixes: 27d1a4d ("iio: adc: ad7380: add alert support") Signed-off-by: David Lechner <dlechner@baylibre.com> Reviewed-by: Julien Stephan <jstephan@baylibre.com> Link: https://patch.msgid.link/20250402-iio-adc-ad7380-fix-event-threshold-shift-v1-1-ad4975c296b2@baylibre.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 79dabbd commit 2d7b60f

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

drivers/iio/adc/ad7380.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,11 +1610,25 @@ static int ad7380_write_event_config(struct iio_dev *indio_dev,
16101610
return ret;
16111611
}
16121612

1613-
static int ad7380_get_alert_th(struct ad7380_state *st,
1613+
static int ad7380_get_alert_th(struct iio_dev *indio_dev,
1614+
const struct iio_chan_spec *chan,
16141615
enum iio_event_direction dir,
16151616
int *val)
16161617
{
1617-
int ret, tmp;
1618+
struct ad7380_state *st = iio_priv(indio_dev);
1619+
const struct iio_scan_type *scan_type;
1620+
int ret, tmp, shift;
1621+
1622+
scan_type = iio_get_current_scan_type(indio_dev, chan);
1623+
if (IS_ERR(scan_type))
1624+
return PTR_ERR(scan_type);
1625+
1626+
/*
1627+
* The register value is 12-bits and is compared to the most significant
1628+
* bits of raw value, therefore a shift is required to convert this to
1629+
* the same scale as the raw value.
1630+
*/
1631+
shift = scan_type->realbits - 12;
16181632

16191633
switch (dir) {
16201634
case IIO_EV_DIR_RISING:
@@ -1624,7 +1638,7 @@ static int ad7380_get_alert_th(struct ad7380_state *st,
16241638
if (ret)
16251639
return ret;
16261640

1627-
*val = FIELD_GET(AD7380_ALERT_HIGH_TH, tmp);
1641+
*val = FIELD_GET(AD7380_ALERT_HIGH_TH, tmp) << shift;
16281642
return IIO_VAL_INT;
16291643
case IIO_EV_DIR_FALLING:
16301644
ret = regmap_read(st->regmap,
@@ -1633,7 +1647,7 @@ static int ad7380_get_alert_th(struct ad7380_state *st,
16331647
if (ret)
16341648
return ret;
16351649

1636-
*val = FIELD_GET(AD7380_ALERT_LOW_TH, tmp);
1650+
*val = FIELD_GET(AD7380_ALERT_LOW_TH, tmp) << shift;
16371651
return IIO_VAL_INT;
16381652
default:
16391653
return -EINVAL;
@@ -1647,15 +1661,14 @@ static int ad7380_read_event_value(struct iio_dev *indio_dev,
16471661
enum iio_event_info info,
16481662
int *val, int *val2)
16491663
{
1650-
struct ad7380_state *st = iio_priv(indio_dev);
16511664
int ret;
16521665

16531666
switch (info) {
16541667
case IIO_EV_INFO_VALUE:
16551668
if (!iio_device_claim_direct(indio_dev))
16561669
return -EBUSY;
16571670

1658-
ret = ad7380_get_alert_th(st, dir, val);
1671+
ret = ad7380_get_alert_th(indio_dev, chan, dir, val);
16591672

16601673
iio_device_release_direct(indio_dev);
16611674
return ret;

0 commit comments

Comments
 (0)