17 #include <linux/slab.h>
18 #include <linux/device.h>
19 #include <linux/module.h>
20 #include <linux/i2c.h>
36 #define DRIVER_NAME "mxs-i2c"
38 #define MXS_I2C_CTRL0 (0x00)
39 #define MXS_I2C_CTRL0_SET (0x04)
41 #define MXS_I2C_CTRL0_SFTRST 0x80000000
42 #define MXS_I2C_CTRL0_SEND_NAK_ON_LAST 0x02000000
43 #define MXS_I2C_CTRL0_RETAIN_CLOCK 0x00200000
44 #define MXS_I2C_CTRL0_POST_SEND_STOP 0x00100000
45 #define MXS_I2C_CTRL0_PRE_SEND_START 0x00080000
46 #define MXS_I2C_CTRL0_MASTER_MODE 0x00020000
47 #define MXS_I2C_CTRL0_DIRECTION 0x00010000
48 #define MXS_I2C_CTRL0_XFER_COUNT(v) ((v) & 0x0000FFFF)
50 #define MXS_I2C_TIMING0 (0x10)
51 #define MXS_I2C_TIMING1 (0x20)
52 #define MXS_I2C_TIMING2 (0x30)
54 #define MXS_I2C_CTRL1 (0x40)
55 #define MXS_I2C_CTRL1_SET (0x44)
56 #define MXS_I2C_CTRL1_CLR (0x48)
58 #define MXS_I2C_CTRL1_BUS_FREE_IRQ 0x80
59 #define MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ 0x40
60 #define MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ 0x20
61 #define MXS_I2C_CTRL1_OVERSIZE_XFER_TERM_IRQ 0x10
62 #define MXS_I2C_CTRL1_EARLY_TERM_IRQ 0x08
63 #define MXS_I2C_CTRL1_MASTER_LOSS_IRQ 0x04
64 #define MXS_I2C_CTRL1_SLAVE_STOP_IRQ 0x02
65 #define MXS_I2C_CTRL1_SLAVE_IRQ 0x01
67 #define MXS_I2C_IRQ_MASK (MXS_I2C_CTRL1_DATA_ENGINE_CMPLT_IRQ | \
68 MXS_I2C_CTRL1_NO_SLAVE_ACK_IRQ | \
69 MXS_I2C_CTRL1_EARLY_TERM_IRQ | \
70 MXS_I2C_CTRL1_MASTER_LOSS_IRQ | \
71 MXS_I2C_CTRL1_SLAVE_STOP_IRQ | \
72 MXS_I2C_CTRL1_SLAVE_IRQ)
75 #define MXS_CMD_I2C_SELECT (MXS_I2C_CTRL0_RETAIN_CLOCK | \
76 MXS_I2C_CTRL0_PRE_SEND_START | \
77 MXS_I2C_CTRL0_MASTER_MODE | \
78 MXS_I2C_CTRL0_DIRECTION | \
79 MXS_I2C_CTRL0_XFER_COUNT(1))
81 #define MXS_CMD_I2C_WRITE (MXS_I2C_CTRL0_PRE_SEND_START | \
82 MXS_I2C_CTRL0_MASTER_MODE | \
83 MXS_I2C_CTRL0_DIRECTION)
85 #define MXS_CMD_I2C_READ (MXS_I2C_CTRL0_SEND_NAK_ON_LAST | \
86 MXS_I2C_CTRL0_MASTER_MODE)
106 .timing0 = 0x00780030,
107 .timing1 = 0x00800030,
108 .timing2 = 0x00300030,
112 .timing0 = 0x000f0007,
113 .timing1 = 0x001f000f,
114 .timing2 = 0x00300030,
155 static void mxs_i2c_dma_finish(
struct mxs_i2c_dev *i2c)
165 static void mxs_i2c_dma_irq_callback(
void *
param)
170 mxs_i2c_dma_finish(i2c);
173 static int mxs_i2c_dma_setup_xfer(
struct i2c_adapter *adap,
189 desc = dmaengine_prep_slave_sg(i2c->
dmach,
194 "Failed to get PIO reg. write descriptor.\n");
195 goto select_init_pio_fail;
201 desc = dmaengine_prep_slave_sg(i2c->
dmach, &i2c->
sg_io[0], 1,
206 "Failed to get DMA data write descriptor.\n");
207 goto select_init_dma_fail;
217 desc = dmaengine_prep_slave_sg(i2c->
dmach,
222 "Failed to get PIO reg. write descriptor.\n");
223 goto select_init_dma_fail;
229 desc = dmaengine_prep_slave_sg(i2c->
dmach, &i2c->
sg_io[1], 1,
234 "Failed to get DMA data write descriptor.\n");
235 goto read_init_dma_fail;
248 desc = dmaengine_prep_slave_sg(i2c->
dmach,
253 "Failed to get PIO reg. write descriptor.\n");
254 goto write_init_pio_fail;
262 desc = dmaengine_prep_slave_sg(i2c->
dmach, i2c->
sg_io, 2,
267 "Failed to get DMA data write descriptor.\n");
268 goto write_init_dma_fail;
276 desc->
callback = mxs_i2c_dma_irq_callback;
280 dmaengine_submit(desc);
281 dma_async_issue_pending(i2c->
dmach);
287 select_init_dma_fail:
289 select_init_pio_fail:
290 dmaengine_terminate_all(i2c->
dmach);
297 dmaengine_terminate_all(i2c->
dmach);
313 dev_dbg(i2c->
dev,
"addr: 0x%04x, len: %d, flags: 0x%x, stop: %d\n",
322 ret = mxs_i2c_dma_setup_xfer(adap, msg, flags);
340 mxs_i2c_dma_finish(i2c);
351 for (i = 0; i < num; i++) {
352 err = mxs_i2c_xfer_msg(adap, &msgs[i], i == (num - 1));
387 .master_xfer = mxs_i2c_xfer,
388 .functionality = mxs_i2c_func,
406 static int mxs_i2c_get_ofdata(
struct mxs_i2c_dev *i2c)
417 ret = of_property_read_u32(node,
"fsl,i2c-dma-channel",
420 dev_err(dev,
"Failed to get DMA channel!\n");
424 ret = of_property_read_u32(node,
"clock-frequency", &speed);
426 dev_warn(dev,
"No I2C speed selected, using 100kHz\n");
427 else if (speed == 400000)
428 i2c->
speed = &mxs_i2c_400kHz_config;
429 else if (speed != 100000)
430 dev_warn(dev,
"Unsupported I2C speed selected, using 100kHz\n");
446 pinctrl = devm_pinctrl_get_select_default(dev);
448 return PTR_ERR(pinctrl);
458 if (!res || irq < 0 || dmairq < 0)
461 res_size = resource_size(res);
469 err = devm_request_irq(dev, irq, mxs_i2c_isr, 0, dev_name(dev), i2c);
474 i2c->
speed = &mxs_i2c_95kHz_config;
477 err = mxs_i2c_get_ofdata(i2c);
488 dev_err(dev,
"Failed to request dma\n");
492 platform_set_drvdata(pdev, i2c);
500 adap->
algo = &mxs_i2c_algo;
503 adap->
dev.of_node = pdev->
dev.of_node;
504 i2c_set_adapdata(adap, i2c);
507 dev_err(dev,
"Failed to add adapter (%d)\n", err);
520 struct mxs_i2c_dev *i2c = platform_get_drvdata(pdev);
532 platform_set_drvdata(pdev,
NULL);
538 { .compatible =
"fsl,imx28-i2c", },
547 .of_match_table = mxs_i2c_dt_ids,
552 static int __init mxs_i2c_init(
void)
558 static void __exit mxs_i2c_exit(
void)