Linux kernel staging patches
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Tomas Borquez <tomasborquez13@gmail.com>
Cc: "Lars-Peter Clausen" <lars@metafoo.de>,
	"Michael Hennerich" <Michael.Hennerich@analog.com>,
	"Rob Herring" <robh@kernel.org>,
	"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
	"Conor Dooley" <conor+dt@kernel.org>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-staging@lists.linux.dev
Subject: Re: [RFC PATCH 2/3] staging: iio: ad9832: convert to iio channels
Date: Sat, 6 Dec 2025 16:17:57 +0000	[thread overview]
Message-ID: <20251206161757.32e4cf0d@jic23-huawei> (raw)
In-Reply-To: <20251205202743.10530-3-tomasborquez13@gmail.com>

On Fri,  5 Dec 2025 17:27:42 -0300
Tomas Borquez <tomasborquez13@gmail.com> wrote:

> Replace the custom frequency and phase sysfs attributes with IIO channels
> using read_raw()/write_raw() callbacks, as well as removing the dds.h
> header.
> 
> Changes:
> - Add iio_chan_spec definitions for 2 frequency and 4 phase channels.
> - Implement read_raw/write_raw for IIO_CHAN_INFO_FREQUENCY/PHASE.
> - Cache frequency and phase values in driver state for readback.
> - Remove dependency on dds.h macros for sysfs.
> - Use guard(mutex) for cleaner locking.
> - Add input validation and consistent error messages.
> 
Hi Tomas,

> NOTE: This changes the userspace ABI, see cover letter.
Given I responded there on ABI, I'll just ignore that aspect for this review.

Code is pretty clean, but there are a few things that belong in other
patches rather than being mixed in here.
For kernel code we are pretty strict on one patch, one type of change.

> Signed-off-by: Tomas Borquez <tomasborquez13@gmail.com>
> ---
>  drivers/staging/iio/frequency/ad9832.c | 232 ++++++++++++++++++-------
>  1 file changed, 168 insertions(+), 64 deletions(-)
> 
> diff --git a/drivers/staging/iio/frequency/ad9832.c b/drivers/staging/iio/frequency/ad9832.c
> index e2ad3e5a7a..79d26009d1 100644
> --- a/drivers/staging/iio/frequency/ad9832.c
> +++ b/drivers/staging/iio/frequency/ad9832.c
> @@ -9,6 +9,7 @@
>  
>  #include <linux/bitfield.h>
>  #include <linux/bits.h>
> +#include <linux/cleanup.h>
>  #include <linux/clk.h>
>  #include <linux/device.h>
>  #include <linux/err.h>
> @@ -23,10 +24,7 @@
>  #include <linux/iio/iio.h>
>  #include <linux/iio/sysfs.h>
>  
> -#include "dds.h"
> -
>  /* Registers */
> -
Trivial but don't make unrelated white space changes in a patch
doing anything else - they add noise and hurt reviewer speed.
Put them in a patch on their own.

>  #define AD9832_FREQ0LL		0x0
>  #define AD9832_FREQ0HL		0x1
>  #define AD9832_FREQ0LM		0x2
> @@ -50,7 +48,6 @@
>  #define AD9832_OUTPUT_EN	0x13
>  
>  /* Command Control Bits */
> -

