1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2002 Motorola GSG-China
6 * Darius Augulis, Teltonika Inc.
9 * Implementation of I2C Adapter/Algorithm Driver
10 * for I2C Bus integrated in Freescale i.MX/MXC processors
12 * Derived from Motorola GSG China I2C example driver
14 * Copyright (C) 2005 Torsten Koschorrek <koschorrek at synertronixx.de
15 * Copyright (C) 2005 Matthias Blaschke <blaschke at synertronixx.de
16 * Copyright (C) 2007 RightHand Technologies, Inc.
17 * Copyright (C) 2008 Darius Augulis <darius.augulis at teltonika.lt>
19 * Copyright 2013 Freescale Semiconductor, Inc.
23 #include <linux/clk.h>
24 #include <linux/completion.h>
25 #include <linux/delay.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/dmaengine.h>
28 #include <linux/dmapool.h>
29 #include <linux/err.h>
30 #include <linux/errno.h>
31 #include <linux/gpio/consumer.h>
32 #include <linux/i2c.h>
33 #include <linux/init.h>
34 #include <linux/interrupt.h>
36 #include <linux/kernel.h>
37 #include <linux/module.h>
39 #include <linux/of_device.h>
40 #include <linux/of_dma.h>
41 #include <linux/pinctrl/consumer.h>
42 #include <linux/platform_data/i2c-imx.h>
43 #include <linux/platform_device.h>
44 #include <linux/pm_runtime.h>
45 #include <linux/sched.h>
46 #include <linux/slab.h>
48 /* This will be the driver name the kernel reports */
49 #define DRIVER_NAME "imx-i2c"
52 #define IMX_I2C_BIT_RATE 100000 /* 100kHz */
55 * Enable DMA if transfer byte size is bigger than this threshold.
56 * As the hardware request, it must bigger than 4 bytes.\
57 * I have set '16' here, maybe it's not the best but I think it's
60 #define DMA_THRESHOLD 16
61 #define DMA_TIMEOUT 1000
64 * the I2C register offset is different between SoCs,
65 * to provid support for all these chips, split the
66 * register offset into a fixed base address and a
67 * variable shift value, then the full register offset
68 * will be calculated by
69 * reg_off = ( reg_base_addr << reg_shift)
71 #define IMX_I2C_IADR 0x00 /* i2c slave address */
72 #define IMX_I2C_IFDR 0x01 /* i2c frequency divider */
73 #define IMX_I2C_I2CR 0x02 /* i2c control */
74 #define IMX_I2C_I2SR 0x03 /* i2c status */
75 #define IMX_I2C_I2DR 0x04 /* i2c transfer data */
77 #define IMX_I2C_REGSHIFT 2
78 #define VF610_I2C_REGSHIFT 0
80 /* Bits of IMX I2C registers */
81 #define I2SR_RXAK 0x01
86 #define I2SR_IAAS 0x40
88 #define I2CR_DMAEN 0x02
89 #define I2CR_RSTA 0x04
90 #define I2CR_TXAK 0x08
92 #define I2CR_MSTA 0x20
93 #define I2CR_IIEN 0x40
96 /* register bits different operating codes definition:
97 * 1) I2SR: Interrupt flags clear operation differ between SoCs:
98 * - write zero to clear(w0c) INT flag on i.MX,
99 * - but write one to clear(w1c) INT flag on Vybrid.
100 * 2) I2CR: I2C module enable operation also differ between SoCs:
101 * - set I2CR_IEN bit enable the module on i.MX,
102 * - but clear I2CR_IEN bit enable the module on Vybrid.
104 #define I2SR_CLR_OPCODE_W0C 0x0
105 #define I2SR_CLR_OPCODE_W1C (I2SR_IAL | I2SR_IIF)
106 #define I2CR_IEN_OPCODE_0 0x0
107 #define I2CR_IEN_OPCODE_1 I2CR_IEN
109 #define I2C_PM_TIMEOUT 10 /* ms */
112 * sorted list of clock divider, register value pairs
113 * taken from table 26-5, p.26-9, Freescale i.MX
114 * Integrated Portable System Processor Reference Manual
115 * Document Number: MC9328MXLRM, Rev. 5.1, 06/2007
117 * Duplicated divider values removed from list
119 struct imx_i2c_clk_pair {
124 static struct imx_i2c_clk_pair imx_i2c_clk_div[] = {
125 { 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
126 { 30, 0x00 }, { 32, 0x24 }, { 36, 0x25 }, { 40, 0x26 },
127 { 42, 0x03 }, { 44, 0x27 }, { 48, 0x28 }, { 52, 0x05 },
128 { 56, 0x29 }, { 60, 0x06 }, { 64, 0x2A }, { 72, 0x2B },
129 { 80, 0x2C }, { 88, 0x09 }, { 96, 0x2D }, { 104, 0x0A },
130 { 112, 0x2E }, { 128, 0x2F }, { 144, 0x0C }, { 160, 0x30 },
131 { 192, 0x31 }, { 224, 0x32 }, { 240, 0x0F }, { 256, 0x33 },
132 { 288, 0x10 }, { 320, 0x34 }, { 384, 0x35 }, { 448, 0x36 },
133 { 480, 0x13 }, { 512, 0x37 }, { 576, 0x14 }, { 640, 0x38 },
134 { 768, 0x39 }, { 896, 0x3A }, { 960, 0x17 }, { 1024, 0x3B },
135 { 1152, 0x18 }, { 1280, 0x3C }, { 1536, 0x3D }, { 1792, 0x3E },
136 { 1920, 0x1B }, { 2048, 0x3F }, { 2304, 0x1C }, { 2560, 0x1D },
137 { 3072, 0x1E }, { 3840, 0x1F }
140 /* Vybrid VF610 clock divider, register value pairs */
141 static struct imx_i2c_clk_pair vf610_i2c_clk_div[] = {
142 { 20, 0x00 }, { 22, 0x01 }, { 24, 0x02 }, { 26, 0x03 },
143 { 28, 0x04 }, { 30, 0x05 }, { 32, 0x09 }, { 34, 0x06 },
144 { 36, 0x0A }, { 40, 0x07 }, { 44, 0x0C }, { 48, 0x0D },
145 { 52, 0x43 }, { 56, 0x0E }, { 60, 0x45 }, { 64, 0x12 },
146 { 68, 0x0F }, { 72, 0x13 }, { 80, 0x14 }, { 88, 0x15 },
147 { 96, 0x19 }, { 104, 0x16 }, { 112, 0x1A }, { 128, 0x17 },
148 { 136, 0x4F }, { 144, 0x1C }, { 160, 0x1D }, { 176, 0x55 },
149 { 192, 0x1E }, { 208, 0x56 }, { 224, 0x22 }, { 228, 0x24 },
150 { 240, 0x1F }, { 256, 0x23 }, { 288, 0x5C }, { 320, 0x25 },
151 { 384, 0x26 }, { 448, 0x2A }, { 480, 0x27 }, { 512, 0x2B },
152 { 576, 0x2C }, { 640, 0x2D }, { 768, 0x31 }, { 896, 0x32 },
153 { 960, 0x2F }, { 1024, 0x33 }, { 1152, 0x34 }, { 1280, 0x35 },
154 { 1536, 0x36 }, { 1792, 0x3A }, { 1920, 0x37 }, { 2048, 0x3B },
155 { 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A },
156 { 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E },
165 struct imx_i2c_hwdata {
166 enum imx_i2c_type devtype;
168 struct imx_i2c_clk_pair *clk_div;
170 unsigned i2sr_clr_opcode;
171 unsigned i2cr_ien_opcode;
175 struct dma_chan *chan_tx;
176 struct dma_chan *chan_rx;
177 struct dma_chan *chan_using;
178 struct completion cmd_complete;
180 unsigned int dma_len;
181 enum dma_transfer_direction dma_transfer_dir;
182 enum dma_data_direction dma_data_dir;
185 struct imx_i2c_struct {
186 struct i2c_adapter adapter;
188 struct notifier_block clk_change_nb;
190 wait_queue_head_t queue;
192 unsigned int disable_delay;
194 unsigned int ifdr; /* IMX_I2C_IFDR */
195 unsigned int cur_clk;
196 unsigned int bitrate;
197 const struct imx_i2c_hwdata *hwdata;
198 struct i2c_bus_recovery_info rinfo;
200 struct pinctrl *pinctrl;
201 struct pinctrl_state *pinctrl_pins_default;
202 struct pinctrl_state *pinctrl_pins_gpio;
204 struct imx_i2c_dma *dma;
207 static const struct imx_i2c_hwdata imx1_i2c_hwdata = {
209 .regshift = IMX_I2C_REGSHIFT,
210 .clk_div = imx_i2c_clk_div,
211 .ndivs = ARRAY_SIZE(imx_i2c_clk_div),
212 .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C,
213 .i2cr_ien_opcode = I2CR_IEN_OPCODE_1,
217 static const struct imx_i2c_hwdata imx21_i2c_hwdata = {
218 .devtype = IMX21_I2C,
219 .regshift = IMX_I2C_REGSHIFT,
220 .clk_div = imx_i2c_clk_div,
221 .ndivs = ARRAY_SIZE(imx_i2c_clk_div),
222 .i2sr_clr_opcode = I2SR_CLR_OPCODE_W0C,
223 .i2cr_ien_opcode = I2CR_IEN_OPCODE_1,
227 static struct imx_i2c_hwdata vf610_i2c_hwdata = {
228 .devtype = VF610_I2C,
229 .regshift = VF610_I2C_REGSHIFT,
230 .clk_div = vf610_i2c_clk_div,
231 .ndivs = ARRAY_SIZE(vf610_i2c_clk_div),
232 .i2sr_clr_opcode = I2SR_CLR_OPCODE_W1C,
233 .i2cr_ien_opcode = I2CR_IEN_OPCODE_0,
237 static const struct platform_device_id imx_i2c_devtype[] = {
240 .driver_data = (kernel_ulong_t)&imx1_i2c_hwdata,
243 .driver_data = (kernel_ulong_t)&imx21_i2c_hwdata,
248 MODULE_DEVICE_TABLE(platform, imx_i2c_devtype);
250 static const struct of_device_id i2c_imx_dt_ids[] = {
251 { .compatible = "fsl,imx1-i2c", .data = &imx1_i2c_hwdata, },
252 { .compatible = "fsl,imx21-i2c", .data = &imx21_i2c_hwdata, },
253 { .compatible = "fsl,vf610-i2c", .data = &vf610_i2c_hwdata, },
256 MODULE_DEVICE_TABLE(of, i2c_imx_dt_ids);
258 static inline int is_imx1_i2c(struct imx_i2c_struct *i2c_imx)
260 return i2c_imx->hwdata->devtype == IMX1_I2C;
263 static inline void imx_i2c_write_reg(unsigned int val,
264 struct imx_i2c_struct *i2c_imx, unsigned int reg)
266 writeb(val, i2c_imx->base + (reg << i2c_imx->hwdata->regshift));
269 static inline unsigned char imx_i2c_read_reg(struct imx_i2c_struct *i2c_imx,
272 return readb(i2c_imx->base + (reg << i2c_imx->hwdata->regshift));
275 /* Functions for DMA support */
276 static void i2c_imx_dma_request(struct imx_i2c_struct *i2c_imx,
279 struct imx_i2c_dma *dma;
280 struct dma_slave_config dma_sconfig;
281 struct device *dev = &i2c_imx->adapter.dev;
284 dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
288 dma->chan_tx = dma_request_slave_channel(dev, "tx");
290 dev_dbg(dev, "can't request DMA tx channel\n");
294 dma_sconfig.dst_addr = phy_addr +
295 (IMX_I2C_I2DR << i2c_imx->hwdata->regshift);
296 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
297 dma_sconfig.dst_maxburst = 1;
298 dma_sconfig.direction = DMA_MEM_TO_DEV;
299 ret = dmaengine_slave_config(dma->chan_tx, &dma_sconfig);
301 dev_dbg(dev, "can't configure tx channel\n");
305 dma->chan_rx = dma_request_slave_channel(dev, "rx");
307 dev_dbg(dev, "can't request DMA rx channel\n");
311 dma_sconfig.src_addr = phy_addr +
312 (IMX_I2C_I2DR << i2c_imx->hwdata->regshift);
313 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
314 dma_sconfig.src_maxburst = 1;
315 dma_sconfig.direction = DMA_DEV_TO_MEM;
316 ret = dmaengine_slave_config(dma->chan_rx, &dma_sconfig);
318 dev_dbg(dev, "can't configure rx channel\n");
323 init_completion(&dma->cmd_complete);
324 dev_info(dev, "using %s (tx) and %s (rx) for DMA transfers\n",
325 dma_chan_name(dma->chan_tx), dma_chan_name(dma->chan_rx));
330 dma_release_channel(dma->chan_rx);
332 dma_release_channel(dma->chan_tx);
334 devm_kfree(dev, dma);
335 dev_info(dev, "can't use DMA, using PIO instead.\n");
338 static void i2c_imx_dma_callback(void *arg)
340 struct imx_i2c_struct *i2c_imx = (struct imx_i2c_struct *)arg;
341 struct imx_i2c_dma *dma = i2c_imx->dma;
343 dma_unmap_single(dma->chan_using->device->dev, dma->dma_buf,
344 dma->dma_len, dma->dma_data_dir);
345 complete(&dma->cmd_complete);
348 static int i2c_imx_dma_xfer(struct imx_i2c_struct *i2c_imx,
349 struct i2c_msg *msgs)
351 struct imx_i2c_dma *dma = i2c_imx->dma;
352 struct dma_async_tx_descriptor *txdesc;
353 struct device *dev = &i2c_imx->adapter.dev;
354 struct device *chan_dev = dma->chan_using->device->dev;
356 dma->dma_buf = dma_map_single(chan_dev, msgs->buf,
357 dma->dma_len, dma->dma_data_dir);
358 if (dma_mapping_error(chan_dev, dma->dma_buf)) {
359 dev_err(dev, "DMA mapping failed\n");
363 txdesc = dmaengine_prep_slave_single(dma->chan_using, dma->dma_buf,
364 dma->dma_len, dma->dma_transfer_dir,
365 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
367 dev_err(dev, "Not able to get desc for DMA xfer\n");
371 reinit_completion(&dma->cmd_complete);
372 txdesc->callback = i2c_imx_dma_callback;
373 txdesc->callback_param = i2c_imx;
374 if (dma_submit_error(dmaengine_submit(txdesc))) {
375 dev_err(dev, "DMA submit failed\n");
379 dma_async_issue_pending(dma->chan_using);
383 dmaengine_terminate_all(dma->chan_using);
385 dma_unmap_single(chan_dev, dma->dma_buf,
386 dma->dma_len, dma->dma_data_dir);
391 static void i2c_imx_dma_free(struct imx_i2c_struct *i2c_imx)
393 struct imx_i2c_dma *dma = i2c_imx->dma;
398 dma_release_channel(dma->chan_tx);
401 dma_release_channel(dma->chan_rx);
404 dma->chan_using = NULL;
407 static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
409 unsigned long orig_jiffies = jiffies;
412 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
415 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
417 /* check for arbitration lost */
418 if (temp & I2SR_IAL) {
420 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
424 if (for_busy && (temp & I2SR_IBB))
426 if (!for_busy && !(temp & I2SR_IBB))
428 if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
429 dev_dbg(&i2c_imx->adapter.dev,
430 "<%s> I2C bus is busy\n", __func__);
439 static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx)
441 wait_event_timeout(i2c_imx->queue, i2c_imx->i2csr & I2SR_IIF, HZ / 10);
443 if (unlikely(!(i2c_imx->i2csr & I2SR_IIF))) {
444 dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
447 dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
452 static int i2c_imx_acked(struct imx_i2c_struct *i2c_imx)
454 if (imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR) & I2SR_RXAK) {
455 dev_dbg(&i2c_imx->adapter.dev, "<%s> No ACK\n", __func__);
456 return -ENXIO; /* No ACK */
459 dev_dbg(&i2c_imx->adapter.dev, "<%s> ACK received\n", __func__);
463 static void i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
464 unsigned int i2c_clk_rate)
466 struct imx_i2c_clk_pair *i2c_clk_div = i2c_imx->hwdata->clk_div;
470 /* Divider value calculation */
471 if (i2c_imx->cur_clk == i2c_clk_rate)
474 i2c_imx->cur_clk = i2c_clk_rate;
476 div = (i2c_clk_rate + i2c_imx->bitrate - 1) / i2c_imx->bitrate;
477 if (div < i2c_clk_div[0].div)
479 else if (div > i2c_clk_div[i2c_imx->hwdata->ndivs - 1].div)
480 i = i2c_imx->hwdata->ndivs - 1;
482 for (i = 0; i2c_clk_div[i].div < div; i++)
485 /* Store divider value */
486 i2c_imx->ifdr = i2c_clk_div[i].val;
489 * There dummy delay is calculated.
490 * It should be about one I2C clock period long.
491 * This delay is used in I2C bus disable function
492 * to fix chip hardware bug.
494 i2c_imx->disable_delay = (500000U * i2c_clk_div[i].div
495 + (i2c_clk_rate / 2) - 1) / (i2c_clk_rate / 2);
497 #ifdef CONFIG_I2C_DEBUG_BUS
498 dev_dbg(&i2c_imx->adapter.dev, "I2C_CLK=%d, REQ DIV=%d\n",
500 dev_dbg(&i2c_imx->adapter.dev, "IFDR[IC]=0x%x, REAL DIV=%d\n",
501 i2c_clk_div[i].val, i2c_clk_div[i].div);
505 static int i2c_imx_clk_notifier_call(struct notifier_block *nb,
506 unsigned long action, void *data)
508 struct clk_notifier_data *ndata = data;
509 struct imx_i2c_struct *i2c_imx = container_of(&ndata->clk,
510 struct imx_i2c_struct,
513 if (action & POST_RATE_CHANGE)
514 i2c_imx_set_clk(i2c_imx, ndata->new_rate);
519 static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
521 unsigned int temp = 0;
524 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
526 imx_i2c_write_reg(i2c_imx->ifdr, i2c_imx, IMX_I2C_IFDR);
527 /* Enable I2C controller */
528 imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
529 imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode, i2c_imx, IMX_I2C_I2CR);
531 /* Wait controller to be stable */
532 usleep_range(50, 150);
534 /* Start I2C transaction */
535 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
537 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
538 result = i2c_imx_bus_busy(i2c_imx, 1);
541 i2c_imx->stopped = 0;
543 temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK;
545 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
549 static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
551 unsigned int temp = 0;
553 if (!i2c_imx->stopped) {
554 /* Stop I2C transaction */
555 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
556 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
557 temp &= ~(I2CR_MSTA | I2CR_MTX);
560 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
562 if (is_imx1_i2c(i2c_imx)) {
564 * This delay caused by an i.MXL hardware bug.
565 * If no (or too short) delay, no "STOP" bit will be generated.
567 udelay(i2c_imx->disable_delay);
570 if (!i2c_imx->stopped) {
571 i2c_imx_bus_busy(i2c_imx, 0);
572 i2c_imx->stopped = 1;
575 /* Disable I2C controller */
576 temp = i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
577 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
580 static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
582 struct imx_i2c_struct *i2c_imx = dev_id;
585 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
586 if (temp & I2SR_IIF) {
587 /* save status register */
588 i2c_imx->i2csr = temp;
590 temp |= (i2c_imx->hwdata->i2sr_clr_opcode & I2SR_IIF);
591 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
592 wake_up(&i2c_imx->queue);
599 static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx,
600 struct i2c_msg *msgs)
603 unsigned long time_left;
604 unsigned int temp = 0;
605 unsigned long orig_jiffies = jiffies;
606 struct imx_i2c_dma *dma = i2c_imx->dma;
607 struct device *dev = &i2c_imx->adapter.dev;
609 dma->chan_using = dma->chan_tx;
610 dma->dma_transfer_dir = DMA_MEM_TO_DEV;
611 dma->dma_data_dir = DMA_TO_DEVICE;
612 dma->dma_len = msgs->len - 1;
613 result = i2c_imx_dma_xfer(i2c_imx, msgs);
617 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
619 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
622 * Write slave address.
623 * The first byte must be transmitted by the CPU.
625 imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR);
626 time_left = wait_for_completion_timeout(
627 &i2c_imx->dma->cmd_complete,
628 msecs_to_jiffies(DMA_TIMEOUT));
629 if (time_left == 0) {
630 dmaengine_terminate_all(dma->chan_using);
634 /* Waiting for transfer complete. */
636 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
639 if (time_after(jiffies, orig_jiffies +
640 msecs_to_jiffies(DMA_TIMEOUT))) {
641 dev_dbg(dev, "<%s> Timeout\n", __func__);
647 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
649 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
651 /* The last data byte must be transferred by the CPU. */
652 imx_i2c_write_reg(msgs->buf[msgs->len-1],
653 i2c_imx, IMX_I2C_I2DR);
654 result = i2c_imx_trx_complete(i2c_imx);
658 return i2c_imx_acked(i2c_imx);
661 static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
662 struct i2c_msg *msgs, bool is_lastmsg)
665 unsigned long time_left;
667 unsigned long orig_jiffies = jiffies;
668 struct imx_i2c_dma *dma = i2c_imx->dma;
669 struct device *dev = &i2c_imx->adapter.dev;
671 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
673 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
675 dma->chan_using = dma->chan_rx;
676 dma->dma_transfer_dir = DMA_DEV_TO_MEM;
677 dma->dma_data_dir = DMA_FROM_DEVICE;
678 /* The last two data bytes must be transferred by the CPU. */
679 dma->dma_len = msgs->len - 2;
680 result = i2c_imx_dma_xfer(i2c_imx, msgs);
684 time_left = wait_for_completion_timeout(
685 &i2c_imx->dma->cmd_complete,
686 msecs_to_jiffies(DMA_TIMEOUT));
687 if (time_left == 0) {
688 dmaengine_terminate_all(dma->chan_using);
692 /* waiting for transfer complete. */
694 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
697 if (time_after(jiffies, orig_jiffies +
698 msecs_to_jiffies(DMA_TIMEOUT))) {
699 dev_dbg(dev, "<%s> Timeout\n", __func__);
705 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
707 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
709 /* read n-1 byte data */
710 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
712 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
714 msgs->buf[msgs->len-2] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
715 /* read n byte data */
716 result = i2c_imx_trx_complete(i2c_imx);
722 * It must generate STOP before read I2DR to prevent
723 * controller from generating another clock cycle
725 dev_dbg(dev, "<%s> clear MSTA\n", __func__);
726 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
727 temp &= ~(I2CR_MSTA | I2CR_MTX);
728 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
729 i2c_imx_bus_busy(i2c_imx, 0);
730 i2c_imx->stopped = 1;
733 * For i2c master receiver repeat restart operation like:
734 * read -> repeat MSTA -> read/write
735 * The controller must set MTX before read the last byte in
736 * the first read operation, otherwise the first read cost
737 * one extra clock cycle.
739 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
741 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
743 msgs->buf[msgs->len-1] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
748 static int i2c_imx_write(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs)
752 dev_dbg(&i2c_imx->adapter.dev, "<%s> write slave address: addr=0x%x\n",
753 __func__, i2c_8bit_addr_from_msg(msgs));
755 /* write slave address */
756 imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR);
757 result = i2c_imx_trx_complete(i2c_imx);
760 result = i2c_imx_acked(i2c_imx);
763 dev_dbg(&i2c_imx->adapter.dev, "<%s> write data\n", __func__);
766 for (i = 0; i < msgs->len; i++) {
767 dev_dbg(&i2c_imx->adapter.dev,
768 "<%s> write byte: B%d=0x%X\n",
769 __func__, i, msgs->buf[i]);
770 imx_i2c_write_reg(msgs->buf[i], i2c_imx, IMX_I2C_I2DR);
771 result = i2c_imx_trx_complete(i2c_imx);
774 result = i2c_imx_acked(i2c_imx);
781 static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bool is_lastmsg)
785 int block_data = msgs->flags & I2C_M_RECV_LEN;
787 dev_dbg(&i2c_imx->adapter.dev,
788 "<%s> write slave address: addr=0x%x\n",
789 __func__, i2c_8bit_addr_from_msg(msgs));
791 /* write slave address */
792 imx_i2c_write_reg(i2c_8bit_addr_from_msg(msgs), i2c_imx, IMX_I2C_I2DR);
793 result = i2c_imx_trx_complete(i2c_imx);
796 result = i2c_imx_acked(i2c_imx);
800 dev_dbg(&i2c_imx->adapter.dev, "<%s> setup bus\n", __func__);
802 /* setup bus to read data */
803 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
807 * Reset the I2CR_TXAK flag initially for SMBus block read since the
810 if ((msgs->len - 1) || block_data)
812 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
813 imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */
815 dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
817 if (i2c_imx->dma && msgs->len >= DMA_THRESHOLD && !block_data)
818 return i2c_imx_dma_read(i2c_imx, msgs, is_lastmsg);
821 for (i = 0; i < msgs->len; i++) {
824 result = i2c_imx_trx_complete(i2c_imx);
828 * First byte is the length of remaining packet
829 * in the SMBus block data read. Add it to
832 if ((!i) && block_data) {
833 len = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
834 if ((len == 0) || (len > I2C_SMBUS_BLOCK_MAX))
836 dev_dbg(&i2c_imx->adapter.dev,
837 "<%s> read length: 0x%X\n",
841 if (i == (msgs->len - 1)) {
844 * It must generate STOP before read I2DR to prevent
845 * controller from generating another clock cycle
847 dev_dbg(&i2c_imx->adapter.dev,
848 "<%s> clear MSTA\n", __func__);
849 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
850 temp &= ~(I2CR_MSTA | I2CR_MTX);
851 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
852 i2c_imx_bus_busy(i2c_imx, 0);
853 i2c_imx->stopped = 1;
856 * For i2c master receiver repeat restart operation like:
857 * read -> repeat MSTA -> read/write
858 * The controller must set MTX before read the last byte in
859 * the first read operation, otherwise the first read cost
860 * one extra clock cycle.
862 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
864 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
866 } else if (i == (msgs->len - 2)) {
867 dev_dbg(&i2c_imx->adapter.dev,
868 "<%s> set TXAK\n", __func__);
869 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
871 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
873 if ((!i) && block_data)
876 msgs->buf[i] = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR);
877 dev_dbg(&i2c_imx->adapter.dev,
878 "<%s> read byte: B%d=0x%X\n",
879 __func__, i, msgs->buf[i]);
884 static int i2c_imx_xfer(struct i2c_adapter *adapter,
885 struct i2c_msg *msgs, int num)
887 unsigned int i, temp;
889 bool is_lastmsg = false;
890 struct imx_i2c_struct *i2c_imx = i2c_get_adapdata(adapter);
892 dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
894 result = pm_runtime_get_sync(i2c_imx->adapter.dev.parent);
898 /* Start I2C transfer */
899 result = i2c_imx_start(i2c_imx);
901 if (i2c_imx->adapter.bus_recovery_info) {
902 i2c_recover_bus(&i2c_imx->adapter);
903 result = i2c_imx_start(i2c_imx);
910 /* read/write data */
911 for (i = 0; i < num; i++) {
916 dev_dbg(&i2c_imx->adapter.dev,
917 "<%s> repeated start\n", __func__);
918 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
920 imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
921 result = i2c_imx_bus_busy(i2c_imx, 1);
925 dev_dbg(&i2c_imx->adapter.dev,
926 "<%s> transfer message: %d\n", __func__, i);
927 /* write/read data */
928 #ifdef CONFIG_I2C_DEBUG_BUS
929 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
930 dev_dbg(&i2c_imx->adapter.dev,
931 "<%s> CONTROL: IEN=%d, IIEN=%d, MSTA=%d, MTX=%d, TXAK=%d, RSTA=%d\n",
933 (temp & I2CR_IEN ? 1 : 0), (temp & I2CR_IIEN ? 1 : 0),
934 (temp & I2CR_MSTA ? 1 : 0), (temp & I2CR_MTX ? 1 : 0),
935 (temp & I2CR_TXAK ? 1 : 0), (temp & I2CR_RSTA ? 1 : 0));
936 temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2SR);
937 dev_dbg(&i2c_imx->adapter.dev,
938 "<%s> STATUS: ICF=%d, IAAS=%d, IBB=%d, IAL=%d, SRW=%d, IIF=%d, RXAK=%d\n",
940 (temp & I2SR_ICF ? 1 : 0), (temp & I2SR_IAAS ? 1 : 0),
941 (temp & I2SR_IBB ? 1 : 0), (temp & I2SR_IAL ? 1 : 0),
942 (temp & I2SR_SRW ? 1 : 0), (temp & I2SR_IIF ? 1 : 0),
943 (temp & I2SR_RXAK ? 1 : 0));
945 if (msgs[i].flags & I2C_M_RD)
946 result = i2c_imx_read(i2c_imx, &msgs[i], is_lastmsg);
948 if (i2c_imx->dma && msgs[i].len >= DMA_THRESHOLD)
949 result = i2c_imx_dma_write(i2c_imx, &msgs[i]);
951 result = i2c_imx_write(i2c_imx, &msgs[i]);
958 /* Stop I2C transfer */
959 i2c_imx_stop(i2c_imx);
961 pm_runtime_mark_last_busy(i2c_imx->adapter.dev.parent);
962 pm_runtime_put_autosuspend(i2c_imx->adapter.dev.parent);
965 dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
966 (result < 0) ? "error" : "success msg",
967 (result < 0) ? result : num);
968 return (result < 0) ? result : num;
971 static void i2c_imx_prepare_recovery(struct i2c_adapter *adap)
973 struct imx_i2c_struct *i2c_imx;
975 i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
977 pinctrl_select_state(i2c_imx->pinctrl, i2c_imx->pinctrl_pins_gpio);
980 static void i2c_imx_unprepare_recovery(struct i2c_adapter *adap)
982 struct imx_i2c_struct *i2c_imx;
984 i2c_imx = container_of(adap, struct imx_i2c_struct, adapter);
986 pinctrl_select_state(i2c_imx->pinctrl, i2c_imx->pinctrl_pins_default);
990 * We switch SCL and SDA to their GPIO function and do some bitbanging
991 * for bus recovery. These alternative pinmux settings can be
992 * described in the device tree by a separate pinctrl state "gpio". If
993 * this is missing this is not a big problem, the only implication is
994 * that we can't do bus recovery.
996 static int i2c_imx_init_recovery_info(struct imx_i2c_struct *i2c_imx,
997 struct platform_device *pdev)
999 struct i2c_bus_recovery_info *rinfo = &i2c_imx->rinfo;
1001 i2c_imx->pinctrl = devm_pinctrl_get(&pdev->dev);
1002 if (!i2c_imx->pinctrl || IS_ERR(i2c_imx->pinctrl)) {
1003 dev_info(&pdev->dev, "can't get pinctrl, bus recovery not supported\n");
1004 return PTR_ERR(i2c_imx->pinctrl);
1007 i2c_imx->pinctrl_pins_default = pinctrl_lookup_state(i2c_imx->pinctrl,
1008 PINCTRL_STATE_DEFAULT);
1009 i2c_imx->pinctrl_pins_gpio = pinctrl_lookup_state(i2c_imx->pinctrl,
1011 rinfo->sda_gpiod = devm_gpiod_get(&pdev->dev, "sda", GPIOD_IN);
1012 rinfo->scl_gpiod = devm_gpiod_get(&pdev->dev, "scl", GPIOD_OUT_HIGH_OPEN_DRAIN);
1014 if (PTR_ERR(rinfo->sda_gpiod) == -EPROBE_DEFER ||
1015 PTR_ERR(rinfo->scl_gpiod) == -EPROBE_DEFER) {
1016 return -EPROBE_DEFER;
1017 } else if (IS_ERR(rinfo->sda_gpiod) ||
1018 IS_ERR(rinfo->scl_gpiod) ||
1019 IS_ERR(i2c_imx->pinctrl_pins_default) ||
1020 IS_ERR(i2c_imx->pinctrl_pins_gpio)) {
1021 dev_dbg(&pdev->dev, "recovery information incomplete\n");
1025 dev_dbg(&pdev->dev, "using scl%s for recovery\n",
1026 rinfo->sda_gpiod ? ",sda" : "");
1028 rinfo->prepare_recovery = i2c_imx_prepare_recovery;
1029 rinfo->unprepare_recovery = i2c_imx_unprepare_recovery;
1030 rinfo->recover_bus = i2c_generic_scl_recovery;
1031 i2c_imx->adapter.bus_recovery_info = rinfo;
1036 static u32 i2c_imx_func(struct i2c_adapter *adapter)
1038 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL
1039 | I2C_FUNC_SMBUS_READ_BLOCK_DATA;
1042 static const struct i2c_algorithm i2c_imx_algo = {
1043 .master_xfer = i2c_imx_xfer,
1044 .functionality = i2c_imx_func,
1047 static int i2c_imx_probe(struct platform_device *pdev)
1049 const struct of_device_id *of_id = of_match_device(i2c_imx_dt_ids,
1051 struct imx_i2c_struct *i2c_imx;
1052 struct resource *res;
1053 struct imxi2c_platform_data *pdata = dev_get_platdata(&pdev->dev);
1056 dma_addr_t phy_addr;
1058 dev_dbg(&pdev->dev, "<%s>\n", __func__);
1060 irq = platform_get_irq(pdev, 0);
1062 dev_err(&pdev->dev, "can't get irq number\n");
1066 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1067 base = devm_ioremap_resource(&pdev->dev, res);
1069 return PTR_ERR(base);
1071 phy_addr = (dma_addr_t)res->start;
1072 i2c_imx = devm_kzalloc(&pdev->dev, sizeof(*i2c_imx), GFP_KERNEL);
1077 i2c_imx->hwdata = of_id->data;
1079 i2c_imx->hwdata = (struct imx_i2c_hwdata *)
1080 platform_get_device_id(pdev)->driver_data;
1082 /* Setup i2c_imx driver structure */
1083 strlcpy(i2c_imx->adapter.name, pdev->name, sizeof(i2c_imx->adapter.name));
1084 i2c_imx->adapter.owner = THIS_MODULE;
1085 i2c_imx->adapter.algo = &i2c_imx_algo;
1086 i2c_imx->adapter.dev.parent = &pdev->dev;
1087 i2c_imx->adapter.nr = pdev->id;
1088 i2c_imx->adapter.dev.of_node = pdev->dev.of_node;
1089 i2c_imx->base = base;
1092 i2c_imx->clk = devm_clk_get(&pdev->dev, NULL);
1093 if (IS_ERR(i2c_imx->clk)) {
1094 dev_err(&pdev->dev, "can't get I2C clock\n");
1095 return PTR_ERR(i2c_imx->clk);
1098 ret = clk_prepare_enable(i2c_imx->clk);
1100 dev_err(&pdev->dev, "can't enable I2C clock, ret=%d\n", ret);
1105 ret = devm_request_irq(&pdev->dev, irq, i2c_imx_isr, IRQF_SHARED,
1106 pdev->name, i2c_imx);
1108 dev_err(&pdev->dev, "can't claim irq %d\n", irq);
1113 init_waitqueue_head(&i2c_imx->queue);
1115 /* Set up adapter data */
1116 i2c_set_adapdata(&i2c_imx->adapter, i2c_imx);
1118 /* Set up platform driver data */
1119 platform_set_drvdata(pdev, i2c_imx);
1121 pm_runtime_set_autosuspend_delay(&pdev->dev, I2C_PM_TIMEOUT);
1122 pm_runtime_use_autosuspend(&pdev->dev);
1123 pm_runtime_set_active(&pdev->dev);
1124 pm_runtime_enable(&pdev->dev);
1126 ret = pm_runtime_get_sync(&pdev->dev);
1130 /* Set up clock divider */
1131 i2c_imx->bitrate = IMX_I2C_BIT_RATE;
1132 ret = of_property_read_u32(pdev->dev.of_node,
1133 "clock-frequency", &i2c_imx->bitrate);
1134 if (ret < 0 && pdata && pdata->bitrate)
1135 i2c_imx->bitrate = pdata->bitrate;
1136 i2c_imx->clk_change_nb.notifier_call = i2c_imx_clk_notifier_call;
1137 clk_notifier_register(i2c_imx->clk, &i2c_imx->clk_change_nb);
1138 i2c_imx_set_clk(i2c_imx, clk_get_rate(i2c_imx->clk));
1140 /* Set up chip registers to defaults */
1141 imx_i2c_write_reg(i2c_imx->hwdata->i2cr_ien_opcode ^ I2CR_IEN,
1142 i2c_imx, IMX_I2C_I2CR);
1143 imx_i2c_write_reg(i2c_imx->hwdata->i2sr_clr_opcode, i2c_imx, IMX_I2C_I2SR);
1145 /* Init optional bus recovery function */
1146 ret = i2c_imx_init_recovery_info(i2c_imx, pdev);
1147 /* Give it another chance if pinctrl used is not ready yet */
1148 if (ret == -EPROBE_DEFER)
1149 goto clk_notifier_unregister;
1151 /* Add I2C adapter */
1152 ret = i2c_add_numbered_adapter(&i2c_imx->adapter);
1154 goto clk_notifier_unregister;
1156 pm_runtime_mark_last_busy(&pdev->dev);
1157 pm_runtime_put_autosuspend(&pdev->dev);
1159 dev_dbg(&i2c_imx->adapter.dev, "claimed irq %d\n", irq);
1160 dev_dbg(&i2c_imx->adapter.dev, "device resources: %pR\n", res);
1161 dev_dbg(&i2c_imx->adapter.dev, "adapter name: \"%s\"\n",
1162 i2c_imx->adapter.name);
1163 dev_info(&i2c_imx->adapter.dev, "IMX I2C adapter registered\n");
1165 /* Init DMA config if supported */
1166 i2c_imx_dma_request(i2c_imx, phy_addr);
1168 return 0; /* Return OK */
1170 clk_notifier_unregister:
1171 clk_notifier_unregister(i2c_imx->clk, &i2c_imx->clk_change_nb);
1173 pm_runtime_put_noidle(&pdev->dev);
1174 pm_runtime_disable(&pdev->dev);
1175 pm_runtime_set_suspended(&pdev->dev);
1176 pm_runtime_dont_use_autosuspend(&pdev->dev);
1179 clk_disable_unprepare(i2c_imx->clk);
1183 static int i2c_imx_remove(struct platform_device *pdev)
1185 struct imx_i2c_struct *i2c_imx = platform_get_drvdata(pdev);
1188 ret = pm_runtime_get_sync(&pdev->dev);
1192 /* remove adapter */
1193 dev_dbg(&i2c_imx->adapter.dev, "adapter removed\n");
1194 i2c_del_adapter(&i2c_imx->adapter);
1197 i2c_imx_dma_free(i2c_imx);
1199 /* setup chip registers to defaults */
1200 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IADR);
1201 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_IFDR);
1202 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2CR);
1203 imx_i2c_write_reg(0, i2c_imx, IMX_I2C_I2SR);
1205 clk_notifier_unregister(i2c_imx->clk, &i2c_imx->clk_change_nb);
1206 clk_disable_unprepare(i2c_imx->clk);
1208 pm_runtime_put_noidle(&pdev->dev);
1209 pm_runtime_disable(&pdev->dev);
1215 static int i2c_imx_runtime_suspend(struct device *dev)
1217 struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev);
1219 clk_disable(i2c_imx->clk);
1224 static int i2c_imx_runtime_resume(struct device *dev)
1226 struct imx_i2c_struct *i2c_imx = dev_get_drvdata(dev);
1229 ret = clk_enable(i2c_imx->clk);
1231 dev_err(dev, "can't enable I2C clock, ret=%d\n", ret);
1236 static const struct dev_pm_ops i2c_imx_pm_ops = {
1237 SET_RUNTIME_PM_OPS(i2c_imx_runtime_suspend,
1238 i2c_imx_runtime_resume, NULL)
1240 #define I2C_IMX_PM_OPS (&i2c_imx_pm_ops)
1242 #define I2C_IMX_PM_OPS NULL
1243 #endif /* CONFIG_PM */
1245 static struct platform_driver i2c_imx_driver = {
1246 .probe = i2c_imx_probe,
1247 .remove = i2c_imx_remove,
1249 .name = DRIVER_NAME,
1250 .pm = I2C_IMX_PM_OPS,
1251 .of_match_table = i2c_imx_dt_ids,
1253 .id_table = imx_i2c_devtype,
1256 static int __init i2c_adap_imx_init(void)
1258 return platform_driver_register(&i2c_imx_driver);
1260 subsys_initcall(i2c_adap_imx_init);
1262 static void __exit i2c_adap_imx_exit(void)
1264 platform_driver_unregister(&i2c_imx_driver);
1266 module_exit(i2c_adap_imx_exit);
1268 MODULE_LICENSE("GPL");
1269 MODULE_AUTHOR("Darius Augulis");
1270 MODULE_DESCRIPTION("I2C adapter driver for IMX I2C bus");
1271 MODULE_ALIAS("platform:" DRIVER_NAME);