4 * Copyright (C) M'boumba Cedric Madianga 2017
5 * Author: M'boumba Cedric Madianga <cedric.madianga@gmail.com>
7 * License terms: GNU General Public License (GPL), version 2
10 #include "i2c-stm32.h"
12 /* Functions for DMA support */
13 struct stm32_i2c_dma
*stm32_i2c_dma_request(struct device
*dev
,
18 struct stm32_i2c_dma
*dma
;
19 struct dma_slave_config dma_sconfig
;
22 dma
= devm_kzalloc(dev
, sizeof(*dma
), GFP_KERNEL
);
24 return ERR_PTR(-ENOMEM
);
26 /* Request and configure I2C TX dma channel */
27 dma
->chan_tx
= dma_request_chan(dev
, "tx");
28 if (IS_ERR(dma
->chan_tx
)) {
29 dev_dbg(dev
, "can't request DMA tx channel\n");
30 ret
= PTR_ERR(dma
->chan_tx
);
34 memset(&dma_sconfig
, 0, sizeof(dma_sconfig
));
35 dma_sconfig
.dst_addr
= phy_addr
+ txdr_offset
;
36 dma_sconfig
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
37 dma_sconfig
.dst_maxburst
= 1;
38 dma_sconfig
.direction
= DMA_MEM_TO_DEV
;
39 ret
= dmaengine_slave_config(dma
->chan_tx
, &dma_sconfig
);
41 dev_err(dev
, "can't configure tx channel\n");
45 /* Request and configure I2C RX dma channel */
46 dma
->chan_rx
= dma_request_chan(dev
, "rx");
47 if (IS_ERR(dma
->chan_rx
)) {
48 dev_err(dev
, "can't request DMA rx channel\n");
49 ret
= PTR_ERR(dma
->chan_rx
);
53 memset(&dma_sconfig
, 0, sizeof(dma_sconfig
));
54 dma_sconfig
.src_addr
= phy_addr
+ rxdr_offset
;
55 dma_sconfig
.src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
56 dma_sconfig
.src_maxburst
= 1;
57 dma_sconfig
.direction
= DMA_DEV_TO_MEM
;
58 ret
= dmaengine_slave_config(dma
->chan_rx
, &dma_sconfig
);
60 dev_err(dev
, "can't configure rx channel\n");
64 init_completion(&dma
->dma_complete
);
66 dev_info(dev
, "using %s (tx) and %s (rx) for DMA transfers\n",
67 dma_chan_name(dma
->chan_tx
), dma_chan_name(dma
->chan_rx
));
72 dma_release_channel(dma
->chan_rx
);
74 dma_release_channel(dma
->chan_tx
);
77 dev_info(dev
, "can't use DMA\n");
82 void stm32_i2c_dma_free(struct stm32_i2c_dma
*dma
)
87 dma_release_channel(dma
->chan_tx
);
90 dma_release_channel(dma
->chan_rx
);
93 dma
->chan_using
= NULL
;
96 int stm32_i2c_prep_dma_xfer(struct device
*dev
, struct stm32_i2c_dma
*dma
,
97 bool rd_wr
, u32 len
, u8
*buf
,
98 dma_async_tx_callback callback
,
99 void *dma_async_param
)
101 struct dma_async_tx_descriptor
*txdesc
;
102 struct device
*chan_dev
;
106 dma
->chan_using
= dma
->chan_rx
;
107 dma
->dma_transfer_dir
= DMA_DEV_TO_MEM
;
108 dma
->dma_data_dir
= DMA_FROM_DEVICE
;
110 dma
->chan_using
= dma
->chan_tx
;
111 dma
->dma_transfer_dir
= DMA_MEM_TO_DEV
;
112 dma
->dma_data_dir
= DMA_TO_DEVICE
;
116 chan_dev
= dma
->chan_using
->device
->dev
;
118 dma
->dma_buf
= dma_map_single(chan_dev
, buf
, dma
->dma_len
,
120 if (dma_mapping_error(chan_dev
, dma
->dma_buf
)) {
121 dev_err(dev
, "DMA mapping failed\n");
125 txdesc
= dmaengine_prep_slave_single(dma
->chan_using
, dma
->dma_buf
,
127 dma
->dma_transfer_dir
,
130 dev_err(dev
, "Not able to get desc for DMA xfer\n");
135 reinit_completion(&dma
->dma_complete
);
137 txdesc
->callback
= callback
;
138 txdesc
->callback_param
= dma_async_param
;
139 ret
= dma_submit_error(dmaengine_submit(txdesc
));
141 dev_err(dev
, "DMA submit failed\n");
145 dma_async_issue_pending(dma
->chan_using
);
150 dma_unmap_single(chan_dev
, dma
->dma_buf
, dma
->dma_len
,