>  	} __aligned(IIO_DMA_MINALIGN);
>  };
>  
> -static unsigned long ad9832_calc_freqreg(unsigned long mclk, unsigned long fout)
> +static unsigned long ad9832_calc_freqreg(unsigned long mclk, u32 fout)
>  {
>  	unsigned long long freqreg = (u64)fout *
>  				     (u64)((u64)1L << AD9832_FREQ_BITS);
> @@ -124,12 +124,24 @@ static unsigned long ad9832_calc_freqreg(unsigned long mclk, unsigned long fout)
>  }
>  
>  static int ad9832_write_frequency(struct ad9832_state *st,
> -				  unsigned int addr, unsigned long fout)
> +				  unsigned int addr, u32 fout)
>  {
>  	unsigned long clk_freq;
>  	unsigned long regval;
>  	u8 regval_bytes[4];
>  	u16 freq_cmd;
> +	int ret, idx;
> +
> +	switch (addr) {
> +	case AD9832_FREQ0HM:
> +		idx = 0;
> +		break;
> +	case AD9832_FREQ1HM:
> +		idx = 1;
> +		break;
> +	default:
> +		return -EINVAL;
> +	}
>  
>  	clk_freq = clk_get_rate(st->mclk);
>  
> @@ -147,14 +159,37 @@ static int ad9832_write_frequency(struct ad9832_state *st,
>  			FIELD_PREP(AD9832_DAT_MSK, regval_bytes[i]));
>  	}
>  
> -	return spi_sync(st->spi, &st->freq_msg);
> +	ret = spi_sync(st->spi, &st->freq_msg);
> +	if (ret)
> +		return ret;
> +
> +	st->freq[idx] = fout;

I'd put a blank line here.  Generally slightly helps readability before
simple return statements like this.

> +	return 0;
>  }
>  
>  static int ad9832_write_phase(struct ad9832_state *st,
> -			      unsigned long addr, unsigned long phase)
> +			      unsigned long addr, u32 phase)
>  {
>  	u8 phase_bytes[2];
>  	u16 phase_cmd;
> +	int ret, idx;
> +
> +	switch (addr) {
> +	case AD9832_PHASE0H:
> +		idx = 0;
> +		break;
> +	case AD9832_PHASE1H:
> +		idx = 1;
> +		break;
> +	case AD9832_PHASE2H:
> +		idx = 2;
> +		break;
> +	case AD9832_PHASE3H:
> +		idx = 3;
> +		break;
> +	default:
> +		return -EINVAL;
> +	}
>  
>  	if (phase >= BIT(AD9832_PHASE_BITS))
>  		return -EINVAL;
> @@ -169,10 +204,77 @@ static int ad9832_write_phase(struct ad9832_state *st,
>  			FIELD_PREP(AD9832_DAT_MSK, phase_bytes[i]));
>  	}
>  
> -	return spi_sync(st->spi, &st->phase_msg);
> +	ret = spi_sync(st->spi, &st->phase_msg);
> +	if (ret)
> +		return ret;
> +
> +	st->phase[idx] = phase;
> +	return 0;
>  }
>  
> -static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
> +static int ad9832_write_raw(struct iio_dev *indio_dev,
> +			    struct iio_chan_spec const *chan,
> +			    int val, int val2, long mask)
> +{
> +	struct ad9832_state *st = iio_priv(indio_dev);
> +
> +	if (val < 0)
> +		return -EINVAL;

Check val2 as well.  Should be zero.


> +
> +	guard(mutex)(&st->lock);
> +	switch (mask) {
> +	case IIO_CHAN_INFO_FREQUENCY:
> +		return ad9832_write_frequency(st, chan->address, val);
> +	case IIO_CHAN_INFO_PHASE:
> +		return ad9832_write_phase(st, chan->address, val);
> +	default:
> +		return -EINVAL;
> +	}
> +}

