Skip to content

Commit 3ccadaa

Browse files
committed
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> (cherry picked from commit 2d7b60f)
1 parent 83f5a62 commit 3ccadaa

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
@@ -1651,11 +1651,25 @@ static int ad7380_write_event_config(struct iio_dev *indio_dev,
16511651
return ret;
16521652
}
16531653

1654-
static int ad7380_get_alert_th(struct ad7380_state *st,
1654+
static int ad7380_get_alert_th(struct iio_dev *indio_dev,
1655+
const struct iio_chan_spec *chan,
16551656
enum iio_event_direction dir,
16561657
int *val)
16571658
{
1658-
int ret, tmp;
1659+
struct ad7380_state *st = iio_priv(indio_dev);
1660+
const struct iio_scan_type *scan_type;
1661+
int ret, tmp, shift;
1662+
1663+
scan_type = iio_get_current_scan_type(indio_dev, chan);
1664+
if (IS_ERR(scan_type))
1665+
return PTR_ERR(scan_type);
1666+
1667+
/*
1668+
* The register value is 12-bits and is compared to the most significant
1669+
* bits of raw value, therefore a shift is required to convert this to
1670+
* the same scale as the raw value.
1671+
*/
1672+
shift = scan_type->realbits - 12;
16591673

16601674
switch (dir) {
16611675
case IIO_EV_DIR_RISING:
@@ -1665,7 +1679,7 @@ static int ad7380_get_alert_th(struct ad7380_state *st,
16651679
if (ret)
16661680
return ret;
16671681

1668-
*val = FIELD_GET(AD7380_ALERT_HIGH_TH, tmp);
1682+
*val = FIELD_GET(AD7380_ALERT_HIGH_TH, tmp) << shift;
16691683
return IIO_VAL_INT;
16701684
case IIO_EV_DIR_FALLING:
16711685
ret = regmap_read(st->regmap,
@@ -1674,7 +1688,7 @@ static int ad7380_get_alert_th(struct ad7380_state *st,
16741688
if (ret)
16751689
return ret;
16761690

1677-
*val = FIELD_GET(AD7380_ALERT_LOW_TH, tmp);
1691+
*val = FIELD_GET(AD7380_ALERT_LOW_TH, tmp) << shift;
16781692
return IIO_VAL_INT;
16791693
default:
16801694
return -EINVAL;
@@ -1688,7 +1702,6 @@ static int ad7380_read_event_value(struct iio_dev *indio_dev,
16881702
enum iio_event_info info,
16891703
int *val, int *val2)
16901704
{
1691-
struct ad7380_state *st = iio_priv(indio_dev);
16921705
int ret;
16931706

16941707
switch (info) {
@@ -1697,7 +1710,7 @@ static int ad7380_read_event_value(struct iio_dev *indio_dev,
16971710
if (ret)
16981711
return ret;
16991712

1700-
ret = ad7380_get_alert_th(st, dir, val);
1713+
ret = ad7380_get_alert_th(indio_dev, chan, dir, val);
17011714

17021715
iio_device_release_direct_mode(indio_dev);
17031716
return ret;

0 commit comments

Comments
 (0)