* [PATCH v1 0/2] spi: Introduce and use spi_bpw_to_bytes()
@ 2025-04-16 6:16 Andy Shevchenko
2025-04-16 6:16 ` [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it Andy Shevchenko
` (2 more replies)
0 siblings, 3 replies; 9+ messages in thread
From: Andy Shevchenko @ 2025-04-16 6:16 UTC (permalink / raw)
To: Mark Brown, Andy Shevchenko, linux-spi, linux-kernel; +Cc: David Lechner
Recently in the discussion with David the idea of having
a common helper popped up. The helper converts the given
bits per word to bytes. The result will always be power-of-two
(e.g. for 37 bits it returns 8 bytes) or 0 for 0 input.
This mini-series introduces it and replaces current users
under drivers/spi and we expect more (and possibly some lurking
in other subsystems).
Mark, if you okay with the idea, please, make this to be an immutable
branch or tag for others to pull.
Andy Shevchenko (2):
spi: Add spi_bpw_to_bytes() helper and use it
spi: dw: Use spi_bpw_to_bytes() helper
drivers/spi/spi-dw-core.c | 2 +-
drivers/spi/spi.c | 2 +-
include/linux/spi/spi.h | 15 +++++++++++++++
3 files changed, 17 insertions(+), 2 deletions(-)
--
2.47.2
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it
2025-04-16 6:16 [PATCH v1 0/2] spi: Introduce and use spi_bpw_to_bytes() Andy Shevchenko
@ 2025-04-16 6:16 ` Andy Shevchenko
2025-04-16 7:03 ` Mukesh Kumar Savaliya
2025-04-16 14:56 ` David Lechner
2025-04-16 6:16 ` [PATCH v1 2/2] spi: dw: Use spi_bpw_to_bytes() helper Andy Shevchenko
2025-04-18 5:25 ` [PATCH v1 0/2] spi: Introduce and use spi_bpw_to_bytes() Mark Brown
2 siblings, 2 replies; 9+ messages in thread
From: Andy Shevchenko @ 2025-04-16 6:16 UTC (permalink / raw)
To: Mark Brown, Andy Shevchenko, linux-spi, linux-kernel; +Cc: David Lechner
This helper converts the given bits per word to bytes. The result
will always be power-of-two (e.g. for 37 bits it returns 8 bytes)
or 0 for 0 input.
There are a couple of cases in SPI that are using the same approach
and at least one more (in IIO) would benefit of it. Add a helper
for everyone.
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
drivers/spi/spi.c | 2 +-
include/linux/spi/spi.h | 15 +++++++++++++++
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index b0e7702951fe..1bc0fdbb1bd7 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -3800,7 +3800,7 @@ int spi_split_transfers_maxwords(struct spi_controller *ctlr,
size_t maxsize;
int ret;
- maxsize = maxwords * roundup_pow_of_two(BITS_TO_BYTES(xfer->bits_per_word));
+ maxsize = maxwords * spi_bpw_to_bytes(xfer->bits_per_word);
if (xfer->len > maxsize) {
ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
maxsize);
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index 834a09bd8ccc..abfc7f5e19e4 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -1340,6 +1340,21 @@ static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw)
return false;
}
+/**
+ * spi_bpw_to_bytes - Covert bits per word to bytes
+ * @bpw: Bits per word
+ *
+ * This function converts the given @bpw to bytes. The result is always
+ * power-of-two (e.g. for 37 bits it returns 8 bytes) or 0 for 0 input.
+ *
+ * Returns:
+ * Bytes for the given @bpw.
+ */
+static inline u32 spi_bpw_to_bytes(u32 bpw)
+{
+ return roundup_pow_of_two(BITS_TO_BYTES(bpw));
+}
+
/**
* spi_controller_xfer_timeout - Compute a suitable timeout value
* @ctlr: SPI device
--
2.47.2
^ permalink raw reply related [flat|nested] 9+ messages in thread
* [PATCH v1 2/2] spi: dw: Use spi_bpw_to_bytes() helper
2025-04-16 6:16 [PATCH v1 0/2] spi: Introduce and use spi_bpw_to_bytes() Andy Shevchenko
2025-04-16 6:16 ` [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it Andy Shevchenko
@ 2025-04-16 6:16 ` Andy Shevchenko
2025-04-18 5:25 ` [PATCH v1 0/2] spi: Introduce and use spi_bpw_to_bytes() Mark Brown
2 siblings, 0 replies; 9+ messages in thread
From: Andy Shevchenko @ 2025-04-16 6:16 UTC (permalink / raw)
To: Mark Brown, Andy Shevchenko, linux-spi, linux-kernel; +Cc: David Lechner
Use existing helper to get amount of bytes (as power-of-two value)
from bits per word.
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
drivers/spi/spi-dw-core.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/spi/spi-dw-core.c b/drivers/spi/spi-dw-core.c
index 941ecc6f59f8..b3b883cb9541 100644
--- a/drivers/spi/spi-dw-core.c
+++ b/drivers/spi/spi-dw-core.c
@@ -423,7 +423,7 @@ static int dw_spi_transfer_one(struct spi_controller *host,
int ret;
dws->dma_mapped = 0;
- dws->n_bytes = roundup_pow_of_two(BITS_TO_BYTES(transfer->bits_per_word));
+ dws->n_bytes = spi_bpw_to_bytes(transfer->bits_per_word);
dws->tx = (void *)transfer->tx_buf;
dws->tx_len = transfer->len / dws->n_bytes;
dws->rx = transfer->rx_buf;
--
2.47.2
^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it
2025-04-16 6:16 ` [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it Andy Shevchenko
@ 2025-04-16 7:03 ` Mukesh Kumar Savaliya
2025-04-16 7:09 ` Andy Shevchenko
2025-04-16 14:56 ` David Lechner
1 sibling, 1 reply; 9+ messages in thread
From: Mukesh Kumar Savaliya @ 2025-04-16 7:03 UTC (permalink / raw)
To: Andy Shevchenko, Mark Brown, linux-spi, linux-kernel; +Cc: David Lechner
On 4/16/2025 11:46 AM, Andy Shevchenko wrote:
> This helper converts the given bits per word to bytes. The result
> will always be power-of-two (e.g. for 37 bits it returns 8 bytes)
> or 0 for 0 input.
>
> There are a couple of cases in SPI that are using the same approach
> and at least one more (in IIO) would benefit of it. Add a helper
> for everyone.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
> drivers/spi/spi.c | 2 +-
> include/linux/spi/spi.h | 15 +++++++++++++++
> 2 files changed, 16 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
> index b0e7702951fe..1bc0fdbb1bd7 100644
> --- a/drivers/spi/spi.c
> +++ b/drivers/spi/spi.c
> @@ -3800,7 +3800,7 @@ int spi_split_transfers_maxwords(struct spi_controller *ctlr,
> size_t maxsize;
> int ret;
>
> - maxsize = maxwords * roundup_pow_of_two(BITS_TO_BYTES(xfer->bits_per_word));
> + maxsize = maxwords * spi_bpw_to_bytes(xfer->bits_per_word);
> if (xfer->len > maxsize) {
> ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
> maxsize);
> diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
> index 834a09bd8ccc..abfc7f5e19e4 100644
> --- a/include/linux/spi/spi.h
> +++ b/include/linux/spi/spi.h
> @@ -1340,6 +1340,21 @@ static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw)
> return false;
> }
>
> +/**
> + * spi_bpw_to_bytes - Covert bits per word to bytes
> + * @bpw: Bits per word
> + *
> + * This function converts the given @bpw to bytes. The result is always
> + * power-of-two (e.g. for 37 bits it returns 8 bytes) or 0 for 0 input.
Would it be good to say in 4 byte aligned /Multiples ?
> + *
> + * Returns:
> + * Bytes for the given @bpw.
Returns: Bytes for the given @bpw.
Good to keep in one line.
> + */> +static inline u32 spi_bpw_to_bytes(u32 bpw)
u8 bpw ?
struct spi_device {
u8 bits_per_word;
}
so arg should be u8.
> +{
> + return roundup_pow_of_two(BITS_TO_BYTES(bpw));
> +}
> +
> /**
> * spi_controller_xfer_timeout - Compute a suitable timeout value
> * @ctlr: SPI device
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it
2025-04-16 7:03 ` Mukesh Kumar Savaliya
@ 2025-04-16 7:09 ` Andy Shevchenko
2025-04-16 7:34 ` Mukesh Kumar Savaliya
0 siblings, 1 reply; 9+ messages in thread
From: Andy Shevchenko @ 2025-04-16 7:09 UTC (permalink / raw)
To: Mukesh Kumar Savaliya; +Cc: Mark Brown, linux-spi, linux-kernel, David Lechner
On Wed, Apr 16, 2025 at 12:33:24PM +0530, Mukesh Kumar Savaliya wrote:
> On 4/16/2025 11:46 AM, Andy Shevchenko wrote:
Thanks for the prompt review, my answers below.
...
> > +/**
> > + * spi_bpw_to_bytes - Covert bits per word to bytes
> > + * @bpw: Bits per word
> > + *
> > + * This function converts the given @bpw to bytes. The result is always
> > + * power-of-two (e.g. for 37 bits it returns 8 bytes) or 0 for 0 input.
> Would it be good to say in 4 byte aligned /Multiples ?
It's not correct. The said wording describes the current behaviour.
> > + * Returns:
> > + * Bytes for the given @bpw.
> Returns: Bytes for the given @bpw.
> Good to keep in one line.
Aligned with the style of the other function in the same header, so I prefer to
leave the style the same.
> > + */> +static inline u32 spi_bpw_to_bytes(u32 bpw)
> u8 bpw ?
Nope. See below why.
> struct spi_device {
> u8 bits_per_word;
> }
> so arg should be u8.
It's aligned with the above bpw related function.
Also note, that this helper might be moved to the global header at some point
as some other subsystems may utilise it, so I don't want to limit this to u8.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it
2025-04-16 7:09 ` Andy Shevchenko
@ 2025-04-16 7:34 ` Mukesh Kumar Savaliya
0 siblings, 0 replies; 9+ messages in thread
From: Mukesh Kumar Savaliya @ 2025-04-16 7:34 UTC (permalink / raw)
To: Andy Shevchenko; +Cc: Mark Brown, linux-spi, linux-kernel, David Lechner
Thanks Andy !
On 4/16/2025 12:39 PM, Andy Shevchenko wrote:
> On Wed, Apr 16, 2025 at 12:33:24PM +0530, Mukesh Kumar Savaliya wrote:
>> On 4/16/2025 11:46 AM, Andy Shevchenko wrote:
>
> Thanks for the prompt review, my answers below.
>
> ...
>
>>> +/**
>>> + * spi_bpw_to_bytes - Covert bits per word to bytes
>>> + * @bpw: Bits per word
>>> + *
>>> + * This function converts the given @bpw to bytes. The result is always
>>> + * power-of-two (e.g. for 37 bits it returns 8 bytes) or 0 for 0 input.
>> Would it be good to say in 4 byte aligned /Multiples ?
>
> It's not correct. The said wording describes the current behaviour.
>
Sure.
>>> + * Returns:
>>> + * Bytes for the given @bpw.
>> Returns: Bytes for the given @bpw.
>> Good to keep in one line.
>
> Aligned with the style of the other function in the same header, so I prefer to
> leave the style the same.
>
Yes, i see.
>>> + */> +static inline u32 spi_bpw_to_bytes(u32 bpw)
>> u8 bpw ?
>
> Nope. See below why.
>
>> struct spi_device {
>> u8 bits_per_word;
>> }
>
>> so arg should be u8.
>
> It's aligned with the above bpw related function.
> Also note, that this helper might be moved to the global header at some point
> as some other subsystems may utilise it, so I don't want to limit this to u8.
>
Okay, if plan to move to global header then it's fine.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it
2025-04-16 6:16 ` [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it Andy Shevchenko
2025-04-16 7:03 ` Mukesh Kumar Savaliya
@ 2025-04-16 14:56 ` David Lechner
2025-04-16 15:14 ` Andy Shevchenko
1 sibling, 1 reply; 9+ messages in thread
From: David Lechner @ 2025-04-16 14:56 UTC (permalink / raw)
To: Andy Shevchenko, Mark Brown, linux-spi, linux-kernel
On 4/16/25 1:16 AM, Andy Shevchenko wrote:
> This helper converts the given bits per word to bytes. The result
> will always be power-of-two (e.g. for 37 bits it returns 8 bytes)
> or 0 for 0 input.
>
> There are a couple of cases in SPI that are using the same approach
> and at least one more (in IIO) would benefit of it. Add a helper
> for everyone.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> ---
> drivers/spi/spi.c | 2 +-
> include/linux/spi/spi.h | 15 +++++++++++++++
> 2 files changed, 16 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
> index b0e7702951fe..1bc0fdbb1bd7 100644
> --- a/drivers/spi/spi.c
> +++ b/drivers/spi/spi.c
> @@ -3800,7 +3800,7 @@ int spi_split_transfers_maxwords(struct spi_controller *ctlr,
> size_t maxsize;
> int ret;
>
> - maxsize = maxwords * roundup_pow_of_two(BITS_TO_BYTES(xfer->bits_per_word));
> + maxsize = maxwords * spi_bpw_to_bytes(xfer->bits_per_word);
> if (xfer->len > maxsize) {
> ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
> maxsize);
> diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
> index 834a09bd8ccc..abfc7f5e19e4 100644
> --- a/include/linux/spi/spi.h
> +++ b/include/linux/spi/spi.h
> @@ -1340,6 +1340,21 @@ static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw)
> return false;
> }
>
> +/**
> + * spi_bpw_to_bytes - Covert bits per word to bytes
> + * @bpw: Bits per word
> + *
> + * This function converts the given @bpw to bytes. The result is always
> + * power-of-two (e.g. for 37 bits it returns 8 bytes) or 0 for 0 input.
The SPI subsystem currently only supports bpw up to 32, so perhaps not
the best choice of value for the example. I would go with 20 bits getting
rounded up to 4 bytes to match the existing docs for @bits_per_word.
> + *
> + * Returns:
> + * Bytes for the given @bpw.
> + */
> +static inline u32 spi_bpw_to_bytes(u32 bpw)
> +{
> + return roundup_pow_of_two(BITS_TO_BYTES(bpw));
Do we need to #include <linux/log2.h> for roundup_pow_of_two()?
> +}
> +
> /**
> * spi_controller_xfer_timeout - Compute a suitable timeout value
> * @ctlr: SPI device
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it
2025-04-16 14:56 ` David Lechner
@ 2025-04-16 15:14 ` Andy Shevchenko
0 siblings, 0 replies; 9+ messages in thread
From: Andy Shevchenko @ 2025-04-16 15:14 UTC (permalink / raw)
To: David Lechner; +Cc: Mark Brown, linux-spi, linux-kernel
On Wed, Apr 16, 2025 at 09:56:12AM -0500, David Lechner wrote:
> On 4/16/25 1:16 AM, Andy Shevchenko wrote:
...
> > +/**
> > + * spi_bpw_to_bytes - Covert bits per word to bytes
> > + * @bpw: Bits per word
> > + *
> > + * This function converts the given @bpw to bytes. The result is always
> > + * power-of-two (e.g. for 37 bits it returns 8 bytes) or 0 for 0 input.
>
> The SPI subsystem currently only supports bpw up to 32, so perhaps not
> the best choice of value for the example. I would go with 20 bits getting
> rounded up to 4 bytes to match the existing docs for @bits_per_word.
Okay, I think I come up with a few examples, so it will show that it's not
4-byte multiple or so.
> > + * Returns:
> > + * Bytes for the given @bpw.
> > + */
> > +static inline u32 spi_bpw_to_bytes(u32 bpw)
> > +{
> > + return roundup_pow_of_two(BITS_TO_BYTES(bpw));
> Do we need to #include <linux/log2.h> for roundup_pow_of_two()?
Right now I prefer not to touch that (it is implicitly included); the headers
needs to have a bigger cleanup and my first attempt had miserably failed.
> > +}
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH v1 0/2] spi: Introduce and use spi_bpw_to_bytes()
2025-04-16 6:16 [PATCH v1 0/2] spi: Introduce and use spi_bpw_to_bytes() Andy Shevchenko
2025-04-16 6:16 ` [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it Andy Shevchenko
2025-04-16 6:16 ` [PATCH v1 2/2] spi: dw: Use spi_bpw_to_bytes() helper Andy Shevchenko
@ 2025-04-18 5:25 ` Mark Brown
2 siblings, 0 replies; 9+ messages in thread
From: Mark Brown @ 2025-04-18 5:25 UTC (permalink / raw)
To: linux-spi, linux-kernel, Andy Shevchenko; +Cc: David Lechner
On Wed, 16 Apr 2025 09:16:33 +0300, Andy Shevchenko wrote:
> Recently in the discussion with David the idea of having
> a common helper popped up. The helper converts the given
> bits per word to bytes. The result will always be power-of-two
> (e.g. for 37 bits it returns 8 bytes) or 0 for 0 input.
>
> This mini-series introduces it and replaces current users
> under drivers/spi and we expect more (and possibly some lurking
> in other subsystems).
>
> [...]
Applied to
https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next
Thanks!
[1/2] spi: Add spi_bpw_to_bytes() helper and use it
commit: 163ddf1fea590229c30a8dc4c29ff4febfb895c3
[2/2] spi: dw: Use spi_bpw_to_bytes() helper
commit: e30b7a75666b3f444abfabed6a144642fa9994d8
All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.
You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.
If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.
Please add any relevant lists and maintainers to the CCs when replying
to this mail.
Thanks,
Mark
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2025-04-18 5:25 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-16 6:16 [PATCH v1 0/2] spi: Introduce and use spi_bpw_to_bytes() Andy Shevchenko
2025-04-16 6:16 ` [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it Andy Shevchenko
2025-04-16 7:03 ` Mukesh Kumar Savaliya
2025-04-16 7:09 ` Andy Shevchenko
2025-04-16 7:34 ` Mukesh Kumar Savaliya
2025-04-16 14:56 ` David Lechner
2025-04-16 15:14 ` Andy Shevchenko
2025-04-16 6:16 ` [PATCH v1 2/2] spi: dw: Use spi_bpw_to_bytes() helper Andy Shevchenko
2025-04-18 5:25 ` [PATCH v1 0/2] spi: Introduce and use spi_bpw_to_bytes() Mark Brown
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).