From: Jonathan Cameron <Jonathan.Cameron@Huawei.com>
To: Andy Shevchenko <andy.shevchenko@gmail.com>
Cc: "Paller, Kim Seer" <KimSeer.Paller@analog.com>,
"jic23@kernel.org" <jic23@kernel.org>,
"lars@metafoo.de" <lars@metafoo.de>,
"lgirdwood@gmail.com" <lgirdwood@gmail.com>,
"broonie@kernel.org" <broonie@kernel.org>,
"Hennerich, Michael" <Michael.Hennerich@analog.com>,
"robh@kernel.org" <robh@kernel.org>,
"krzysztof.kozlowski@linaro.org" <krzysztof.kozlowski@linaro.org>,
"conor+dt@kernel.org" <conor+dt@kernel.org>,
"linux-iio@vger.kernel.org" <linux-iio@vger.kernel.org>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"devicetree@vger.kernel.org" <devicetree@vger.kernel.org>,
kernel test robot <lkp@intel.com>
Subject: Re: [PATCH v8 2/2] iio: adc: max14001: New driver
Date: Wed, 5 Jul 2023 17:28:34 +0800 [thread overview]
Message-ID: <20230705172834.00001853@Huawei.com> (raw)
In-Reply-To: <CAHp75VfGFXtX2UCV+EzSMGaRMc5=WUpUJpRFB_K6NMJO2+iszg@mail.gmail.com>
On Wed, 5 Jul 2023 11:53:17 +0300
Andy Shevchenko <andy.shevchenko@gmail.com> wrote:
> On Wed, Jul 5, 2023 at 10:55 AM Jonathan Cameron
> <Jonathan.Cameron@huawei.com> wrote:
> > > > From: Jonathan Cameron <Jonathan.Cameron@Huawei.com>
> > > > Sent: Sunday, July 2, 2023 6:04 PM
> > > > On Thu, 22 Jun 2023 22:32:27 +0800
> > > > Kim Seer Paller <kimseer.paller@analog.com> wrote:
>
> ...
>
> > > > > + /*
> > > > > + * Convert transmit buffer to big-endian format and reverse transmit
> > > > > + * buffer to align with the LSB-first input on SDI port.
> > > > > + */
> > > > > + st->spi_tx_buffer =
> > > > cpu_to_be16(bitrev16(FIELD_PREP(MAX14001_ADDR_MASK,
> > > > > + reg_addr)));
> > > > > +
> > > > > + ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
> > > > > + if (ret)
> > > > > + return ret;
> > > > > +
> > > > > + /*
> > > > > + * Align received data from the receive buffer, reversing and reordering
> > > > > + * it to match the expected MSB-first format.
> > > > > + */
> > > > > + *data = (__force u16)(be16_to_cpu(bitrev16(st->spi_rx_buffer))) &
> > > > > +
> > > > MAX14001_DATA_MASK;
> > > > > +
> > > > These sequences still confuse me a lot because I'd expect the values in tx
> > > > to have the opposite operations applied to those for rx and that's not the
> > > > case.
> > > >
> > > > Let's take a le system.
> > > > tx = cpu_to_be16(bitrev16(x))
> > > > = cpu_to_be16((__bitrev8(x & 0xff) << 8) | __bitrev8(x >> 8));
> > > > = __bitrev8(x & 0xff) | (__bitrev8(x >> 8) << 8)
> > > > or swap all the bits in each byte, but don't swap the bytes.
> > > >
> > > > rx = cpu_to_be16(bitrev16(x))
> > > > = be16_to_cpu(((__bitrev8(x & 0xff) << 8) | __bitrev8(x >> 8)_
> > > > = __bitrev8(x & 0xff) | __bitrev(x >> 8)
> > > >
> > > > also swap all the bits in each byte, but don't swap the bytes.
> > > >
> > > > So it is the reverse because the bytes swaps unwind themselves somewhat.
> > > > For a be system cpu_to_be16 etc are noop.
> > > > tx = (__bitrev8(x & 0xff) << 8) | __bitrev8(x >> 8)
> > > > rx = (__bitrev8(x & 0xff) << 8) | __bitrev8(x >> 8)
> > > >
> > > > So in this case swap all 16 bits.
> > > >
> > > > Now, given I'd expected them to be reversed for the tx vs rx case.
> > > > E.g.
> > > > tx = cpu_to_be16(bitrev16(x))
> > > > As above.
> > > > For rx, le host
> > > > rx = bitrev16(be16_to_cpu(x))
> > > > = __bitrev8((x >> 8) & 0xff) << 8) | __bitrev8((((x & 0xff) << 8) >> 8)
> > > > same as above (if you swap the two terms I think.
> > > >
> > > > For be the be16_to_cpu is a noop again, so it's just bitrev16(x) as expected.
> > > >
> > > > Hence if I've understood this correctly you could reverse the terms so that
> > > > it was 'obvious' you were doing the opposite for the tx term vs the rx one
> > > > without making the slightest bit of difference....
> > > >
> > > > hmm. Might be worth doing simply to avoid questions.
> > >
> > > Thank you for your feedback. I have tested the modifications based on your
> > > suggestions, taking the le system into account, and it appears that the code is
> > > functioning correctly. Before sending the new patch version, I would like to
> > > confirm if this aligns with your comments.
>
> > Yes. This looks good to me.
>
> I think the implementation is still incorrect. See below.
>
> > > static int max14001_read(void *context, unsigned int reg_addr, unsigned int *data)
> > > {
> > > struct max14001_state *st = context;
> > > int ret;
> > >
> > > struct spi_transfer xfers[] = {
> > > {
> > > .tx_buf = &st->spi_tx_buffer,
> > > .len = sizeof(st->spi_tx_buffer),
> > > .cs_change = 1,
> > > }, {
> > > .rx_buf = &st->spi_rx_buffer,
> > > .len = sizeof(st->spi_rx_buffer),
> > > },
> > > };
>
> > > st->spi_tx_buffer = cpu_to_be16(bitrev16(FIELD_PREP(MAX14001_ADDR_MASK, reg_addr)));
>
> Here we got bits in CPU order, reversed them and converted to BE16.
>
> > > ret = spi_sync_transfer(st->spi, xfers, ARRAY_SIZE(xfers));
> > > if (ret)
> > > return ret;
>
> > > *data = cpu_to_be16(bitrev16(st->spi_rx_buffer));
>
> Here we take __be16 response, reverse them and convert to BE16?!
> This is weird. You should have be16_to_cpu() somewhere, not the opposite.
Good point - though functionally they end up the same (and the bitrev
is making mess of type markings anyway). It is more logical
to ensure the direction is reversed as you suggest.
>
> > > return 0;
> > > }
>
> Isn't, btw, the reinvented spi_...write_then_read() (or what is it
> called?) call?
I think the cs_change = 1 prevents using that.
Jonathan
>
> > > static int max14001_write(void *context, unsigned int reg_addr, unsigned int data)
> > > {
> > > struct max14001_state *st = context;
> > >
> > > st->spi_tx_buffer = cpu_to_be16(bitrev16(
> > > FIELD_PREP(MAX14001_ADDR_MASK, reg_addr) |
> > > FIELD_PREP(MAX14001_SET_WRITE_BIT, 1) |
> > > FIELD_PREP(MAX14001_DATA_MASK, data)));
> > >
> > > return spi_write(st->spi, &st->spi_tx_buffer, sizeof(st->spi_tx_buffer));
> > > }
>
next prev parent reply other threads:[~2023-07-05 9:28 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-22 14:32 [PATCH v8 1/2] dt-bindings: iio: adc: add max14001 Kim Seer Paller
2023-06-22 14:32 ` [PATCH v8 2/2] iio: adc: max14001: New driver Kim Seer Paller
2023-07-02 10:04 ` Jonathan Cameron
2023-07-04 9:40 ` Paller, Kim Seer
2023-07-05 7:55 ` Jonathan Cameron
2023-07-05 8:53 ` Andy Shevchenko
2023-07-05 9:28 ` Jonathan Cameron [this message]
2023-07-05 9:36 ` Andy Shevchenko
2023-07-06 1:25 ` 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=20230705172834.00001853@Huawei.com \
--to=jonathan.cameron@huawei.com \
--cc=KimSeer.Paller@analog.com \
--cc=Michael.Hennerich@analog.com \
--cc=andy.shevchenko@gmail.com \
--cc=broonie@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=jic23@kernel.org \
--cc=krzysztof.kozlowski@linaro.org \
--cc=lars@metafoo.de \
--cc=lgirdwood@gmail.com \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=lkp@intel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.