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
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
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
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
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
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.
© 2016 - 2025 Red Hat, Inc.