1 // SPDX-License-Identifier: GPL-2.0+
3 * This is i.MX low power i2c controller driver.
5 * Copyright 2016 Freescale Semiconductor, Inc.
9 #include <linux/completion.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/of_device.h>
21 #include <linux/pinctrl/consumer.h>
22 #include <linux/platform_device.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
27 #define DRIVER_NAME "imx-lpi2c"
29 #define LPI2C_PARAM 0x04 /* i2c RX/TX FIFO size */
30 #define LPI2C_MCR 0x10 /* i2c contrl register */
31 #define LPI2C_MSR 0x14 /* i2c status register */
32 #define LPI2C_MIER 0x18 /* i2c interrupt enable */
33 #define LPI2C_MCFGR0 0x20 /* i2c master configuration */
34 #define LPI2C_MCFGR1 0x24 /* i2c master configuration */
35 #define LPI2C_MCFGR2 0x28 /* i2c master configuration */
36 #define LPI2C_MCFGR3 0x2C /* i2c master configuration */
37 #define LPI2C_MCCR0 0x48 /* i2c master clk configuration */
38 #define LPI2C_MCCR1 0x50 /* i2c master clk configuration */
39 #define LPI2C_MFCR 0x58 /* i2c master FIFO control */
40 #define LPI2C_MFSR 0x5C /* i2c master FIFO status */
41 #define LPI2C_MTDR 0x60 /* i2c master TX data register */
42 #define LPI2C_MRDR 0x70 /* i2c master RX data register */
45 #define TRAN_DATA 0X00
46 #define RECV_DATA 0X01
48 #define RECV_DISCARD 0X03
49 #define GEN_START 0X04
50 #define START_NACK 0X05
51 #define START_HIGH 0X06
52 #define START_HIGH_NACK 0X07
54 #define MCR_MEN BIT(0)
55 #define MCR_RST BIT(1)
56 #define MCR_DOZEN BIT(2)
57 #define MCR_DBGEN BIT(3)
58 #define MCR_RTF BIT(8)
59 #define MCR_RRF BIT(9)
60 #define MSR_TDF BIT(0)
61 #define MSR_RDF BIT(1)
62 #define MSR_SDF BIT(9)
63 #define MSR_NDF BIT(10)
64 #define MSR_ALF BIT(11)
65 #define MSR_MBF BIT(24)
66 #define MSR_BBF BIT(25)
67 #define MIER_TDIE BIT(0)
68 #define MIER_RDIE BIT(1)
69 #define MIER_SDIE BIT(9)
70 #define MIER_NDIE BIT(10)
71 #define MCFGR1_AUTOSTOP BIT(8)
72 #define MCFGR1_IGNACK BIT(9)
73 #define MRDR_RXEMPTY BIT(14)
75 #define I2C_CLK_RATIO 2
76 #define CHUNK_DATA 256
78 #define I2C_PM_TIMEOUT 10 /* ms */
81 STANDARD, /* 100+Kbps */
83 FAST_PLUS, /* 1.0+Mbps */
85 ULTRA_FAST, /* 5.0+Mbps */
88 enum lpi2c_imx_pincfg {
95 struct lpi2c_imx_struct {
96 struct i2c_adapter adapter;
98 struct clk_bulk_data *clks;
102 struct completion complete;
104 unsigned int delivered;
105 unsigned int block_data;
106 unsigned int bitrate;
107 unsigned int txfifosize;
108 unsigned int rxfifosize;
109 enum lpi2c_imx_mode mode;
112 static void lpi2c_imx_intctrl(struct lpi2c_imx_struct *lpi2c_imx,
115 writel(enable, lpi2c_imx->base + LPI2C_MIER);
118 static int lpi2c_imx_bus_busy(struct lpi2c_imx_struct *lpi2c_imx)
120 unsigned long orig_jiffies = jiffies;
124 temp = readl(lpi2c_imx->base + LPI2C_MSR);
126 /* check for arbitration lost, clear if set */
127 if (temp & MSR_ALF) {
128 writel(temp, lpi2c_imx->base + LPI2C_MSR);
132 if (temp & (MSR_BBF | MSR_MBF))
135 if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
136 dev_dbg(&lpi2c_imx->adapter.dev, "bus not work\n");
145 static void lpi2c_imx_set_mode(struct lpi2c_imx_struct *lpi2c_imx)
147 unsigned int bitrate = lpi2c_imx->bitrate;
148 enum lpi2c_imx_mode mode;
150 if (bitrate < I2C_MAX_FAST_MODE_FREQ)
152 else if (bitrate < I2C_MAX_FAST_MODE_PLUS_FREQ)
154 else if (bitrate < I2C_MAX_HIGH_SPEED_MODE_FREQ)
156 else if (bitrate < I2C_MAX_ULTRA_FAST_MODE_FREQ)
161 lpi2c_imx->mode = mode;
164 static int lpi2c_imx_start(struct lpi2c_imx_struct *lpi2c_imx,
165 struct i2c_msg *msgs)
169 temp = readl(lpi2c_imx->base + LPI2C_MCR);
170 temp |= MCR_RRF | MCR_RTF;
171 writel(temp, lpi2c_imx->base + LPI2C_MCR);
172 writel(0x7f00, lpi2c_imx->base + LPI2C_MSR);
174 temp = i2c_8bit_addr_from_msg(msgs) | (GEN_START << 8);
175 writel(temp, lpi2c_imx->base + LPI2C_MTDR);
177 return lpi2c_imx_bus_busy(lpi2c_imx);
180 static void lpi2c_imx_stop(struct lpi2c_imx_struct *lpi2c_imx)
182 unsigned long orig_jiffies = jiffies;
185 writel(GEN_STOP << 8, lpi2c_imx->base + LPI2C_MTDR);
188 temp = readl(lpi2c_imx->base + LPI2C_MSR);
192 if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
193 dev_dbg(&lpi2c_imx->adapter.dev, "stop timeout\n");
201 /* CLKLO = I2C_CLK_RATIO * CLKHI, SETHOLD = CLKHI, DATAVD = CLKHI/2 */
202 static int lpi2c_imx_config(struct lpi2c_imx_struct *lpi2c_imx)
204 u8 prescale, filt, sethold, datavd;
205 unsigned int clk_rate, clk_cycle, clkhi, clklo;
206 enum lpi2c_imx_pincfg pincfg;
209 lpi2c_imx_set_mode(lpi2c_imx);
211 clk_rate = clk_get_rate(lpi2c_imx->clks[0].clk);
215 if (lpi2c_imx->mode == HS || lpi2c_imx->mode == ULTRA_FAST)
220 for (prescale = 0; prescale <= 7; prescale++) {
221 clk_cycle = clk_rate / ((1 << prescale) * lpi2c_imx->bitrate)
223 clkhi = DIV_ROUND_UP(clk_cycle, I2C_CLK_RATIO + 1);
224 clklo = clk_cycle - clkhi;
232 /* set MCFGR1: PINCFG, PRESCALE, IGNACK */
233 if (lpi2c_imx->mode == ULTRA_FAST)
237 temp = prescale | pincfg << 24;
239 if (lpi2c_imx->mode == ULTRA_FAST)
240 temp |= MCFGR1_IGNACK;
242 writel(temp, lpi2c_imx->base + LPI2C_MCFGR1);
244 /* set MCFGR2: FILTSDA, FILTSCL */
245 temp = (filt << 16) | (filt << 24);
246 writel(temp, lpi2c_imx->base + LPI2C_MCFGR2);
248 /* set MCCR: DATAVD, SETHOLD, CLKHI, CLKLO */
251 temp = datavd << 24 | sethold << 16 | clkhi << 8 | clklo;
253 if (lpi2c_imx->mode == HS)
254 writel(temp, lpi2c_imx->base + LPI2C_MCCR1);
256 writel(temp, lpi2c_imx->base + LPI2C_MCCR0);
261 static int lpi2c_imx_master_enable(struct lpi2c_imx_struct *lpi2c_imx)
266 ret = pm_runtime_resume_and_get(lpi2c_imx->adapter.dev.parent);
271 writel(temp, lpi2c_imx->base + LPI2C_MCR);
272 writel(0, lpi2c_imx->base + LPI2C_MCR);
274 ret = lpi2c_imx_config(lpi2c_imx);
278 temp = readl(lpi2c_imx->base + LPI2C_MCR);
280 writel(temp, lpi2c_imx->base + LPI2C_MCR);
285 pm_runtime_mark_last_busy(lpi2c_imx->adapter.dev.parent);
286 pm_runtime_put_autosuspend(lpi2c_imx->adapter.dev.parent);
291 static int lpi2c_imx_master_disable(struct lpi2c_imx_struct *lpi2c_imx)
295 temp = readl(lpi2c_imx->base + LPI2C_MCR);
297 writel(temp, lpi2c_imx->base + LPI2C_MCR);
299 pm_runtime_mark_last_busy(lpi2c_imx->adapter.dev.parent);
300 pm_runtime_put_autosuspend(lpi2c_imx->adapter.dev.parent);
305 static int lpi2c_imx_msg_complete(struct lpi2c_imx_struct *lpi2c_imx)
307 unsigned long timeout;
309 timeout = wait_for_completion_timeout(&lpi2c_imx->complete, HZ);
311 return timeout ? 0 : -ETIMEDOUT;
314 static int lpi2c_imx_txfifo_empty(struct lpi2c_imx_struct *lpi2c_imx)
316 unsigned long orig_jiffies = jiffies;
320 txcnt = readl(lpi2c_imx->base + LPI2C_MFSR) & 0xff;
322 if (readl(lpi2c_imx->base + LPI2C_MSR) & MSR_NDF) {
323 dev_dbg(&lpi2c_imx->adapter.dev, "NDF detected\n");
327 if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
328 dev_dbg(&lpi2c_imx->adapter.dev, "txfifo empty timeout\n");
338 static void lpi2c_imx_set_tx_watermark(struct lpi2c_imx_struct *lpi2c_imx)
340 writel(lpi2c_imx->txfifosize >> 1, lpi2c_imx->base + LPI2C_MFCR);
343 static void lpi2c_imx_set_rx_watermark(struct lpi2c_imx_struct *lpi2c_imx)
345 unsigned int temp, remaining;
347 remaining = lpi2c_imx->msglen - lpi2c_imx->delivered;
349 if (remaining > (lpi2c_imx->rxfifosize >> 1))
350 temp = lpi2c_imx->rxfifosize >> 1;
354 writel(temp << 16, lpi2c_imx->base + LPI2C_MFCR);
357 static void lpi2c_imx_write_txfifo(struct lpi2c_imx_struct *lpi2c_imx)
359 unsigned int data, txcnt;
361 txcnt = readl(lpi2c_imx->base + LPI2C_MFSR) & 0xff;
363 while (txcnt < lpi2c_imx->txfifosize) {
364 if (lpi2c_imx->delivered == lpi2c_imx->msglen)
367 data = lpi2c_imx->tx_buf[lpi2c_imx->delivered++];
368 writel(data, lpi2c_imx->base + LPI2C_MTDR);
372 if (lpi2c_imx->delivered < lpi2c_imx->msglen)
373 lpi2c_imx_intctrl(lpi2c_imx, MIER_TDIE | MIER_NDIE);
375 complete(&lpi2c_imx->complete);
378 static void lpi2c_imx_read_rxfifo(struct lpi2c_imx_struct *lpi2c_imx)
380 unsigned int blocklen, remaining;
381 unsigned int temp, data;
384 data = readl(lpi2c_imx->base + LPI2C_MRDR);
385 if (data & MRDR_RXEMPTY)
388 lpi2c_imx->rx_buf[lpi2c_imx->delivered++] = data & 0xff;
392 * First byte is the length of remaining packet in the SMBus block
393 * data read. Add it to msgs->len.
395 if (lpi2c_imx->block_data) {
396 blocklen = lpi2c_imx->rx_buf[0];
397 lpi2c_imx->msglen += blocklen;
400 remaining = lpi2c_imx->msglen - lpi2c_imx->delivered;
403 complete(&lpi2c_imx->complete);
407 /* not finished, still waiting for rx data */
408 lpi2c_imx_set_rx_watermark(lpi2c_imx);
410 /* multiple receive commands */
411 if (lpi2c_imx->block_data) {
412 lpi2c_imx->block_data = 0;
414 temp |= (RECV_DATA << 8);
415 writel(temp, lpi2c_imx->base + LPI2C_MTDR);
416 } else if (!(lpi2c_imx->delivered & 0xff)) {
417 temp = (remaining > CHUNK_DATA ? CHUNK_DATA : remaining) - 1;
418 temp |= (RECV_DATA << 8);
419 writel(temp, lpi2c_imx->base + LPI2C_MTDR);
422 lpi2c_imx_intctrl(lpi2c_imx, MIER_RDIE);
425 static void lpi2c_imx_write(struct lpi2c_imx_struct *lpi2c_imx,
426 struct i2c_msg *msgs)
428 lpi2c_imx->tx_buf = msgs->buf;
429 lpi2c_imx_set_tx_watermark(lpi2c_imx);
430 lpi2c_imx_write_txfifo(lpi2c_imx);
433 static void lpi2c_imx_read(struct lpi2c_imx_struct *lpi2c_imx,
434 struct i2c_msg *msgs)
438 lpi2c_imx->rx_buf = msgs->buf;
439 lpi2c_imx->block_data = msgs->flags & I2C_M_RECV_LEN;
441 lpi2c_imx_set_rx_watermark(lpi2c_imx);
442 temp = msgs->len > CHUNK_DATA ? CHUNK_DATA - 1 : msgs->len - 1;
443 temp |= (RECV_DATA << 8);
444 writel(temp, lpi2c_imx->base + LPI2C_MTDR);
446 lpi2c_imx_intctrl(lpi2c_imx, MIER_RDIE | MIER_NDIE);
449 static int lpi2c_imx_xfer(struct i2c_adapter *adapter,
450 struct i2c_msg *msgs, int num)
452 struct lpi2c_imx_struct *lpi2c_imx = i2c_get_adapdata(adapter);
456 result = lpi2c_imx_master_enable(lpi2c_imx);
460 for (i = 0; i < num; i++) {
461 result = lpi2c_imx_start(lpi2c_imx, &msgs[i]);
466 if (num == 1 && msgs[0].len == 0)
469 lpi2c_imx->rx_buf = NULL;
470 lpi2c_imx->tx_buf = NULL;
471 lpi2c_imx->delivered = 0;
472 lpi2c_imx->msglen = msgs[i].len;
473 init_completion(&lpi2c_imx->complete);
475 if (msgs[i].flags & I2C_M_RD)
476 lpi2c_imx_read(lpi2c_imx, &msgs[i]);
478 lpi2c_imx_write(lpi2c_imx, &msgs[i]);
480 result = lpi2c_imx_msg_complete(lpi2c_imx);
484 if (!(msgs[i].flags & I2C_M_RD)) {
485 result = lpi2c_imx_txfifo_empty(lpi2c_imx);
492 lpi2c_imx_stop(lpi2c_imx);
494 temp = readl(lpi2c_imx->base + LPI2C_MSR);
495 if ((temp & MSR_NDF) && !result)
499 lpi2c_imx_master_disable(lpi2c_imx);
501 dev_dbg(&lpi2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
502 (result < 0) ? "error" : "success msg",
503 (result < 0) ? result : num);
505 return (result < 0) ? result : num;
508 static irqreturn_t lpi2c_imx_isr(int irq, void *dev_id)
510 struct lpi2c_imx_struct *lpi2c_imx = dev_id;
511 unsigned int enabled;
514 enabled = readl(lpi2c_imx->base + LPI2C_MIER);
516 lpi2c_imx_intctrl(lpi2c_imx, 0);
517 temp = readl(lpi2c_imx->base + LPI2C_MSR);
521 lpi2c_imx_read_rxfifo(lpi2c_imx);
524 lpi2c_imx_write_txfifo(lpi2c_imx);
527 complete(&lpi2c_imx->complete);
532 static u32 lpi2c_imx_func(struct i2c_adapter *adapter)
534 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
535 I2C_FUNC_SMBUS_READ_BLOCK_DATA;
538 static const struct i2c_algorithm lpi2c_imx_algo = {
539 .master_xfer = lpi2c_imx_xfer,
540 .functionality = lpi2c_imx_func,
543 static const struct of_device_id lpi2c_imx_of_match[] = {
544 { .compatible = "fsl,imx7ulp-lpi2c" },
547 MODULE_DEVICE_TABLE(of, lpi2c_imx_of_match);
549 static int lpi2c_imx_probe(struct platform_device *pdev)
551 struct lpi2c_imx_struct *lpi2c_imx;
555 lpi2c_imx = devm_kzalloc(&pdev->dev, sizeof(*lpi2c_imx), GFP_KERNEL);
559 lpi2c_imx->base = devm_platform_ioremap_resource(pdev, 0);
560 if (IS_ERR(lpi2c_imx->base))
561 return PTR_ERR(lpi2c_imx->base);
563 irq = platform_get_irq(pdev, 0);
567 lpi2c_imx->adapter.owner = THIS_MODULE;
568 lpi2c_imx->adapter.algo = &lpi2c_imx_algo;
569 lpi2c_imx->adapter.dev.parent = &pdev->dev;
570 lpi2c_imx->adapter.dev.of_node = pdev->dev.of_node;
571 strscpy(lpi2c_imx->adapter.name, pdev->name,
572 sizeof(lpi2c_imx->adapter.name));
574 ret = devm_clk_bulk_get_all(&pdev->dev, &lpi2c_imx->clks);
576 dev_err(&pdev->dev, "can't get I2C peripheral clock, ret=%d\n", ret);
579 lpi2c_imx->num_clks = ret;
581 ret = of_property_read_u32(pdev->dev.of_node,
582 "clock-frequency", &lpi2c_imx->bitrate);
584 lpi2c_imx->bitrate = I2C_MAX_STANDARD_MODE_FREQ;
586 ret = devm_request_irq(&pdev->dev, irq, lpi2c_imx_isr, 0,
587 pdev->name, lpi2c_imx);
589 dev_err(&pdev->dev, "can't claim irq %d\n", irq);
593 i2c_set_adapdata(&lpi2c_imx->adapter, lpi2c_imx);
594 platform_set_drvdata(pdev, lpi2c_imx);
596 ret = clk_bulk_prepare_enable(lpi2c_imx->num_clks, lpi2c_imx->clks);
600 pm_runtime_set_autosuspend_delay(&pdev->dev, I2C_PM_TIMEOUT);
601 pm_runtime_use_autosuspend(&pdev->dev);
602 pm_runtime_get_noresume(&pdev->dev);
603 pm_runtime_set_active(&pdev->dev);
604 pm_runtime_enable(&pdev->dev);
606 temp = readl(lpi2c_imx->base + LPI2C_PARAM);
607 lpi2c_imx->txfifosize = 1 << (temp & 0x0f);
608 lpi2c_imx->rxfifosize = 1 << ((temp >> 8) & 0x0f);
610 ret = i2c_add_adapter(&lpi2c_imx->adapter);
614 pm_runtime_mark_last_busy(&pdev->dev);
615 pm_runtime_put_autosuspend(&pdev->dev);
617 dev_info(&lpi2c_imx->adapter.dev, "LPI2C adapter registered\n");
622 pm_runtime_put(&pdev->dev);
623 pm_runtime_disable(&pdev->dev);
624 pm_runtime_dont_use_autosuspend(&pdev->dev);
629 static void lpi2c_imx_remove(struct platform_device *pdev)
631 struct lpi2c_imx_struct *lpi2c_imx = platform_get_drvdata(pdev);
633 i2c_del_adapter(&lpi2c_imx->adapter);
635 pm_runtime_disable(&pdev->dev);
636 pm_runtime_dont_use_autosuspend(&pdev->dev);
639 static int __maybe_unused lpi2c_runtime_suspend(struct device *dev)
641 struct lpi2c_imx_struct *lpi2c_imx = dev_get_drvdata(dev);
643 clk_bulk_disable(lpi2c_imx->num_clks, lpi2c_imx->clks);
644 pinctrl_pm_select_sleep_state(dev);
649 static int __maybe_unused lpi2c_runtime_resume(struct device *dev)
651 struct lpi2c_imx_struct *lpi2c_imx = dev_get_drvdata(dev);
654 pinctrl_pm_select_default_state(dev);
655 ret = clk_bulk_enable(lpi2c_imx->num_clks, lpi2c_imx->clks);
657 dev_err(dev, "failed to enable I2C clock, ret=%d\n", ret);
664 static const struct dev_pm_ops lpi2c_pm_ops = {
665 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
666 pm_runtime_force_resume)
667 SET_RUNTIME_PM_OPS(lpi2c_runtime_suspend,
668 lpi2c_runtime_resume, NULL)
671 static struct platform_driver lpi2c_imx_driver = {
672 .probe = lpi2c_imx_probe,
673 .remove_new = lpi2c_imx_remove,
676 .of_match_table = lpi2c_imx_of_match,
681 module_platform_driver(lpi2c_imx_driver);
683 MODULE_AUTHOR("Gao Pan <pandy.gao@nxp.com>");
684 MODULE_DESCRIPTION("I2C adapter driver for LPI2C bus");
685 MODULE_LICENSE("GPL");