spi: Introduce and use spi_bpw_to_bytes()
cover spi: Introduce and use spi_bpw_to_bytes()
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(-)
Signature · 1 line
2.47.2
PATCH 1/2 spi: Add spi_bpw_to_bytes() helper and use it
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.
--- drivers/spi/spi.c | 2 +- include/linux/spi/spi.h | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-)
drivers/spi/spi.c +1 -1
@@ -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);
include/linux/spi/spi.h +15 -0
@@ -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
Signature · 1 line
2.47.2
Re: [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it
Quoted 32 lines · Andy Shevchenko
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 <[email protected]>--- drivers/spi/spi.c | 2 +- include/linux/spi/spi.h | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-)drivers/spi/spi.c +1
-1@@ -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);include/linux/spi/spi.h +0
-0
include/linux/spi/spi.h static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw) · line 1348
Quoted 9 lines
include/linux/spi/spi.h +6
-0@@ -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 ?
include/linux/spi/spi.h static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw) · line 1351
Quoted 3 lines
include/linux/spi/spi.h +3
-0+* +* Returns: +* Bytes for the given @bpw.
Returns: Bytes for the given @bpw.
Good to keep in one line.
include/linux/spi/spi.h static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw) · line 1352
Quoted 1 line
include/linux/spi/spi.h +1
-0+ */> +static inline u32 spi_bpw_to_bytes(u32 bpw)
u8 bpw ?
struct spi_device {
u8 bits_per_word;
}
so arg should be u8.
Quoted 7 lines
include/linux/spi/spi.h +4
-0+{ +return roundup_pow_of_two (BITS_TO_BYTES (bpw)); +} +/** * spi_controller_xfer_timeout - Compute a suitable timeout value * @ctlr: SPI device
Re: [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it
Quoted 1 line · Mukesh Kumar Savaliya
On 4/16/2025 11:46 AM, Andy Shevchenko wrote:
Thanks for the prompt review, my answers below.
...
Quoted 7 lines
Quoted 6 lines
+/**
+ *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.
Quoted 4 lines
Quoted 2 lines
+ * 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.
Quoted 2 lines
Quoted 1 line
+ */> +static inline u32
spi_bpw_to_bytes(u32 bpw)u8 bpw ?
Nope. See below why.
Quoted 5 lines
struct
spi_device{
u8bits_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.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it
Thanks Andy !
Quoted 13 lines · Andy Shevchenko
Quoted 1 line · Mukesh Kumar Savaliya
On 4/16/2025 11:46 AM, Andy Shevchenko wrote:
Thanks for the prompt review, my answers below.
...
Quoted 6 lines
Quoted 6 lines
+/**
+ *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.
Quoted 4 lines
Quoted 1 line
Would it be good to say in 4 byte aligned /Multiples ?
It's not correct. The said wording describes the current behaviour.
Sure.
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
+ * 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.
Quoted 9 lines
Quoted 2 lines
Quoted 1 line
+ */> +static inline u32
spi_bpw_to_bytes(u32 bpw)u8 bpw ?
Nope. See below why.
Quoted 3 lines
struct
spi_device{
u8bits_per_word;
}
Quoted 6 lines
Quoted 1 line
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.
Re: [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it
In reply to Andy ShevchenkoQuoted 32 lines · Andy Shevchenko
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 <[email protected]>--- drivers/spi/spi.c | 2 +- include/linux/spi/spi.h | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-)drivers/spi/spi.c +1
-1@@ -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);include/linux/spi/spi.h +0
-0
include/linux/spi/spi.h static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw) · line 1348
Quoted 9 lines
include/linux/spi/spi.h +6
-0@@ -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.
include/linux/spi/spi.h static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw) · line 1355
Quoted 7 lines
include/linux/spi/spi.h +7
-0+* +* 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()?
Quoted 5 lines
include/linux/spi/spi.h +2
-0+} +/** * spi_controller_xfer_timeout - Compute a suitable timeout value * @ctlr: SPI device
Re: [PATCH v1 1/2] spi: Add spi_bpw_to_bytes() helper and use it
Quoted 1 line · David Lechner
On 4/16/25 1:16 AM, Andy Shevchenko wrote:
...
Quoted 10 lines
Quoted 6 lines
+/**
+ *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.
Quoted 8 lines
include/linux/spi/spi.h static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw) · line 1355
Quoted 6 lines
+ * Returns:
+ * Bytes for the given @bpw.
+ */include/linux/spi/spi.h +3
-0+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> forroundup_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.
Quoted 1 line
Quoted 1 line
+}
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH 2/2 spi: dw: Use spi_bpw_to_bytes() helper
In reply to Andy ShevchenkoUse existing helper to get amount of bytes (as power-of-two value) from bits per word.
--- drivers/spi/spi-dw-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi-dw-core.c +1 -1
@@ -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;
Signature · 1 line
2.47.2
Mark Brown
maintainerQuoted 10 lines · Andy Shevchenko
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/spiand 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: e30b7a75666b3f444abfabed6a144642fa9994d8All 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