xref: /linux/drivers/i2c/busses/i2c-mt65xx.c (revision be5ce0e97cc7a5c0d2da45d617b7bc567c3d3fa1)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ce38815dSXudong Chen /*
3ce38815dSXudong Chen  * Copyright (c) 2014 MediaTek Inc.
4ce38815dSXudong Chen  * Author: Xudong Chen <xudong.chen@mediatek.com>
5ce38815dSXudong Chen  */
6ce38815dSXudong Chen 
7ce38815dSXudong Chen #include <linux/clk.h>
8ce38815dSXudong Chen #include <linux/completion.h>
9ce38815dSXudong Chen #include <linux/delay.h>
10ce38815dSXudong Chen #include <linux/device.h>
11ce38815dSXudong Chen #include <linux/dma-mapping.h>
12ce38815dSXudong Chen #include <linux/err.h>
13ce38815dSXudong Chen #include <linux/errno.h>
14ce38815dSXudong Chen #include <linux/i2c.h>
15ce38815dSXudong Chen #include <linux/init.h>
16ce38815dSXudong Chen #include <linux/interrupt.h>
17ce38815dSXudong Chen #include <linux/io.h>
18ce38815dSXudong Chen #include <linux/kernel.h>
19ce38815dSXudong Chen #include <linux/mm.h>
20ce38815dSXudong Chen #include <linux/module.h>
21ce38815dSXudong Chen #include <linux/of_address.h>
226e29577fSRyder Lee #include <linux/of_device.h>
23ce38815dSXudong Chen #include <linux/of_irq.h>
24ce38815dSXudong Chen #include <linux/platform_device.h>
25ce38815dSXudong Chen #include <linux/scatterlist.h>
26ce38815dSXudong Chen #include <linux/sched.h>
27ce38815dSXudong Chen #include <linux/slab.h>
28ce38815dSXudong Chen 
29b2ed11e2SEddie Huang #define I2C_RS_TRANSFER			(1 << 4)
30cad6dc5dSQii Wang #define I2C_ARB_LOST			(1 << 3)
31ce38815dSXudong Chen #define I2C_HS_NACKERR			(1 << 2)
32ce38815dSXudong Chen #define I2C_ACKERR			(1 << 1)
33ce38815dSXudong Chen #define I2C_TRANSAC_COMP		(1 << 0)
34ce38815dSXudong Chen #define I2C_TRANSAC_START		(1 << 0)
35b2ed11e2SEddie Huang #define I2C_RS_MUL_CNFG			(1 << 15)
36b2ed11e2SEddie Huang #define I2C_RS_MUL_TRIG			(1 << 14)
37ce38815dSXudong Chen #define I2C_DCM_DISABLE			0x0000
38ce38815dSXudong Chen #define I2C_IO_CONFIG_OPEN_DRAIN	0x0003
39ce38815dSXudong Chen #define I2C_IO_CONFIG_PUSH_PULL		0x0000
40ce38815dSXudong Chen #define I2C_SOFT_RST			0x0001
41ce38815dSXudong Chen #define I2C_FIFO_ADDR_CLR		0x0001
42ce38815dSXudong Chen #define I2C_DELAY_LEN			0x0002
43ce38815dSXudong Chen #define I2C_TIME_CLR_VALUE		0x0000
44ce38815dSXudong Chen #define I2C_TIME_DEFAULT_VALUE		0x0003
45ce38815dSXudong Chen #define I2C_WRRD_TRANAC_VALUE		0x0002
46ce38815dSXudong Chen #define I2C_RD_TRANAC_VALUE		0x0001
47*be5ce0e9SQii Wang #define I2C_SCL_MIS_COMP_VALUE		0x0000
48ce38815dSXudong Chen 
49ce38815dSXudong Chen #define I2C_DMA_CON_TX			0x0000
50ce38815dSXudong Chen #define I2C_DMA_CON_RX			0x0001
51ce38815dSXudong Chen #define I2C_DMA_START_EN		0x0001
52ce38815dSXudong Chen #define I2C_DMA_INT_FLAG_NONE		0x0000
53ce38815dSXudong Chen #define I2C_DMA_CLR_FLAG		0x0000
54ea89ef1fSEddie Huang #define I2C_DMA_HARD_RST		0x0002
55f4f4fed6SLiguo Zhang #define I2C_DMA_4G_MODE			0x0001
56ce38815dSXudong Chen 
57ce38815dSXudong Chen #define MAX_SAMPLE_CNT_DIV		8
58ce38815dSXudong Chen #define MAX_STEP_CNT_DIV		64
59*be5ce0e9SQii Wang #define MAX_CLOCK_DIV			256
60ce38815dSXudong Chen #define MAX_HS_STEP_CNT_DIV		8
61*be5ce0e9SQii Wang #define I2C_STANDARD_MODE_BUFFER	(1000 / 2)
62*be5ce0e9SQii Wang #define I2C_FAST_MODE_BUFFER		(300 / 2)
63*be5ce0e9SQii Wang #define I2C_FAST_MODE_PLUS_BUFFER	(20 / 2)
64ce38815dSXudong Chen 
65ce38815dSXudong Chen #define I2C_CONTROL_RS                  (0x1 << 1)
66ce38815dSXudong Chen #define I2C_CONTROL_DMA_EN              (0x1 << 2)
67ce38815dSXudong Chen #define I2C_CONTROL_CLK_EXT_EN          (0x1 << 3)
68ce38815dSXudong Chen #define I2C_CONTROL_DIR_CHANGE          (0x1 << 4)
69ce38815dSXudong Chen #define I2C_CONTROL_ACKERR_DET_EN       (0x1 << 5)
70ce38815dSXudong Chen #define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6)
71a15c91baSQii Wang #define I2C_CONTROL_DMAACK_EN           (0x1 << 8)
72a15c91baSQii Wang #define I2C_CONTROL_ASYNC_MODE          (0x1 << 9)
73ce38815dSXudong Chen #define I2C_CONTROL_WRAPPER             (0x1 << 0)
74ce38815dSXudong Chen 
75ce38815dSXudong Chen #define I2C_DRV_NAME		"i2c-mt65xx"
76ce38815dSXudong Chen 
77ce38815dSXudong Chen enum DMA_REGS_OFFSET {
78ce38815dSXudong Chen 	OFFSET_INT_FLAG = 0x0,
79ce38815dSXudong Chen 	OFFSET_INT_EN = 0x04,
80ce38815dSXudong Chen 	OFFSET_EN = 0x08,
81ea89ef1fSEddie Huang 	OFFSET_RST = 0x0c,
82ce38815dSXudong Chen 	OFFSET_CON = 0x18,
83ce38815dSXudong Chen 	OFFSET_TX_MEM_ADDR = 0x1c,
84ce38815dSXudong Chen 	OFFSET_RX_MEM_ADDR = 0x20,
85ce38815dSXudong Chen 	OFFSET_TX_LEN = 0x24,
86ce38815dSXudong Chen 	OFFSET_RX_LEN = 0x28,
87f4f4fed6SLiguo Zhang 	OFFSET_TX_4G_MODE = 0x54,
88f4f4fed6SLiguo Zhang 	OFFSET_RX_4G_MODE = 0x58,
89ce38815dSXudong Chen };
90ce38815dSXudong Chen 
91ce38815dSXudong Chen enum i2c_trans_st_rs {
92ce38815dSXudong Chen 	I2C_TRANS_STOP = 0,
93ce38815dSXudong Chen 	I2C_TRANS_REPEATED_START,
94ce38815dSXudong Chen };
95ce38815dSXudong Chen 
96ce38815dSXudong Chen enum mtk_trans_op {
97ce38815dSXudong Chen 	I2C_MASTER_WR = 1,
98ce38815dSXudong Chen 	I2C_MASTER_RD,
99ce38815dSXudong Chen 	I2C_MASTER_WRRD,
100ce38815dSXudong Chen };
101ce38815dSXudong Chen 
102ce38815dSXudong Chen enum I2C_REGS_OFFSET {
103bc6eaf17SQii Wang 	OFFSET_DATA_PORT,
104bc6eaf17SQii Wang 	OFFSET_SLAVE_ADDR,
105bc6eaf17SQii Wang 	OFFSET_INTR_MASK,
106bc6eaf17SQii Wang 	OFFSET_INTR_STAT,
107bc6eaf17SQii Wang 	OFFSET_CONTROL,
108bc6eaf17SQii Wang 	OFFSET_TRANSFER_LEN,
109bc6eaf17SQii Wang 	OFFSET_TRANSAC_LEN,
110bc6eaf17SQii Wang 	OFFSET_DELAY_LEN,
111bc6eaf17SQii Wang 	OFFSET_TIMING,
112bc6eaf17SQii Wang 	OFFSET_START,
113bc6eaf17SQii Wang 	OFFSET_EXT_CONF,
114bc6eaf17SQii Wang 	OFFSET_FIFO_STAT,
115bc6eaf17SQii Wang 	OFFSET_FIFO_THRESH,
116bc6eaf17SQii Wang 	OFFSET_FIFO_ADDR_CLR,
117bc6eaf17SQii Wang 	OFFSET_IO_CONFIG,
118bc6eaf17SQii Wang 	OFFSET_RSV_DEBUG,
119bc6eaf17SQii Wang 	OFFSET_HS,
120bc6eaf17SQii Wang 	OFFSET_SOFTRESET,
121bc6eaf17SQii Wang 	OFFSET_DCM_EN,
122bc6eaf17SQii Wang 	OFFSET_PATH_DIR,
123bc6eaf17SQii Wang 	OFFSET_DEBUGSTAT,
124bc6eaf17SQii Wang 	OFFSET_DEBUGCTRL,
125bc6eaf17SQii Wang 	OFFSET_TRANSFER_LEN_AUX,
126bc6eaf17SQii Wang 	OFFSET_CLOCK_DIV,
12725708278SQii Wang 	OFFSET_LTIMING,
128*be5ce0e9SQii Wang 	OFFSET_SCL_HIGH_LOW_RATIO,
129*be5ce0e9SQii Wang 	OFFSET_HS_SCL_HIGH_LOW_RATIO,
130*be5ce0e9SQii Wang 	OFFSET_SCL_MIS_COMP_POINT,
131*be5ce0e9SQii Wang 	OFFSET_STA_STO_AC_TIMING,
132*be5ce0e9SQii Wang 	OFFSET_HS_STA_STO_AC_TIMING,
133*be5ce0e9SQii Wang 	OFFSET_SDA_TIMING,
134bc6eaf17SQii Wang };
135bc6eaf17SQii Wang 
136bc6eaf17SQii Wang static const u16 mt_i2c_regs_v1[] = {
137bc6eaf17SQii Wang 	[OFFSET_DATA_PORT] = 0x0,
138bc6eaf17SQii Wang 	[OFFSET_SLAVE_ADDR] = 0x4,
139bc6eaf17SQii Wang 	[OFFSET_INTR_MASK] = 0x8,
140bc6eaf17SQii Wang 	[OFFSET_INTR_STAT] = 0xc,
141bc6eaf17SQii Wang 	[OFFSET_CONTROL] = 0x10,
142bc6eaf17SQii Wang 	[OFFSET_TRANSFER_LEN] = 0x14,
143bc6eaf17SQii Wang 	[OFFSET_TRANSAC_LEN] = 0x18,
144bc6eaf17SQii Wang 	[OFFSET_DELAY_LEN] = 0x1c,
145bc6eaf17SQii Wang 	[OFFSET_TIMING] = 0x20,
146bc6eaf17SQii Wang 	[OFFSET_START] = 0x24,
147bc6eaf17SQii Wang 	[OFFSET_EXT_CONF] = 0x28,
148bc6eaf17SQii Wang 	[OFFSET_FIFO_STAT] = 0x30,
149bc6eaf17SQii Wang 	[OFFSET_FIFO_THRESH] = 0x34,
150bc6eaf17SQii Wang 	[OFFSET_FIFO_ADDR_CLR] = 0x38,
151bc6eaf17SQii Wang 	[OFFSET_IO_CONFIG] = 0x40,
152bc6eaf17SQii Wang 	[OFFSET_RSV_DEBUG] = 0x44,
153bc6eaf17SQii Wang 	[OFFSET_HS] = 0x48,
154bc6eaf17SQii Wang 	[OFFSET_SOFTRESET] = 0x50,
155bc6eaf17SQii Wang 	[OFFSET_DCM_EN] = 0x54,
156bc6eaf17SQii Wang 	[OFFSET_PATH_DIR] = 0x60,
157bc6eaf17SQii Wang 	[OFFSET_DEBUGSTAT] = 0x64,
158bc6eaf17SQii Wang 	[OFFSET_DEBUGCTRL] = 0x68,
159bc6eaf17SQii Wang 	[OFFSET_TRANSFER_LEN_AUX] = 0x6c,
160bc6eaf17SQii Wang 	[OFFSET_CLOCK_DIV] = 0x70,
161*be5ce0e9SQii Wang 	[OFFSET_SCL_HIGH_LOW_RATIO] = 0x74,
162*be5ce0e9SQii Wang 	[OFFSET_HS_SCL_HIGH_LOW_RATIO] = 0x78,
163*be5ce0e9SQii Wang 	[OFFSET_SCL_MIS_COMP_POINT] = 0x7C,
164*be5ce0e9SQii Wang 	[OFFSET_STA_STO_AC_TIMING] = 0x80,
165*be5ce0e9SQii Wang 	[OFFSET_HS_STA_STO_AC_TIMING] = 0x84,
166*be5ce0e9SQii Wang 	[OFFSET_SDA_TIMING] = 0x88,
167ce38815dSXudong Chen };
168ce38815dSXudong Chen 
16925708278SQii Wang static const u16 mt_i2c_regs_v2[] = {
17025708278SQii Wang 	[OFFSET_DATA_PORT] = 0x0,
17125708278SQii Wang 	[OFFSET_SLAVE_ADDR] = 0x4,
17225708278SQii Wang 	[OFFSET_INTR_MASK] = 0x8,
17325708278SQii Wang 	[OFFSET_INTR_STAT] = 0xc,
17425708278SQii Wang 	[OFFSET_CONTROL] = 0x10,
17525708278SQii Wang 	[OFFSET_TRANSFER_LEN] = 0x14,
17625708278SQii Wang 	[OFFSET_TRANSAC_LEN] = 0x18,
17725708278SQii Wang 	[OFFSET_DELAY_LEN] = 0x1c,
17825708278SQii Wang 	[OFFSET_TIMING] = 0x20,
17925708278SQii Wang 	[OFFSET_START] = 0x24,
18025708278SQii Wang 	[OFFSET_EXT_CONF] = 0x28,
18125708278SQii Wang 	[OFFSET_LTIMING] = 0x2c,
18225708278SQii Wang 	[OFFSET_HS] = 0x30,
18325708278SQii Wang 	[OFFSET_IO_CONFIG] = 0x34,
18425708278SQii Wang 	[OFFSET_FIFO_ADDR_CLR] = 0x38,
185*be5ce0e9SQii Wang 	[OFFSET_SDA_TIMING] = 0x3c,
18625708278SQii Wang 	[OFFSET_TRANSFER_LEN_AUX] = 0x44,
18725708278SQii Wang 	[OFFSET_CLOCK_DIV] = 0x48,
18825708278SQii Wang 	[OFFSET_SOFTRESET] = 0x50,
189*be5ce0e9SQii Wang 	[OFFSET_SCL_MIS_COMP_POINT] = 0x90,
19025708278SQii Wang 	[OFFSET_DEBUGSTAT] = 0xe0,
19125708278SQii Wang 	[OFFSET_DEBUGCTRL] = 0xe8,
19225708278SQii Wang 	[OFFSET_FIFO_STAT] = 0xf4,
19325708278SQii Wang 	[OFFSET_FIFO_THRESH] = 0xf8,
19425708278SQii Wang 	[OFFSET_DCM_EN] = 0xf88,
19525708278SQii Wang };
19625708278SQii Wang 
197ce38815dSXudong Chen struct mtk_i2c_compatible {
198ce38815dSXudong Chen 	const struct i2c_adapter_quirks *quirks;
199bc6eaf17SQii Wang 	const u16 *regs;
200ce38815dSXudong Chen 	unsigned char pmic_i2c: 1;
201ce38815dSXudong Chen 	unsigned char dcm: 1;
202b2ed11e2SEddie Huang 	unsigned char auto_restart: 1;
203173b77e8SLiguo Zhang 	unsigned char aux_len_reg: 1;
204f4f4fed6SLiguo Zhang 	unsigned char support_33bits: 1;
2055a10e7d7SJun Gao 	unsigned char timing_adjust: 1;
206a15c91baSQii Wang 	unsigned char dma_sync: 1;
20725708278SQii Wang 	unsigned char ltiming_adjust: 1;
208ce38815dSXudong Chen };
209ce38815dSXudong Chen 
210*be5ce0e9SQii Wang struct mtk_i2c_ac_timing {
211*be5ce0e9SQii Wang 	u16 htiming;
212*be5ce0e9SQii Wang 	u16 ltiming;
213*be5ce0e9SQii Wang 	u16 hs;
214*be5ce0e9SQii Wang 	u16 ext;
215*be5ce0e9SQii Wang 	u16 inter_clk_div;
216*be5ce0e9SQii Wang 	u16 scl_hl_ratio;
217*be5ce0e9SQii Wang 	u16 hs_scl_hl_ratio;
218*be5ce0e9SQii Wang 	u16 sta_stop;
219*be5ce0e9SQii Wang 	u16 hs_sta_stop;
220*be5ce0e9SQii Wang 	u16 sda_timing;
221*be5ce0e9SQii Wang };
222*be5ce0e9SQii Wang 
223ce38815dSXudong Chen struct mtk_i2c {
224ce38815dSXudong Chen 	struct i2c_adapter adap;	/* i2c host adapter */
225ce38815dSXudong Chen 	struct device *dev;
226ce38815dSXudong Chen 	struct completion msg_complete;
227ce38815dSXudong Chen 
228ce38815dSXudong Chen 	/* set in i2c probe */
229ce38815dSXudong Chen 	void __iomem *base;		/* i2c base addr */
230ce38815dSXudong Chen 	void __iomem *pdmabase;		/* dma base address*/
231ce38815dSXudong Chen 	struct clk *clk_main;		/* main clock for i2c bus */
232ce38815dSXudong Chen 	struct clk *clk_dma;		/* DMA clock for i2c via DMA */
233ce38815dSXudong Chen 	struct clk *clk_pmic;		/* PMIC clock for i2c from PMIC */
234cad6dc5dSQii Wang 	struct clk *clk_arb;		/* Arbitrator clock for i2c */
235ce38815dSXudong Chen 	bool have_pmic;			/* can use i2c pins from PMIC */
236ce38815dSXudong Chen 	bool use_push_pull;		/* IO config push-pull mode */
237ce38815dSXudong Chen 
238ce38815dSXudong Chen 	u16 irq_stat;			/* interrupt status */
239f2326401SJun Gao 	unsigned int clk_src_div;
240ce38815dSXudong Chen 	unsigned int speed_hz;		/* The speed in transfer */
241ce38815dSXudong Chen 	enum mtk_trans_op op;
242ce38815dSXudong Chen 	u16 timing_reg;
243ce38815dSXudong Chen 	u16 high_speed_reg;
24425708278SQii Wang 	u16 ltiming_reg;
245173b77e8SLiguo Zhang 	unsigned char auto_restart;
2468378d01fSLiguo Zhang 	bool ignore_restart_irq;
247*be5ce0e9SQii Wang 	struct mtk_i2c_ac_timing ac_timing;
248ce38815dSXudong Chen 	const struct mtk_i2c_compatible *dev_comp;
249ce38815dSXudong Chen };
250ce38815dSXudong Chen 
251*be5ce0e9SQii Wang /**
252*be5ce0e9SQii Wang  * struct i2c_spec_values:
253*be5ce0e9SQii Wang  * min_low_ns: min LOW period of the SCL clock
254*be5ce0e9SQii Wang  * min_su_sta_ns: min set-up time for a repeated START condition
255*be5ce0e9SQii Wang  * max_hd_dat_ns: max data hold time
256*be5ce0e9SQii Wang  * min_su_dat_ns: min data set-up time
257*be5ce0e9SQii Wang  */
258*be5ce0e9SQii Wang struct i2c_spec_values {
259*be5ce0e9SQii Wang 	unsigned int min_low_ns;
260*be5ce0e9SQii Wang 	unsigned int min_high_ns;
261*be5ce0e9SQii Wang 	unsigned int min_su_sta_ns;
262*be5ce0e9SQii Wang 	unsigned int max_hd_dat_ns;
263*be5ce0e9SQii Wang 	unsigned int min_su_dat_ns;
264*be5ce0e9SQii Wang };
265*be5ce0e9SQii Wang 
266*be5ce0e9SQii Wang static const struct i2c_spec_values standard_mode_spec = {
267*be5ce0e9SQii Wang 	.min_low_ns = 4700 + I2C_STANDARD_MODE_BUFFER,
268*be5ce0e9SQii Wang 	.min_su_sta_ns = 4700 + I2C_STANDARD_MODE_BUFFER,
269*be5ce0e9SQii Wang 	.max_hd_dat_ns = 3450 - I2C_STANDARD_MODE_BUFFER,
270*be5ce0e9SQii Wang 	.min_su_dat_ns = 250 + I2C_STANDARD_MODE_BUFFER,
271*be5ce0e9SQii Wang };
272*be5ce0e9SQii Wang 
273*be5ce0e9SQii Wang static const struct i2c_spec_values fast_mode_spec = {
274*be5ce0e9SQii Wang 	.min_low_ns = 1300 + I2C_FAST_MODE_BUFFER,
275*be5ce0e9SQii Wang 	.min_su_sta_ns = 600 + I2C_FAST_MODE_BUFFER,
276*be5ce0e9SQii Wang 	.max_hd_dat_ns = 900 - I2C_FAST_MODE_BUFFER,
277*be5ce0e9SQii Wang 	.min_su_dat_ns = 100 + I2C_FAST_MODE_BUFFER,
278*be5ce0e9SQii Wang };
279*be5ce0e9SQii Wang 
280*be5ce0e9SQii Wang static const struct i2c_spec_values fast_mode_plus_spec = {
281*be5ce0e9SQii Wang 	.min_low_ns = 500 + I2C_FAST_MODE_PLUS_BUFFER,
282*be5ce0e9SQii Wang 	.min_su_sta_ns = 260 + I2C_FAST_MODE_PLUS_BUFFER,
283*be5ce0e9SQii Wang 	.max_hd_dat_ns = 400 - I2C_FAST_MODE_PLUS_BUFFER,
284*be5ce0e9SQii Wang 	.min_su_dat_ns = 50 + I2C_FAST_MODE_PLUS_BUFFER,
285*be5ce0e9SQii Wang };
286*be5ce0e9SQii Wang 
287ce38815dSXudong Chen static const struct i2c_adapter_quirks mt6577_i2c_quirks = {
288ce38815dSXudong Chen 	.flags = I2C_AQ_COMB_WRITE_THEN_READ,
289ce38815dSXudong Chen 	.max_num_msgs = 1,
290ce38815dSXudong Chen 	.max_write_len = 255,
291ce38815dSXudong Chen 	.max_read_len = 255,
292ce38815dSXudong Chen 	.max_comb_1st_msg_len = 255,
293ce38815dSXudong Chen 	.max_comb_2nd_msg_len = 31,
294ce38815dSXudong Chen };
295ce38815dSXudong Chen 
2961304fe09SJun Gao static const struct i2c_adapter_quirks mt7622_i2c_quirks = {
2971304fe09SJun Gao 	.max_num_msgs = 255,
2981304fe09SJun Gao };
2991304fe09SJun Gao 
300abf4923eSHsin-Yi Wang static const struct i2c_adapter_quirks mt8183_i2c_quirks = {
301abf4923eSHsin-Yi Wang 	.flags = I2C_AQ_NO_ZERO_LEN,
302abf4923eSHsin-Yi Wang };
303abf4923eSHsin-Yi Wang 
3045a10e7d7SJun Gao static const struct mtk_i2c_compatible mt2712_compat = {
305bc6eaf17SQii Wang 	.regs = mt_i2c_regs_v1,
3065a10e7d7SJun Gao 	.pmic_i2c = 0,
3075a10e7d7SJun Gao 	.dcm = 1,
3085a10e7d7SJun Gao 	.auto_restart = 1,
3095a10e7d7SJun Gao 	.aux_len_reg = 1,
3105a10e7d7SJun Gao 	.support_33bits = 1,
3115a10e7d7SJun Gao 	.timing_adjust = 1,
312a15c91baSQii Wang 	.dma_sync = 0,
31325708278SQii Wang 	.ltiming_adjust = 0,
3145a10e7d7SJun Gao };
3155a10e7d7SJun Gao 
316ce38815dSXudong Chen static const struct mtk_i2c_compatible mt6577_compat = {
317ce38815dSXudong Chen 	.quirks = &mt6577_i2c_quirks,
318bc6eaf17SQii Wang 	.regs = mt_i2c_regs_v1,
319ce38815dSXudong Chen 	.pmic_i2c = 0,
320ce38815dSXudong Chen 	.dcm = 1,
321b2ed11e2SEddie Huang 	.auto_restart = 0,
322173b77e8SLiguo Zhang 	.aux_len_reg = 0,
323f4f4fed6SLiguo Zhang 	.support_33bits = 0,
3245a10e7d7SJun Gao 	.timing_adjust = 0,
325a15c91baSQii Wang 	.dma_sync = 0,
32625708278SQii Wang 	.ltiming_adjust = 0,
327ce38815dSXudong Chen };
328ce38815dSXudong Chen 
329ce38815dSXudong Chen static const struct mtk_i2c_compatible mt6589_compat = {
330ce38815dSXudong Chen 	.quirks = &mt6577_i2c_quirks,
331bc6eaf17SQii Wang 	.regs = mt_i2c_regs_v1,
332ce38815dSXudong Chen 	.pmic_i2c = 1,
333ce38815dSXudong Chen 	.dcm = 0,
334b2ed11e2SEddie Huang 	.auto_restart = 0,
335173b77e8SLiguo Zhang 	.aux_len_reg = 0,
336f4f4fed6SLiguo Zhang 	.support_33bits = 0,
3375a10e7d7SJun Gao 	.timing_adjust = 0,
338a15c91baSQii Wang 	.dma_sync = 0,
33925708278SQii Wang 	.ltiming_adjust = 0,
340b2ed11e2SEddie Huang };
341b2ed11e2SEddie Huang 
3421304fe09SJun Gao static const struct mtk_i2c_compatible mt7622_compat = {
3431304fe09SJun Gao 	.quirks = &mt7622_i2c_quirks,
344bc6eaf17SQii Wang 	.regs = mt_i2c_regs_v1,
3451304fe09SJun Gao 	.pmic_i2c = 0,
3461304fe09SJun Gao 	.dcm = 1,
3471304fe09SJun Gao 	.auto_restart = 1,
3481304fe09SJun Gao 	.aux_len_reg = 1,
3491304fe09SJun Gao 	.support_33bits = 0,
3505a10e7d7SJun Gao 	.timing_adjust = 0,
351a15c91baSQii Wang 	.dma_sync = 0,
35225708278SQii Wang 	.ltiming_adjust = 0,
3531304fe09SJun Gao };
3541304fe09SJun Gao 
355b2ed11e2SEddie Huang static const struct mtk_i2c_compatible mt8173_compat = {
356bc6eaf17SQii Wang 	.regs = mt_i2c_regs_v1,
357b2ed11e2SEddie Huang 	.pmic_i2c = 0,
358b2ed11e2SEddie Huang 	.dcm = 1,
359b2ed11e2SEddie Huang 	.auto_restart = 1,
360173b77e8SLiguo Zhang 	.aux_len_reg = 1,
361f4f4fed6SLiguo Zhang 	.support_33bits = 1,
3625a10e7d7SJun Gao 	.timing_adjust = 0,
363a15c91baSQii Wang 	.dma_sync = 0,
36425708278SQii Wang 	.ltiming_adjust = 0,
36525708278SQii Wang };
36625708278SQii Wang 
36725708278SQii Wang static const struct mtk_i2c_compatible mt8183_compat = {
368abf4923eSHsin-Yi Wang 	.quirks = &mt8183_i2c_quirks,
36925708278SQii Wang 	.regs = mt_i2c_regs_v2,
37025708278SQii Wang 	.pmic_i2c = 0,
37125708278SQii Wang 	.dcm = 0,
37225708278SQii Wang 	.auto_restart = 1,
37325708278SQii Wang 	.aux_len_reg = 1,
37425708278SQii Wang 	.support_33bits = 1,
37525708278SQii Wang 	.timing_adjust = 1,
37625708278SQii Wang 	.dma_sync = 1,
37725708278SQii Wang 	.ltiming_adjust = 1,
378ce38815dSXudong Chen };
379ce38815dSXudong Chen 
380ce38815dSXudong Chen static const struct of_device_id mtk_i2c_of_match[] = {
3815a10e7d7SJun Gao 	{ .compatible = "mediatek,mt2712-i2c", .data = &mt2712_compat },
382ce38815dSXudong Chen 	{ .compatible = "mediatek,mt6577-i2c", .data = &mt6577_compat },
383ce38815dSXudong Chen 	{ .compatible = "mediatek,mt6589-i2c", .data = &mt6589_compat },
3841304fe09SJun Gao 	{ .compatible = "mediatek,mt7622-i2c", .data = &mt7622_compat },
385b2ed11e2SEddie Huang 	{ .compatible = "mediatek,mt8173-i2c", .data = &mt8173_compat },
38625708278SQii Wang 	{ .compatible = "mediatek,mt8183-i2c", .data = &mt8183_compat },
387ce38815dSXudong Chen 	{}
388ce38815dSXudong Chen };
389ce38815dSXudong Chen MODULE_DEVICE_TABLE(of, mtk_i2c_of_match);
390ce38815dSXudong Chen 
391bc6eaf17SQii Wang static u16 mtk_i2c_readw(struct mtk_i2c *i2c, enum I2C_REGS_OFFSET reg)
392bc6eaf17SQii Wang {
393bc6eaf17SQii Wang 	return readw(i2c->base + i2c->dev_comp->regs[reg]);
394bc6eaf17SQii Wang }
395bc6eaf17SQii Wang 
396bc6eaf17SQii Wang static void mtk_i2c_writew(struct mtk_i2c *i2c, u16 val,
397bc6eaf17SQii Wang 			   enum I2C_REGS_OFFSET reg)
398bc6eaf17SQii Wang {
399bc6eaf17SQii Wang 	writew(val, i2c->base + i2c->dev_comp->regs[reg]);
400bc6eaf17SQii Wang }
401bc6eaf17SQii Wang 
402ce38815dSXudong Chen static int mtk_i2c_clock_enable(struct mtk_i2c *i2c)
403ce38815dSXudong Chen {
404ce38815dSXudong Chen 	int ret;
405ce38815dSXudong Chen 
406ce38815dSXudong Chen 	ret = clk_prepare_enable(i2c->clk_dma);
407ce38815dSXudong Chen 	if (ret)
408ce38815dSXudong Chen 		return ret;
409ce38815dSXudong Chen 
410ce38815dSXudong Chen 	ret = clk_prepare_enable(i2c->clk_main);
411ce38815dSXudong Chen 	if (ret)
412ce38815dSXudong Chen 		goto err_main;
413ce38815dSXudong Chen 
414ce38815dSXudong Chen 	if (i2c->have_pmic) {
415ce38815dSXudong Chen 		ret = clk_prepare_enable(i2c->clk_pmic);
416ce38815dSXudong Chen 		if (ret)
417ce38815dSXudong Chen 			goto err_pmic;
418ce38815dSXudong Chen 	}
419cad6dc5dSQii Wang 
420cad6dc5dSQii Wang 	if (i2c->clk_arb) {
421cad6dc5dSQii Wang 		ret = clk_prepare_enable(i2c->clk_arb);
422cad6dc5dSQii Wang 		if (ret)
423cad6dc5dSQii Wang 			goto err_arb;
424cad6dc5dSQii Wang 	}
425cad6dc5dSQii Wang 
426ce38815dSXudong Chen 	return 0;
427ce38815dSXudong Chen 
428cad6dc5dSQii Wang err_arb:
429cad6dc5dSQii Wang 	if (i2c->have_pmic)
430cad6dc5dSQii Wang 		clk_disable_unprepare(i2c->clk_pmic);
431ce38815dSXudong Chen err_pmic:
432ce38815dSXudong Chen 	clk_disable_unprepare(i2c->clk_main);
433ce38815dSXudong Chen err_main:
434ce38815dSXudong Chen 	clk_disable_unprepare(i2c->clk_dma);
435ce38815dSXudong Chen 
436ce38815dSXudong Chen 	return ret;
437ce38815dSXudong Chen }
438ce38815dSXudong Chen 
439ce38815dSXudong Chen static void mtk_i2c_clock_disable(struct mtk_i2c *i2c)
440ce38815dSXudong Chen {
441cad6dc5dSQii Wang 	if (i2c->clk_arb)
442cad6dc5dSQii Wang 		clk_disable_unprepare(i2c->clk_arb);
443cad6dc5dSQii Wang 
444ce38815dSXudong Chen 	if (i2c->have_pmic)
445ce38815dSXudong Chen 		clk_disable_unprepare(i2c->clk_pmic);
446ce38815dSXudong Chen 
447ce38815dSXudong Chen 	clk_disable_unprepare(i2c->clk_main);
448ce38815dSXudong Chen 	clk_disable_unprepare(i2c->clk_dma);
449ce38815dSXudong Chen }
450ce38815dSXudong Chen 
451ce38815dSXudong Chen static void mtk_i2c_init_hw(struct mtk_i2c *i2c)
452ce38815dSXudong Chen {
453ce38815dSXudong Chen 	u16 control_reg;
454ce38815dSXudong Chen 
455bc6eaf17SQii Wang 	mtk_i2c_writew(i2c, I2C_SOFT_RST, OFFSET_SOFTRESET);
456ce38815dSXudong Chen 
457ce38815dSXudong Chen 	/* Set ioconfig */
458ce38815dSXudong Chen 	if (i2c->use_push_pull)
459bc6eaf17SQii Wang 		mtk_i2c_writew(i2c, I2C_IO_CONFIG_PUSH_PULL, OFFSET_IO_CONFIG);
460ce38815dSXudong Chen 	else
461bc6eaf17SQii Wang 		mtk_i2c_writew(i2c, I2C_IO_CONFIG_OPEN_DRAIN, OFFSET_IO_CONFIG);
462ce38815dSXudong Chen 
463ce38815dSXudong Chen 	if (i2c->dev_comp->dcm)
464bc6eaf17SQii Wang 		mtk_i2c_writew(i2c, I2C_DCM_DISABLE, OFFSET_DCM_EN);
465ce38815dSXudong Chen 
466bc6eaf17SQii Wang 	mtk_i2c_writew(i2c, i2c->timing_reg, OFFSET_TIMING);
467bc6eaf17SQii Wang 	mtk_i2c_writew(i2c, i2c->high_speed_reg, OFFSET_HS);
46825708278SQii Wang 	if (i2c->dev_comp->ltiming_adjust)
46925708278SQii Wang 		mtk_i2c_writew(i2c, i2c->ltiming_reg, OFFSET_LTIMING);
470ce38815dSXudong Chen 
471*be5ce0e9SQii Wang 	if (i2c->dev_comp->timing_adjust) {
472*be5ce0e9SQii Wang 		mtk_i2c_writew(i2c, i2c->ac_timing.ext, OFFSET_EXT_CONF);
473*be5ce0e9SQii Wang 		mtk_i2c_writew(i2c, i2c->ac_timing.inter_clk_div,
474*be5ce0e9SQii Wang 			       OFFSET_CLOCK_DIV);
475*be5ce0e9SQii Wang 		mtk_i2c_writew(i2c, I2C_SCL_MIS_COMP_VALUE,
476*be5ce0e9SQii Wang 			       OFFSET_SCL_MIS_COMP_POINT);
477*be5ce0e9SQii Wang 		mtk_i2c_writew(i2c, i2c->ac_timing.sda_timing,
478*be5ce0e9SQii Wang 			       OFFSET_SDA_TIMING);
479*be5ce0e9SQii Wang 
480*be5ce0e9SQii Wang 		if (i2c->dev_comp->ltiming_adjust) {
481*be5ce0e9SQii Wang 			mtk_i2c_writew(i2c, i2c->ac_timing.htiming,
482*be5ce0e9SQii Wang 				       OFFSET_TIMING);
483*be5ce0e9SQii Wang 			mtk_i2c_writew(i2c, i2c->ac_timing.hs, OFFSET_HS);
484*be5ce0e9SQii Wang 			mtk_i2c_writew(i2c, i2c->ac_timing.ltiming,
485*be5ce0e9SQii Wang 				       OFFSET_LTIMING);
486*be5ce0e9SQii Wang 		} else {
487*be5ce0e9SQii Wang 			mtk_i2c_writew(i2c, i2c->ac_timing.scl_hl_ratio,
488*be5ce0e9SQii Wang 				       OFFSET_SCL_HIGH_LOW_RATIO);
489*be5ce0e9SQii Wang 			mtk_i2c_writew(i2c, i2c->ac_timing.hs_scl_hl_ratio,
490*be5ce0e9SQii Wang 				       OFFSET_HS_SCL_HIGH_LOW_RATIO);
491*be5ce0e9SQii Wang 			mtk_i2c_writew(i2c, i2c->ac_timing.sta_stop,
492*be5ce0e9SQii Wang 				       OFFSET_STA_STO_AC_TIMING);
493*be5ce0e9SQii Wang 			mtk_i2c_writew(i2c, i2c->ac_timing.hs_sta_stop,
494*be5ce0e9SQii Wang 				       OFFSET_HS_STA_STO_AC_TIMING);
495*be5ce0e9SQii Wang 		}
496*be5ce0e9SQii Wang 	}
497*be5ce0e9SQii Wang 
498ce38815dSXudong Chen 	/* If use i2c pin from PMIC mt6397 side, need set PATH_DIR first */
499ce38815dSXudong Chen 	if (i2c->have_pmic)
500bc6eaf17SQii Wang 		mtk_i2c_writew(i2c, I2C_CONTROL_WRAPPER, OFFSET_PATH_DIR);
501ce38815dSXudong Chen 
502ce38815dSXudong Chen 	control_reg = I2C_CONTROL_ACKERR_DET_EN |
503ce38815dSXudong Chen 		      I2C_CONTROL_CLK_EXT_EN | I2C_CONTROL_DMA_EN;
504a15c91baSQii Wang 	if (i2c->dev_comp->dma_sync)
505a15c91baSQii Wang 		control_reg |= I2C_CONTROL_DMAACK_EN | I2C_CONTROL_ASYNC_MODE;
506a15c91baSQii Wang 
507bc6eaf17SQii Wang 	mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL);
508bc6eaf17SQii Wang 	mtk_i2c_writew(i2c, I2C_DELAY_LEN, OFFSET_DELAY_LEN);
509ea89ef1fSEddie Huang 
510ea89ef1fSEddie Huang 	writel(I2C_DMA_HARD_RST, i2c->pdmabase + OFFSET_RST);
511ea89ef1fSEddie Huang 	udelay(50);
512ea89ef1fSEddie Huang 	writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_RST);
513ce38815dSXudong Chen }
514ce38815dSXudong Chen 
515*be5ce0e9SQii Wang static const struct i2c_spec_values *mtk_i2c_get_spec(unsigned int speed)
516*be5ce0e9SQii Wang {
517*be5ce0e9SQii Wang 	if (speed <= I2C_MAX_STANDARD_MODE_FREQ)
518*be5ce0e9SQii Wang 		return &standard_mode_spec;
519*be5ce0e9SQii Wang 	else if (speed <= I2C_MAX_FAST_MODE_FREQ)
520*be5ce0e9SQii Wang 		return &fast_mode_spec;
521*be5ce0e9SQii Wang 	else
522*be5ce0e9SQii Wang 		return &fast_mode_plus_spec;
523*be5ce0e9SQii Wang }
524*be5ce0e9SQii Wang 
525*be5ce0e9SQii Wang static int mtk_i2c_max_step_cnt(unsigned int target_speed)
526*be5ce0e9SQii Wang {
527*be5ce0e9SQii Wang 	if (target_speed > I2C_MAX_FAST_MODE_FREQ)
528*be5ce0e9SQii Wang 		return MAX_HS_STEP_CNT_DIV;
529*be5ce0e9SQii Wang 	else
530*be5ce0e9SQii Wang 		return MAX_STEP_CNT_DIV;
531*be5ce0e9SQii Wang }
532*be5ce0e9SQii Wang 
533*be5ce0e9SQii Wang /*
534*be5ce0e9SQii Wang  * Check and Calculate i2c ac-timing
535*be5ce0e9SQii Wang  *
536*be5ce0e9SQii Wang  * Hardware design:
537*be5ce0e9SQii Wang  * sample_ns = (1000000000 * (sample_cnt + 1)) / clk_src
538*be5ce0e9SQii Wang  * xxx_cnt_div =  spec->min_xxx_ns / sample_ns
539*be5ce0e9SQii Wang  *
540*be5ce0e9SQii Wang  * Sample_ns is rounded down for xxx_cnt_div would be greater
541*be5ce0e9SQii Wang  * than the smallest spec.
542*be5ce0e9SQii Wang  * The sda_timing is chosen as the middle value between
543*be5ce0e9SQii Wang  * the largest and smallest.
544*be5ce0e9SQii Wang  */
545*be5ce0e9SQii Wang static int mtk_i2c_check_ac_timing(struct mtk_i2c *i2c,
546*be5ce0e9SQii Wang 				   unsigned int clk_src,
547*be5ce0e9SQii Wang 				   unsigned int check_speed,
548*be5ce0e9SQii Wang 				   unsigned int step_cnt,
549*be5ce0e9SQii Wang 				   unsigned int sample_cnt)
550*be5ce0e9SQii Wang {
551*be5ce0e9SQii Wang 	const struct i2c_spec_values *spec;
552*be5ce0e9SQii Wang 	unsigned int su_sta_cnt, low_cnt, high_cnt, max_step_cnt;
553*be5ce0e9SQii Wang 	unsigned int sda_max, sda_min, clk_ns, max_sta_cnt = 0x3f;
554*be5ce0e9SQii Wang 	unsigned int sample_ns = div_u64(1000000000ULL * (sample_cnt + 1),
555*be5ce0e9SQii Wang 					 clk_src);
556*be5ce0e9SQii Wang 
557*be5ce0e9SQii Wang 	if (!i2c->dev_comp->timing_adjust)
558*be5ce0e9SQii Wang 		return 0;
559*be5ce0e9SQii Wang 
560*be5ce0e9SQii Wang 	if (i2c->dev_comp->ltiming_adjust)
561*be5ce0e9SQii Wang 		max_sta_cnt = 0x100;
562*be5ce0e9SQii Wang 
563*be5ce0e9SQii Wang 	spec = mtk_i2c_get_spec(check_speed);
564*be5ce0e9SQii Wang 
565*be5ce0e9SQii Wang 	if (i2c->dev_comp->ltiming_adjust)
566*be5ce0e9SQii Wang 		clk_ns = 1000000000 / clk_src;
567*be5ce0e9SQii Wang 	else
568*be5ce0e9SQii Wang 		clk_ns = sample_ns / 2;
569*be5ce0e9SQii Wang 
570*be5ce0e9SQii Wang 	su_sta_cnt = DIV_ROUND_UP(spec->min_su_sta_ns, clk_ns);
571*be5ce0e9SQii Wang 	if (su_sta_cnt > max_sta_cnt)
572*be5ce0e9SQii Wang 		return -1;
573*be5ce0e9SQii Wang 
574*be5ce0e9SQii Wang 	low_cnt = DIV_ROUND_UP(spec->min_low_ns, sample_ns);
575*be5ce0e9SQii Wang 	max_step_cnt = mtk_i2c_max_step_cnt(check_speed);
576*be5ce0e9SQii Wang 	if ((2 * step_cnt) > low_cnt && low_cnt < max_step_cnt) {
577*be5ce0e9SQii Wang 		if (low_cnt > step_cnt) {
578*be5ce0e9SQii Wang 			high_cnt = 2 * step_cnt - low_cnt;
579*be5ce0e9SQii Wang 		} else {
580*be5ce0e9SQii Wang 			high_cnt = step_cnt;
581*be5ce0e9SQii Wang 			low_cnt = step_cnt;
582*be5ce0e9SQii Wang 		}
583*be5ce0e9SQii Wang 	} else {
584*be5ce0e9SQii Wang 		return -2;
585*be5ce0e9SQii Wang 	}
586*be5ce0e9SQii Wang 
587*be5ce0e9SQii Wang 	sda_max = spec->max_hd_dat_ns / sample_ns;
588*be5ce0e9SQii Wang 	if (sda_max > low_cnt)
589*be5ce0e9SQii Wang 		sda_max = 0;
590*be5ce0e9SQii Wang 
591*be5ce0e9SQii Wang 	sda_min = DIV_ROUND_UP(spec->min_su_dat_ns, sample_ns);
592*be5ce0e9SQii Wang 	if (sda_min < low_cnt)
593*be5ce0e9SQii Wang 		sda_min = 0;
594*be5ce0e9SQii Wang 
595*be5ce0e9SQii Wang 	if (sda_min > sda_max)
596*be5ce0e9SQii Wang 		return -3;
597*be5ce0e9SQii Wang 
598*be5ce0e9SQii Wang 	if (check_speed > I2C_MAX_FAST_MODE_FREQ) {
599*be5ce0e9SQii Wang 		if (i2c->dev_comp->ltiming_adjust) {
600*be5ce0e9SQii Wang 			i2c->ac_timing.hs = I2C_TIME_DEFAULT_VALUE |
601*be5ce0e9SQii Wang 				(sample_cnt << 12) | (high_cnt << 8);
602*be5ce0e9SQii Wang 			i2c->ac_timing.ltiming &= ~GENMASK(15, 9);
603*be5ce0e9SQii Wang 			i2c->ac_timing.ltiming |= (sample_cnt << 12) |
604*be5ce0e9SQii Wang 				(low_cnt << 9);
605*be5ce0e9SQii Wang 			i2c->ac_timing.ext &= ~GENMASK(7, 1);
606*be5ce0e9SQii Wang 			i2c->ac_timing.ext |= (su_sta_cnt << 1) | (1 << 0);
607*be5ce0e9SQii Wang 		} else {
608*be5ce0e9SQii Wang 			i2c->ac_timing.hs_scl_hl_ratio = (1 << 12) |
609*be5ce0e9SQii Wang 				(high_cnt << 6) | low_cnt;
610*be5ce0e9SQii Wang 			i2c->ac_timing.hs_sta_stop = (su_sta_cnt << 8) |
611*be5ce0e9SQii Wang 				su_sta_cnt;
612*be5ce0e9SQii Wang 		}
613*be5ce0e9SQii Wang 		i2c->ac_timing.sda_timing &= ~GENMASK(11, 6);
614*be5ce0e9SQii Wang 		i2c->ac_timing.sda_timing |= (1 << 12) |
615*be5ce0e9SQii Wang 			((sda_max + sda_min) / 2) << 6;
616*be5ce0e9SQii Wang 	} else {
617*be5ce0e9SQii Wang 		if (i2c->dev_comp->ltiming_adjust) {
618*be5ce0e9SQii Wang 			i2c->ac_timing.htiming = (sample_cnt << 8) | (high_cnt);
619*be5ce0e9SQii Wang 			i2c->ac_timing.ltiming = (sample_cnt << 6) | (low_cnt);
620*be5ce0e9SQii Wang 			i2c->ac_timing.ext = (su_sta_cnt << 8) | (1 << 0);
621*be5ce0e9SQii Wang 		} else {
622*be5ce0e9SQii Wang 			i2c->ac_timing.scl_hl_ratio = (1 << 12) |
623*be5ce0e9SQii Wang 				(high_cnt << 6) | low_cnt;
624*be5ce0e9SQii Wang 			i2c->ac_timing.sta_stop = (su_sta_cnt << 8) |
625*be5ce0e9SQii Wang 				su_sta_cnt;
626*be5ce0e9SQii Wang 		}
627*be5ce0e9SQii Wang 
628*be5ce0e9SQii Wang 		i2c->ac_timing.sda_timing = (1 << 12) |
629*be5ce0e9SQii Wang 			(sda_max + sda_min) / 2;
630*be5ce0e9SQii Wang 	}
631*be5ce0e9SQii Wang 
632*be5ce0e9SQii Wang 	return 0;
633*be5ce0e9SQii Wang }
634*be5ce0e9SQii Wang 
635ce38815dSXudong Chen /*
636ce38815dSXudong Chen  * Calculate i2c port speed
637ce38815dSXudong Chen  *
638ce38815dSXudong Chen  * Hardware design:
639ce38815dSXudong Chen  * i2c_bus_freq = parent_clk / (clock_div * 2 * sample_cnt * step_cnt)
640ce38815dSXudong Chen  * clock_div: fixed in hardware, but may be various in different SoCs
641ce38815dSXudong Chen  *
642ce38815dSXudong Chen  * The calculation want to pick the highest bus frequency that is still
643ce38815dSXudong Chen  * less than or equal to i2c->speed_hz. The calculation try to get
644ce38815dSXudong Chen  * sample_cnt and step_cn
645ce38815dSXudong Chen  */
646f2326401SJun Gao static int mtk_i2c_calculate_speed(struct mtk_i2c *i2c, unsigned int clk_src,
647f2326401SJun Gao 				   unsigned int target_speed,
648f2326401SJun Gao 				   unsigned int *timing_step_cnt,
649f2326401SJun Gao 				   unsigned int *timing_sample_cnt)
650ce38815dSXudong Chen {
651ce38815dSXudong Chen 	unsigned int step_cnt;
652ce38815dSXudong Chen 	unsigned int sample_cnt;
653ce38815dSXudong Chen 	unsigned int max_step_cnt;
654ce38815dSXudong Chen 	unsigned int base_sample_cnt = MAX_SAMPLE_CNT_DIV;
655ce38815dSXudong Chen 	unsigned int base_step_cnt;
656ce38815dSXudong Chen 	unsigned int opt_div;
657ce38815dSXudong Chen 	unsigned int best_mul;
658ce38815dSXudong Chen 	unsigned int cnt_mul;
659*be5ce0e9SQii Wang 	int ret = -EINVAL;
660ce38815dSXudong Chen 
66190224e64SAndy Shevchenko 	if (target_speed > I2C_MAX_FAST_MODE_PLUS_FREQ)
66290224e64SAndy Shevchenko 		target_speed = I2C_MAX_FAST_MODE_PLUS_FREQ;
663ce38815dSXudong Chen 
664*be5ce0e9SQii Wang 	max_step_cnt = mtk_i2c_max_step_cnt(target_speed);
665ce38815dSXudong Chen 	base_step_cnt = max_step_cnt;
666ce38815dSXudong Chen 	/* Find the best combination */
667ce38815dSXudong Chen 	opt_div = DIV_ROUND_UP(clk_src >> 1, target_speed);
668ce38815dSXudong Chen 	best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt;
669ce38815dSXudong Chen 
670ce38815dSXudong Chen 	/* Search for the best pair (sample_cnt, step_cnt) with
671ce38815dSXudong Chen 	 * 0 < sample_cnt < MAX_SAMPLE_CNT_DIV
672ce38815dSXudong Chen 	 * 0 < step_cnt < max_step_cnt
673ce38815dSXudong Chen 	 * sample_cnt * step_cnt >= opt_div
674ce38815dSXudong Chen 	 * optimizing for sample_cnt * step_cnt being minimal
675ce38815dSXudong Chen 	 */
676ce38815dSXudong Chen 	for (sample_cnt = 1; sample_cnt <= MAX_SAMPLE_CNT_DIV; sample_cnt++) {
677ce38815dSXudong Chen 		step_cnt = DIV_ROUND_UP(opt_div, sample_cnt);
678ce38815dSXudong Chen 		cnt_mul = step_cnt * sample_cnt;
679ce38815dSXudong Chen 		if (step_cnt > max_step_cnt)
680ce38815dSXudong Chen 			continue;
681ce38815dSXudong Chen 
682ce38815dSXudong Chen 		if (cnt_mul < best_mul) {
683*be5ce0e9SQii Wang 			ret = mtk_i2c_check_ac_timing(i2c, clk_src,
684*be5ce0e9SQii Wang 				target_speed, step_cnt - 1, sample_cnt - 1);
685*be5ce0e9SQii Wang 			if (ret)
686*be5ce0e9SQii Wang 				continue;
687*be5ce0e9SQii Wang 
688ce38815dSXudong Chen 			best_mul = cnt_mul;
689ce38815dSXudong Chen 			base_sample_cnt = sample_cnt;
690ce38815dSXudong Chen 			base_step_cnt = step_cnt;
691ce38815dSXudong Chen 			if (best_mul == opt_div)
692ce38815dSXudong Chen 				break;
693ce38815dSXudong Chen 		}
694ce38815dSXudong Chen 	}
695ce38815dSXudong Chen 
696*be5ce0e9SQii Wang 	if (ret)
697*be5ce0e9SQii Wang 		return -EINVAL;
698*be5ce0e9SQii Wang 
699ce38815dSXudong Chen 	sample_cnt = base_sample_cnt;
700ce38815dSXudong Chen 	step_cnt = base_step_cnt;
701ce38815dSXudong Chen 
702ce38815dSXudong Chen 	if ((clk_src / (2 * sample_cnt * step_cnt)) > target_speed) {
703ce38815dSXudong Chen 		/* In this case, hardware can't support such
704ce38815dSXudong Chen 		 * low i2c_bus_freq
705ce38815dSXudong Chen 		 */
706ce38815dSXudong Chen 		dev_dbg(i2c->dev, "Unsupported speed (%uhz)\n",	target_speed);
707ce38815dSXudong Chen 		return -EINVAL;
708ce38815dSXudong Chen 	}
709ce38815dSXudong Chen 
710f2326401SJun Gao 	*timing_step_cnt = step_cnt - 1;
711f2326401SJun Gao 	*timing_sample_cnt = sample_cnt - 1;
712f2326401SJun Gao 
713f2326401SJun Gao 	return 0;
714f2326401SJun Gao }
715f2326401SJun Gao 
716f2326401SJun Gao static int mtk_i2c_set_speed(struct mtk_i2c *i2c, unsigned int parent_clk)
717f2326401SJun Gao {
718f2326401SJun Gao 	unsigned int clk_src;
719f2326401SJun Gao 	unsigned int step_cnt;
720f2326401SJun Gao 	unsigned int sample_cnt;
72125708278SQii Wang 	unsigned int l_step_cnt;
72225708278SQii Wang 	unsigned int l_sample_cnt;
723f2326401SJun Gao 	unsigned int target_speed;
724*be5ce0e9SQii Wang 	unsigned int clk_div;
725*be5ce0e9SQii Wang 	unsigned int max_clk_div;
726f2326401SJun Gao 	int ret;
727f2326401SJun Gao 
728f2326401SJun Gao 	target_speed = i2c->speed_hz;
729*be5ce0e9SQii Wang 	parent_clk /= i2c->clk_src_div;
730*be5ce0e9SQii Wang 
731*be5ce0e9SQii Wang 	if (i2c->dev_comp->timing_adjust)
732*be5ce0e9SQii Wang 		max_clk_div = MAX_CLOCK_DIV;
733*be5ce0e9SQii Wang 	else
734*be5ce0e9SQii Wang 		max_clk_div = 1;
735*be5ce0e9SQii Wang 
736*be5ce0e9SQii Wang 	for (clk_div = 1; clk_div <= max_clk_div; clk_div++) {
737*be5ce0e9SQii Wang 		clk_src = parent_clk / clk_div;
738ce38815dSXudong Chen 
73990224e64SAndy Shevchenko 		if (target_speed > I2C_MAX_FAST_MODE_FREQ) {
740f2326401SJun Gao 			/* Set master code speed register */
741*be5ce0e9SQii Wang 			ret = mtk_i2c_calculate_speed(i2c, clk_src,
742*be5ce0e9SQii Wang 						      I2C_MAX_FAST_MODE_FREQ,
743*be5ce0e9SQii Wang 						      &l_step_cnt,
744*be5ce0e9SQii Wang 						      &l_sample_cnt);
745f2326401SJun Gao 			if (ret < 0)
746*be5ce0e9SQii Wang 				continue;
747f2326401SJun Gao 
74825708278SQii Wang 			i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt;
749f2326401SJun Gao 
750ce38815dSXudong Chen 			/* Set the high speed mode register */
751*be5ce0e9SQii Wang 			ret = mtk_i2c_calculate_speed(i2c, clk_src,
752*be5ce0e9SQii Wang 						      target_speed, &step_cnt,
753*be5ce0e9SQii Wang 						      &sample_cnt);
754f2326401SJun Gao 			if (ret < 0)
755*be5ce0e9SQii Wang 				continue;
756f2326401SJun Gao 
757ce38815dSXudong Chen 			i2c->high_speed_reg = I2C_TIME_DEFAULT_VALUE |
758ce38815dSXudong Chen 					(sample_cnt << 12) | (step_cnt << 8);
75925708278SQii Wang 
76025708278SQii Wang 			if (i2c->dev_comp->ltiming_adjust)
761*be5ce0e9SQii Wang 				i2c->ltiming_reg =
762*be5ce0e9SQii Wang 					(l_sample_cnt << 6) | l_step_cnt |
76325708278SQii Wang 					(sample_cnt << 12) | (step_cnt << 9);
764ce38815dSXudong Chen 		} else {
765*be5ce0e9SQii Wang 			ret = mtk_i2c_calculate_speed(i2c, clk_src,
766*be5ce0e9SQii Wang 						      target_speed, &l_step_cnt,
767*be5ce0e9SQii Wang 						      &l_sample_cnt);
768f2326401SJun Gao 			if (ret < 0)
769*be5ce0e9SQii Wang 				continue;
770f2326401SJun Gao 
771*be5ce0e9SQii Wang 			i2c->timing_reg = (l_sample_cnt << 8) | l_step_cnt;
772f2326401SJun Gao 
773ce38815dSXudong Chen 			/* Disable the high speed transaction */
774ce38815dSXudong Chen 			i2c->high_speed_reg = I2C_TIME_CLR_VALUE;
77525708278SQii Wang 
77625708278SQii Wang 			if (i2c->dev_comp->ltiming_adjust)
777*be5ce0e9SQii Wang 				i2c->ltiming_reg =
778*be5ce0e9SQii Wang 					(l_sample_cnt << 6) | l_step_cnt;
779ce38815dSXudong Chen 		}
780ce38815dSXudong Chen 
781*be5ce0e9SQii Wang 		break;
782*be5ce0e9SQii Wang 	}
783*be5ce0e9SQii Wang 
784*be5ce0e9SQii Wang 	i2c->ac_timing.inter_clk_div = clk_div - 1;
785*be5ce0e9SQii Wang 
786ce38815dSXudong Chen 	return 0;
787ce38815dSXudong Chen }
788ce38815dSXudong Chen 
789f4f4fed6SLiguo Zhang static inline u32 mtk_i2c_set_4g_mode(dma_addr_t addr)
790f4f4fed6SLiguo Zhang {
791f4f4fed6SLiguo Zhang 	return (addr & BIT_ULL(32)) ? I2C_DMA_4G_MODE : I2C_DMA_CLR_FLAG;
792f4f4fed6SLiguo Zhang }
793f4f4fed6SLiguo Zhang 
794b2ed11e2SEddie Huang static int mtk_i2c_do_transfer(struct mtk_i2c *i2c, struct i2c_msg *msgs,
795b2ed11e2SEddie Huang 			       int num, int left_num)
796ce38815dSXudong Chen {
797ce38815dSXudong Chen 	u16 addr_reg;
798b2ed11e2SEddie Huang 	u16 start_reg;
799ce38815dSXudong Chen 	u16 control_reg;
800b2ed11e2SEddie Huang 	u16 restart_flag = 0;
801f4f4fed6SLiguo Zhang 	u32 reg_4g_mode;
802fc66b39fSJun Gao 	u8 *dma_rd_buf = NULL;
803fc66b39fSJun Gao 	u8 *dma_wr_buf = NULL;
804ce38815dSXudong Chen 	dma_addr_t rpaddr = 0;
805ce38815dSXudong Chen 	dma_addr_t wpaddr = 0;
806ce38815dSXudong Chen 	int ret;
807ce38815dSXudong Chen 
808ce38815dSXudong Chen 	i2c->irq_stat = 0;
809ce38815dSXudong Chen 
810173b77e8SLiguo Zhang 	if (i2c->auto_restart)
811b2ed11e2SEddie Huang 		restart_flag = I2C_RS_TRANSFER;
812b2ed11e2SEddie Huang 
813ce38815dSXudong Chen 	reinit_completion(&i2c->msg_complete);
814ce38815dSXudong Chen 
815bc6eaf17SQii Wang 	control_reg = mtk_i2c_readw(i2c, OFFSET_CONTROL) &
816ce38815dSXudong Chen 			~(I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS);
81790224e64SAndy Shevchenko 	if ((i2c->speed_hz > I2C_MAX_FAST_MODE_FREQ) || (left_num >= 1))
818ce38815dSXudong Chen 		control_reg |= I2C_CONTROL_RS;
819ce38815dSXudong Chen 
820ce38815dSXudong Chen 	if (i2c->op == I2C_MASTER_WRRD)
821ce38815dSXudong Chen 		control_reg |= I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS;
822ce38815dSXudong Chen 
823bc6eaf17SQii Wang 	mtk_i2c_writew(i2c, control_reg, OFFSET_CONTROL);
824ce38815dSXudong Chen 
8250d47ce21SWolfram Sang 	addr_reg = i2c_8bit_addr_from_msg(msgs);
826bc6eaf17SQii Wang 	mtk_i2c_writew(i2c, addr_reg, OFFSET_SLAVE_ADDR);
827ce38815dSXudong Chen 
828ce38815dSXudong Chen 	/* Clear interrupt status */
829bc6eaf17SQii Wang 	mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR |
830cad6dc5dSQii Wang 			    I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_STAT);
831bc6eaf17SQii Wang 
832bc6eaf17SQii Wang 	mtk_i2c_writew(i2c, I2C_FIFO_ADDR_CLR, OFFSET_FIFO_ADDR_CLR);
833ce38815dSXudong Chen 
834ce38815dSXudong Chen 	/* Enable interrupt */
835bc6eaf17SQii Wang 	mtk_i2c_writew(i2c, restart_flag | I2C_HS_NACKERR | I2C_ACKERR |
836cad6dc5dSQii Wang 			    I2C_ARB_LOST | I2C_TRANSAC_COMP, OFFSET_INTR_MASK);
837ce38815dSXudong Chen 
838ce38815dSXudong Chen 	/* Set transfer and transaction len */
839ce38815dSXudong Chen 	if (i2c->op == I2C_MASTER_WRRD) {
840173b77e8SLiguo Zhang 		if (i2c->dev_comp->aux_len_reg) {
841bc6eaf17SQii Wang 			mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN);
842bc6eaf17SQii Wang 			mtk_i2c_writew(i2c, (msgs + 1)->len,
843173b77e8SLiguo Zhang 					    OFFSET_TRANSFER_LEN_AUX);
844173b77e8SLiguo Zhang 		} else {
845bc6eaf17SQii Wang 			mtk_i2c_writew(i2c, msgs->len | ((msgs + 1)->len) << 8,
846bc6eaf17SQii Wang 					    OFFSET_TRANSFER_LEN);
847173b77e8SLiguo Zhang 		}
848bc6eaf17SQii Wang 		mtk_i2c_writew(i2c, I2C_WRRD_TRANAC_VALUE, OFFSET_TRANSAC_LEN);
849ce38815dSXudong Chen 	} else {
850bc6eaf17SQii Wang 		mtk_i2c_writew(i2c, msgs->len, OFFSET_TRANSFER_LEN);
851bc6eaf17SQii Wang 		mtk_i2c_writew(i2c, num, OFFSET_TRANSAC_LEN);
852ce38815dSXudong Chen 	}
853ce38815dSXudong Chen 
854ce38815dSXudong Chen 	/* Prepare buffer data to start transfer */
855ce38815dSXudong Chen 	if (i2c->op == I2C_MASTER_RD) {
856ce38815dSXudong Chen 		writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG);
857ce38815dSXudong Chen 		writel(I2C_DMA_CON_RX, i2c->pdmabase + OFFSET_CON);
858fc66b39fSJun Gao 
859bc1a7f75SHsin-Yi Wang 		dma_rd_buf = i2c_get_dma_safe_msg_buf(msgs, 1);
860fc66b39fSJun Gao 		if (!dma_rd_buf)
861ce38815dSXudong Chen 			return -ENOMEM;
862f4f4fed6SLiguo Zhang 
863fc66b39fSJun Gao 		rpaddr = dma_map_single(i2c->dev, dma_rd_buf,
864fc66b39fSJun Gao 					msgs->len, DMA_FROM_DEVICE);
865fc66b39fSJun Gao 		if (dma_mapping_error(i2c->dev, rpaddr)) {
866fc66b39fSJun Gao 			i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, false);
867fc66b39fSJun Gao 
868fc66b39fSJun Gao 			return -ENOMEM;
869fc66b39fSJun Gao 		}
870fc66b39fSJun Gao 
871f4f4fed6SLiguo Zhang 		if (i2c->dev_comp->support_33bits) {
872f4f4fed6SLiguo Zhang 			reg_4g_mode = mtk_i2c_set_4g_mode(rpaddr);
873f4f4fed6SLiguo Zhang 			writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE);
874f4f4fed6SLiguo Zhang 		}
875f4f4fed6SLiguo Zhang 
876ce38815dSXudong Chen 		writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR);
877ce38815dSXudong Chen 		writel(msgs->len, i2c->pdmabase + OFFSET_RX_LEN);
878ce38815dSXudong Chen 	} else if (i2c->op == I2C_MASTER_WR) {
879ce38815dSXudong Chen 		writel(I2C_DMA_INT_FLAG_NONE, i2c->pdmabase + OFFSET_INT_FLAG);
880ce38815dSXudong Chen 		writel(I2C_DMA_CON_TX, i2c->pdmabase + OFFSET_CON);
881fc66b39fSJun Gao 
882bc1a7f75SHsin-Yi Wang 		dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1);
883fc66b39fSJun Gao 		if (!dma_wr_buf)
884ce38815dSXudong Chen 			return -ENOMEM;
885f4f4fed6SLiguo Zhang 
886fc66b39fSJun Gao 		wpaddr = dma_map_single(i2c->dev, dma_wr_buf,
887fc66b39fSJun Gao 					msgs->len, DMA_TO_DEVICE);
888fc66b39fSJun Gao 		if (dma_mapping_error(i2c->dev, wpaddr)) {
889fc66b39fSJun Gao 			i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false);
890fc66b39fSJun Gao 
891fc66b39fSJun Gao 			return -ENOMEM;
892fc66b39fSJun Gao 		}
893fc66b39fSJun Gao 
894f4f4fed6SLiguo Zhang 		if (i2c->dev_comp->support_33bits) {
895f4f4fed6SLiguo Zhang 			reg_4g_mode = mtk_i2c_set_4g_mode(wpaddr);
896f4f4fed6SLiguo Zhang 			writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE);
897f4f4fed6SLiguo Zhang 		}
898f4f4fed6SLiguo Zhang 
899ce38815dSXudong Chen 		writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR);
900ce38815dSXudong Chen 		writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN);
901ce38815dSXudong Chen 	} else {
902ce38815dSXudong Chen 		writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_INT_FLAG);
903ce38815dSXudong Chen 		writel(I2C_DMA_CLR_FLAG, i2c->pdmabase + OFFSET_CON);
904fc66b39fSJun Gao 
905bc1a7f75SHsin-Yi Wang 		dma_wr_buf = i2c_get_dma_safe_msg_buf(msgs, 1);
906fc66b39fSJun Gao 		if (!dma_wr_buf)
907ce38815dSXudong Chen 			return -ENOMEM;
908fc66b39fSJun Gao 
909fc66b39fSJun Gao 		wpaddr = dma_map_single(i2c->dev, dma_wr_buf,
910fc66b39fSJun Gao 					msgs->len, DMA_TO_DEVICE);
911fc66b39fSJun Gao 		if (dma_mapping_error(i2c->dev, wpaddr)) {
912fc66b39fSJun Gao 			i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false);
913fc66b39fSJun Gao 
914fc66b39fSJun Gao 			return -ENOMEM;
915fc66b39fSJun Gao 		}
916fc66b39fSJun Gao 
917bc1a7f75SHsin-Yi Wang 		dma_rd_buf = i2c_get_dma_safe_msg_buf((msgs + 1), 1);
918fc66b39fSJun Gao 		if (!dma_rd_buf) {
919fc66b39fSJun Gao 			dma_unmap_single(i2c->dev, wpaddr,
920fc66b39fSJun Gao 					 msgs->len, DMA_TO_DEVICE);
921fc66b39fSJun Gao 
922fc66b39fSJun Gao 			i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false);
923fc66b39fSJun Gao 
924fc66b39fSJun Gao 			return -ENOMEM;
925fc66b39fSJun Gao 		}
926fc66b39fSJun Gao 
927fc66b39fSJun Gao 		rpaddr = dma_map_single(i2c->dev, dma_rd_buf,
928ce38815dSXudong Chen 					(msgs + 1)->len,
929ce38815dSXudong Chen 					DMA_FROM_DEVICE);
930ce38815dSXudong Chen 		if (dma_mapping_error(i2c->dev, rpaddr)) {
931ce38815dSXudong Chen 			dma_unmap_single(i2c->dev, wpaddr,
932ce38815dSXudong Chen 					 msgs->len, DMA_TO_DEVICE);
933fc66b39fSJun Gao 
934fc66b39fSJun Gao 			i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, false);
935fc66b39fSJun Gao 			i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), false);
936fc66b39fSJun Gao 
937ce38815dSXudong Chen 			return -ENOMEM;
938ce38815dSXudong Chen 		}
939f4f4fed6SLiguo Zhang 
940f4f4fed6SLiguo Zhang 		if (i2c->dev_comp->support_33bits) {
941f4f4fed6SLiguo Zhang 			reg_4g_mode = mtk_i2c_set_4g_mode(wpaddr);
942f4f4fed6SLiguo Zhang 			writel(reg_4g_mode, i2c->pdmabase + OFFSET_TX_4G_MODE);
943f4f4fed6SLiguo Zhang 
944f4f4fed6SLiguo Zhang 			reg_4g_mode = mtk_i2c_set_4g_mode(rpaddr);
945f4f4fed6SLiguo Zhang 			writel(reg_4g_mode, i2c->pdmabase + OFFSET_RX_4G_MODE);
946f4f4fed6SLiguo Zhang 		}
947f4f4fed6SLiguo Zhang 
948ce38815dSXudong Chen 		writel((u32)wpaddr, i2c->pdmabase + OFFSET_TX_MEM_ADDR);
949ce38815dSXudong Chen 		writel((u32)rpaddr, i2c->pdmabase + OFFSET_RX_MEM_ADDR);
950ce38815dSXudong Chen 		writel(msgs->len, i2c->pdmabase + OFFSET_TX_LEN);
951ce38815dSXudong Chen 		writel((msgs + 1)->len, i2c->pdmabase + OFFSET_RX_LEN);
952ce38815dSXudong Chen 	}
953ce38815dSXudong Chen 
954ce38815dSXudong Chen 	writel(I2C_DMA_START_EN, i2c->pdmabase + OFFSET_EN);
955b2ed11e2SEddie Huang 
956173b77e8SLiguo Zhang 	if (!i2c->auto_restart) {
957b2ed11e2SEddie Huang 		start_reg = I2C_TRANSAC_START;
958b2ed11e2SEddie Huang 	} else {
959b2ed11e2SEddie Huang 		start_reg = I2C_TRANSAC_START | I2C_RS_MUL_TRIG;
960b2ed11e2SEddie Huang 		if (left_num >= 1)
961b2ed11e2SEddie Huang 			start_reg |= I2C_RS_MUL_CNFG;
962b2ed11e2SEddie Huang 	}
963bc6eaf17SQii Wang 	mtk_i2c_writew(i2c, start_reg, OFFSET_START);
964ce38815dSXudong Chen 
965ce38815dSXudong Chen 	ret = wait_for_completion_timeout(&i2c->msg_complete,
966ce38815dSXudong Chen 					  i2c->adap.timeout);
967ce38815dSXudong Chen 
968ce38815dSXudong Chen 	/* Clear interrupt mask */
969bc6eaf17SQii Wang 	mtk_i2c_writew(i2c, ~(restart_flag | I2C_HS_NACKERR | I2C_ACKERR |
970cad6dc5dSQii Wang 			    I2C_ARB_LOST | I2C_TRANSAC_COMP), OFFSET_INTR_MASK);
971ce38815dSXudong Chen 
972ce38815dSXudong Chen 	if (i2c->op == I2C_MASTER_WR) {
973ce38815dSXudong Chen 		dma_unmap_single(i2c->dev, wpaddr,
974ce38815dSXudong Chen 				 msgs->len, DMA_TO_DEVICE);
975fc66b39fSJun Gao 
976fc66b39fSJun Gao 		i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true);
977ce38815dSXudong Chen 	} else if (i2c->op == I2C_MASTER_RD) {
978ce38815dSXudong Chen 		dma_unmap_single(i2c->dev, rpaddr,
979ce38815dSXudong Chen 				 msgs->len, DMA_FROM_DEVICE);
980fc66b39fSJun Gao 
981fc66b39fSJun Gao 		i2c_put_dma_safe_msg_buf(dma_rd_buf, msgs, true);
982ce38815dSXudong Chen 	} else {
983ce38815dSXudong Chen 		dma_unmap_single(i2c->dev, wpaddr, msgs->len,
984ce38815dSXudong Chen 				 DMA_TO_DEVICE);
985ce38815dSXudong Chen 		dma_unmap_single(i2c->dev, rpaddr, (msgs + 1)->len,
986ce38815dSXudong Chen 				 DMA_FROM_DEVICE);
987fc66b39fSJun Gao 
988fc66b39fSJun Gao 		i2c_put_dma_safe_msg_buf(dma_wr_buf, msgs, true);
989fc66b39fSJun Gao 		i2c_put_dma_safe_msg_buf(dma_rd_buf, (msgs + 1), true);
990ce38815dSXudong Chen 	}
991ce38815dSXudong Chen 
992ce38815dSXudong Chen 	if (ret == 0) {
993ce38815dSXudong Chen 		dev_dbg(i2c->dev, "addr: %x, transfer timeout\n", msgs->addr);
994ce38815dSXudong Chen 		mtk_i2c_init_hw(i2c);
995ce38815dSXudong Chen 		return -ETIMEDOUT;
996ce38815dSXudong Chen 	}
997ce38815dSXudong Chen 
998ce38815dSXudong Chen 	if (i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR)) {
999ce38815dSXudong Chen 		dev_dbg(i2c->dev, "addr: %x, transfer ACK error\n", msgs->addr);
1000ce38815dSXudong Chen 		mtk_i2c_init_hw(i2c);
1001ce38815dSXudong Chen 		return -ENXIO;
1002ce38815dSXudong Chen 	}
1003ce38815dSXudong Chen 
1004ce38815dSXudong Chen 	return 0;
1005ce38815dSXudong Chen }
1006ce38815dSXudong Chen 
1007ce38815dSXudong Chen static int mtk_i2c_transfer(struct i2c_adapter *adap,
1008ce38815dSXudong Chen 			    struct i2c_msg msgs[], int num)
1009ce38815dSXudong Chen {
1010ce38815dSXudong Chen 	int ret;
1011ce38815dSXudong Chen 	int left_num = num;
1012ce38815dSXudong Chen 	struct mtk_i2c *i2c = i2c_get_adapdata(adap);
1013ce38815dSXudong Chen 
1014ce38815dSXudong Chen 	ret = mtk_i2c_clock_enable(i2c);
1015ce38815dSXudong Chen 	if (ret)
1016ce38815dSXudong Chen 		return ret;
1017ce38815dSXudong Chen 
1018173b77e8SLiguo Zhang 	i2c->auto_restart = i2c->dev_comp->auto_restart;
1019173b77e8SLiguo Zhang 
1020173b77e8SLiguo Zhang 	/* checking if we can skip restart and optimize using WRRD mode */
1021173b77e8SLiguo Zhang 	if (i2c->auto_restart && num == 2) {
1022173b77e8SLiguo Zhang 		if (!(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD) &&
1023173b77e8SLiguo Zhang 		    msgs[0].addr == msgs[1].addr) {
1024173b77e8SLiguo Zhang 			i2c->auto_restart = 0;
1025173b77e8SLiguo Zhang 		}
1026173b77e8SLiguo Zhang 	}
1027173b77e8SLiguo Zhang 
102890224e64SAndy Shevchenko 	if (i2c->auto_restart && num >= 2 && i2c->speed_hz > I2C_MAX_FAST_MODE_FREQ)
10298378d01fSLiguo Zhang 		/* ignore the first restart irq after the master code,
10308378d01fSLiguo Zhang 		 * otherwise the first transfer will be discarded.
10318378d01fSLiguo Zhang 		 */
10328378d01fSLiguo Zhang 		i2c->ignore_restart_irq = true;
10338378d01fSLiguo Zhang 	else
10348378d01fSLiguo Zhang 		i2c->ignore_restart_irq = false;
10358378d01fSLiguo Zhang 
1036b2ed11e2SEddie Huang 	while (left_num--) {
1037ce38815dSXudong Chen 		if (!msgs->buf) {
1038ce38815dSXudong Chen 			dev_dbg(i2c->dev, "data buffer is NULL.\n");
1039ce38815dSXudong Chen 			ret = -EINVAL;
1040ce38815dSXudong Chen 			goto err_exit;
1041ce38815dSXudong Chen 		}
1042ce38815dSXudong Chen 
1043ce38815dSXudong Chen 		if (msgs->flags & I2C_M_RD)
1044ce38815dSXudong Chen 			i2c->op = I2C_MASTER_RD;
1045ce38815dSXudong Chen 		else
1046ce38815dSXudong Chen 			i2c->op = I2C_MASTER_WR;
1047ce38815dSXudong Chen 
1048173b77e8SLiguo Zhang 		if (!i2c->auto_restart) {
1049ce38815dSXudong Chen 			if (num > 1) {
1050ce38815dSXudong Chen 				/* combined two messages into one transaction */
1051ce38815dSXudong Chen 				i2c->op = I2C_MASTER_WRRD;
1052ce38815dSXudong Chen 				left_num--;
1053ce38815dSXudong Chen 			}
1054b2ed11e2SEddie Huang 		}
1055ce38815dSXudong Chen 
1056ce38815dSXudong Chen 		/* always use DMA mode. */
1057b2ed11e2SEddie Huang 		ret = mtk_i2c_do_transfer(i2c, msgs, num, left_num);
1058ce38815dSXudong Chen 		if (ret < 0)
1059ce38815dSXudong Chen 			goto err_exit;
1060ce38815dSXudong Chen 
1061b2ed11e2SEddie Huang 		msgs++;
1062b2ed11e2SEddie Huang 	}
1063ce38815dSXudong Chen 	/* the return value is number of executed messages */
1064ce38815dSXudong Chen 	ret = num;
1065ce38815dSXudong Chen 
1066ce38815dSXudong Chen err_exit:
1067ce38815dSXudong Chen 	mtk_i2c_clock_disable(i2c);
1068ce38815dSXudong Chen 	return ret;
1069ce38815dSXudong Chen }
1070ce38815dSXudong Chen 
1071ce38815dSXudong Chen static irqreturn_t mtk_i2c_irq(int irqno, void *dev_id)
1072ce38815dSXudong Chen {
1073ce38815dSXudong Chen 	struct mtk_i2c *i2c = dev_id;
1074b2ed11e2SEddie Huang 	u16 restart_flag = 0;
107528c0a843SEddie Huang 	u16 intr_stat;
1076b2ed11e2SEddie Huang 
1077173b77e8SLiguo Zhang 	if (i2c->auto_restart)
1078b2ed11e2SEddie Huang 		restart_flag = I2C_RS_TRANSFER;
1079ce38815dSXudong Chen 
1080bc6eaf17SQii Wang 	intr_stat = mtk_i2c_readw(i2c, OFFSET_INTR_STAT);
1081bc6eaf17SQii Wang 	mtk_i2c_writew(i2c, intr_stat, OFFSET_INTR_STAT);
1082ce38815dSXudong Chen 
108328c0a843SEddie Huang 	/*
108428c0a843SEddie Huang 	 * when occurs ack error, i2c controller generate two interrupts
108528c0a843SEddie Huang 	 * first is the ack error interrupt, then the complete interrupt
108628c0a843SEddie Huang 	 * i2c->irq_stat need keep the two interrupt value.
108728c0a843SEddie Huang 	 */
108828c0a843SEddie Huang 	i2c->irq_stat |= intr_stat;
10898378d01fSLiguo Zhang 
10908378d01fSLiguo Zhang 	if (i2c->ignore_restart_irq && (i2c->irq_stat & restart_flag)) {
10918378d01fSLiguo Zhang 		i2c->ignore_restart_irq = false;
10928378d01fSLiguo Zhang 		i2c->irq_stat = 0;
1093bc6eaf17SQii Wang 		mtk_i2c_writew(i2c, I2C_RS_MUL_CNFG | I2C_RS_MUL_TRIG |
1094bc6eaf17SQii Wang 				    I2C_TRANSAC_START, OFFSET_START);
10958378d01fSLiguo Zhang 	} else {
109628c0a843SEddie Huang 		if (i2c->irq_stat & (I2C_TRANSAC_COMP | restart_flag))
1097ce38815dSXudong Chen 			complete(&i2c->msg_complete);
10988378d01fSLiguo Zhang 	}
1099ce38815dSXudong Chen 
1100ce38815dSXudong Chen 	return IRQ_HANDLED;
1101ce38815dSXudong Chen }
1102ce38815dSXudong Chen 
1103ce38815dSXudong Chen static u32 mtk_i2c_functionality(struct i2c_adapter *adap)
1104ce38815dSXudong Chen {
110562931ac2SFabien Parent 	if (i2c_check_quirks(adap, I2C_AQ_NO_ZERO_LEN))
1106abf4923eSHsin-Yi Wang 		return I2C_FUNC_I2C |
1107abf4923eSHsin-Yi Wang 			(I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
1108abf4923eSHsin-Yi Wang 	else
1109ce38815dSXudong Chen 		return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
1110ce38815dSXudong Chen }
1111ce38815dSXudong Chen 
1112ce38815dSXudong Chen static const struct i2c_algorithm mtk_i2c_algorithm = {
1113ce38815dSXudong Chen 	.master_xfer = mtk_i2c_transfer,
1114ce38815dSXudong Chen 	.functionality = mtk_i2c_functionality,
1115ce38815dSXudong Chen };
1116ce38815dSXudong Chen 
1117f2326401SJun Gao static int mtk_i2c_parse_dt(struct device_node *np, struct mtk_i2c *i2c)
1118ce38815dSXudong Chen {
1119ce38815dSXudong Chen 	int ret;
1120ce38815dSXudong Chen 
1121ce38815dSXudong Chen 	ret = of_property_read_u32(np, "clock-frequency", &i2c->speed_hz);
1122ce38815dSXudong Chen 	if (ret < 0)
112390224e64SAndy Shevchenko 		i2c->speed_hz = I2C_MAX_STANDARD_MODE_FREQ;
1124ce38815dSXudong Chen 
1125f2326401SJun Gao 	ret = of_property_read_u32(np, "clock-div", &i2c->clk_src_div);
1126ce38815dSXudong Chen 	if (ret < 0)
1127ce38815dSXudong Chen 		return ret;
1128ce38815dSXudong Chen 
1129f2326401SJun Gao 	if (i2c->clk_src_div == 0)
1130ce38815dSXudong Chen 		return -EINVAL;
1131ce38815dSXudong Chen 
1132ce38815dSXudong Chen 	i2c->have_pmic = of_property_read_bool(np, "mediatek,have-pmic");
1133ce38815dSXudong Chen 	i2c->use_push_pull =
1134ce38815dSXudong Chen 		of_property_read_bool(np, "mediatek,use-push-pull");
1135ce38815dSXudong Chen 
1136ce38815dSXudong Chen 	return 0;
1137ce38815dSXudong Chen }
1138ce38815dSXudong Chen 
1139ce38815dSXudong Chen static int mtk_i2c_probe(struct platform_device *pdev)
1140ce38815dSXudong Chen {
1141ce38815dSXudong Chen 	int ret = 0;
1142ce38815dSXudong Chen 	struct mtk_i2c *i2c;
1143ce38815dSXudong Chen 	struct clk *clk;
1144ce38815dSXudong Chen 	struct resource *res;
1145ce38815dSXudong Chen 	int irq;
1146ce38815dSXudong Chen 
1147ce38815dSXudong Chen 	i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
1148ce38815dSXudong Chen 	if (!i2c)
1149ce38815dSXudong Chen 		return -ENOMEM;
1150ce38815dSXudong Chen 
1151ce38815dSXudong Chen 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1152ce38815dSXudong Chen 	i2c->base = devm_ioremap_resource(&pdev->dev, res);
1153ce38815dSXudong Chen 	if (IS_ERR(i2c->base))
1154ce38815dSXudong Chen 		return PTR_ERR(i2c->base);
1155ce38815dSXudong Chen 
1156ce38815dSXudong Chen 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1157ce38815dSXudong Chen 	i2c->pdmabase = devm_ioremap_resource(&pdev->dev, res);
1158ce38815dSXudong Chen 	if (IS_ERR(i2c->pdmabase))
1159ce38815dSXudong Chen 		return PTR_ERR(i2c->pdmabase);
1160ce38815dSXudong Chen 
1161ce38815dSXudong Chen 	irq = platform_get_irq(pdev, 0);
1162ce38815dSXudong Chen 	if (irq <= 0)
1163ce38815dSXudong Chen 		return irq;
1164ce38815dSXudong Chen 
1165ce38815dSXudong Chen 	init_completion(&i2c->msg_complete);
1166ce38815dSXudong Chen 
11676e29577fSRyder Lee 	i2c->dev_comp = of_device_get_match_data(&pdev->dev);
1168ce38815dSXudong Chen 	i2c->adap.dev.of_node = pdev->dev.of_node;
1169ce38815dSXudong Chen 	i2c->dev = &pdev->dev;
1170ce38815dSXudong Chen 	i2c->adap.dev.parent = &pdev->dev;
1171ce38815dSXudong Chen 	i2c->adap.owner = THIS_MODULE;
1172ce38815dSXudong Chen 	i2c->adap.algo = &mtk_i2c_algorithm;
1173ce38815dSXudong Chen 	i2c->adap.quirks = i2c->dev_comp->quirks;
1174ce38815dSXudong Chen 	i2c->adap.timeout = 2 * HZ;
1175ce38815dSXudong Chen 	i2c->adap.retries = 1;
1176ce38815dSXudong Chen 
11775a10e7d7SJun Gao 	ret = mtk_i2c_parse_dt(pdev->dev.of_node, i2c);
11785a10e7d7SJun Gao 	if (ret)
11795a10e7d7SJun Gao 		return -EINVAL;
11805a10e7d7SJun Gao 
1181ce38815dSXudong Chen 	if (i2c->have_pmic && !i2c->dev_comp->pmic_i2c)
1182ce38815dSXudong Chen 		return -EINVAL;
1183ce38815dSXudong Chen 
1184ce38815dSXudong Chen 	i2c->clk_main = devm_clk_get(&pdev->dev, "main");
1185ce38815dSXudong Chen 	if (IS_ERR(i2c->clk_main)) {
1186ce38815dSXudong Chen 		dev_err(&pdev->dev, "cannot get main clock\n");
1187ce38815dSXudong Chen 		return PTR_ERR(i2c->clk_main);
1188ce38815dSXudong Chen 	}
1189ce38815dSXudong Chen 
1190ce38815dSXudong Chen 	i2c->clk_dma = devm_clk_get(&pdev->dev, "dma");
1191ce38815dSXudong Chen 	if (IS_ERR(i2c->clk_dma)) {
1192ce38815dSXudong Chen 		dev_err(&pdev->dev, "cannot get dma clock\n");
1193ce38815dSXudong Chen 		return PTR_ERR(i2c->clk_dma);
1194ce38815dSXudong Chen 	}
1195ce38815dSXudong Chen 
1196cad6dc5dSQii Wang 	i2c->clk_arb = devm_clk_get(&pdev->dev, "arb");
1197cad6dc5dSQii Wang 	if (IS_ERR(i2c->clk_arb))
1198cad6dc5dSQii Wang 		i2c->clk_arb = NULL;
1199cad6dc5dSQii Wang 
1200ce38815dSXudong Chen 	clk = i2c->clk_main;
1201ce38815dSXudong Chen 	if (i2c->have_pmic) {
1202ce38815dSXudong Chen 		i2c->clk_pmic = devm_clk_get(&pdev->dev, "pmic");
1203ce38815dSXudong Chen 		if (IS_ERR(i2c->clk_pmic)) {
1204ce38815dSXudong Chen 			dev_err(&pdev->dev, "cannot get pmic clock\n");
1205ce38815dSXudong Chen 			return PTR_ERR(i2c->clk_pmic);
1206ce38815dSXudong Chen 		}
1207ce38815dSXudong Chen 		clk = i2c->clk_pmic;
1208ce38815dSXudong Chen 	}
1209ce38815dSXudong Chen 
1210ce38815dSXudong Chen 	strlcpy(i2c->adap.name, I2C_DRV_NAME, sizeof(i2c->adap.name));
1211ce38815dSXudong Chen 
1212f2326401SJun Gao 	ret = mtk_i2c_set_speed(i2c, clk_get_rate(clk));
1213ce38815dSXudong Chen 	if (ret) {
1214ce38815dSXudong Chen 		dev_err(&pdev->dev, "Failed to set the speed.\n");
1215ce38815dSXudong Chen 		return -EINVAL;
1216ce38815dSXudong Chen 	}
1217ce38815dSXudong Chen 
1218f4f4fed6SLiguo Zhang 	if (i2c->dev_comp->support_33bits) {
1219f4f4fed6SLiguo Zhang 		ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(33));
1220f4f4fed6SLiguo Zhang 		if (ret) {
1221f4f4fed6SLiguo Zhang 			dev_err(&pdev->dev, "dma_set_mask return error.\n");
1222f4f4fed6SLiguo Zhang 			return ret;
1223f4f4fed6SLiguo Zhang 		}
1224f4f4fed6SLiguo Zhang 	}
1225f4f4fed6SLiguo Zhang 
1226ce38815dSXudong Chen 	ret = mtk_i2c_clock_enable(i2c);
1227ce38815dSXudong Chen 	if (ret) {
1228ce38815dSXudong Chen 		dev_err(&pdev->dev, "clock enable failed!\n");
1229ce38815dSXudong Chen 		return ret;
1230ce38815dSXudong Chen 	}
1231ce38815dSXudong Chen 	mtk_i2c_init_hw(i2c);
1232ce38815dSXudong Chen 	mtk_i2c_clock_disable(i2c);
1233ce38815dSXudong Chen 
1234ce38815dSXudong Chen 	ret = devm_request_irq(&pdev->dev, irq, mtk_i2c_irq,
1235ce38815dSXudong Chen 			       IRQF_TRIGGER_NONE, I2C_DRV_NAME, i2c);
1236ce38815dSXudong Chen 	if (ret < 0) {
1237ce38815dSXudong Chen 		dev_err(&pdev->dev,
1238ce38815dSXudong Chen 			"Request I2C IRQ %d fail\n", irq);
1239ce38815dSXudong Chen 		return ret;
1240ce38815dSXudong Chen 	}
1241ce38815dSXudong Chen 
1242ce38815dSXudong Chen 	i2c_set_adapdata(&i2c->adap, i2c);
1243ce38815dSXudong Chen 	ret = i2c_add_adapter(&i2c->adap);
1244ea734404SWolfram Sang 	if (ret)
1245ce38815dSXudong Chen 		return ret;
1246ce38815dSXudong Chen 
1247ce38815dSXudong Chen 	platform_set_drvdata(pdev, i2c);
1248ce38815dSXudong Chen 
1249ce38815dSXudong Chen 	return 0;
1250ce38815dSXudong Chen }
1251ce38815dSXudong Chen 
1252ce38815dSXudong Chen static int mtk_i2c_remove(struct platform_device *pdev)
1253ce38815dSXudong Chen {
1254ce38815dSXudong Chen 	struct mtk_i2c *i2c = platform_get_drvdata(pdev);
1255ce38815dSXudong Chen 
1256ce38815dSXudong Chen 	i2c_del_adapter(&i2c->adap);
1257ce38815dSXudong Chen 
1258ce38815dSXudong Chen 	return 0;
1259ce38815dSXudong Chen }
1260ce38815dSXudong Chen 
126109027e08SLiguo Zhang #ifdef CONFIG_PM_SLEEP
126209027e08SLiguo Zhang static int mtk_i2c_resume(struct device *dev)
126309027e08SLiguo Zhang {
1264f6762cedSJun Gao 	int ret;
126509027e08SLiguo Zhang 	struct mtk_i2c *i2c = dev_get_drvdata(dev);
126609027e08SLiguo Zhang 
1267f6762cedSJun Gao 	ret = mtk_i2c_clock_enable(i2c);
1268f6762cedSJun Gao 	if (ret) {
1269f6762cedSJun Gao 		dev_err(dev, "clock enable failed!\n");
1270f6762cedSJun Gao 		return ret;
1271f6762cedSJun Gao 	}
1272f6762cedSJun Gao 
127309027e08SLiguo Zhang 	mtk_i2c_init_hw(i2c);
127409027e08SLiguo Zhang 
1275f6762cedSJun Gao 	mtk_i2c_clock_disable(i2c);
1276f6762cedSJun Gao 
127709027e08SLiguo Zhang 	return 0;
127809027e08SLiguo Zhang }
127909027e08SLiguo Zhang #endif
128009027e08SLiguo Zhang 
128109027e08SLiguo Zhang static const struct dev_pm_ops mtk_i2c_pm = {
128209027e08SLiguo Zhang 	SET_SYSTEM_SLEEP_PM_OPS(NULL, mtk_i2c_resume)
128309027e08SLiguo Zhang };
128409027e08SLiguo Zhang 
1285ce38815dSXudong Chen static struct platform_driver mtk_i2c_driver = {
1286ce38815dSXudong Chen 	.probe = mtk_i2c_probe,
1287ce38815dSXudong Chen 	.remove = mtk_i2c_remove,
1288ce38815dSXudong Chen 	.driver = {
1289ce38815dSXudong Chen 		.name = I2C_DRV_NAME,
129009027e08SLiguo Zhang 		.pm = &mtk_i2c_pm,
1291ce38815dSXudong Chen 		.of_match_table = of_match_ptr(mtk_i2c_of_match),
1292ce38815dSXudong Chen 	},
1293ce38815dSXudong Chen };
1294ce38815dSXudong Chen 
1295ce38815dSXudong Chen module_platform_driver(mtk_i2c_driver);
1296ce38815dSXudong Chen 
1297ce38815dSXudong Chen MODULE_LICENSE("GPL v2");
1298ce38815dSXudong Chen MODULE_DESCRIPTION("MediaTek I2C Bus Driver");
1299ce38815dSXudong Chen MODULE_AUTHOR("Xudong Chen <xudong.chen@mediatek.com>");
1300