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)
=============== =================
0 0
5 1
9 2
2 4
3 8
=============== =================
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 | 25 +++++++++++++++++++++++++
2 files changed, 26 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..2611f86180a3 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -1340,6 +1340,31 @@ 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)
+ * =============== =================
+ * 0 0
+ * 5 1
+ * 9 2
+ * 2 4
+ * 3 8
+ * =============== =================
+ *
+ * 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 Thu, Apr 17, 2025 at 06:17:52PM +0300, Andy Shevchenko wrote: > 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) > =============== ================= > 0 0 > 5 1 > 9 2 > 2 4 > 3 8 My gosh, it lost the couple of characters here, should be 21 and 37 respectively... I'll fix this in v3. > =============== ================= ... > + * This function converts the given @bpw to bytes. The result is always > + * power-of-two, e.g., > + * > + * =============== ================= > + * Input (in bits) Output (in bytes) > + * =============== ================= > + * 0 0 > + * 5 1 > + * 9 2 > + * 2 4 > + * 3 8 As per above, > + * =============== ================= -- With Best Regards, Andy Shevchenko
© 2016 - 2025 Red Hat, Inc.