Skip to content

Commit 163ddf1

Browse files
andy-shevbroonie
authored andcommitted
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. 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>
1 parent 8ffd015 commit 163ddf1

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

drivers/spi/spi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3802,7 +3802,7 @@ int spi_split_transfers_maxwords(struct spi_controller *ctlr,
38023802
size_t maxsize;
38033803
int ret;
38043804

3805-
maxsize = maxwords * roundup_pow_of_two(BITS_TO_BYTES(xfer->bits_per_word));
3805+
maxsize = maxwords * spi_bpw_to_bytes(xfer->bits_per_word);
38063806
if (xfer->len > maxsize) {
38073807
ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
38083808
maxsize);

include/linux/spi/spi.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,32 @@ static inline bool spi_is_bpw_supported(struct spi_device *spi, u32 bpw)
13251325
return false;
13261326
}
13271327

1328+
/**
1329+
* spi_bpw_to_bytes - Covert bits per word to bytes
1330+
* @bpw: Bits per word
1331+
*
1332+
* This function converts the given @bpw to bytes. The result is always
1333+
* power-of-two, e.g.,
1334+
*
1335+
* =============== =================
1336+
* Input (in bits) Output (in bytes)
1337+
* =============== =================
1338+
* 5 1
1339+
* 9 2
1340+
* 21 4
1341+
* 37 8
1342+
* =============== =================
1343+
*
1344+
* It will return 0 for the 0 input.
1345+
*
1346+
* Returns:
1347+
* Bytes for the given @bpw.
1348+
*/
1349+
static inline u32 spi_bpw_to_bytes(u32 bpw)
1350+
{
1351+
return roundup_pow_of_two(BITS_TO_BYTES(bpw));
1352+
}
1353+
13281354
/**
13291355
* spi_controller_xfer_timeout - Compute a suitable timeout value
13301356
* @ctlr: SPI device

0 commit comments

Comments
 (0)