aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c
diff options
context:
space:
mode:
authorYao Yuan <yao.yuan@freescale.com>2014-11-18 05:31:06 -0500
committerWolfram Sang <wsa@the-dreams.de>2014-11-18 10:01:27 -0500
commitce1a78840ff7ab846065d5b65eaac959bafe1949 (patch)
tree6ed38c7249c8f274d94211ee0d4cae727910bda2 /drivers/i2c
parent2fbed5119d6a07a6777b2131262587df338df22b (diff)
i2c: imx: add DMA support for freescale i2c driver
Add dma support for i2c. This function depend on DMA driver. You can turn on it by write both the dmas and dma-name properties in dts node. DMA is optional, even DMA request unsuccessfully, i2c can also work well. Signed-off-by: Yuan Yao <yao.yuan@freescale.com> Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
Diffstat (limited to 'drivers/i2c')
-rw-r--r--drivers/i2c/busses/i2c-imx.c335
1 files changed, 333 insertions, 2 deletions
diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
index d137289edfca..d0668d0d626d 100644
--- a/drivers/i2c/busses/i2c-imx.c
+++ b/drivers/i2c/busses/i2c-imx.c
@@ -33,7 +33,11 @@
33*******************************************************************************/ 33*******************************************************************************/
34 34
35#include <linux/clk.h> 35#include <linux/clk.h>
36#include <linux/completion.h>
36#include <linux/delay.h> 37#include <linux/delay.h>
38#include <linux/dma-mapping.h>
39#include <linux/dmaengine.h>
40#include <linux/dmapool.h>
37#include <linux/err.h> 41#include <linux/err.h>
38#include <linux/errno.h> 42#include <linux/errno.h>
39#include <linux/i2c.h> 43#include <linux/i2c.h>
@@ -44,6 +48,7 @@
44#include <linux/module.h> 48#include <linux/module.h>
45#include <linux/of.h> 49#include <linux/of.h>
46#include <linux/of_device.h> 50#include <linux/of_device.h>
51#include <linux/of_dma.h>
47#include <linux/platform_data/i2c-imx.h> 52#include <linux/platform_data/i2c-imx.h>
48#include <linux/platform_device.h> 53#include <linux/platform_device.h>
49#include <linux/sched.h> 54#include <linux/sched.h>
@@ -58,6 +63,15 @@
58/* Default value */ 63/* Default value */
59#define IMX_I2C_BIT_RATE 100000 /* 100kHz */ 64#define IMX_I2C_BIT_RATE 100000 /* 100kHz */
60 65
66/*
67 * Enable DMA if transfer byte size is bigger than this threshold.
68 * As the hardware request, it must bigger than 4 bytes.\
69 * I have set '16' here, maybe it's not the best but I think it's
70 * the appropriate.
71 */
72#define DMA_THRESHOLD 16
73#define DMA_TIMEOUT 1000
74
61/* IMX I2C registers: 75/* IMX I2C registers:
62 * the I2C register offset is different between SoCs, 76 * the I2C register offset is different between SoCs,
63 * to provid support for all these chips, split the 77 * to provid support for all these chips, split the
@@ -83,6 +97,7 @@
83#define I2SR_IBB 0x20 97#define I2SR_IBB 0x20
84#define I2SR_IAAS 0x40 98#define I2SR_IAAS 0x40
85#define I2SR_ICF 0x80 99#define I2SR_ICF 0x80
100#define I2CR_DMAEN 0x02
86#define I2CR_RSTA 0x04 101#define I2CR_RSTA 0x04
87#define I2CR_TXAK 0x08 102#define I2CR_TXAK 0x08
88#define I2CR_MTX 0x10 103#define I2CR_MTX 0x10
@@ -169,6 +184,17 @@ struct imx_i2c_hwdata {
169 unsigned i2cr_ien_opcode; 184 unsigned i2cr_ien_opcode;
170}; 185};
171 186
187struct imx_i2c_dma {
188 struct dma_chan *chan_tx;
189 struct dma_chan *chan_rx;
190 struct dma_chan *chan_using;
191 struct completion cmd_complete;
192 dma_addr_t dma_buf;
193 unsigned int dma_len;
194 enum dma_transfer_direction dma_transfer_dir;
195 enum dma_data_direction dma_data_dir;
196};
197
172struct imx_i2c_struct { 198struct imx_i2c_struct {
173 struct i2c_adapter adapter; 199 struct i2c_adapter adapter;
174 struct clk *clk; 200 struct clk *clk;
@@ -181,6 +207,8 @@ struct imx_i2c_struct {
181 unsigned int cur_clk; 207 unsigned int cur_clk;
182 unsigned int bitrate; 208 unsigned int bitrate;
183 const struct imx_i2c_hwdata *hwdata; 209 const struct imx_i2c_hwdata *hwdata;
210
211 struct imx_i2c_dma *dma;
184}; 212};
185 213
186static const struct imx_i2c_hwdata imx1_i2c_hwdata = { 214static const struct imx_i2c_hwdata imx1_i2c_hwdata = {
@@ -251,6 +279,138 @@ static inline unsigned char imx_i2c_read_reg(struct imx_i2c_struct *i2c_imx,
251 return readb(i2c_imx->base + (reg << i2c_imx->hwdata->regshift)); 279 return readb(i2c_imx->base + (reg << i2c_imx->hwdata->regshift));
252} 280}
253 281
282/* Functions for DMA support */
283static void i2c_imx_dma_request(struct imx_i2c_struct *i2c_imx,
284 dma_addr_t phy_addr)
285{
286 struct imx_i2c_dma *dma;
287 struct dma_slave_config dma_sconfig;
288 struct device *dev = &i2c_imx->adapter.dev;
289 int ret;
290
291 dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
292 if (!dma)
293 return;
294
295 dma->chan_tx = dma_request_slave_channel(dev, "tx");
296 if (!dma->chan_tx) {
297 dev_dbg(dev, "can't request DMA tx channel\n");
298 ret = -ENODEV;
299 goto fail_al;
300 }
301
302 dma_sconfig.dst_addr = phy_addr +
303 (IMX_I2C_I2DR << i2c_imx->hwdata->regshift);
304 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
305 dma_sconfig.dst_maxburst = 1;
306 dma_sconfig.direction = DMA_MEM_TO_DEV;
307 ret = dmaengine_slave_config(dma->chan_tx, &dma_sconfig);
308 if (ret < 0) {
309 dev_dbg(dev, "can't configure tx channel\n");
310 goto fail_tx;
311 }
312
313 dma->chan_rx = dma_request_slave_channel(dev, "rx");
314 if (!dma->chan_rx) {
315 dev_dbg(dev, "can't request DMA rx channel\n");
316 ret = -ENODEV;
317 goto fail_tx;
318 }
319
320 dma_sconfig.src_addr = phy_addr +
321 (IMX_I2C_I2DR << i2c_imx->hwdata->regshift);
322 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
323 dma_sconfig.src_maxburst = 1;
324 dma_sconfig.direction = DMA_DEV_TO_MEM;
325 ret = dmaengine_slave_config(dma->chan_rx, &dma_sconfig);
326 if (ret < 0) {
327 dev_dbg(dev, "can't configure rx channel\n");
328 goto fail_rx;
329 }
330
331 i2c_imx->dma = dma;
332 init_completion(&dma->cmd_complete);
333 dev_info(dev, "using %s (tx) and %s (rx) for DMA transfers\n",
334 dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
335
336 return;
337
338fail_rx:
339 dma_release_channel(dma->chan_rx);
340fail_tx:
341 dma_release_channel(dma->chan_tx);
342fail_al:
343 devm_kfree(dev, dma);
344 dev_info(dev, "can't use DMA\n");
345}
346
347static void i2c_imx_dma_callback(void *arg)
348{
349 struct imx_i2c_struct *i2c_imx = (struct imx_i2c_struct *)arg;
350 struct imx_i2c_dma *dma = i2c_imx->dma;
351
352 dma_unmap_single(dma->chan_using->device->dev, dma->dma_buf,
353 dma->dma_len, dma->dma_data_dir);
354 complete(&dma->cmd_complete);
355}
356
357static int i2c_imx_dma_xfer(struct imx_i2c_struct *i2c_imx,
358 struct i2c_msg *msgs)
359{
360 struct imx_i2c_dma *dma = i2c_imx->dma;
361 struct dma_async_tx_descriptor *txdesc;
362 struct device *dev = &i2c_imx->adapter.dev;
363 struct device *chan_dev = dma->chan_using->device->dev;
364
365 dma->dma_buf = dma_map_single(chan_dev, msgs->buf,
366 dma->dma_len, dma->dma_data_dir);
367 if (dma_mapping_error(chan_dev, dma->dma_buf)) {
368 dev_err(dev, "DMA mapping failed\n");
369 goto err_map;
370 }
371
372 txdesc = dmaengine_prep_slave_single(dma->chan_using, dma->dma_buf,
373 dma->dma_len, dma->dma_transfer_dir,
374 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
375 if (!txdesc) {
376 dev_err(dev, "Not able to get desc for DMA xfer\n");
377 goto err_desc;
378 }
379
380 txdesc->callback = i2c_imx_dma_callback;
381 txdesc->callback_param = i2c_imx;
382 if (dma_submit_error(dmaengine_submit(txdesc))) {
383 dev_err(dev, "DMA submit failed\n");
384 goto err_submit;
385 }
386
387 dma_async_issue_pending(dma->chan_using);
388 return 0;
389
390err_submit:
391err_desc:
392 dma_unmap_single(chan_dev, dma->dma_buf,
393 dma->dma_len, dma->dma_data_dir);
394err_map:
395 return -EINVAL;
396}
397
398static void i2c_imx_dma_free(struct imx_i2c_struct *i2c_imx)
399{
400 struct imx_i2c_dma *dma = i2c_imx->dma;
401
402 dma->dma_buf = 0;
403 dma->dma_len = 0;
404
405 dma_release_channel(dma->chan_tx);
406 dma->chan_tx = NULL;
407
408 dma_release_channel(dma->chan_rx);
409 dma->chan_rx = NULL;
410
411 dma->chan_using = NULL;
412}
413
254/** Functions for IMX I2C adapter driver *************************************** 414/** Functions for IMX I2C adapter driver ***************************************
255*******************************************************************************/ 415*******************************************************************************/
256 416
@@ -382,6 +542,7 @@ static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
382 i2c_imx->stopped = 0; 542 i2c_imx->stopped = 0;
383 543
384 temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK; 544 temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK;
545 temp &= ~I2CR_DMAEN;
385 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); 546 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
386 return result; 547 return result;
387} 548}
@@ -395,6 +556,8 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
395 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__); 556 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
396 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR); 557 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
397 temp &= ~(I2CR_MSTA | I2CR_MTX); 558 temp &= ~(I2CR_MSTA | I2CR_MTX);
559 if (i2c_imx->dma)
560 temp &= ~I2CR_DMAEN;
398 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR); 561 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
399 } 562 }
400 if (is_imx1_i2c(i2c_imx)) { 563 if (is_imx1_i2c(i2c_imx)) {
@@ -435,6 +598,159 @@ static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
435 return IRQ_NONE; 598 return IRQ_NONE;
436} 599}
437 600
601static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx,
602 struct i2c_msg *msgs)
603{
604 int result;
605 unsigned int temp = 0;
606 unsigned long orig_jiffies = jiffies;
607 struct imx_i2c_dma *dma = i2c_imx->dma;
608 struct device *dev = &i2c_imx->adapter.dev;
609
610 dma->chan_using = dma->chan_tx;
611 dma->dma_transfer_dir = DMA_MEM_TO_DEV;
612 dma->dma_data_dir = DMA_TO_DEVICE;
613 dma->dma_len = msgs->len - 1;
614 result = i2c_imx_dma_xfer(i2c_imx, msgs);
615 if (result)
616 return result;
617
618 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
619 temp |= I2CR_DMAEN;
620 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
621
622 /*
623 * Write slave address.
624 * The first byte must be transmitted by the CPU.
625 */
626 imx_i2c_write_reg(msgs->addr << 1, i2c_imx, IMX_I2C_I2DR);
627 reinit_completion(&i2c_imx->dma->cmd_complete);
628 result = wait_for_completion_timeout(
629 &i2c_imx->dma->cmd_complete,
630 msecs_to_jiffies(DMA_TIMEOUT));
631 if (result <= 0) {
632 dmaengine_terminate_all(dma->chan_using);
633 return result ?: -ETIMEDOUT;
634 }
635
636 /* Waiting for transfer complete. */
637 while (1) {
638 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
639 if (temp & I2SR_ICF)
640 break;
641 if (time_after(jiffies, orig_jiffies +
642 msecs_to_jiffies(DMA_TIMEOUT))) {
643 dev_dbg(dev, "<%s> Timeout\n", __func__);
644 return -ETIMEDOUT;
645 }
646 schedule();
647 }
648
649 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
650 temp &= ~I2CR_DMAEN;
651 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
652
653 /* The last data byte must be transferred by the CPU. */
654 imx_i2c_write_reg(msgs->buf[msgs->len-1],
655 i2c_imx, IMX_I2C_I2DR);
656 result = i2c_imx_trx_complete(i2c_imx);
657 if (result)
658 return result;
659
660 result = i2c_imx_acked(i2c_imx);
661 if (result)
662 return result;
663
664 return 0;
665}
666
667static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
668 struct i2c_msg *msgs, bool is_lastmsg)
669{
670 int result;
671 unsigned int temp;
672 unsigned long orig_jiffies = jiffies;
673 struct imx_i2c_dma *dma = i2c_imx->dma;
674 struct device *dev = &i2c_imx->adapter.dev;
675
676 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
677 temp |= I2CR_DMAEN;
678 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
679
680 dma->chan_using = dma->chan_rx;
681 dma->dma_transfer_dir = DMA_DEV_TO_MEM;
682 dma->dma_data_dir = DMA_FROM_DEVICE;
683 /* The last two data bytes must be transferred by the CPU. */
684 dma->dma_len = msgs->len - 2;
685 result = i2c_imx_dma_xfer(i2c_imx, msgs);
686 if (result)
687 return result;
688
689 reinit_completion(&i2c_imx->dma->cmd_complete);
690 result = wait_for_completion_timeout(
691 &i2c_imx->dma->cmd_complete,
692 msecs_to_jiffies(DMA_TIMEOUT));
693 if (result <= 0) {
694 dmaengine_terminate_all(dma->chan_using);
695 return result ?: -ETIMEDOUT;
696 }
697
698 /* waiting for transfer complete. */
699 while (1) {
700 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
701 if (temp & I2SR_ICF)
702 break;
703 if (time_after(jiffies, orig_jiffies +
704 msecs_to_jiffies(DMA_TIMEOUT))) {
705 dev_dbg(dev, "<%s> Timeout\n", __func__);
706 return -ETIMEDOUT;
707 }
708 schedule();
709 }
710
711 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
712 temp &= ~I2CR_DMAEN;
713 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
714
715 /* read n-1 byte data */
716 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
717 temp |= I2CR_TXAK;
718 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
719
720 msgs->buf[msgs->len-2] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
721 /* read n byte data */
722 result = i2c_imx_trx_complete(i2c_imx);
723 if (result)
724 return result;
725
726 if (is_lastmsg) {
727 /*
728 * It must generate STOP before read I2DR to prevent
729 * controller from generating another clock cycle
730 */
731 dev_dbg(dev, "<%s> clear MSTA\n", __func__);
732 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
733 temp &= ~(I2CR_MSTA | I2CR_MTX);
734 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
735 i2c_imx_bus_busy(i2c_imx, 0);
736 i2c_imx->stopped = 1;
737 } else {
738 /*
739 * For i2c master receiver repeat restart operation like:
740 * read -> repeat MSTA -> read/write
741 * The controller must set MTX before read the last byte in
742 * the first read operation, otherwise the first read cost
743 * one extra clock cycle.
744 */
745 temp = readb(i2c_imx->base + IMX_I2C_I2CR);
746 temp |= I2CR_MTX;
747 writeb(temp, i2c_imx->base + IMX_I2C_I2CR);
748 }
749 msgs->buf[msgs->len-1] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
750
751 return 0;
752}
753
438static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs) 754static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
439{ 755{
440 int i, result; 756 int i, result;
@@ -504,6 +820,9 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bo
504 820
505 dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__); 821 dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
506 822
823 if (i2c_imx->dma && msgs->len >= DMA_THRESHOLD && !block_data)
824 return i2c_imx_dma_read(i2c_imx, msgs, is_lastmsg);
825
507 /* read data */ 826 /* read data */
508 for (i = 0; i < msgs->len; i++) { 827 for (i = 0; i < msgs->len; i++) {
509 u8 len = 0; 828 u8 len = 0;
@@ -618,8 +937,12 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter,
618#endif 937#endif
619 if (msgs[i].flags & I2C_M_RD) 938 if (msgs[i].flags & I2C_M_RD)
620 result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg); 939 result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg);
621 else 940 else {
622 result = i2c_imx_write(i2c_imx, &msgs[i]); 941 if (i2c_imx->dma && msgs[i].len >= DMA_THRESHOLD)
942 result = i2c_imx_dma_write(i2c_imx, &msgs[i]);
943 else
944 result = i2c_imx_write(i2c_imx, &msgs[i]);
945 }
623 if (result) 946 if (result)
624 goto fail0; 947 goto fail0;
625 } 948 }
@@ -654,6 +977,7 @@ static int i2c_imx_probe(struct platform_device *pdev)
654 struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev); 977 struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev);
655 void __iomem *base; 978 void __iomem *base;
656 int irq, ret; 979 int irq, ret;
980 dma_addr_t phy_addr;
657 981
658 dev_dbg(&pdev->dev, "<%s>\n", __func__); 982 dev_dbg(&pdev->dev, "<%s>\n", __func__);
659 983
@@ -668,6 +992,7 @@ static int i2c_imx_probe(struct platform_device *pdev)
668 if (IS_ERR(base)) 992 if (IS_ERR(base))
669 return PTR_ERR(base); 993 return PTR_ERR(base);
670 994
995 phy_addr = (dma_addr_t)res->start;
671 i2c_imx = devm_kzalloc(&pdev->dev, sizeof(*i2c_imx), GFP_KERNEL); 996 i2c_imx = devm_kzalloc(&pdev->dev, sizeof(*i2c_imx), GFP_KERNEL);
672 if (!i2c_imx) 997 if (!i2c_imx)
673 return -ENOMEM; 998 return -ENOMEM;
@@ -742,6 +1067,9 @@ static int i2c_imx_probe(struct platform_device *pdev)
742 i2c_imx->adapter.name); 1067 i2c_imx->adapter.name);
743 dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n"); 1068 dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
744 1069
1070 /* Init DMA config if support*/
1071 i2c_imx_dma_request(i2c_imx, phy_addr);
1072
745 return 0; /* Return OK */ 1073 return 0; /* Return OK */
746 1074
747clk_disable: 1075clk_disable:
@@ -757,6 +1085,9 @@ static int i2c_imx_remove(struct platform_device *pdev)
757 dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n"); 1085 dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n");
758 i2c_del_adapter(&i2c_imx->adapter); 1086 i2c_del_adapter(&i2c_imx->adapter);
759 1087
1088 if (i2c_imx->dma)
1089 i2c_imx_dma_free(i2c_imx);
1090
760 /* setup chip registers to defaults */ 1091 /* setup chip registers to defaults */
761 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR); 1092 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR);
762 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR); 1093 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR);