Skip to contentSkip to conversation

spi: Introduce and use spi_bpw_to_bytes()

PATCH In review Andy Shevchenko 4 participants 7 messages Apr 17, 2025 – Apr 18, 2025 SPI

lore
Share & tools
lore
#0 lore raw

v3 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. More details are in the respective code comment.

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.

In v3:
- fixed the typos in the examples

In v2:
- improved examples in the code comment and commit message (David)

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   | 26 ++++++++++++++++++++++++++
 3 files changed, 28 insertions(+), 2 deletions(-)
Signature · 1 line
2.47.2
#1 lore raw

PATCH v3 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.,

    ===============    =================
    Input (in bits)    Output (in bytes)
    ===============    =================
            5                   1
            9                   2
            21                  4
            37                  8
    ===============    =================

It will return 0 for the 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 | 26 ++++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
drivers/spi/spi.c +1 -1
index b0e7702951fe..1bc0fdbb1bd7 100644
@@ -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 +26 -0
index 834a09bd8ccc..c921ff902f0c 100644
@@ -1340,6 +1340,32 @@ 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.,
+ *
+ *  ===============    =================
+ *  Input (in bits)    Output (in bytes)
+ *  ===============    =================
+ *          5                   1
+ *          9                   2
+ *          21                  4
+ *          37                  8
+ *  ===============    =================
+ *
+ * It will return 0 for the 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
#2 lore raw

PATCH v3 2/2 spi: dw: Use spi_bpw_to_bytes() helper

In reply to Andy Shevchenko

Use 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
index 941ecc6f59f8..b3b883cb9541 100644
@@ -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
Reviewed-by on the v3 series
#3 lore raw
In reply to Andy Shevchenko
Quoted 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. More details are in the respective code comment.

This mini-series introduces it and replaces current users under drivers/spi and we expect more (and possibly some lurking in other subsystems).

Quoted 9 lines

Mark, if you okay with the idea, please, make this to be an immutable branch or tag for others to pull.

In v3:
- fixed the typos in the examples

In v2:
- improved examples in the code comment and commit message (David)

#4 lore raw
Quoted 14 lines · David Lechner
Quoted 13 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. More details are in the respective code comment.

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.

Quoted 7 lines
Quoted 5 lines

In v3:
- fixed the typos in the examples

In v2:
- improved examples in the code comment and commit message (David)

Thank you for the review!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Acked-by on the v3 series
#5 lore raw
In reply to Andy Shevchenko
Quoted 19 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. More details are in the respective code comment.

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.

In v3:
- fixed the typos in the examples

In v2:
- improved examples in the code comment and commit message (David)

Quoted 9 lines

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   | 26 ++++++++++++++++++++++++++
  3 files changed, 28 insertions(+), 2 deletions(-)
maintainer
#6 lore raw
In reply to Andy Shevchenko
Quoted 11 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. More details are in the respective code comment.

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