2 * This is i.MX low power i2c controller driver.
4 * Copyright 2016 Freescale Semiconductor, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/clk.h>
19 #include <linux/completion.h>
20 #include <linux/delay.h>
21 #include <linux/err.h>
22 #include <linux/errno.h>
23 #include <linux/i2c.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
30 #include <linux/of_device.h>
31 #include <linux/pinctrl/consumer.h>
32 #include <linux/platform_device.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/sched.h>
35 #include <linux/slab.h>
37 #define DRIVER_NAME "imx-lpi2c"
39 #define LPI2C_PARAM 0x04 /* i2c RX/TX FIFO size */
40 #define LPI2C_MCR 0x10 /* i2c contrl register */
41 #define LPI2C_MSR 0x14 /* i2c status register */
42 #define LPI2C_MIER 0x18 /* i2c interrupt enable */
43 #define LPI2C_MCFGR0 0x20 /* i2c master configuration */
44 #define LPI2C_MCFGR1 0x24 /* i2c master configuration */
45 #define LPI2C_MCFGR2 0x28 /* i2c master configuration */
46 #define LPI2C_MCFGR3 0x2C /* i2c master configuration */
47 #define LPI2C_MCCR0 0x48 /* i2c master clk configuration */
48 #define LPI2C_MCCR1 0x50 /* i2c master clk configuration */
49 #define LPI2C_MFCR 0x58 /* i2c master FIFO control */
50 #define LPI2C_MFSR 0x5C /* i2c master FIFO status */
51 #define LPI2C_MTDR 0x60 /* i2c master TX data register */
52 #define LPI2C_MRDR 0x70 /* i2c master RX data register */
55 #define TRAN_DATA 0X00
56 #define RECV_DATA 0X01
58 #define RECV_DISCARD 0X03
59 #define GEN_START 0X04
60 #define START_NACK 0X05
61 #define START_HIGH 0X06
62 #define START_HIGH_NACK 0X07
64 #define MCR_MEN BIT(0)
65 #define MCR_RST BIT(1)
66 #define MCR_DOZEN BIT(2)
67 #define MCR_DBGEN BIT(3)
68 #define MCR_RTF BIT(8)
69 #define MCR_RRF BIT(9)
70 #define MSR_TDF BIT(0)
71 #define MSR_RDF BIT(1)
72 #define MSR_SDF BIT(9)
73 #define MSR_NDF BIT(10)
74 #define MSR_ALF BIT(11)
75 #define MSR_MBF BIT(24)
76 #define MSR_BBF BIT(25)
77 #define MIER_TDIE BIT(0)
78 #define MIER_RDIE BIT(1)
79 #define MIER_SDIE BIT(9)
80 #define MIER_NDIE BIT(10)
81 #define MCFGR1_AUTOSTOP BIT(8)
82 #define MCFGR1_IGNACK BIT(9)
83 #define MRDR_RXEMPTY BIT(14)
85 #define I2C_CLK_RATIO 2
86 #define CHUNK_DATA 256
88 #define LPI2C_DEFAULT_RATE 100000
89 #define STARDARD_MAX_BITRATE 400000
90 #define FAST_MAX_BITRATE 1000000
91 #define FAST_PLUS_MAX_BITRATE 3400000
92 #define HIGHSPEED_MAX_BITRATE 5000000
94 #define I2C_PM_TIMEOUT 10 /* ms */
97 STANDARD
, /* 100+Kbps */
99 FAST_PLUS
, /* 1.0+Mbps */
101 ULTRA_FAST
, /* 5.0+Mbps */
104 enum lpi2c_imx_pincfg
{
111 struct lpi2c_imx_struct
{
112 struct i2c_adapter adapter
;
117 struct completion complete
;
119 unsigned int delivered
;
120 unsigned int block_data
;
121 unsigned int bitrate
;
122 unsigned int txfifosize
;
123 unsigned int rxfifosize
;
124 enum lpi2c_imx_mode mode
;
127 static void lpi2c_imx_intctrl(struct lpi2c_imx_struct
*lpi2c_imx
,
130 writel(enable
, lpi2c_imx
->base
+ LPI2C_MIER
);
133 static int lpi2c_imx_bus_busy(struct lpi2c_imx_struct
*lpi2c_imx
)
135 unsigned long orig_jiffies
= jiffies
;
139 temp
= readl(lpi2c_imx
->base
+ LPI2C_MSR
);
141 /* check for arbitration lost, clear if set */
142 if (temp
& MSR_ALF
) {
143 writel(temp
, lpi2c_imx
->base
+ LPI2C_MSR
);
147 if (temp
& (MSR_BBF
| MSR_MBF
))
150 if (time_after(jiffies
, orig_jiffies
+ msecs_to_jiffies(500))) {
151 dev_dbg(&lpi2c_imx
->adapter
.dev
, "bus not work\n");
160 static void lpi2c_imx_set_mode(struct lpi2c_imx_struct
*lpi2c_imx
)
162 unsigned int bitrate
= lpi2c_imx
->bitrate
;
163 enum lpi2c_imx_mode mode
;
165 if (bitrate
< STARDARD_MAX_BITRATE
)
167 else if (bitrate
< FAST_MAX_BITRATE
)
169 else if (bitrate
< FAST_PLUS_MAX_BITRATE
)
171 else if (bitrate
< HIGHSPEED_MAX_BITRATE
)
176 lpi2c_imx
->mode
= mode
;
179 static int lpi2c_imx_start(struct lpi2c_imx_struct
*lpi2c_imx
,
180 struct i2c_msg
*msgs
)
185 temp
= readl(lpi2c_imx
->base
+ LPI2C_MCR
);
186 temp
|= MCR_RRF
| MCR_RTF
;
187 writel(temp
, lpi2c_imx
->base
+ LPI2C_MCR
);
188 writel(0x7f00, lpi2c_imx
->base
+ LPI2C_MSR
);
190 read
= msgs
->flags
& I2C_M_RD
;
191 temp
= (msgs
->addr
<< 1 | read
) | (GEN_START
<< 8);
192 writel(temp
, lpi2c_imx
->base
+ LPI2C_MTDR
);
194 return lpi2c_imx_bus_busy(lpi2c_imx
);
197 static void lpi2c_imx_stop(struct lpi2c_imx_struct
*lpi2c_imx
)
199 unsigned long orig_jiffies
= jiffies
;
202 writel(GEN_STOP
<< 8, lpi2c_imx
->base
+ LPI2C_MTDR
);
205 temp
= readl(lpi2c_imx
->base
+ LPI2C_MSR
);
209 if (time_after(jiffies
, orig_jiffies
+ msecs_to_jiffies(500))) {
210 dev_dbg(&lpi2c_imx
->adapter
.dev
, "stop timeout\n");
218 /* CLKLO = I2C_CLK_RATIO * CLKHI, SETHOLD = CLKHI, DATAVD = CLKHI/2 */
219 static int lpi2c_imx_config(struct lpi2c_imx_struct
*lpi2c_imx
)
221 u8 prescale
, filt
, sethold
, clkhi
, clklo
, datavd
;
222 unsigned int clk_rate
, clk_cycle
;
223 enum lpi2c_imx_pincfg pincfg
;
226 lpi2c_imx_set_mode(lpi2c_imx
);
228 clk_rate
= clk_get_rate(lpi2c_imx
->clk
);
229 if (lpi2c_imx
->mode
== HS
|| lpi2c_imx
->mode
== ULTRA_FAST
)
234 for (prescale
= 0; prescale
<= 7; prescale
++) {
235 clk_cycle
= clk_rate
/ ((1 << prescale
) * lpi2c_imx
->bitrate
)
237 clkhi
= (clk_cycle
+ I2C_CLK_RATIO
) / (I2C_CLK_RATIO
+ 1);
238 clklo
= clk_cycle
- clkhi
;
246 /* set MCFGR1: PINCFG, PRESCALE, IGNACK */
247 if (lpi2c_imx
->mode
== ULTRA_FAST
)
251 temp
= prescale
| pincfg
<< 24;
253 if (lpi2c_imx
->mode
== ULTRA_FAST
)
254 temp
|= MCFGR1_IGNACK
;
256 writel(temp
, lpi2c_imx
->base
+ LPI2C_MCFGR1
);
258 /* set MCFGR2: FILTSDA, FILTSCL */
259 temp
= (filt
<< 16) | (filt
<< 24);
260 writel(temp
, lpi2c_imx
->base
+ LPI2C_MCFGR2
);
262 /* set MCCR: DATAVD, SETHOLD, CLKHI, CLKLO */
265 temp
= datavd
<< 24 | sethold
<< 16 | clkhi
<< 8 | clklo
;
267 if (lpi2c_imx
->mode
== HS
)
268 writel(temp
, lpi2c_imx
->base
+ LPI2C_MCCR1
);
270 writel(temp
, lpi2c_imx
->base
+ LPI2C_MCCR0
);
275 static int lpi2c_imx_master_enable(struct lpi2c_imx_struct
*lpi2c_imx
)
280 ret
= pm_runtime_get_sync(lpi2c_imx
->adapter
.dev
.parent
);
285 writel(temp
, lpi2c_imx
->base
+ LPI2C_MCR
);
286 writel(0, lpi2c_imx
->base
+ LPI2C_MCR
);
288 ret
= lpi2c_imx_config(lpi2c_imx
);
292 temp
= readl(lpi2c_imx
->base
+ LPI2C_MCR
);
294 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_master_disable(struct lpi2c_imx_struct
*lpi2c_imx
)
309 temp
= readl(lpi2c_imx
->base
+ LPI2C_MCR
);
311 writel(temp
, lpi2c_imx
->base
+ LPI2C_MCR
);
313 pm_runtime_mark_last_busy(lpi2c_imx
->adapter
.dev
.parent
);
314 pm_runtime_put_autosuspend(lpi2c_imx
->adapter
.dev
.parent
);
319 static int lpi2c_imx_msg_complete(struct lpi2c_imx_struct
*lpi2c_imx
)
321 unsigned long timeout
;
323 timeout
= wait_for_completion_timeout(&lpi2c_imx
->complete
, HZ
);
325 return timeout
? 0 : -ETIMEDOUT
;
328 static int lpi2c_imx_txfifo_empty(struct lpi2c_imx_struct
*lpi2c_imx
)
330 unsigned long orig_jiffies
= jiffies
;
334 txcnt
= readl(lpi2c_imx
->base
+ LPI2C_MFSR
) & 0xff;
336 if (readl(lpi2c_imx
->base
+ LPI2C_MSR
) & MSR_NDF
) {
337 dev_dbg(&lpi2c_imx
->adapter
.dev
, "NDF detected\n");
341 if (time_after(jiffies
, orig_jiffies
+ msecs_to_jiffies(500))) {
342 dev_dbg(&lpi2c_imx
->adapter
.dev
, "txfifo empty timeout\n");
352 static void lpi2c_imx_set_tx_watermark(struct lpi2c_imx_struct
*lpi2c_imx
)
354 writel(lpi2c_imx
->txfifosize
>> 1, lpi2c_imx
->base
+ LPI2C_MFCR
);
357 static void lpi2c_imx_set_rx_watermark(struct lpi2c_imx_struct
*lpi2c_imx
)
359 unsigned int temp
, remaining
;
361 remaining
= lpi2c_imx
->msglen
- lpi2c_imx
->delivered
;
363 if (remaining
> (lpi2c_imx
->rxfifosize
>> 1))
364 temp
= lpi2c_imx
->rxfifosize
>> 1;
368 writel(temp
<< 16, lpi2c_imx
->base
+ LPI2C_MFCR
);
371 static void lpi2c_imx_write_txfifo(struct lpi2c_imx_struct
*lpi2c_imx
)
373 unsigned int data
, txcnt
;
375 txcnt
= readl(lpi2c_imx
->base
+ LPI2C_MFSR
) & 0xff;
377 while (txcnt
< lpi2c_imx
->txfifosize
) {
378 if (lpi2c_imx
->delivered
== lpi2c_imx
->msglen
)
381 data
= lpi2c_imx
->tx_buf
[lpi2c_imx
->delivered
++];
382 writel(data
, lpi2c_imx
->base
+ LPI2C_MTDR
);
386 if (lpi2c_imx
->delivered
< lpi2c_imx
->msglen
)
387 lpi2c_imx_intctrl(lpi2c_imx
, MIER_TDIE
| MIER_NDIE
);
389 complete(&lpi2c_imx
->complete
);
392 static void lpi2c_imx_read_rxfifo(struct lpi2c_imx_struct
*lpi2c_imx
)
394 unsigned int blocklen
, remaining
;
395 unsigned int temp
, data
;
398 data
= readl(lpi2c_imx
->base
+ LPI2C_MRDR
);
399 if (data
& MRDR_RXEMPTY
)
402 lpi2c_imx
->rx_buf
[lpi2c_imx
->delivered
++] = data
& 0xff;
406 * First byte is the length of remaining packet in the SMBus block
407 * data read. Add it to msgs->len.
409 if (lpi2c_imx
->block_data
) {
410 blocklen
= lpi2c_imx
->rx_buf
[0];
411 lpi2c_imx
->msglen
+= blocklen
;
414 remaining
= lpi2c_imx
->msglen
- lpi2c_imx
->delivered
;
417 complete(&lpi2c_imx
->complete
);
421 /* not finished, still waiting for rx data */
422 lpi2c_imx_set_rx_watermark(lpi2c_imx
);
424 /* multiple receive commands */
425 if (lpi2c_imx
->block_data
) {
426 lpi2c_imx
->block_data
= 0;
428 temp
|= (RECV_DATA
<< 8);
429 writel(temp
, lpi2c_imx
->base
+ LPI2C_MTDR
);
430 } else if (!(lpi2c_imx
->delivered
& 0xff)) {
431 temp
= (remaining
> CHUNK_DATA
? CHUNK_DATA
: remaining
) - 1;
432 temp
|= (RECV_DATA
<< 8);
433 writel(temp
, lpi2c_imx
->base
+ LPI2C_MTDR
);
436 lpi2c_imx_intctrl(lpi2c_imx
, MIER_RDIE
);
439 static void lpi2c_imx_write(struct lpi2c_imx_struct
*lpi2c_imx
,
440 struct i2c_msg
*msgs
)
442 lpi2c_imx
->tx_buf
= msgs
->buf
;
443 lpi2c_imx_set_tx_watermark(lpi2c_imx
);
444 lpi2c_imx_write_txfifo(lpi2c_imx
);
447 static void lpi2c_imx_read(struct lpi2c_imx_struct
*lpi2c_imx
,
448 struct i2c_msg
*msgs
)
452 lpi2c_imx
->rx_buf
= msgs
->buf
;
453 lpi2c_imx
->block_data
= msgs
->flags
& I2C_M_RECV_LEN
;
455 lpi2c_imx_set_rx_watermark(lpi2c_imx
);
456 temp
= msgs
->len
> CHUNK_DATA
? CHUNK_DATA
- 1 : msgs
->len
- 1;
457 temp
|= (RECV_DATA
<< 8);
458 writel(temp
, lpi2c_imx
->base
+ LPI2C_MTDR
);
460 lpi2c_imx_intctrl(lpi2c_imx
, MIER_RDIE
| MIER_NDIE
);
463 static int lpi2c_imx_xfer(struct i2c_adapter
*adapter
,
464 struct i2c_msg
*msgs
, int num
)
466 struct lpi2c_imx_struct
*lpi2c_imx
= i2c_get_adapdata(adapter
);
470 result
= lpi2c_imx_master_enable(lpi2c_imx
);
474 for (i
= 0; i
< num
; i
++) {
475 result
= lpi2c_imx_start(lpi2c_imx
, &msgs
[i
]);
480 if (num
== 1 && msgs
[0].len
== 0)
483 lpi2c_imx
->delivered
= 0;
484 lpi2c_imx
->msglen
= msgs
[i
].len
;
485 init_completion(&lpi2c_imx
->complete
);
487 if (msgs
[i
].flags
& I2C_M_RD
)
488 lpi2c_imx_read(lpi2c_imx
, &msgs
[i
]);
490 lpi2c_imx_write(lpi2c_imx
, &msgs
[i
]);
492 result
= lpi2c_imx_msg_complete(lpi2c_imx
);
496 if (!(msgs
[i
].flags
& I2C_M_RD
)) {
497 result
= lpi2c_imx_txfifo_empty(lpi2c_imx
);
504 lpi2c_imx_stop(lpi2c_imx
);
506 temp
= readl(lpi2c_imx
->base
+ LPI2C_MSR
);
507 if ((temp
& MSR_NDF
) && !result
)
511 lpi2c_imx_master_disable(lpi2c_imx
);
513 dev_dbg(&lpi2c_imx
->adapter
.dev
, "<%s> exit with: %s: %d\n", __func__
,
514 (result
< 0) ? "error" : "success msg",
515 (result
< 0) ? result
: num
);
517 return (result
< 0) ? result
: num
;
520 static irqreturn_t
lpi2c_imx_isr(int irq
, void *dev_id
)
522 struct lpi2c_imx_struct
*lpi2c_imx
= dev_id
;
525 lpi2c_imx_intctrl(lpi2c_imx
, 0);
526 temp
= readl(lpi2c_imx
->base
+ LPI2C_MSR
);
529 lpi2c_imx_read_rxfifo(lpi2c_imx
);
532 lpi2c_imx_write_txfifo(lpi2c_imx
);
535 complete(&lpi2c_imx
->complete
);
540 static u32
lpi2c_imx_func(struct i2c_adapter
*adapter
)
542 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
|
543 I2C_FUNC_SMBUS_READ_BLOCK_DATA
;
546 static const struct i2c_algorithm lpi2c_imx_algo
= {
547 .master_xfer
= lpi2c_imx_xfer
,
548 .functionality
= lpi2c_imx_func
,
551 static const struct of_device_id lpi2c_imx_of_match
[] = {
552 { .compatible
= "fsl,imx7ulp-lpi2c" },
553 { .compatible
= "fsl,imx8dv-lpi2c" },
556 MODULE_DEVICE_TABLE(of
, lpi2c_imx_of_match
);
558 static int lpi2c_imx_probe(struct platform_device
*pdev
)
560 struct lpi2c_imx_struct
*lpi2c_imx
;
561 struct resource
*res
;
565 lpi2c_imx
= devm_kzalloc(&pdev
->dev
, sizeof(*lpi2c_imx
), GFP_KERNEL
);
569 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
570 lpi2c_imx
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
571 if (IS_ERR(lpi2c_imx
->base
))
572 return PTR_ERR(lpi2c_imx
->base
);
574 irq
= platform_get_irq(pdev
, 0);
576 dev_err(&pdev
->dev
, "can't get irq number\n");
580 lpi2c_imx
->adapter
.owner
= THIS_MODULE
;
581 lpi2c_imx
->adapter
.algo
= &lpi2c_imx_algo
;
582 lpi2c_imx
->adapter
.dev
.parent
= &pdev
->dev
;
583 lpi2c_imx
->adapter
.dev
.of_node
= pdev
->dev
.of_node
;
584 strlcpy(lpi2c_imx
->adapter
.name
, pdev
->name
,
585 sizeof(lpi2c_imx
->adapter
.name
));
587 lpi2c_imx
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
588 if (IS_ERR(lpi2c_imx
->clk
)) {
589 dev_err(&pdev
->dev
, "can't get I2C peripheral clock\n");
590 return PTR_ERR(lpi2c_imx
->clk
);
593 ret
= of_property_read_u32(pdev
->dev
.of_node
,
594 "clock-frequency", &lpi2c_imx
->bitrate
);
596 lpi2c_imx
->bitrate
= LPI2C_DEFAULT_RATE
;
598 ret
= devm_request_irq(&pdev
->dev
, irq
, lpi2c_imx_isr
, 0,
599 pdev
->name
, lpi2c_imx
);
601 dev_err(&pdev
->dev
, "can't claim irq %d\n", irq
);
605 i2c_set_adapdata(&lpi2c_imx
->adapter
, lpi2c_imx
);
606 platform_set_drvdata(pdev
, lpi2c_imx
);
608 ret
= clk_prepare_enable(lpi2c_imx
->clk
);
610 dev_err(&pdev
->dev
, "clk enable failed %d\n", ret
);
614 pm_runtime_set_autosuspend_delay(&pdev
->dev
, I2C_PM_TIMEOUT
);
615 pm_runtime_use_autosuspend(&pdev
->dev
);
616 pm_runtime_get_noresume(&pdev
->dev
);
617 pm_runtime_set_active(&pdev
->dev
);
618 pm_runtime_enable(&pdev
->dev
);
620 temp
= readl(lpi2c_imx
->base
+ LPI2C_PARAM
);
621 lpi2c_imx
->txfifosize
= 1 << (temp
& 0x0f);
622 lpi2c_imx
->rxfifosize
= 1 << ((temp
>> 8) & 0x0f);
624 ret
= i2c_add_adapter(&lpi2c_imx
->adapter
);
628 pm_runtime_mark_last_busy(&pdev
->dev
);
629 pm_runtime_put_autosuspend(&pdev
->dev
);
631 dev_info(&lpi2c_imx
->adapter
.dev
, "LPI2C adapter registered\n");
636 pm_runtime_put(&pdev
->dev
);
637 pm_runtime_disable(&pdev
->dev
);
638 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
643 static int lpi2c_imx_remove(struct platform_device
*pdev
)
645 struct lpi2c_imx_struct
*lpi2c_imx
= platform_get_drvdata(pdev
);
647 i2c_del_adapter(&lpi2c_imx
->adapter
);
649 pm_runtime_disable(&pdev
->dev
);
650 pm_runtime_dont_use_autosuspend(&pdev
->dev
);
655 #ifdef CONFIG_PM_SLEEP
656 static int lpi2c_runtime_suspend(struct device
*dev
)
658 struct lpi2c_imx_struct
*lpi2c_imx
= dev_get_drvdata(dev
);
660 clk_disable_unprepare(lpi2c_imx
->clk
);
661 pinctrl_pm_select_sleep_state(dev
);
666 static int lpi2c_runtime_resume(struct device
*dev
)
668 struct lpi2c_imx_struct
*lpi2c_imx
= dev_get_drvdata(dev
);
671 pinctrl_pm_select_default_state(dev
);
672 ret
= clk_prepare_enable(lpi2c_imx
->clk
);
674 dev_err(dev
, "failed to enable I2C clock, ret=%d\n", ret
);
681 static const struct dev_pm_ops lpi2c_pm_ops
= {
682 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
683 pm_runtime_force_resume
)
684 SET_RUNTIME_PM_OPS(lpi2c_runtime_suspend
,
685 lpi2c_runtime_resume
, NULL
)
687 #define IMX_LPI2C_PM (&lpi2c_pm_ops)
689 #define IMX_LPI2C_PM NULL
692 static struct platform_driver lpi2c_imx_driver
= {
693 .probe
= lpi2c_imx_probe
,
694 .remove
= lpi2c_imx_remove
,
697 .of_match_table
= lpi2c_imx_of_match
,
702 module_platform_driver(lpi2c_imx_driver
);
704 MODULE_AUTHOR("Gao Pan <pandy.gao@nxp.com>");
705 MODULE_DESCRIPTION("I2C adapter driver for LPI2C bus");
706 MODULE_LICENSE("GPL");