1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 MediaTek Inc.
4 * Author: Xudong Chen <xudong.chen@mediatek.com>
8 #include <linux/completion.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/err.h>
13 #include <linux/errno.h>
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/of_address.h>
22 #include <linux/of_device.h>
23 #include <linux/of_irq.h>
24 #include <linux/platform_device.h>
25 #include <linux/scatterlist.h>
26 #include <linux/sched.h>
27 #include <linux/slab.h>
29 #define I2C_RS_TRANSFER (1 << 4)
30 #define I2C_ARB_LOST (1 << 3)
31 #define I2C_HS_NACKERR (1 << 2)
32 #define I2C_ACKERR (1 << 1)
33 #define I2C_TRANSAC_COMP (1 << 0)
34 #define I2C_TRANSAC_START (1 << 0)
35 #define I2C_RS_MUL_CNFG (1 << 15)
36 #define I2C_RS_MUL_TRIG (1 << 14)
37 #define I2C_DCM_DISABLE 0x0000
38 #define I2C_IO_CONFIG_OPEN_DRAIN 0x0003
39 #define I2C_IO_CONFIG_PUSH_PULL 0x0000
40 #define I2C_SOFT_RST 0x0001
41 #define I2C_FIFO_ADDR_CLR 0x0001
42 #define I2C_DELAY_LEN 0x0002
43 #define I2C_ST_START_CON 0x8001
44 #define I2C_FS_START_CON 0x1800
45 #define I2C_TIME_CLR_VALUE 0x0000
46 #define I2C_TIME_DEFAULT_VALUE 0x0003
47 #define I2C_WRRD_TRANAC_VALUE 0x0002
48 #define I2C_RD_TRANAC_VALUE 0x0001
50 #define I2C_DMA_CON_TX 0x0000
51 #define I2C_DMA_CON_RX 0x0001
52 #define I2C_DMA_START_EN 0x0001
53 #define I2C_DMA_INT_FLAG_NONE 0x0000
54 #define I2C_DMA_CLR_FLAG 0x0000
55 #define I2C_DMA_HARD_RST 0x0002
56 #define I2C_DMA_4G_MODE 0x0001
58 #define I2C_DEFAULT_CLK_DIV 5
59 #define I2C_DEFAULT_SPEED 100000 /* hz */
60 #define MAX_FS_MODE_SPEED 400000
61 #define MAX_HS_MODE_SPEED 3400000
62 #define MAX_SAMPLE_CNT_DIV 8
63 #define MAX_STEP_CNT_DIV 64
64 #define MAX_HS_STEP_CNT_DIV 8
66 #define I2C_CONTROL_RS (0x1 << 1)
67 #define I2C_CONTROL_DMA_EN (0x1 << 2)
68 #define I2C_CONTROL_CLK_EXT_EN (0x1 << 3)
69 #define I2C_CONTROL_DIR_CHANGE (0x1 << 4)
70 #define I2C_CONTROL_ACKERR_DET_EN (0x1 << 5)
71 #define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6)
72 #define I2C_CONTROL_DMAACK_EN (0x1 << 8)
73 #define I2C_CONTROL_ASYNC_MODE (0x1 << 9)
74 #define I2C_CONTROL_WRAPPER (0x1 << 0)
76 #define I2C_DRV_NAME "i2c-mt65xx"
78 enum DMA_REGS_OFFSET
{
79 OFFSET_INT_FLAG
= 0x0,
84 OFFSET_TX_MEM_ADDR
= 0x1c,
85 OFFSET_RX_MEM_ADDR
= 0x20,
88 OFFSET_TX_4G_MODE
= 0x54,
89 OFFSET_RX_4G_MODE
= 0x58,
92 enum i2c_trans_st_rs
{
94 I2C_TRANS_REPEATED_START
,
103 enum I2C_REGS_OFFSET
{
117 OFFSET_FIFO_ADDR_CLR
,
126 OFFSET_TRANSFER_LEN_AUX
,
131 static const u16 mt_i2c_regs_v1
[] = {
132 [OFFSET_DATA_PORT
] = 0x0,
133 [OFFSET_SLAVE_ADDR
] = 0x4,
134 [OFFSET_INTR_MASK
] = 0x8,
135 [OFFSET_INTR_STAT
] = 0xc,
136 [OFFSET_CONTROL
] = 0x10,
137 [OFFSET_TRANSFER_LEN
] = 0x14,
138 [OFFSET_TRANSAC_LEN
] = 0x18,
139 [OFFSET_DELAY_LEN
] = 0x1c,
140 [OFFSET_TIMING
] = 0x20,
141 [OFFSET_START
] = 0x24,
142 [OFFSET_EXT_CONF
] = 0x28,
143 [OFFSET_FIFO_STAT
] = 0x30,
144 [OFFSET_FIFO_THRESH
] = 0x34,
145 [OFFSET_FIFO_ADDR_CLR
] = 0x38,
146 [OFFSET_IO_CONFIG
] = 0x40,
147 [OFFSET_RSV_DEBUG
] = 0x44,
149 [OFFSET_SOFTRESET
] = 0x50,
150 [OFFSET_DCM_EN
] = 0x54,
151 [OFFSET_PATH_DIR
] = 0x60,
152 [OFFSET_DEBUGSTAT
] = 0x64,
153 [OFFSET_DEBUGCTRL
] = 0x68,
154 [OFFSET_TRANSFER_LEN_AUX
] = 0x6c,
155 [OFFSET_CLOCK_DIV
] = 0x70,
158 static const u16 mt_i2c_regs_v2
[] = {
159 [OFFSET_DATA_PORT
] = 0x0,
160 [OFFSET_SLAVE_ADDR
] = 0x4,
161 [OFFSET_INTR_MASK
] = 0x8,
162 [OFFSET_INTR_STAT
] = 0xc,
163 [OFFSET_CONTROL
] = 0x10,
164 [OFFSET_TRANSFER_LEN
] = 0x14,
165 [OFFSET_TRANSAC_LEN
] = 0x18,
166 [OFFSET_DELAY_LEN
] = 0x1c,
167 [OFFSET_TIMING
] = 0x20,
168 [OFFSET_START
] = 0x24,
169 [OFFSET_EXT_CONF
] = 0x28,
170 [OFFSET_LTIMING
] = 0x2c,
172 [OFFSET_IO_CONFIG
] = 0x34,
173 [OFFSET_FIFO_ADDR_CLR
] = 0x38,
174 [OFFSET_TRANSFER_LEN_AUX
] = 0x44,
175 [OFFSET_CLOCK_DIV
] = 0x48,
176 [OFFSET_SOFTRESET
] = 0x50,
177 [OFFSET_DEBUGSTAT
] = 0xe0,
178 [OFFSET_DEBUGCTRL
] = 0xe8,
179 [OFFSET_FIFO_STAT
] = 0xf4,
180 [OFFSET_FIFO_THRESH
] = 0xf8,
181 [OFFSET_DCM_EN
] = 0xf88,
184 struct mtk_i2c_compatible
{
185 const struct i2c_adapter_quirks
*quirks
;
187 unsigned char pmic_i2c
: 1;
188 unsigned char dcm
: 1;
189 unsigned char auto_restart
: 1;
190 unsigned char aux_len_reg
: 1;
191 unsigned char support_33bits
: 1;
192 unsigned char timing_adjust
: 1;
193 unsigned char dma_sync
: 1;
194 unsigned char ltiming_adjust
: 1;
198 struct i2c_adapter adap
; /* i2c host adapter */
200 struct completion msg_complete
;
202 /* set in i2c probe */
203 void __iomem
*base
; /* i2c base addr */
204 void __iomem
*pdmabase
; /* dma base address*/
205 struct clk
*clk_main
; /* main clock for i2c bus */
206 struct clk
*clk_dma
; /* DMA clock for i2c via DMA */
207 struct clk
*clk_pmic
; /* PMIC clock for i2c from PMIC */
208 struct clk
*clk_arb
; /* Arbitrator clock for i2c */
209 bool have_pmic
; /* can use i2c pins from PMIC */
210 bool use_push_pull
; /* IO config push-pull mode */
212 u16 irq_stat
; /* interrupt status */
213 unsigned int clk_src_div
;
214 unsigned int speed_hz
; /* The speed in transfer */
215 enum mtk_trans_op op
;
219 unsigned char auto_restart
;
220 bool ignore_restart_irq
;
221 const struct mtk_i2c_compatible
*dev_comp
;
224 static const struct i2c_adapter_quirks mt6577_i2c_quirks
= {
225 .flags
= I2C_AQ_COMB_WRITE_THEN_READ
,
227 .max_write_len
= 255,
229 .max_comb_1st_msg_len
= 255,
230 .max_comb_2nd_msg_len
= 31,
233 static const struct i2c_adapter_quirks mt7622_i2c_quirks
= {
237 static const struct i2c_adapter_quirks mt8183_i2c_quirks
= {
238 .flags
= I2C_AQ_NO_ZERO_LEN
,
241 static const struct mtk_i2c_compatible mt2712_compat
= {
242 .regs
= mt_i2c_regs_v1
,
253 static const struct mtk_i2c_compatible mt6577_compat
= {
254 .quirks
= &mt6577_i2c_quirks
,
255 .regs
= mt_i2c_regs_v1
,
266 static const struct mtk_i2c_compatible mt6589_compat
= {
267 .quirks
= &mt6577_i2c_quirks
,
268 .regs
= mt_i2c_regs_v1
,
279 static const struct mtk_i2c_compatible mt7622_compat
= {
280 .quirks
= &mt7622_i2c_quirks
,
281 .regs
= mt_i2c_regs_v1
,
292 static const struct mtk_i2c_compatible mt8173_compat
= {
293 .regs
= mt_i2c_regs_v1
,
304 static const struct mtk_i2c_compatible mt8183_compat
= {
305 .quirks
= &mt8183_i2c_quirks
,
306 .regs
= mt_i2c_regs_v2
,
317 static const struct of_device_id mtk_i2c_of_match
[] = {
318 { .compatible
= "mediatek,mt2712-i2c", .data
= &mt2712_compat
},
319 { .compatible
= "mediatek,mt6577-i2c", .data
= &mt6577_compat
},
320 { .compatible
= "mediatek,mt6589-i2c", .data
= &mt6589_compat
},
321 { .compatible
= "mediatek,mt7622-i2c", .data
= &mt7622_compat
},
322 { .compatible
= "mediatek,mt8173-i2c", .data
= &mt8173_compat
},
323 { .compatible
= "mediatek,mt8183-i2c", .data
= &mt8183_compat
},
326 MODULE_DEVICE_TABLE(of
, mtk_i2c_of_match
);
328 static u16
mtk_i2c_readw(struct mtk_i2c
*i2c
, enum I2C_REGS_OFFSET reg
)
330 return readw(i2c
->base
+ i2c
->dev_comp
->regs
[reg
]);
333 static void mtk_i2c_writew(struct mtk_i2c
*i2c
, u16 val
,
334 enum I2C_REGS_OFFSET reg
)
336 writew(val
, i2c
->base
+ i2c
->dev_comp
->regs
[reg
]);
339 static int mtk_i2c_clock_enable(struct mtk_i2c
*i2c
)
343 ret
= clk_prepare_enable(i2c
->clk_dma
);
347 ret
= clk_prepare_enable(i2c
->clk_main
);
351 if (i2c
->have_pmic
) {
352 ret
= clk_prepare_enable(i2c
->clk_pmic
);
358 ret
= clk_prepare_enable(i2c
->clk_arb
);
367 clk_disable_unprepare(i2c
->clk_pmic
);
369 clk_disable_unprepare(i2c
->clk_main
);
371 clk_disable_unprepare(i2c
->clk_dma
);
376 static void mtk_i2c_clock_disable(struct mtk_i2c
*i2c
)
379 clk_disable_unprepare(i2c
->clk_arb
);
382 clk_disable_unprepare(i2c
->clk_pmic
);
384 clk_disable_unprepare(i2c
->clk_main
);
385 clk_disable_unprepare(i2c
->clk_dma
);
388 static void mtk_i2c_init_hw(struct mtk_i2c
*i2c
)
392 mtk_i2c_writew(i2c
, I2C_SOFT_RST
, OFFSET_SOFTRESET
);
395 if (i2c
->use_push_pull
)
396 mtk_i2c_writew(i2c
, I2C_IO_CONFIG_PUSH_PULL
, OFFSET_IO_CONFIG
);
398 mtk_i2c_writew(i2c
, I2C_IO_CONFIG_OPEN_DRAIN
, OFFSET_IO_CONFIG
);
400 if (i2c
->dev_comp
->dcm
)
401 mtk_i2c_writew(i2c
, I2C_DCM_DISABLE
, OFFSET_DCM_EN
);
403 if (i2c
->dev_comp
->timing_adjust
)
404 mtk_i2c_writew(i2c
, I2C_DEFAULT_CLK_DIV
- 1, OFFSET_CLOCK_DIV
);
406 mtk_i2c_writew(i2c
, i2c
->timing_reg
, OFFSET_TIMING
);
407 mtk_i2c_writew(i2c
, i2c
->high_speed_reg
, OFFSET_HS
);
408 if (i2c
->dev_comp
->ltiming_adjust
)
409 mtk_i2c_writew(i2c
, i2c
->ltiming_reg
, OFFSET_LTIMING
);
411 /* If use i2c pin from PMIC mt6397 side, need set PATH_DIR first */
413 mtk_i2c_writew(i2c
, I2C_CONTROL_WRAPPER
, OFFSET_PATH_DIR
);
415 control_reg
= I2C_CONTROL_ACKERR_DET_EN
|
416 I2C_CONTROL_CLK_EXT_EN
| I2C_CONTROL_DMA_EN
;
417 if (i2c
->dev_comp
->dma_sync
)
418 control_reg
|= I2C_CONTROL_DMAACK_EN
| I2C_CONTROL_ASYNC_MODE
;
420 mtk_i2c_writew(i2c
, control_reg
, OFFSET_CONTROL
);
421 mtk_i2c_writew(i2c
, I2C_DELAY_LEN
, OFFSET_DELAY_LEN
);
423 writel(I2C_DMA_HARD_RST
, i2c
->pdmabase
+ OFFSET_RST
);
425 writel(I2C_DMA_CLR_FLAG
, i2c
->pdmabase
+ OFFSET_RST
);
429 * Calculate i2c port speed
432 * i2c_bus_freq = parent_clk / (clock_div * 2 * sample_cnt * step_cnt)
433 * clock_div: fixed in hardware, but may be various in different SoCs
435 * The calculation want to pick the highest bus frequency that is still
436 * less than or equal to i2c->speed_hz. The calculation try to get
437 * sample_cnt and step_cn
439 static int mtk_i2c_calculate_speed(struct mtk_i2c
*i2c
, unsigned int clk_src
,
440 unsigned int target_speed
,
441 unsigned int *timing_step_cnt
,
442 unsigned int *timing_sample_cnt
)
444 unsigned int step_cnt
;
445 unsigned int sample_cnt
;
446 unsigned int max_step_cnt
;
447 unsigned int base_sample_cnt
= MAX_SAMPLE_CNT_DIV
;
448 unsigned int base_step_cnt
;
449 unsigned int opt_div
;
450 unsigned int best_mul
;
451 unsigned int cnt_mul
;
453 if (target_speed
> MAX_HS_MODE_SPEED
)
454 target_speed
= MAX_HS_MODE_SPEED
;
456 if (target_speed
> MAX_FS_MODE_SPEED
)
457 max_step_cnt
= MAX_HS_STEP_CNT_DIV
;
459 max_step_cnt
= MAX_STEP_CNT_DIV
;
461 base_step_cnt
= max_step_cnt
;
462 /* Find the best combination */
463 opt_div
= DIV_ROUND_UP(clk_src
>> 1, target_speed
);
464 best_mul
= MAX_SAMPLE_CNT_DIV
* max_step_cnt
;
466 /* Search for the best pair (sample_cnt, step_cnt) with
467 * 0 < sample_cnt < MAX_SAMPLE_CNT_DIV
468 * 0 < step_cnt < max_step_cnt
469 * sample_cnt * step_cnt >= opt_div
470 * optimizing for sample_cnt * step_cnt being minimal
472 for (sample_cnt
= 1; sample_cnt
<= MAX_SAMPLE_CNT_DIV
; sample_cnt
++) {
473 step_cnt
= DIV_ROUND_UP(opt_div
, sample_cnt
);
474 cnt_mul
= step_cnt
* sample_cnt
;
475 if (step_cnt
> max_step_cnt
)
478 if (cnt_mul
< best_mul
) {
480 base_sample_cnt
= sample_cnt
;
481 base_step_cnt
= step_cnt
;
482 if (best_mul
== opt_div
)
487 sample_cnt
= base_sample_cnt
;
488 step_cnt
= base_step_cnt
;
490 if ((clk_src
/ (2 * sample_cnt
* step_cnt
)) > target_speed
) {
491 /* In this case, hardware can't support such
494 dev_dbg(i2c
->dev
, "Unsupported speed (%uhz)\n", target_speed
);
498 *timing_step_cnt
= step_cnt
- 1;
499 *timing_sample_cnt
= sample_cnt
- 1;
504 static int mtk_i2c_set_speed(struct mtk_i2c
*i2c
, unsigned int parent_clk
)
506 unsigned int clk_src
;
507 unsigned int step_cnt
;
508 unsigned int sample_cnt
;
509 unsigned int l_step_cnt
;
510 unsigned int l_sample_cnt
;
511 unsigned int target_speed
;
514 clk_src
= parent_clk
/ i2c
->clk_src_div
;
515 target_speed
= i2c
->speed_hz
;
517 if (target_speed
> MAX_FS_MODE_SPEED
) {
518 /* Set master code speed register */
519 ret
= mtk_i2c_calculate_speed(i2c
, clk_src
, MAX_FS_MODE_SPEED
,
520 &l_step_cnt
, &l_sample_cnt
);
524 i2c
->timing_reg
= (l_sample_cnt
<< 8) | l_step_cnt
;
526 /* Set the high speed mode register */
527 ret
= mtk_i2c_calculate_speed(i2c
, clk_src
, target_speed
,
528 &step_cnt
, &sample_cnt
);
532 i2c
->high_speed_reg
= I2C_TIME_DEFAULT_VALUE
|
533 (sample_cnt
<< 12) | (step_cnt
<< 8);
535 if (i2c
->dev_comp
->ltiming_adjust
)
536 i2c
->ltiming_reg
= (l_sample_cnt
<< 6) | l_step_cnt
|
537 (sample_cnt
<< 12) | (step_cnt
<< 9);
539 ret
= mtk_i2c_calculate_speed(i2c
, clk_src
, target_speed
,
540 &step_cnt
, &sample_cnt
);
544 i2c
->timing_reg
= (sample_cnt
<< 8) | step_cnt
;
546 /* Disable the high speed transaction */
547 i2c
->high_speed_reg
= I2C_TIME_CLR_VALUE
;
549 if (i2c
->dev_comp
->ltiming_adjust
)
550 i2c
->ltiming_reg
= (sample_cnt
<< 6) | step_cnt
;
556 static inline u32
mtk_i2c_set_4g_mode(dma_addr_t addr
)
558 return (addr
& BIT_ULL(32)) ? I2C_DMA_4G_MODE
: I2C_DMA_CLR_FLAG
;
561 static int mtk_i2c_do_transfer(struct mtk_i2c
*i2c
, struct i2c_msg
*msgs
,
562 int num
, int left_num
)
567 u16 restart_flag
= 0;
569 u8
*dma_rd_buf
= NULL
;
570 u8
*dma_wr_buf
= NULL
;
571 dma_addr_t rpaddr
= 0;
572 dma_addr_t wpaddr
= 0;
577 if (i2c
->auto_restart
)
578 restart_flag
= I2C_RS_TRANSFER
;
580 reinit_completion(&i2c
->msg_complete
);
582 control_reg
= mtk_i2c_readw(i2c
, OFFSET_CONTROL
) &
583 ~(I2C_CONTROL_DIR_CHANGE
| I2C_CONTROL_RS
);
584 if ((i2c
->speed_hz
> MAX_FS_MODE_SPEED
) || (left_num
>= 1))
585 control_reg
|= I2C_CONTROL_RS
;
587 if (i2c
->op
== I2C_MASTER_WRRD
)
588 control_reg
|= I2C_CONTROL_DIR_CHANGE
| I2C_CONTROL_RS
;
590 mtk_i2c_writew(i2c
, control_reg
, OFFSET_CONTROL
);
592 /* set start condition */
593 if (i2c
->speed_hz
<= I2C_DEFAULT_SPEED
)
594 mtk_i2c_writew(i2c
, I2C_ST_START_CON
, OFFSET_EXT_CONF
);
596 mtk_i2c_writew(i2c
, I2C_FS_START_CON
, OFFSET_EXT_CONF
);
598 addr_reg
= i2c_8bit_addr_from_msg(msgs
);
599 mtk_i2c_writew(i2c
, addr_reg
, OFFSET_SLAVE_ADDR
);
601 /* Clear interrupt status */
602 mtk_i2c_writew(i2c
, restart_flag
| I2C_HS_NACKERR
| I2C_ACKERR
|
603 I2C_ARB_LOST
| I2C_TRANSAC_COMP
, OFFSET_INTR_STAT
);
605 mtk_i2c_writew(i2c
, I2C_FIFO_ADDR_CLR
, OFFSET_FIFO_ADDR_CLR
);
607 /* Enable interrupt */
608 mtk_i2c_writew(i2c
, restart_flag
| I2C_HS_NACKERR
| I2C_ACKERR
|
609 I2C_ARB_LOST
| I2C_TRANSAC_COMP
, OFFSET_INTR_MASK
);
611 /* Set transfer and transaction len */
612 if (i2c
->op
== I2C_MASTER_WRRD
) {
613 if (i2c
->dev_comp
->aux_len_reg
) {
614 mtk_i2c_writew(i2c
, msgs
->len
, OFFSET_TRANSFER_LEN
);
615 mtk_i2c_writew(i2c
, (msgs
+ 1)->len
,
616 OFFSET_TRANSFER_LEN_AUX
);
618 mtk_i2c_writew(i2c
, msgs
->len
| ((msgs
+ 1)->len
) << 8,
619 OFFSET_TRANSFER_LEN
);
621 mtk_i2c_writew(i2c
, I2C_WRRD_TRANAC_VALUE
, OFFSET_TRANSAC_LEN
);
623 mtk_i2c_writew(i2c
, msgs
->len
, OFFSET_TRANSFER_LEN
);
624 mtk_i2c_writew(i2c
, num
, OFFSET_TRANSAC_LEN
);
627 /* Prepare buffer data to start transfer */
628 if (i2c
->op
== I2C_MASTER_RD
) {
629 writel(I2C_DMA_INT_FLAG_NONE
, i2c
->pdmabase
+ OFFSET_INT_FLAG
);
630 writel(I2C_DMA_CON_RX
, i2c
->pdmabase
+ OFFSET_CON
);
632 dma_rd_buf
= i2c_get_dma_safe_msg_buf(msgs
, 1);
636 rpaddr
= dma_map_single(i2c
->dev
, dma_rd_buf
,
637 msgs
->len
, DMA_FROM_DEVICE
);
638 if (dma_mapping_error(i2c
->dev
, rpaddr
)) {
639 i2c_put_dma_safe_msg_buf(dma_rd_buf
, msgs
, false);
644 if (i2c
->dev_comp
->support_33bits
) {
645 reg_4g_mode
= mtk_i2c_set_4g_mode(rpaddr
);
646 writel(reg_4g_mode
, i2c
->pdmabase
+ OFFSET_RX_4G_MODE
);
649 writel((u32
)rpaddr
, i2c
->pdmabase
+ OFFSET_RX_MEM_ADDR
);
650 writel(msgs
->len
, i2c
->pdmabase
+ OFFSET_RX_LEN
);
651 } else if (i2c
->op
== I2C_MASTER_WR
) {
652 writel(I2C_DMA_INT_FLAG_NONE
, i2c
->pdmabase
+ OFFSET_INT_FLAG
);
653 writel(I2C_DMA_CON_TX
, i2c
->pdmabase
+ OFFSET_CON
);
655 dma_wr_buf
= i2c_get_dma_safe_msg_buf(msgs
, 1);
659 wpaddr
= dma_map_single(i2c
->dev
, dma_wr_buf
,
660 msgs
->len
, DMA_TO_DEVICE
);
661 if (dma_mapping_error(i2c
->dev
, wpaddr
)) {
662 i2c_put_dma_safe_msg_buf(dma_wr_buf
, msgs
, false);
667 if (i2c
->dev_comp
->support_33bits
) {
668 reg_4g_mode
= mtk_i2c_set_4g_mode(wpaddr
);
669 writel(reg_4g_mode
, i2c
->pdmabase
+ OFFSET_TX_4G_MODE
);
672 writel((u32
)wpaddr
, i2c
->pdmabase
+ OFFSET_TX_MEM_ADDR
);
673 writel(msgs
->len
, i2c
->pdmabase
+ OFFSET_TX_LEN
);
675 writel(I2C_DMA_CLR_FLAG
, i2c
->pdmabase
+ OFFSET_INT_FLAG
);
676 writel(I2C_DMA_CLR_FLAG
, i2c
->pdmabase
+ OFFSET_CON
);
678 dma_wr_buf
= i2c_get_dma_safe_msg_buf(msgs
, 1);
682 wpaddr
= dma_map_single(i2c
->dev
, dma_wr_buf
,
683 msgs
->len
, DMA_TO_DEVICE
);
684 if (dma_mapping_error(i2c
->dev
, wpaddr
)) {
685 i2c_put_dma_safe_msg_buf(dma_wr_buf
, msgs
, false);
690 dma_rd_buf
= i2c_get_dma_safe_msg_buf((msgs
+ 1), 1);
692 dma_unmap_single(i2c
->dev
, wpaddr
,
693 msgs
->len
, DMA_TO_DEVICE
);
695 i2c_put_dma_safe_msg_buf(dma_wr_buf
, msgs
, false);
700 rpaddr
= dma_map_single(i2c
->dev
, dma_rd_buf
,
703 if (dma_mapping_error(i2c
->dev
, rpaddr
)) {
704 dma_unmap_single(i2c
->dev
, wpaddr
,
705 msgs
->len
, DMA_TO_DEVICE
);
707 i2c_put_dma_safe_msg_buf(dma_wr_buf
, msgs
, false);
708 i2c_put_dma_safe_msg_buf(dma_rd_buf
, (msgs
+ 1), false);
713 if (i2c
->dev_comp
->support_33bits
) {
714 reg_4g_mode
= mtk_i2c_set_4g_mode(wpaddr
);
715 writel(reg_4g_mode
, i2c
->pdmabase
+ OFFSET_TX_4G_MODE
);
717 reg_4g_mode
= mtk_i2c_set_4g_mode(rpaddr
);
718 writel(reg_4g_mode
, i2c
->pdmabase
+ OFFSET_RX_4G_MODE
);
721 writel((u32
)wpaddr
, i2c
->pdmabase
+ OFFSET_TX_MEM_ADDR
);
722 writel((u32
)rpaddr
, i2c
->pdmabase
+ OFFSET_RX_MEM_ADDR
);
723 writel(msgs
->len
, i2c
->pdmabase
+ OFFSET_TX_LEN
);
724 writel((msgs
+ 1)->len
, i2c
->pdmabase
+ OFFSET_RX_LEN
);
727 writel(I2C_DMA_START_EN
, i2c
->pdmabase
+ OFFSET_EN
);
729 if (!i2c
->auto_restart
) {
730 start_reg
= I2C_TRANSAC_START
;
732 start_reg
= I2C_TRANSAC_START
| I2C_RS_MUL_TRIG
;
734 start_reg
|= I2C_RS_MUL_CNFG
;
736 mtk_i2c_writew(i2c
, start_reg
, OFFSET_START
);
738 ret
= wait_for_completion_timeout(&i2c
->msg_complete
,
741 /* Clear interrupt mask */
742 mtk_i2c_writew(i2c
, ~(restart_flag
| I2C_HS_NACKERR
| I2C_ACKERR
|
743 I2C_ARB_LOST
| I2C_TRANSAC_COMP
), OFFSET_INTR_MASK
);
745 if (i2c
->op
== I2C_MASTER_WR
) {
746 dma_unmap_single(i2c
->dev
, wpaddr
,
747 msgs
->len
, DMA_TO_DEVICE
);
749 i2c_put_dma_safe_msg_buf(dma_wr_buf
, msgs
, true);
750 } else if (i2c
->op
== I2C_MASTER_RD
) {
751 dma_unmap_single(i2c
->dev
, rpaddr
,
752 msgs
->len
, DMA_FROM_DEVICE
);
754 i2c_put_dma_safe_msg_buf(dma_rd_buf
, msgs
, true);
756 dma_unmap_single(i2c
->dev
, wpaddr
, msgs
->len
,
758 dma_unmap_single(i2c
->dev
, rpaddr
, (msgs
+ 1)->len
,
761 i2c_put_dma_safe_msg_buf(dma_wr_buf
, msgs
, true);
762 i2c_put_dma_safe_msg_buf(dma_rd_buf
, (msgs
+ 1), true);
766 dev_dbg(i2c
->dev
, "addr: %x, transfer timeout\n", msgs
->addr
);
767 mtk_i2c_init_hw(i2c
);
771 if (i2c
->irq_stat
& (I2C_HS_NACKERR
| I2C_ACKERR
)) {
772 dev_dbg(i2c
->dev
, "addr: %x, transfer ACK error\n", msgs
->addr
);
773 mtk_i2c_init_hw(i2c
);
780 static int mtk_i2c_transfer(struct i2c_adapter
*adap
,
781 struct i2c_msg msgs
[], int num
)
785 struct mtk_i2c
*i2c
= i2c_get_adapdata(adap
);
787 ret
= mtk_i2c_clock_enable(i2c
);
791 i2c
->auto_restart
= i2c
->dev_comp
->auto_restart
;
793 /* checking if we can skip restart and optimize using WRRD mode */
794 if (i2c
->auto_restart
&& num
== 2) {
795 if (!(msgs
[0].flags
& I2C_M_RD
) && (msgs
[1].flags
& I2C_M_RD
) &&
796 msgs
[0].addr
== msgs
[1].addr
) {
797 i2c
->auto_restart
= 0;
801 if (i2c
->auto_restart
&& num
>= 2 && i2c
->speed_hz
> MAX_FS_MODE_SPEED
)
802 /* ignore the first restart irq after the master code,
803 * otherwise the first transfer will be discarded.
805 i2c
->ignore_restart_irq
= true;
807 i2c
->ignore_restart_irq
= false;
811 dev_dbg(i2c
->dev
, "data buffer is NULL.\n");
816 if (msgs
->flags
& I2C_M_RD
)
817 i2c
->op
= I2C_MASTER_RD
;
819 i2c
->op
= I2C_MASTER_WR
;
821 if (!i2c
->auto_restart
) {
823 /* combined two messages into one transaction */
824 i2c
->op
= I2C_MASTER_WRRD
;
829 /* always use DMA mode. */
830 ret
= mtk_i2c_do_transfer(i2c
, msgs
, num
, left_num
);
836 /* the return value is number of executed messages */
840 mtk_i2c_clock_disable(i2c
);
844 static irqreturn_t
mtk_i2c_irq(int irqno
, void *dev_id
)
846 struct mtk_i2c
*i2c
= dev_id
;
847 u16 restart_flag
= 0;
850 if (i2c
->auto_restart
)
851 restart_flag
= I2C_RS_TRANSFER
;
853 intr_stat
= mtk_i2c_readw(i2c
, OFFSET_INTR_STAT
);
854 mtk_i2c_writew(i2c
, intr_stat
, OFFSET_INTR_STAT
);
857 * when occurs ack error, i2c controller generate two interrupts
858 * first is the ack error interrupt, then the complete interrupt
859 * i2c->irq_stat need keep the two interrupt value.
861 i2c
->irq_stat
|= intr_stat
;
863 if (i2c
->ignore_restart_irq
&& (i2c
->irq_stat
& restart_flag
)) {
864 i2c
->ignore_restart_irq
= false;
866 mtk_i2c_writew(i2c
, I2C_RS_MUL_CNFG
| I2C_RS_MUL_TRIG
|
867 I2C_TRANSAC_START
, OFFSET_START
);
869 if (i2c
->irq_stat
& (I2C_TRANSAC_COMP
| restart_flag
))
870 complete(&i2c
->msg_complete
);
876 static u32
mtk_i2c_functionality(struct i2c_adapter
*adap
)
878 if (i2c_check_quirks(adap
, I2C_AQ_NO_ZERO_LEN
))
879 return I2C_FUNC_I2C
|
880 (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
);
882 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
885 static const struct i2c_algorithm mtk_i2c_algorithm
= {
886 .master_xfer
= mtk_i2c_transfer
,
887 .functionality
= mtk_i2c_functionality
,
890 static int mtk_i2c_parse_dt(struct device_node
*np
, struct mtk_i2c
*i2c
)
894 ret
= of_property_read_u32(np
, "clock-frequency", &i2c
->speed_hz
);
896 i2c
->speed_hz
= I2C_DEFAULT_SPEED
;
898 ret
= of_property_read_u32(np
, "clock-div", &i2c
->clk_src_div
);
902 if (i2c
->clk_src_div
== 0)
905 i2c
->have_pmic
= of_property_read_bool(np
, "mediatek,have-pmic");
907 of_property_read_bool(np
, "mediatek,use-push-pull");
912 static int mtk_i2c_probe(struct platform_device
*pdev
)
917 struct resource
*res
;
920 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
924 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
925 i2c
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
926 if (IS_ERR(i2c
->base
))
927 return PTR_ERR(i2c
->base
);
929 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
930 i2c
->pdmabase
= devm_ioremap_resource(&pdev
->dev
, res
);
931 if (IS_ERR(i2c
->pdmabase
))
932 return PTR_ERR(i2c
->pdmabase
);
934 irq
= platform_get_irq(pdev
, 0);
938 init_completion(&i2c
->msg_complete
);
940 i2c
->dev_comp
= of_device_get_match_data(&pdev
->dev
);
941 i2c
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
942 i2c
->dev
= &pdev
->dev
;
943 i2c
->adap
.dev
.parent
= &pdev
->dev
;
944 i2c
->adap
.owner
= THIS_MODULE
;
945 i2c
->adap
.algo
= &mtk_i2c_algorithm
;
946 i2c
->adap
.quirks
= i2c
->dev_comp
->quirks
;
947 i2c
->adap
.timeout
= 2 * HZ
;
948 i2c
->adap
.retries
= 1;
950 ret
= mtk_i2c_parse_dt(pdev
->dev
.of_node
, i2c
);
954 if (i2c
->dev_comp
->timing_adjust
)
955 i2c
->clk_src_div
*= I2C_DEFAULT_CLK_DIV
;
957 if (i2c
->have_pmic
&& !i2c
->dev_comp
->pmic_i2c
)
960 i2c
->clk_main
= devm_clk_get(&pdev
->dev
, "main");
961 if (IS_ERR(i2c
->clk_main
)) {
962 dev_err(&pdev
->dev
, "cannot get main clock\n");
963 return PTR_ERR(i2c
->clk_main
);
966 i2c
->clk_dma
= devm_clk_get(&pdev
->dev
, "dma");
967 if (IS_ERR(i2c
->clk_dma
)) {
968 dev_err(&pdev
->dev
, "cannot get dma clock\n");
969 return PTR_ERR(i2c
->clk_dma
);
972 i2c
->clk_arb
= devm_clk_get(&pdev
->dev
, "arb");
973 if (IS_ERR(i2c
->clk_arb
))
977 if (i2c
->have_pmic
) {
978 i2c
->clk_pmic
= devm_clk_get(&pdev
->dev
, "pmic");
979 if (IS_ERR(i2c
->clk_pmic
)) {
980 dev_err(&pdev
->dev
, "cannot get pmic clock\n");
981 return PTR_ERR(i2c
->clk_pmic
);
986 strlcpy(i2c
->adap
.name
, I2C_DRV_NAME
, sizeof(i2c
->adap
.name
));
988 ret
= mtk_i2c_set_speed(i2c
, clk_get_rate(clk
));
990 dev_err(&pdev
->dev
, "Failed to set the speed.\n");
994 if (i2c
->dev_comp
->support_33bits
) {
995 ret
= dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(33));
997 dev_err(&pdev
->dev
, "dma_set_mask return error.\n");
1002 ret
= mtk_i2c_clock_enable(i2c
);
1004 dev_err(&pdev
->dev
, "clock enable failed!\n");
1007 mtk_i2c_init_hw(i2c
);
1008 mtk_i2c_clock_disable(i2c
);
1010 ret
= devm_request_irq(&pdev
->dev
, irq
, mtk_i2c_irq
,
1011 IRQF_TRIGGER_NONE
, I2C_DRV_NAME
, i2c
);
1014 "Request I2C IRQ %d fail\n", irq
);
1018 i2c_set_adapdata(&i2c
->adap
, i2c
);
1019 ret
= i2c_add_adapter(&i2c
->adap
);
1023 platform_set_drvdata(pdev
, i2c
);
1028 static int mtk_i2c_remove(struct platform_device
*pdev
)
1030 struct mtk_i2c
*i2c
= platform_get_drvdata(pdev
);
1032 i2c_del_adapter(&i2c
->adap
);
1037 #ifdef CONFIG_PM_SLEEP
1038 static int mtk_i2c_resume(struct device
*dev
)
1041 struct mtk_i2c
*i2c
= dev_get_drvdata(dev
);
1043 ret
= mtk_i2c_clock_enable(i2c
);
1045 dev_err(dev
, "clock enable failed!\n");
1049 mtk_i2c_init_hw(i2c
);
1051 mtk_i2c_clock_disable(i2c
);
1057 static const struct dev_pm_ops mtk_i2c_pm
= {
1058 SET_SYSTEM_SLEEP_PM_OPS(NULL
, mtk_i2c_resume
)
1061 static struct platform_driver mtk_i2c_driver
= {
1062 .probe
= mtk_i2c_probe
,
1063 .remove
= mtk_i2c_remove
,
1065 .name
= I2C_DRV_NAME
,
1067 .of_match_table
= of_match_ptr(mtk_i2c_of_match
),
1071 module_platform_driver(mtk_i2c_driver
);
1073 MODULE_LICENSE("GPL v2");
1074 MODULE_DESCRIPTION("MediaTek I2C Bus Driver");
1075 MODULE_AUTHOR("Xudong Chen <xudong.chen@mediatek.com>");