Linux IIO development
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Andrew Ijano <andrew.ijano@gmail.com>
Cc: andrew.lopes@alumni.usp.br, gustavobastos@usp.br,
	dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org,
	jstephan@baylibre.com, linux-iio@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4] iio: accel: sca3000: replace usages of internal read data helpers by spi helpers
Date: Sun, 11 May 2025 12:34:16 +0100	[thread overview]
Message-ID: <20250511123416.729eb50f@jic23-huawei> (raw)
In-Reply-To: <20250510190759.23921-1-andrew.lopes@alumni.usp.br>

On Sat, 10 May 2025 16:07:09 -0300
Andrew Ijano <andrew.ijano@gmail.com> wrote:

> Remove usages of sca3000_read_data() and sca3000_read_data_short()
> functions, replacing it by spi_w8r8() and spi_w8r16() helpers. Just
> one case that reads large buffers is left using an internal helper.
> 
> This is an old driver that was not making full use of the newer
> infrastructure.
> 
> Signed-off-by: Andrew Ijano <andrew.lopes@alumni.usp.br>
> Co-developed-by: Gustavo Bastos <gustavobastos@usp.br>
> Signed-off-by: Gustavo Bastos <gustavobastos@usp.br>
> Suggested-by: Jonathan Cameron <jic23@kernel.org>

A few things inline but clearly main thing is to reply to Andy's other
points on v3.

> ---
>  drivers/iio/accel/sca3000.c | 169 +++++++++++++++---------------------
>  1 file changed, 72 insertions(+), 97 deletions(-)
> 
> diff --git a/drivers/iio/accel/sca3000.c b/drivers/iio/accel/sca3000.c
> index aabe4491efd7..7794efc35970 100644
> --- a/drivers/iio/accel/sca3000.c
> +++ b/drivers/iio/accel/sca3000.c
> @@ -281,10 +281,11 @@ static int sca3000_write_reg(struct sca3000_state *st, u8 address, u8 val)
>  	return spi_write(st->us, st->tx, 2);
>  }
>  
> -static int sca3000_read_data_short(struct sca3000_state *st,
> -				   u8 reg_address_high,
> -				   int len)
> +static int sca3000_read_data(struct sca3000_state *st,
> +			     u8 reg_address_high,
> +			     int len)

I'd keep this where the original sca3000_read_data() is
That will give a shorter, more obvious diff and puts the code near where it
is called helping review.

