mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: David Lechner <dlechner@baylibre.com>
To: Rodrigo Gobbi <rodrigo.gobbi.7@gmail.com>,
	jic23@kernel.org, nuno.sa@analog.com, andy@kernel.org,
	conor@kernel.org
Cc: ~lkcamp/patches@lists.sr.ht, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] iio: adc: spear_adc: mask SPEAR_ADC_STATUS_AVG_SAMPLE before setting register
Date: Sat, 21 Jun 2025 14:18:00 -0500	[thread overview]
Message-ID: <8437d9db-8123-4c50-9f5b-b9adf265f32c@baylibre.com> (raw)
In-Reply-To: <20250621185301.9536-1-rodrigo.gobbi.7@gmail.com>

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.


      reply	other threads:[~2025-06-21 19:18 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-21 18:41 Rodrigo Gobbi
2025-06-21 19:18 ` David Lechner [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8437d9db-8123-4c50-9f5b-b9adf265f32c@baylibre.com \
    --to=dlechner@baylibre.com \
    --cc=andy@kernel.org \
    --cc=conor@kernel.org \
    --cc=jic23@kernel.org \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=rodrigo.gobbi.7@gmail.com \
    --cc=~lkcamp/patches@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®