Add an optional .transfer_batch() callback to struct mipi_dsi_host_ops
and a mipi_dsi_dcs_write_batch() helper (analogous to i2c_transfer() and
spi_sync_transfer()) so panel drivers can submit a sequence of DCS
commands together.

When a DSI host implements .transfer_batch(), its hardware can pack
multiple command packets into a single DMA transfer and transmit them
atomically within one blanking interval.  Hosts that do not implement
.transfer_batch() fall back transparently to sequential .transfer()
calls.

Signed-off-by: Vladimir Bely <[email protected]>
---
 drivers/gpu/drm/drm_mipi_dsi.c | 98 ++++++++++++++++++++++++++++++++++
 include/drm/drm_mipi_dsi.h     | 21 ++++++++
 2 files changed, 119 insertions(+)

diff --git a/drivers/gpu/drm/drm_mipi_dsi.c b/drivers/gpu/drm/drm_mipi_dsi.c
index 3ac1dd5ad..03c077764 100644
--- a/drivers/gpu/drm/drm_mipi_dsi.c
+++ b/drivers/gpu/drm/drm_mipi_dsi.c
@@ -459,6 +459,37 @@ static ssize_t mipi_dsi_device_transfer(struct 
mipi_dsi_device *dsi,
        return ops->transfer(dsi->host, msg);
 }
 
