spi: Add spi_bpw_to_bytes() helper and use it
authorAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Thu, 17 Apr 2025 15:24:46 +0000 (18:24 +0300)
committerMark Brown <broonie@kernel.org>
Thu, 17 Apr 2025 16:30:40 +0000 (17:30 +0100)
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.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: David Lechner <dlechner@baylibre.com>
Link: https://patch.msgid.link/20250417152529.490582-2-andriy.shevchenko@linux.intel.com
Acked-by: Mukesh Kumar Savaliya <quic_msavaliy@quicinc.com>
Signed-off-by: Mark Brown <broonie@kernel.org>
drivers/spi/spi.c
include/linux/spi/spi.h

index 90e27729ef6b22adb6958060d9d72240c3246489..cdec46a5ab72842e997224c23629c8a5ee663aaf 100644 (file)
@@ -3802,7 +3802,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);
index 0ba5e49bace42cb9386af34cbef064fc45740dbd..5b872fe3b1a2a705c98bbf6c448cdcafa82128e2 100644 (file)
@@ -1325,6 +1325,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