Skip to content

Commit e94bc5d

Browse files
Wolfram SangWolfram Sang
authored andcommitted
i2c: add helpers to ease DMA handling
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>
1 parent 521a72e commit e94bc5d

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

drivers/i2c/i2c-core-base.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,6 +2200,52 @@ void i2c_put_adapter(struct i2c_adapter *adap)
22002200
}
22012201
EXPORT_SYMBOL(i2c_put_adapter);
22022202

2203+
/**
2204+
* i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
2205+
* @msg: the message to be checked
2206+
* @threshold: the minimum number of bytes for which using DMA makes sense
2207+
*
2208+
* Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
2209+
* Or a valid pointer to be used with DMA. After use, release it by
2210+
* calling i2c_release_dma_safe_msg_buf().
2211+
*
2212+
* This function must only be called from process context!
2213+
*/
2214+
u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
2215+
{
2216+
if (msg->len < threshold)
2217+
return NULL;
2218+
2219+
if (msg->flags & I2C_M_DMA_SAFE)
2220+
return msg->buf;
2221+
2222+
pr_debug("using bounce buffer for addr=0x%02x, len=%d\n",
2223+
msg->addr, msg->len);
2224+
2225+
if (msg->flags & I2C_M_RD)
2226+
return kzalloc(msg->len, GFP_KERNEL);
2227+
else
2228+
return kmemdup(msg->buf, msg->len, GFP_KERNEL);
2229+
}
2230+
EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);
2231+
2232+
/**
2233+
* i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
2234+
* @msg: the message to be synced with
2235+
* @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
2236+
*/
2237+
void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf)
2238+
{
2239+
if (!buf || buf == msg->buf)
2240+
return;
2241+
2242+
if (msg->flags & I2C_M_RD)
2243+
memcpy(msg->buf, buf, msg->len);
2244+
2245+
kfree(buf);
2246+
}
2247+
EXPORT_SYMBOL_GPL(i2c_release_dma_safe_msg_buf);
2248+
22032249
MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
22042250
MODULE_DESCRIPTION("I2C-Bus main module");
22052251
MODULE_LICENSE("GPL");

include/linux/i2c.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,9 @@ static inline u8 i2c_8bit_addr_from_msg(const struct i2c_msg *msg)
768768
return (msg->addr << 1) | (msg->flags & I2C_M_RD ? 1 : 0);
769769
}
770770

771+
u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold);
772+
void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf);
773+
771774
int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr);
772775
/**
773776
* module_i2c_driver() - Helper macro for registering a modular I2C driver

0 commit comments

Comments
 (0)