From: Matti Vaittinen <mazziesaccount@gmail.com>
To: David Lechner <dlechner@baylibre.com>,
Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>
Cc: Jonathan Cameron <jic23@kernel.org>,
Lars-Peter Clausen <lars@metafoo.de>,
Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>, Nuno Sa <nuno.sa@analog.com>,
Javier Carrasco <javier.carrasco.cruz@gmail.com>,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/7] iio: adc: ti-adc128s052: Simplify using be16_to_cpu()
Date: Thu, 3 Apr 2025 08:16:43 +0300 [thread overview]
Message-ID: <1189b539-adb4-46f9-9783-c6577b57a304@gmail.com> (raw)
In-Reply-To: <4c3e0d23-2582-4acf-8e90-542c8f8c385f@baylibre.com>
On 03/04/2025 00:04, David Lechner wrote:
> 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@gmail.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;
Thanks again for the review David :)
I am unsure which way to go. I kind of like having the __be16 in the
struct, as it immediately yells "data from device is big-endian". OTOH,
I've never loved unions (and, it silences the above "yelling" quite a
bit). I still think this might be the first time I really see a valid
use-case for an union :) And, you're right this adds more lines,
besides, the cast doesn't look that ugly to me. Yet, I originally had a
cast probably as simple as this (and I also had the __be16 in the
struct), and Jonathan suggested using union to avoid it...
At the end of the day, I suppose I am Okay with any of these 3
approaches. Original cast, union or this cast you suggest. Jonathan, any
preferences on your side?
>
>> }
>>
>> static int adc128_read_raw(struct iio_dev *indio_dev,
>
Yours,
-- Matti
next prev parent reply other threads:[~2025-04-03 5:16 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-02 6:07 [PATCH v2 0/7] Support ROHM BD79104 ADC Matti Vaittinen
2025-04-02 6:08 ` [PATCH v2 1/7] dt-bindings: " Matti Vaittinen
2025-04-02 6:09 ` [PATCH v2 2/7] iio: adc: ti-adc128s052: Simplify using be16_to_cpu() Matti Vaittinen
2025-04-02 21:04 ` David Lechner
2025-04-03 5:16 ` Matti Vaittinen [this message]
2025-04-05 17:29 ` Jonathan Cameron
2025-04-07 5:23 ` Matti Vaittinen
2025-04-07 18:49 ` Jonathan Cameron
2025-04-02 6:09 ` [PATCH v2 3/7] iio: adc: ti-adc128s052: Be consistent with arrays Matti Vaittinen
2025-04-02 6:09 ` [PATCH v2 4/7] iio: adc: ti-adc128s052: Use devm_mutex_init() Matti Vaittinen
2025-04-02 6:09 ` [PATCH v2 5/7] iio: adc: ti-adc128s052: Simplify using guard(mutex) Matti Vaittinen
2025-04-02 6:10 ` [PATCH v2 6/7] iio: adc: ti-adc128s052: Support ROHM BD79104 Matti Vaittinen
2025-04-02 6:10 ` [PATCH RFC v2 7/7] iio: ti-adc128s052: Drop variable vref Matti Vaittinen
2025-04-02 20:49 ` David Lechner
2025-04-03 5:18 ` Matti Vaittinen
2025-04-05 16:25 ` Jonathan Cameron
2025-04-02 6:19 ` [PATCH v2 0/7] Support ROHM BD79104 ADC Matti Vaittinen
2025-04-05 17:36 ` 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=1189b539-adb4-46f9-9783-c6577b57a304@gmail.com \
--to=mazziesaccount@gmail.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=javier.carrasco.cruz@gmail.com \
--cc=jic23@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=matti.vaittinen@fi.rohmeurope.com \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
/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®