+static int mipi_dsi_device_transfer_batch(struct mipi_dsi_device *dsi,
+                                         struct mipi_dsi_msg *msgs,
+                                         size_t count)
+{
+       const struct mipi_dsi_host_ops *ops = dsi->host->ops;
+       size_t i;
+
+       if (!count)
+               return 0;
+
+       if (!ops || (!ops->transfer_batch && !ops->transfer))
+               return -ENOSYS;
+
+       if (dsi->mode_flags & MIPI_DSI_MODE_LPM) {
+               for (i = 0; i < count; i++)
+                       msgs[i].flags |= MIPI_DSI_MSG_USE_LPM;
+       }
+
+       if (ops->transfer_batch)
+               return ops->transfer_batch(dsi->host, msgs, count);
+
+       for (i = 0; i < count; i++) {
+               ssize_t ret = ops->transfer(dsi->host, &msgs[i]);
+
+               if (ret < 0)
+                       return ret;
+       }
+
+       return 0;
+}
+
 /**
  * mipi_dsi_packet_format_is_short - check if a packet is of the short format
  * @type: MIPI DSI data type of the packet
@@ -950,6 +981,73 @@ ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device 
*dsi,
 }
 EXPORT_SYMBOL(mipi_dsi_dcs_write_buffer);
 
+/**
+ * mipi_dsi_dcs_write_batch() - transmit a batch of DCS commands
+ * @dsi: DSI peripheral device
+ * @cmds: array of DCS command buffers to transmit in order
+ * @count: number of entries in @cmds
+ *
+ * Builds a DSI message for each DCS command in @cmds and submits them via
+ * the host's .transfer_batch() callback when supported, falling back to
+ * sequential .transfer() calls otherwise.
+ *
+ * Return: 0 on success or a negative error code on failure.
+ */
+int mipi_dsi_dcs_write_batch(struct mipi_dsi_device *dsi,
+                            const struct mipi_dsi_dcs_cmd *cmds, size_t count)
+{
+       struct mipi_dsi_msg stack_msgs[8];
+       struct mipi_dsi_msg *msgs = stack_msgs;
+       size_t i;
+       int ret;
+
+       if (!count)
+               return 0;
+
+       if (!cmds)
+               return -EINVAL;
+
+       if (count > ARRAY_SIZE(stack_msgs)) {
+               msgs = kcalloc(count, sizeof(*msgs), GFP_KERNEL);
+               if (!msgs)
+                       return -ENOMEM;
+       }
+
+       for (i = 0; i < count; i++) {
+               if (!cmds[i].data || !cmds[i].len) {
+                       ret = -EINVAL;
+                       goto out;
+               }
+
+               msgs[i] = (struct mipi_dsi_msg) {
+                       .channel = dsi->channel,
+                       .tx_buf = cmds[i].data,
+                       .tx_len = cmds[i].len,
+               };
+
+               switch (cmds[i].len) {
+               case 1:
+                       msgs[i].type = MIPI_DSI_DCS_SHORT_WRITE;
+                       break;
+               case 2:
+                       msgs[i].type = MIPI_DSI_DCS_SHORT_WRITE_PARAM;
+                       break;
+               default:
+                       msgs[i].type = MIPI_DSI_DCS_LONG_WRITE;
+                       break;
+               }
+       }
+
+       ret = mipi_dsi_device_transfer_batch(dsi, msgs, count);
+
+out:
+       if (msgs != stack_msgs)
+               kfree(msgs);
+
+       return ret;
+}
+EXPORT_SYMBOL(mipi_dsi_dcs_write_batch);
+
 /**
  * mipi_dsi_dcs_write_buffer_chatty - mipi_dsi_dcs_write_buffer() w/ an error 
log
  * @dsi: DSI peripheral device
diff --git a/include/drm/drm_mipi_dsi.h b/include/drm/drm_mipi_dsi.h
index 7ff439672..e728f6c1c 100644
--- a/include/drm/drm_mipi_dsi.h
+++ b/include/drm/drm_mipi_dsi.h
@@ -64,11 +64,22 @@ struct mipi_dsi_packet {
 int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
                           const struct mipi_dsi_msg *msg);
 
+/**
+ * struct mipi_dsi_dcs_cmd - DCS command buffer for batch transfers
+ * @data: buffer containing the DCS command and parameters
+ * @len: size of @data in bytes
+ */
+struct mipi_dsi_dcs_cmd {
+       const void *data;
+       size_t len;
+};
+
 /**
  * struct mipi_dsi_host_ops - DSI bus operations
  * @attach: attach DSI device to DSI host
  * @detach: detach DSI device from DSI host
  * @transfer: transmit a DSI packet
+ * @transfer_batch: optionally transmit a batch of DSI packets in one operation
  *
  * DSI packets transmitted by .transfer() are passed in as mipi_dsi_msg
  * structures. This structure contains information about the type of packet
@@ -78,6 +89,11 @@ int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
  * transmitted for write packets or the number of bytes received for read
  * packets.
  *
+ * When implemented, .transfer_batch() transmits @count packets in order and
+ * returns 0 on success or a negative error code on failure. Host drivers whose
+ * hardware supports packing multiple command packets into a single DMA 
transfer
+ * can use it to send a sequence of commands within a single blanking period.
+ *
  * Note that typically DSI packet transmission is atomic, so the .transfer()
  * function will seldomly return anything other than the number of bytes
  * contained in the transmit buffer on success.
@@ -94,6 +110,9 @@ struct mipi_dsi_host_ops {
                      struct mipi_dsi_device *dsi);
        ssize_t (*transfer)(struct mipi_dsi_host *host,
                            const struct mipi_dsi_msg *msg);
+       int (*transfer_batch)(struct mipi_dsi_host *host,
+                             const struct mipi_dsi_msg *msgs,
+                             size_t count);
 };
 
 /**
@@ -336,6 +355,8 @@ enum mipi_dsi_dcs_tear_mode {
 
 ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
                                  const void *data, size_t len);
+int mipi_dsi_dcs_write_batch(struct mipi_dsi_device *dsi,
+                            const struct mipi_dsi_dcs_cmd *cmds, size_t count);
 int mipi_dsi_dcs_write_buffer_chatty(struct mipi_dsi_device *dsi,
                                     const void *data, size_t len);
 void mipi_dsi_dcs_write_buffer_multi(struct mipi_dsi_multi_context *ctx,
-- 
2.43.0

Reply via email to