>  {
> +	int ret;
>  	struct spi_transfer xfer[2] = {
>  		{
>  			.len = 1,
> @@ -296,7 +297,10 @@ static int sca3000_read_data_short(struct sca3000_state *st,
>  	};
>  	st->tx[0] = SCA3000_READ_REG(reg_address_high);
>  
> -	return spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
> +	ret = spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
> +	if (ret)
> +		dev_err(&st->us->dev, "problem reading register\n");
> +	return ret;
>  }

...

>  /**
> @@ -412,10 +416,8 @@ static int sca3000_read_ctrl_reg(struct sca3000_state *st,
>  	ret = sca3000_write_reg(st, SCA3000_REG_CTRL_SEL_ADDR, ctrl_reg);
>  	if (ret)
>  		goto error_ret;
> -	ret = sca3000_read_data_short(st, SCA3000_REG_CTRL_DATA_ADDR, 1);
> -	if (ret)
> -		goto error_ret;
> -	return st->rx[0];
> +
> +	ret = spi_w8r8(st->us, SCA3000_READ_REG(SCA3000_REG_CTRL_DATA_ADDR));
>  error_ret:
>  	return ret;
This shows the age of the code.  Just return in error paths above rather
than a error_ret: label

Might be a good idea to do a precursor patch tidying up any cases of this
before the one doin gthe spi changes in ehre.

>  }
> @@ -432,13 +434,13 @@ static int sca3000_print_rev(struct iio_dev *indio_dev)
>  	struct sca3000_state *st = iio_priv(indio_dev);
>  
>  	mutex_lock(&st->lock);

Another patch to use guard(mutex)(&st->lock); etc would be help clean this
up by allowing direct return in the error path.

> -	ret = sca3000_read_data_short(st, SCA3000_REG_REVID_ADDR, 1);
> +	ret = spi_w8r8(st->us, SCA3000_READ_REG(SCA3000_REG_REVID_ADDR));
>  	if (ret < 0)
>  		goto error_ret;
>  	dev_info(&indio_dev->dev,
>  		 "sca3000 revision major=%lu, minor=%lu\n",
> -		 st->rx[0] & SCA3000_REG_REVID_MAJOR_MASK,
> -		 st->rx[0] & SCA3000_REG_REVID_MINOR_MASK);
> +		 ret & SCA3000_REG_REVID_MAJOR_MASK,
> +		 ret & SCA3000_REG_REVID_MINOR_MASK);
>  error_ret:
>  	mutex_unlock(&st->lock);
>  


>  static int sca3000_read_raw(struct iio_dev *indio_dev,
> @@ -722,6 +724,7 @@ static int sca3000_read_raw(struct iio_dev *indio_dev,
>  	struct sca3000_state *st = iio_priv(indio_dev);
>  	int ret;
>  	u8 address;
> +	__be16 raw_val;
>  
>  	switch (mask) {
>  	case IIO_CHAN_INFO_RAW:
> @@ -732,25 +735,24 @@ static int sca3000_read_raw(struct iio_dev *indio_dev,
>  				return -EBUSY;
>  			}
>  			address = sca3000_addresses[chan->address][0];
> -			ret = sca3000_read_data_short(st, address, 2);
> +			ret = spi_w8r16(st->us, SCA3000_READ_REG(address));

spi_w8r16be() then no need for the endian conversion below.


>  			if (ret < 0) {
>  				mutex_unlock(&st->lock);
>  				return ret;
>  			}
> -			*val = sign_extend32(be16_to_cpup((__be16 *)st->rx) >>
> -					     chan->scan_type.shift,
> +			raw_val = (__be16) ret;
> +			*val = sign_extend32(be16_to_cpu(raw_val) >> chan->scan_type.shift,
>  					     chan->scan_type.realbits - 1);
>  		} else {
>  			/* get the temperature when available */
> -			ret = sca3000_read_data_short(st,
> -						      SCA3000_REG_TEMP_MSB_ADDR,
> -						      2);
> +			ret = spi_w8r16(st->us,
> +						SCA3000_READ_REG(SCA3000_REG_TEMP_MSB_ADDR));

As above. spi_w8r16be()


>  			if (ret < 0) {
>  				mutex_unlock(&st->lock);
>  				return ret;
>  			}
> -			*val = (be16_to_cpup((__be16 *)st->rx) >>
> -				chan->scan_type.shift) &
> +			raw_val = (__be16) ret;
> +			*val = (be16_to_cpu(raw_val) >> chan->scan_type.shift) &
>  				GENMASK(chan->scan_type.realbits - 1, 0);
>  		}
>  		mutex_unlock(&st->lock);

>  

As above. Put the new implementation of this here so we can easily see what
changed.

> -static int sca3000_read_data(struct sca3000_state *st,
> -			     u8 reg_address_high,
> -			     u8 *rx,
> -			     int len)
> -{
> -	int ret;
> -	struct spi_transfer xfer[2] = {
> -		{
> -			.len = 1,
> -			.tx_buf = st->tx,
> -		}, {
> -			.len = len,
> -			.rx_buf = rx,
> -		}
> -	};
> -
> -	st->tx[0] = SCA3000_READ_REG(reg_address_high);
> -	ret = spi_sync_transfer(st->us, xfer, ARRAY_SIZE(xfer));
> -	if (ret) {
> -		dev_err(&st->us->dev, "problem reading register\n");
> -		return ret;
> -	}
> -
> -	return 0;
> -}
> -
Jonathan

  parent reply	other threads:[~2025-05-11 11:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-10 19:07 [PATCH v4] iio: accel: sca3000: replace usages of internal read data helpers by spi helpers Andrew Ijano
2025-05-10 21:39 ` Andy Shevchenko
2025-05-11 17:35   ` Andrew Ijano
2025-05-11 11:34 ` Jonathan Cameron [this message]
     [not found]   ` <CANZih_QHNEuFTQ2NysUVOJ-PmrL-ASFeG5b8xBTbRSG=3ho-vw@mail.gmail.com>
2025-05-11 18:36     ` Fwd: " Andrew Ijano
2025-05-15 16:53       ` 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=20250511123416.729eb50f@jic23-huawei \
    --to=jic23@kernel.org \
    --cc=andrew.ijano@gmail.com \
    --cc=andrew.lopes@alumni.usp.br \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=gustavobastos@usp.br \
    --cc=jstephan@baylibre.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.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