[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <4c3e0d23-2582-4acf-8e90-542c8f8c385f@baylibre.com>
Date: Wed, 2 Apr 2025 16:04:09 -0500
From: David Lechner <dlechner@...libre.com>
To: Matti Vaittinen <mazziesaccount@...il.com>,
Matti Vaittinen <matti.vaittinen@...rohmeurope.com>
Cc: Jonathan Cameron <jic23@...nel.org>, Lars-Peter Clausen
<lars@...afoo.de>, Rob Herring <robh@...nel.org>,
Krzysztof Kozlowski <krzk+dt@...nel.org>, Conor Dooley
<conor+dt@...nel.org>, Nuno Sa <nuno.sa@...log.com>,
Javier Carrasco <javier.carrasco.cruz@...il.com>, linux-iio@...r.kernel.org,
devicetree@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2 2/7] iio: adc: ti-adc128s052: Simplify using
be16_to_cpu()
On 4/2/25 1:09 AM, Matti Vaittinen wrote:
> The register data is 12-bit big-endian data. Use be16_to_cpu() to do
> the conversion, and simple bitwise AND for masking to make it more
> obvious.
>
> Signed-off-by: Matti Vaittinen <mazziesaccount@...il.com>
> ---
> Revision history:
> v1 => v2:
> - Fix commit msg to reflect the fact there was no bug
> - Drop Fixes tag
> - Use union for rx / tx buffer to avoid casting
> - Keep the shared message protected by the mutex
> ---
> drivers/iio/adc/ti-adc128s052.c | 18 ++++++++++--------
> 1 file changed, 10 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/iio/adc/ti-adc128s052.c b/drivers/iio/adc/ti-adc128s052.c
> index a456ea78462f..3e69a5fce010 100644
> --- a/drivers/iio/adc/ti-adc128s052.c
> +++ b/drivers/iio/adc/ti-adc128s052.c
> @@ -28,32 +28,34 @@ struct adc128 {
> struct regulator *reg;
> struct mutex lock;
>
> - u8 buffer[2] __aligned(IIO_DMA_MINALIGN);
> + union {
> + __be16 rx_buffer;
> + u8 tx_buffer[2];
> + } __aligned(IIO_DMA_MINALIGN);
> };
>
> static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
> {
> int ret;
> + char *msg = &adc->tx_buffer[0];
>
> mutex_lock(&adc->lock);
>
> - adc->buffer[0] = channel << 3;
> - adc->buffer[1] = 0;
> + msg[0] = channel << 3;
> + msg[1] = 0;
>
> - ret = spi_write(adc->spi, &adc->buffer, 2);
> + ret = spi_write(adc->spi, msg, sizeof(adc->tx_buffer));
> if (ret < 0) {
> mutex_unlock(&adc->lock);
> return ret;
> }
>
> - ret = spi_read(adc->spi, &adc->buffer, 2);
> -
> + ret = spi_read(adc->spi, &adc->rx_buffer, 2);
> mutex_unlock(&adc->lock);
> -
> if (ret < 0)
> return ret;
>
> - return ((adc->buffer[0] << 8 | adc->buffer[1]) & 0xFFF);
> + return be16_to_cpu(adc->rx_buffer) & 0xFFF;
The cast isn't exactly beautiful, but this would save a lot of
lines of diff and a few lines of code by avoiding the need for
the union and the local msg variable.
return be16_to_cpup((__be16 *)adc->buffer) & 0xFFF;
> }
>
> static int adc128_read_raw(struct iio_dev *indio_dev,
Powered by blists - more mailing lists