From: Matti Vaittinen <mazziesaccount@gmail.com>
To: Jonathan Cameron <jic23@kernel.org>
Cc: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>,
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>,
David Lechner <dlechner@baylibre.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 2/6] iio: adc: ti-adc128s052: Fix ADC value on BE systems
Date: Mon, 31 Mar 2025 15:07:43 +0300 [thread overview]
Message-ID: <7d871d2d-bb42-43d5-96b0-88f24987d522@gmail.com> (raw)
In-Reply-To: <20250331121124.4fed1d44@jic23-huawei>
On 31/03/2025 14:11, Jonathan Cameron wrote:
> On Mon, 31 Mar 2025 11:02:55 +0300
> Matti Vaittinen <mazziesaccount@gmail.com> wrote:
>
>> ADCs supported by the ti-adc128s052 driver do return the ADC data in 16
>> bits using big-endian format. The driver does unconditionally swap the
>> bytes. This leads to wrong values being reported to users on big endian
>> systems.
>>
>> Fix this by using the be16_to_cpu() instead of doing unconditional byte
>> swapping.
Appears this was one of the patches I should've never written. Nothing
went right :) Sorry for the noise. I'll try improving for the v2
> It's not doing unconditional byte swap that I can see. The
> adc->buffer[0] << 8 | adc->buffer[1]
> will work on big or little endian systems as we are explicitly saying
> which byte represents higher bit values in a 16 bit output so on little
> endian it's a byte swap, but on big endian it's a noop (the compiler might
> noticed that and replace this code sequence with an assignment)
>
> Good cleanup, but not a fix as such unless I'm missing something.
No, you're not missing anything. I am the one who just got confused. I
am not exactly sure what I was thinking. :rolleyes: This definitely
isn't a fix. And, as a not a fix needing porting, I may squash this with
some other patch. I need to take another look at this :)
>> Fixes: 913b86468674 ("iio: adc: Add TI ADC128S052")
>> Cc: stable@vger.kernel.org
>> Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
>> ---
>> I have no big endian machines on my hands to test this. Problem was
>> spotted by reading the code, which leaves some room for errors.
>> Careful reviewing is appreciated!
>> ---
>> drivers/iio/adc/ti-adc128s052.c | 13 +++++++------
>> 1 file changed, 7 insertions(+), 6 deletions(-)
>>
>> diff --git a/drivers/iio/adc/ti-adc128s052.c b/drivers/iio/adc/ti-adc128s052.c
>> index a456ea78462f..d1e31122ea0d 100644
>> --- a/drivers/iio/adc/ti-adc128s052.c
>> +++ b/drivers/iio/adc/ti-adc128s052.c
>> @@ -28,19 +28,20 @@ struct adc128 {
>> struct regulator *reg;
>> struct mutex lock;
>>
>> - u8 buffer[2] __aligned(IIO_DMA_MINALIGN);
>> + __be16 buffer __aligned(IIO_DMA_MINALIGN);
>> };
>>
>> static int adc128_adc_conversion(struct adc128 *adc, u8 channel)
>> {
>> int ret;
>> + char *msg = (char *)&adc->buffer;
>>
>> - mutex_lock(&adc->lock);
>> + msg[0] = channel << 3;
>> + msg[1] = 0;
>
> Given you are writing shared state why move this out of the lock?
Very Valid Point. I'm not 100% sure what I thought of, probably assumed
IIO core would serialize the calls. That would've been nasty bug! I
appreciate your sharp eyes :)
Thanks!
Yours,
-- Matti
next prev parent reply other threads:[~2025-03-31 12:07 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-31 8:01 [PATCH 0/6] Support ROHM BD79104 ADC Matti Vaittinen
2025-03-31 8:02 ` [PATCH 1/6] dt-bindings: " Matti Vaittinen
2025-03-31 15:47 ` Conor Dooley
2025-03-31 8:02 ` [PATCH 2/6] iio: adc: ti-adc128s052: Fix ADC value on BE systems Matti Vaittinen
2025-03-31 11:11 ` Jonathan Cameron
2025-03-31 12:07 ` Matti Vaittinen [this message]
2025-03-31 8:03 ` [PATCH 3/6] iio: adc: ti-adc128s052: Be consistent with arrays Matti Vaittinen
2025-03-31 11:13 ` Jonathan Cameron
2025-03-31 8:03 ` [PATCH 4/6] iio: adc: ti-adc128s052: Use devm_mutex_init() Matti Vaittinen
2025-03-31 11:13 ` Jonathan Cameron
2025-03-31 8:03 ` [PATCH 5/6] iio: adc: ti-adc128s052: Simplify using guard(mutex) Matti Vaittinen
2025-03-31 11:14 ` Jonathan Cameron
2025-03-31 8:03 ` [PATCH 6/6] iio: adc: ti-adc128s052: Support ROHM BD79104 Matti Vaittinen
2025-03-31 11:22 ` Jonathan Cameron
2025-04-01 12:33 ` Matti Vaittinen
2025-04-05 17:43 ` Jonathan Cameron
2025-04-07 6:10 ` Matti Vaittinen
2025-04-07 18:44 ` 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=7d871d2d-bb42-43d5-96b0-88f24987d522@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;
as well as URLs for NNTP newsgroup(s).