iio: adc: spear_adc: mask SPEAR_ADC_STATUS_AVG_SAMPLE before setting register
PATCH 1/1 iio: adc: spear_adc: mask SPEAR_ADC_STATUS_AVG_SAMPLE before setting register
SPEAR_ADC_STATUS_AVG_SAMPLE info is a bit field coded inside the following
bits: 5,6,7 and 8 of a device status register. Since the value came from dt, mask it in order to avoid touching other register bits.
---
During the submission of patches related to the yaml creation of spear_adc [1], there was a discussion about the average-samples value/dt property. It turns out that the dt property value is direclty written to a device status register. Considering some defines at the .c file, we have this:
#define SPEAR_ADC_STATUS_ADC_ENABLE BIT(4)
#define SPEAR_ADC_STATUS_AVG_SAMPLE(x) ((x) << 5)
#define SPEAR_ADC_STATUS_VREF_INTERNAL BIT(9)
SPEAR_ADC_STATUS_AVG_SAMPLE info is a bit field coded inside the following
bits: 5,6,7 and 8. The bit 9 is a different info, vref in this case. Currently, there is no control if the average-samples is outside of allowed range, which is 0x15 since the driver exists. There is no documentation about how that information is managed by the device, but I`m suggesting to add a mask with the max value for those bits before configuring the samples bits.
@Conor, I`m considering to add the "Suggested-by:" with your name here due [1]. Are you ok with that? If you agree, I can submit a v2 with that. Tks and regards.
[1] https://lore.kernel.org/linux-devicetree/20250506-equivocal-snooper-8a7d1ce931c8@spud/#t
--- drivers/iio/adc/spear_adc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/adc/spear_adc.c +3 -1
@@ -37,6 +37,8 @@#define SPEAR_ADC_DATA_MASK 0x03ff#define SPEAR_ADC_DATA_BITS 10 +#define SPEAR_ADC_AVG_SAMPLE_MASK 0x01E0 +#define SPEAR_ADC_MOD_NAME "spear-adc"#define SPEAR_ADC_CHANNEL_NUM 8
@@ -158,7 +160,7 @@ static int spear_adc_read_raw(struct iio_dev *indio_dev,mutex_lock (&st->lock); status =SPEAR_ADC_STATUS_CHANNEL_NUM (chan->channel) | -SPEAR_ADC_STATUS_AVG_SAMPLE (st->avg_samples) | + (SPEAR_ADC_STATUS_AVG_SAMPLE (st->avg_samples) &SPEAR_ADC_AVG_SAMPLE_MASK ) |SPEAR_ADC_STATUS_START_CONVERSION |SPEAR_ADC_STATUS_ADC_ENABLE ;if (st->vref_external ==0 )
Signature · 1 line
2.49.0
David Lechner
reviewerQuoted 3 lines · Rodrigo Gobbi
SPEAR_ADC_STATUS_AVG_SAMPLEinfo is a bit field coded inside the following
bits: 5,6,7 and 8 of a device status register. Since the value came from dt, mask it in order to avoid touching other register bits.
We have macros already that is the usual way for doing stuff like this in the kernel.
Quoted 8 lines
Signed-off-by Rodrigo Gobbi <[email protected]>---
During the submission of patches related to the yaml creation ofspear_adc[1], there was a discussion about the average-samples value/dt property. It turns out that the dt property value is direclty written to a device status register. Considering some defines at the .c file, we have this:
Quoted 4 lines
#define
SPEAR_ADC_STATUS_ADC_ENABLEBIT(4)#define
SPEAR_ADC_STATUS_AVG_SAMPLE(x)((x) << 5)
Replace this line with:
#define SPEAR_ADC_STATUS_AVG_SAMPLE_MASK GENMASK(8, 5)
Quoted 23 lines
#define
SPEAR_ADC_STATUS_VREF_INTERNALBIT(9)
SPEAR_ADC_STATUS_AVG_SAMPLEinfo is a bit field coded inside the following
bits: 5,6,7 and 8. The bit 9 is a different info, vref in this case. Currently, there is no control if the average-samples is outside of allowed range, which is0x15since the driver exists. There is no documentation about how that information is managed by the device, but I`m suggesting to add a mask with the max value for those bits before configuring the samples bits.@Conor, I`m considering to add the "Suggested-by:" with your name here due [1]. Are you ok with that? If you agree, I can submit a v2 with that. Tks and regards.
[1] https://lore.kernel.org/linux-devicetree/20250506-equivocal-snooper-8a7d1ce931c8@spud/#t
--- drivers/iio/adc/spear_adc.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/iio/adc/spear_adc.c +0
-0
drivers/iio/adc/spear_adc.c line 40
Quoted 4 lines
drivers/iio/adc/spear_adc.c +1
-0@@ -37,6 +37,8 @@#define SPEAR_ADC_DATA_MASK 0x03ff#define SPEAR_ADC_DATA_BITS 10 +#define SPEAR_ADC_AVG_SAMPLE_MASK 0x01E0
...instead of adding this.
drivers/iio/adc/spear_adc.c static int spear_adc_read_raw(struct iio_dev *indio_dev, · line 163
Quoted 10 lines
drivers/iio/adc/spear_adc.c +2
-1+#define SPEAR_ADC_MOD_NAME "spear-adc"#define SPEAR_ADC_CHANNEL_NUM 8@@ -158,7 +160,7 @@ static int spear_adc_read_raw(struct iio_dev *indio_dev,mutex_lock (&st->lock); status =SPEAR_ADC_STATUS_CHANNEL_NUM (chan->channel) | - SPEAR_ADC_STATUS_AVG_SAMPLE(st->avg_samples) | + (SPEAR_ADC_STATUS_AVG_SAMPLE(st->avg_samples) & SPEAR_ADC_AVG_SAMPLE_MASK) |
Then here we can use:
FIELD_PREP(SPEAR_ADC_STATUS_AVG_SAMPLE_MASK, st->avg_samples)
(#include <linux/bitfield.h> for this)
drivers/iio/adc/spear_adc.c static int spear_adc_read_raw(struct iio_dev *indio_dev, · line 166
Quoted 3 lines
drivers/iio/adc/spear_adc.c +0
-0SPEAR_ADC_STATUS_START_CONVERSION |SPEAR_ADC_STATUS_ADC_ENABLE ;if (st->vref_external ==0 )
And do something similar with SPEAR_ADC_STATUS_CHANNEL_NUM() while we are at it as if follows the same pattern.