[PATCH] iio: adc: spear_adc: mask SPEAR_ADC_STATUS_AVG_SAMPLE before setting register

Rodrigo Gobbi posted 1 patch 7 months, 3 weeks ago
drivers/iio/adc/spear_adc.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] iio: adc: spear_adc: mask SPEAR_ADC_STATUS_AVG_SAMPLE before setting register
Posted by Rodrigo Gobbi 7 months, 3 weeks ago
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.

Signed-off-by: Rodrigo Gobbi <rodrigo.gobbi.7@gmail.com>
---
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(-)

diff --git a/drivers/iio/adc/spear_adc.c b/drivers/iio/adc/spear_adc.c
index e3a865c79686..cf412ece90cf 100644
--- a/drivers/iio/adc/spear_adc.c
+++ b/drivers/iio/adc/spear_adc.c
@@ -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)
-- 
2.49.0
Re: [PATCH] iio: adc: spear_adc: mask SPEAR_ADC_STATUS_AVG_SAMPLE before setting register
Posted by David Lechner 7 months, 3 weeks ago
On 6/21/25 1:41 PM, Rodrigo Gobbi wrote:
> 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.

We have macros already that is the usual way for doing stuff like
this in the kernel.

> 
> Signed-off-by: Rodrigo Gobbi <rodrigo.gobbi.7@gmail.com>
> ---
> 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)

Replace this line with:

#define SPEAR_ADC_STATUS_AVG_SAMPLE_MASK	GENMASK(8, 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(-)
> 
> diff --git a/drivers/iio/adc/spear_adc.c b/drivers/iio/adc/spear_adc.c
> index e3a865c79686..cf412ece90cf 100644
> --- a/drivers/iio/adc/spear_adc.c
> +++ b/drivers/iio/adc/spear_adc.c
> @@ -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.

> +
>  #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)

>  			SPEAR_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.