]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
i2c: add helpers to ease DMA handling
authorKai-Heng Feng <kai.heng.feng@canonical.com>
Thu, 25 Apr 2019 04:43:33 +0000 (12:43 +0800)
committerKleber Sacilotto de Souza <kleber.souza@canonical.com>
Wed, 15 May 2019 12:46:29 +0000 (14:46 +0200)
BugLink: https://bugs.launchpad.net/bugs/1787775
From: Wolfram Sang <wsa+renesas@sang-engineering.com>

One helper checks if DMA is suitable and optionally creates a bounce
buffer, if not. The other function returns the bounce buffer and makes
sure the data is properly copied back to the message.

Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Wolfram Sang <wsa+renesas@sang-engineering.com>
Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
(cherry picked from commit e94bc5d18be03dac8e9d73d30c5523728edeff76)
Signed-off-by: Kai-Heng Feng <kai.heng.feng@canonical.com>
Acked-by: Khalid Elmously <khalid.elmously@canonical.com>
Acked-by: Kleber Souza <kleber.souza@canonical.com>
Signed-off-by: Khalid Elmously <khalid.elmously@canonical.com>
drivers/i2c/i2c-core-base.c
include/linux/i2c.h

index f7829a74140cf1b764d8ed3f8e00ec4e60f9eaad..b6d3dcb62992e653baf8f848e84476433619baf9 100644 (file)
@@ -2265,6 +2265,52 @@ void i2c_put_adapter(struct i2c_adapter *adap)
 }
 EXPORT_SYMBOL(i2c_put_adapter);
 
+/**
+ * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
+ * @msg: the message to be checked
+ * @threshold: the minimum number of bytes for which using DMA makes sense
+ *
+ * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
+ *        Or a valid pointer to be used with DMA. After use, release it by
+ *        calling i2c_release_dma_safe_msg_buf().
+ *
+ * This function must only be called from process context!
+ */
+u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
+{
+       if (msg->len < threshold)
+               return NULL;
+
+       if (msg->flags & I2C_M_DMA_SAFE)
+               return msg->buf;
+
+       pr_debug("using bounce buffer for addr=0x%02x, len=%d\n",
+                msg->addr, msg->len);
+
+       if (msg->flags & I2C_M_RD)
+               return kzalloc(msg->len, GFP_KERNEL);
+       else
+               return kmemdup(msg->buf, msg->len, GFP_KERNEL);
+}
+EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);
+
+/**
+ * i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
+ * @msg: the message to be synced with
+ * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
+ */
+void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf)
+{
+       if (!buf || buf == msg->buf)
+               return;
+
+       if (msg->flags & I2C_M_RD)
+               memcpy(msg->buf, buf, msg->len);
+
+       kfree(buf);
+}
+EXPORT_SYMBOL_GPL(i2c_release_dma_safe_msg_buf);
+
 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
 MODULE_DESCRIPTION("I2C-Bus main module");
 MODULE_LICENSE("GPL");
index 0f774406fad0e4da2d5261afad7cddf1da0f984b..a0b57de91e21d37a30e1d379c16e922524492a42 100644 (file)
@@ -769,6 +769,9 @@ static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)
        return (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0);
 }
 
+u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold);
+void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf);
+
 int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr);
 /**
  * module_i2c_driver() - Helper macro for registering a modular I2C driver