From: "Leonard Göhrs" <l.goehrs@pengutronix.de>
To: Mark Brown <broonie@kernel.org>
Cc: kernel@pengutronix.de, "Leonard Göhrs" <l.goehrs@pengutronix.de>,
linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v1 1/2] spi: core: add spi_split_transfers_maxwords
Date: Fri, 10 Mar 2023 10:20:52 +0100 [thread overview]
Message-ID: <20230310092053.1006459-1-l.goehrs@pengutronix.de> (raw)
Add spi_split_transfers_maxwords() function that splits
spi_transfers transparently into multiple transfers
that are below a given number of SPI words.
This function reuses most of its code from
spi_split_transfers_maxsize() and for transfers with
eight or less bits per word actually behaves the same.
Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>
---
drivers/spi/spi.c | 49 +++++++++++++++++++++++++++++++++++++++++
include/linux/spi/spi.h | 4 ++++
2 files changed, 53 insertions(+)
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 44b85a8d47f1..165e4a286080 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -3621,6 +3621,55 @@ int spi_split_transfers_maxsize(struct spi_controller *ctlr,
}
EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize);
+
+/**
+ * spi_split_transfers_maxwords - split spi transfers into multiple transfers
+ * when an individual transfer exceeds a
+ * certain number of SPI words
+ * @ctlr: the @spi_controller for this transfer
+ * @msg: the @spi_message to transform
+ * @maxwords: the number of words to limit each transfer to
+ * @gfp: GFP allocation flags
+ *
+ * Return: status of transformation
+ */
+int spi_split_transfers_maxwords(struct spi_controller *ctlr,
+ struct spi_message *msg,
+ size_t maxwords,
+ gfp_t gfp)
+{
+ struct spi_transfer *xfer;
+
+ /*
+ * Iterate over the transfer_list,
+ * but note that xfer is advanced to the last transfer inserted
+ * to avoid checking sizes again unnecessarily (also xfer does
+ * potentially belong to a different list by the time the
+ * replacement has happened).
+ */
+ list_for_each_entry(xfer, &msg->transfers, transfer_list) {
+ size_t maxsize;
+ int ret;
+
+ if (xfer->bits_per_word <= 8)
+ maxsize = maxwords;
+ else if (xfer->bits_per_word <= 16)
+ maxsize = 2 * maxwords;
+ else
+ maxsize = 4 * maxwords;
+
+ if (xfer->len > maxsize) {
+ ret = __spi_split_transfer_maxsize(ctlr, msg, &xfer,
+ maxsize, gfp);
+ if (ret)
+ return ret;
+ }
+ }
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(spi_split_transfers_maxwords);
+
/*-------------------------------------------------------------------------*/
/* Core methods for SPI controller protocol drivers. Some of the
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index 4fa26b9a3572..f8dff44d77e5 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -1295,6 +1295,10 @@ extern int spi_split_transfers_maxsize(struct spi_controller *ctlr,
struct spi_message *msg,
size_t maxsize,
gfp_t gfp);
+extern int spi_split_transfers_maxwords(struct spi_controller *ctlr,
+ struct spi_message *msg,
+ size_t maxwords,
+ gfp_t gfp);
/*---------------------------------------------------------------------------*/
base-commit: fe15c26ee26efa11741a7b632e9f23b01aca4cc6
--
2.30.2
next reply other threads:[~2023-03-10 9:26 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-10 9:20 Leonard Göhrs [this message]
2023-03-10 9:20 ` [PATCH v1 2/2] spi: stm32: split large transfers based on word size instead of bytes Leonard Göhrs
2023-03-16 12:57 ` Alain Volmat
2023-03-16 14:26 ` [PATCH v1 1/2] spi: core: add spi_split_transfers_maxwords Mark Brown
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230310092053.1006459-1-l.goehrs@pengutronix.de \
--to=l.goehrs@pengutronix.de \
--cc=broonie@kernel.org \
--cc=kernel@pengutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-spi@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).