> +
> +static ssize_t ad9832_store(struct device *dev,
> +			    struct device_attribute *attr,
>  			    const char *buf, size_t len)
>  {
>  	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
> @@ -183,20 +285,10 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
>  
>  	ret = kstrtoul(buf, 10, &val);
>  	if (ret)
> -		goto error_ret;
> +		return ret;
>  
> -	mutex_lock(&st->lock);
> -	switch ((u32)this_attr->address) {
> -	case AD9832_FREQ0HM:
> -	case AD9832_FREQ1HM:
> -		ret = ad9832_write_frequency(st, this_attr->address, val);
> -		break;
> -	case AD9832_PHASE0H:
> -	case AD9832_PHASE1H:
> -	case AD9832_PHASE2H:
> -	case AD9832_PHASE3H:
> -		ret = ad9832_write_phase(st, this_attr->address, val);
> -		break;
> +	guard(mutex)(&st->lock);

Ideally do guard() changes in a separate patch as seems unrelated to the
other stuff going on here. I'd suggest making that change first.

> +	switch (this_attr->address) {
>  	case AD9832_PINCTRL_EN:
>  		st->ctrl_ss &= ~AD9832_SELSRC;
>  		st->ctrl_ss |= FIELD_PREP(AD9832_SELSRC, val ? 0 : 1);
> @@ -206,13 +298,13 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
>  		ret = spi_sync(st->spi, &st->msg);
>  		break;
>  	case AD9832_FREQ_SYM:
> -		if (val == 1 || val == 0) {
> -			st->ctrl_fp &= ~AD9832_FREQ;
> -			st->ctrl_fp |= FIELD_PREP(AD9832_FREQ, val ? 1 : 0);
> -		} else {
> +		if (val != 1 && val != 0) {
>  			ret = -EINVAL;

With guard, should be able to directly return in error cases
simplifying the flow and helping code readability.  That's one of the
nicest things guard() enables.

>  			break;
>  		}
> +
> +		st->ctrl_fp &= ~AD9832_FREQ;
> +		st->ctrl_fp |= FIELD_PREP(AD9832_FREQ, val);
>  		st->data = cpu_to_be16(FIELD_PREP(AD9832_CMD_MSK, AD9832_CMD_FPSELECT) |
>  						  st->ctrl_fp);
>  		ret = spi_sync(st->spi, &st->msg);
> @@ -243,47 +335,56 @@ static ssize_t ad9832_write(struct device *dev, struct device_attribute *attr,
>  	default:
>  		ret = -ENODEV;
>  	}
> -	mutex_unlock(&st->lock);
>  
> -error_ret:
>  	return ret ? ret : len;
>  }

>  
> @@ -309,15 +412,15 @@ static int ad9832_probe(struct spi_device *spi)
>  
>  	ret = devm_regulator_get_enable(&spi->dev, "avdd");
>  	if (ret)
> -		return dev_err_probe(&spi->dev, ret, "failed to enable specified AVDD voltage\n");
> +		return dev_err_probe(&spi->dev, ret, "failed to enable AVDD supply\n");
>  
>  	ret = devm_regulator_get_enable(&spi->dev, "dvdd");
>  	if (ret)
> -		return dev_err_probe(&spi->dev, ret, "Failed to enable specified DVDD supply\n");
> +		return dev_err_probe(&spi->dev, ret, "failed to enable DVDD supply\n");
>  
>  	st->mclk = devm_clk_get_enabled(&spi->dev, "mclk");
>  	if (IS_ERR(st->mclk))
> -		return PTR_ERR(st->mclk);
> +		return dev_err_probe(&spi->dev, PTR_ERR(st->mclk), "failed to enable MCLK\n");
This is an unrelated change.  Do it in a separate patch for just the dev_err_probe()
usage.

>  


  reply	other threads:[~2025-12-06 16:18 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-05 20:27 [RFC PATCH 0/3] ad9832: driver cleanup Tomas Borquez
2025-12-05 20:27 ` [RFC PATCH 1/3] staging: iio: ad9832: remove platform_data support Tomas Borquez
2025-12-06 16:24   ` Andy Shevchenko
2025-12-07 12:46     ` Jonathan Cameron
2025-12-05 20:27 ` [RFC PATCH 2/3] staging: iio: ad9832: convert to iio channels Tomas Borquez
2025-12-06 16:17   ` Jonathan Cameron [this message]
2025-12-05 20:27 ` [RFC PATCH 3/3] dt-bindings: iio: add analog devices ad9832/ad9835 Tomas Borquez
2025-12-06 16:26   ` Jonathan Cameron
2025-12-16  6:08   ` Krzysztof Kozlowski
2025-12-16 14:10   ` Marcelo Schmitt
2025-12-06 16:09 ` [RFC PATCH 0/3] ad9832: driver cleanup Jonathan Cameron

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=20251206161757.32e4cf0d@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=Michael.Hennerich@analog.com \
    --cc=andy@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=krzk+dt@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-staging@lists.linux.dev \
    --cc=nuno.sa@analog.com \
    --cc=robh@kernel.org \
    --cc=tomasborquez13@gmail.com \
    /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