blob: d2cbe2a891b63c6639f4198fba9194fc565e990e [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (c) 2014 MediaTek Inc.
3 * Author: Xudong.chen <xudong.chen@mediatek.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/slab.h>
18#include <linux/i2c.h>
19#include <linux/init.h>
20#include <linux/interrupt.h>
21#include <linux/sched.h>
22#include <linux/delay.h>
23#include <linux/errno.h>
24#include <linux/err.h>
25#include <linux/device.h>
26#include <linux/platform_device.h>
27#include <linux/wait.h>
28#include <linux/mm.h>
29#include <linux/dma-mapping.h>
30#include <linux/scatterlist.h>
31#include <linux/io.h>
32#include <linux/of_address.h>
33#include <linux/of_irq.h>
34#include <linux/clk.h>
35#include <linux/clk-provider.h>
36
37#if 0
38#include <mtk_cpufreq_hybrid.h>
39#endif
40#ifdef CONFIG_MTK_GPU_SPM_DVFS_SUPPORT
41#include <mtk_kbase_spm.h>
42#endif
43#ifdef CONFIG_MTK_TINYSYS_SCP_SUPPORT
44#include <scp_helper.h>
45#endif
46#include "mtk_secure_api.h"
47#include "i2c-mtk.h"
48
49static struct i2c_dma_info g_dma_regs[I2C_MAX_CHANNEL];
50static struct mt_i2c *g_mt_i2c[I2C_MAX_CHANNEL];
51static struct mtk_i2c_compatible i2c_common_compat;
52
53
54static inline void _i2c_writew(u16 value, struct mt_i2c *i2c, u16 offset)
55{
56 writew(value, i2c->base + offset);
57}
58
59static inline u16 _i2c_readw(struct mt_i2c *i2c, u16 offset)
60{
61 return readw(i2c->base + offset);
62}
63
64#define raw_i2c_writew(val, i2c, ch_ofs, ofs) \
65 do { \
66 if (((i2c)->dev_comp->ver != 0x2) || (V2_##ofs != 0xfff)) \
67 _i2c_writew(val, i2c, ch_ofs + \
68 (((i2c)->dev_comp->ver == 0x2) ? \
69 (V2_##ofs) : ofs)); \
70 } while (0)
71
72#define raw_i2c_readw(i2c, ch_ofs, ofs) \
73 ({ \
74 u16 value = 0; \
75 if (((i2c)->dev_comp->ver != 0x2) || (V2_##ofs != 0xfff)) \
76 value = _i2c_readw(i2c, ch_ofs + \
77 (((i2c)->dev_comp->ver == 0x2) ? \
78 (V2_##ofs) : ofs)); \
79 value; \
80 })
81
82#define i2c_writew(val, i2c, ofs) raw_i2c_writew(val, i2c, i2c->ch_offset, ofs)
83
84#define i2c_readw(i2c, ofs) raw_i2c_readw(i2c, i2c->ch_offset, ofs)
85
86#define i2c_writew_shadow(val, i2c, ofs) raw_i2c_writew(val, i2c, 0, ofs)
87
88#define i2c_readw_shadow(i2c, ofs) raw_i2c_readw(i2c, 0, ofs)
89
90void __iomem *cg_base;
91
92s32 map_cg_regs(struct mt_i2c *i2c)
93{
94 struct device_node *cg_node;
95 int ret = -1;
96
97 if (!cg_base && i2c->dev_comp->clk_compatible[0]) {
98 cg_node = of_find_compatible_node(NULL, NULL,
99 i2c->dev_comp->clk_compatible);
100 if (!cg_node) {
101 pr_info("Cannot find cg_node\n");
102 return -ENODEV;
103 }
104 cg_base = of_iomap(cg_node, 0);
105 if (!cg_base) {
106 pr_info("cg_base iomap failed\n");
107 return -ENOMEM;
108 }
109 ret = 0;
110 }
111
112 return ret;
113}
114
115void dump_cg_regs(struct mt_i2c *i2c)
116{
117 u32 clk_sta_val, clk_sta_offs, cg_bit;
118 u32 clk_sel_val, arbit_val, clk_sel_offs, arbit_offs;
119
120 if (!cg_base || i2c->id >= I2C_MAX_CHANNEL) {
121 pr_info("cg_base %p, i2c id = %d\n", cg_base, i2c->id);
122 return;
123 }
124
125 clk_sta_offs = i2c->clk_sta_offset;
126 clk_sta_val = readl(cg_base + clk_sta_offs);
127 cg_bit = i2c->cg_bit;
128
129 pr_info("[I2C] cg regs dump:\n"
130 "name %s, offset 0x%x: value = 0x%08x, bit %d, clock %s\n",
131 i2c->dev_comp->clk_compatible,
132 clk_sta_offs, clk_sta_val, cg_bit,
133 clk_sta_val & (1 << cg_bit) ? "off":"on");
134
135 /* Dump clk source & arbit bit */
136 clk_sel_offs = i2c->dev_comp->clk_sel_offset;
137 clk_sel_val = readl(cg_base + clk_sel_offs);
138 arbit_offs = i2c->dev_comp->arbit_offset;
139 arbit_val = readl(cg_base + arbit_offs);
140 pr_info("[I2C] clk src & arbit dump:\n"
141 "name: %s, clk_sel_offs: 0x%x, val=0x%08x, arbit_offs: 0x%x, val=0x%08x\n",
142 i2c->dev_comp->clk_compatible,
143 clk_sel_offs, clk_sel_val,
144 arbit_offs, arbit_val);
145}
146
147void __iomem *dma_base;
148
149s32 map_dma_regs(void)
150{
151 struct device_node *dma_node;
152
153 dma_node = of_find_compatible_node(NULL, NULL, "mediatek,ap_dma");
154 if (!dma_node) {
155 pr_info("Cannot find dma_node\n");
156 return -ENODEV;
157 }
158 dma_base = of_iomap(dma_node, 0);
159 if (!dma_base) {
160 pr_info("dma_base iomap failed\n");
161 return -ENOMEM;
162 }
163 return 0;
164}
165
166void dump_dma_regs(void)
167{
168 int status;
169 int i;
170
171 if (!dma_base) {
172 pr_info("dma_base NULL\n");
173 return;
174 }
175
176 status = readl(dma_base + 8);
177 pr_info("DMA RUNNING STATUS : 0x%x .\n", status);
178 for (i = 0; i < 21 ; i++) {
179 if (status & (0x1 << i))
180 pr_info("DMA[%d] CONTROL REG : 0x%x, DEBUG : 0x%x .\n",
181 i,
182 readl(dma_base + 0x80 + 0x80 * i + 0x18),
183 readl(dma_base + 0x80 + 0x80 * i + 0x50));
184 }
185
186}
187
188static inline void i2c_writel_dma(u32 value, struct mt_i2c *i2c, u8 offset)
189{
190 writel(value, i2c->pdmabase + i2c->ch_offset_dma + offset);
191}
192
193static inline u32 i2c_readl_dma(struct mt_i2c *i2c, u8 offset)
194{
195 return readl(i2c->pdmabase + i2c->ch_offset_dma + offset);
196}
197
198static void record_i2c_dma_info(struct mt_i2c *i2c)
199{
200 g_dma_regs[i2c->id].base =
201 (unsigned long)i2c->pdmabase;
202 g_dma_regs[i2c->id].int_flag =
203 i2c_readl_dma(i2c, OFFSET_INT_FLAG);
204 g_dma_regs[i2c->id].int_en =
205 i2c_readl_dma(i2c, OFFSET_INT_EN);
206 g_dma_regs[i2c->id].en =
207 i2c_readl_dma(i2c, OFFSET_EN);
208 g_dma_regs[i2c->id].rst =
209 i2c_readl_dma(i2c, OFFSET_RST);
210 g_dma_regs[i2c->id].stop =
211 i2c_readl_dma(i2c, OFFSET_STOP);
212 g_dma_regs[i2c->id].flush =
213 i2c_readl_dma(i2c, OFFSET_FLUSH);
214 g_dma_regs[i2c->id].con =
215 i2c_readl_dma(i2c, OFFSET_CON);
216 g_dma_regs[i2c->id].tx_mem_addr =
217 i2c_readl_dma(i2c, OFFSET_TX_MEM_ADDR);
218 g_dma_regs[i2c->id].rx_mem_addr =
219 i2c_readl_dma(i2c, OFFSET_RX_MEM_ADDR);
220 g_dma_regs[i2c->id].tx_len =
221 i2c_readl_dma(i2c, OFFSET_TX_LEN);
222 g_dma_regs[i2c->id].rx_len =
223 i2c_readl_dma(i2c, OFFSET_RX_LEN);
224 g_dma_regs[i2c->id].int_buf_size =
225 i2c_readl_dma(i2c, OFFSET_INT_BUF_SIZE);
226 g_dma_regs[i2c->id].debug_sta =
227 i2c_readl_dma(i2c, OFFSET_DEBUG_STA);
228 g_dma_regs[i2c->id].tx_mem_addr2 =
229 i2c_readl_dma(i2c, OFFSET_TX_MEM_ADDR2);
230 g_dma_regs[i2c->id].rx_mem_addr2 =
231 i2c_readl_dma(i2c, OFFSET_RX_MEM_ADDR2);
232}
233
234static void record_i2c_info(struct mt_i2c *i2c, int tmo)
235{
236 int idx = i2c->rec_idx;
237
238 i2c->rec_info[idx].slave_addr = i2c_readw(i2c, OFFSET_SLAVE_ADDR);
239 i2c->rec_info[idx].intr_stat = i2c->irq_stat;
240 i2c->rec_info[idx].control = i2c_readw(i2c, OFFSET_CONTROL);
241 i2c->rec_info[idx].fifo_stat = i2c_readw(i2c, OFFSET_FIFO_STAT);
242 i2c->rec_info[idx].debug_stat = i2c_readw(i2c, OFFSET_DEBUGSTAT);
243 i2c->rec_info[idx].tmo = tmo;
244 i2c->rec_info[idx].end_time = sched_clock();
245
246 i2c->rec_idx++;
247 if (i2c->rec_idx == I2C_RECORD_LEN)
248 i2c->rec_idx = 0;
249}
250
251static void dump_i2c_info(struct mt_i2c *i2c)
252{
253 int i;
254 int idx = i2c->rec_idx;
255 unsigned long long endtime;
256 unsigned long ns;
257
258 if (i2c->buffermode) /* no i2c history @ buffermode */
259 return;
260
261 dev_info(i2c->dev, "last transfer info:\n");
262
263 for (i = 0; i < I2C_RECORD_LEN; i++) {
264 if (idx == 0)
265 idx = I2C_RECORD_LEN;
266 idx--;
267 endtime = i2c->rec_info[idx].end_time;
268 ns = do_div(endtime, 1000000000);
269 dev_info(i2c->dev,
270 "[%02d] [%5lu.%06lu] SLAVE_ADDR=%x,INTR_STAT=%x,CONTROL=%x,FIFO_STAT=%x,DEBUGSTAT=%x, tmo=%d\n",
271 i,
272 (unsigned long)endtime,
273 ns/1000,
274 i2c->rec_info[idx].slave_addr,
275 i2c->rec_info[idx].intr_stat,
276 i2c->rec_info[idx].control,
277 i2c->rec_info[idx].fifo_stat,
278 i2c->rec_info[idx].debug_stat,
279 i2c->rec_info[idx].tmo
280 );
281 }
282}
283
284static int mt_i2c_clock_enable(struct mt_i2c *i2c)
285{
286#if !defined(CONFIG_MT_I2C_FPGA_ENABLE)
287 int ret = 0;
288
289 ret = clk_prepare_enable(i2c->clk_dma);
290 if (ret)
291 return ret;
292
293 if (i2c->clk_pal != NULL) {
294 ret = clk_prepare_enable(i2c->clk_pal);
295 if (ret)
296 goto err_main;
297 }
298
299 if (i2c->clk_arb != NULL) {
300 ret = clk_prepare_enable(i2c->clk_arb);
301 if (ret)
302 return ret;
303 }
304 ret = clk_prepare_enable(i2c->clk_main);
305 if (ret)
306 goto err_main;
307
308 if (i2c->have_pmic) {
309 ret = clk_prepare_enable(i2c->clk_pmic);
310 if (ret)
311 goto err_pmic;
312 }
313 spin_lock(&i2c->cg_lock);
314 if (i2c->suspended)
315 ret = -EIO;
316 else
317 i2c->cg_cnt++;
318 spin_unlock(&i2c->cg_lock);
319 if (ret) {
320 dev_info(i2c->dev, "err, access at suspend no irq stage\n");
321 goto err_cg;
322 }
323
324 return 0;
325
326err_cg:
327 if (i2c->have_pmic)
328 clk_disable_unprepare(i2c->clk_pmic);
329err_pmic:
330 clk_disable_unprepare(i2c->clk_main);
331err_main:
332 if (i2c->clk_arb)
333 clk_disable_unprepare(i2c->clk_arb);
334 clk_disable_unprepare(i2c->clk_dma);
335 return ret;
336#else
337 return 0;
338#endif
339}
340
341static void mt_i2c_clock_disable(struct mt_i2c *i2c)
342{
343#if !defined(CONFIG_MT_I2C_FPGA_ENABLE)
344 if (i2c->have_pmic)
345 clk_disable_unprepare(i2c->clk_pmic);
346
347 clk_disable_unprepare(i2c->clk_main);
348 if (i2c->clk_pal != NULL)
349 clk_disable_unprepare(i2c->clk_pal);
350
351 if (i2c->clk_arb != NULL)
352 clk_disable_unprepare(i2c->clk_arb);
353
354 clk_disable_unprepare(i2c->clk_dma);
355 spin_lock(&i2c->cg_lock);
356 i2c->cg_cnt--;
357 spin_unlock(&i2c->cg_lock);
358#endif
359}
360
361static int i2c_get_semaphore(struct mt_i2c *i2c)
362{
363#if 0
364 int count = 100;
365#endif
366 #if 0
367 if (i2c->appm) {
368 if (cpuhvfs_get_dvfsp_semaphore(SEMA_I2C_DRV) != 0) {
369 dev_info(i2c->dev, "sema time out 2ms\n");
370 if (cpuhvfs_get_dvfsp_semaphore(SEMA_I2C_DRV) != 0) {
371 dev_info(i2c->dev, "sema time out 4ms\n");
372 i2c_dump_info(i2c);
373 WARN_ON(1);
374 return -EBUSY;
375 }
376 }
377 }
378 #endif
379
380#ifdef CONFIG_MTK_GPU_SPM_DVFS_SUPPORT
381 if (i2c->gpupm) {
382 if (dvfs_gpu_pm_spin_lock_for_vgpu() != 0) {
383 dev_info(i2c->dev, "sema time out.\n");
384 return -EBUSY;
385 }
386 }
387#endif
388
389 switch (i2c->id) {
390#if 0
391 case 0:
392 while ((get_scp_semaphore(SEMAPHORE_I2C0) != 1) && count > 0)
393 count--;
394 return count > 0 ? 0 : -EBUSY;
395 case 1:
396 while ((get_scp_semaphore(SEMAPHORE_I2C1) != 1) && count > 0)
397 count--;
398 return count > 0 ? 0 : -EBUSY;
399#endif
400 default:
401 return 0;
402 }
403}
404
405static int i2c_release_semaphore(struct mt_i2c *i2c)
406{
407 #if 0
408 if (i2c->appm)
409 cpuhvfs_release_dvfsp_semaphore(SEMA_I2C_DRV);
410 #endif
411#ifdef CONFIG_MTK_GPU_SPM_DVFS_SUPPORT
412 if (i2c->gpupm)
413 dvfs_gpu_pm_spin_unlock_for_vgpu();
414#endif
415
416 switch (i2c->id) {
417#if 0
418 case 0:
419 return release_scp_semaphore(SEMAPHORE_I2C0) == 1 ? 0 : -EBUSY;
420 case 1:
421 return release_scp_semaphore(SEMAPHORE_I2C1) == 1 ? 0 : -EBUSY;
422#endif
423 default:
424 return 0;
425 }
426}
427static void free_i2c_dma_bufs(struct mt_i2c *i2c)
428{
429 dma_free_coherent(i2c->adap.dev.parent, PAGE_SIZE,
430 i2c->dma_buf.vaddr, i2c->dma_buf.paddr);
431}
432
433static inline void mt_i2c_wait_done(struct mt_i2c *i2c, u16 ch_off)
434{
435 u16 start, tmo;
436
437 start = raw_i2c_readw(i2c, ch_off, OFFSET_START) & I2C_TRANSAC_START;
438 if (start) {
439 dev_info(i2c->dev, "wait transfer done before cg off.\n");
440
441 tmo = 100;
442 do {
443 msleep(20);
444 start = raw_i2c_readw(i2c, ch_off, OFFSET_START) &
445 I2C_TRANSAC_START;
446 tmo--;
447 } while (start && tmo);
448
449 if (start && !tmo) {
450 dev_info(i2c->dev, "wait transfer timeout.\n");
451 i2c_dump_info(i2c);
452 }
453 }
454}
455
456static inline void mt_i2c_init_hw(struct mt_i2c *i2c)
457{
458 /* clear interrupt status */
459 i2c_writew_shadow(0, i2c, OFFSET_INTR_MASK);
460 i2c->irq_stat = i2c_readw_shadow(i2c, OFFSET_INTR_STAT);
461 i2c_writew_shadow(i2c->irq_stat, i2c, OFFSET_INTR_STAT);
462
463 i2c_writew_shadow(I2C_SOFT_RST, i2c, OFFSET_SOFTRESET);
464 /* Set ioconfig */
465 if (i2c->use_push_pull)
466 i2c_writew_shadow(I2C_IO_CONFIG_PUSH_PULL,
467 i2c, OFFSET_IO_CONFIG);
468 else
469 i2c_writew_shadow(I2C_IO_CONFIG_OPEN_DRAIN,
470 i2c, OFFSET_IO_CONFIG);
471 if (i2c->have_dcm)
472 i2c_writew_shadow(I2C_DCM_DISABLE, i2c, OFFSET_DCM_EN);
473
474 if (i2c->dev_comp->ver != 0x2)
475 i2c_writew_shadow(i2c->timing_reg, i2c, OFFSET_TIMING);
476 else
477 i2c_writew_shadow(i2c->timing_reg | I2C_TIMEOUT_EN, i2c,
478 OFFSET_TIMING);
479
480 if (i2c->dev_comp->set_ltiming)
481 i2c_writew_shadow(i2c->ltiming_reg, i2c, OFFSET_LTIMING);
482 i2c_writew_shadow(i2c->high_speed_reg, i2c, OFFSET_HS);
483 /* DMA warm reset, and waits for EN to become 0 */
484 i2c_writel_dma(I2C_DMA_WARM_RST, i2c, OFFSET_RST);
485 udelay(5);
486 if (i2c_readl_dma(i2c, OFFSET_EN) != 0) {
487 dev_info(i2c->dev, "DMA bus hang .\n");
488 dump_dma_regs();
489 WARN_ON(1);
490 }
491}
492
493/* calculate i2c port speed */
494static int mtk_i2c_calculate_speed(struct mt_i2c *i2c,
495 unsigned int clk_src_in_hz,
496 unsigned int speed_hz,
497 unsigned int *timing_step_cnt,
498 unsigned int *timing_sample_cnt)
499{
500 unsigned int khz;
501 unsigned int step_cnt;
502 unsigned int sample_cnt;
503 unsigned int sclk;
504 unsigned int hclk;
505 unsigned int max_step_cnt;
506 unsigned int sample_div = MAX_SAMPLE_CNT_DIV;
507 unsigned int step_div;
508 unsigned int min_div;
509 unsigned int best_mul;
510 unsigned int cnt_mul;
511
512 if (speed_hz > MAX_HS_MODE_SPEED) {
513 if (i2c->dev_comp->check_max_freq)
514 return -EINVAL;
515 max_step_cnt = MAX_HS_STEP_CNT_DIV;
516 } else if (speed_hz > MAX_FS_MODE_SPEED) {
517 max_step_cnt = MAX_HS_STEP_CNT_DIV;
518 } else {
519 max_step_cnt = MAX_STEP_CNT_DIV;
520 }
521 step_div = max_step_cnt;
522
523 /* Find the best combination */
524 khz = speed_hz / 1000;
525 hclk = clk_src_in_hz / 1000;
526 min_div = ((hclk >> 1) + khz - 1) / khz;
527 best_mul = MAX_SAMPLE_CNT_DIV * max_step_cnt;
528 for (sample_cnt = 1; sample_cnt <= MAX_SAMPLE_CNT_DIV; sample_cnt++) {
529 step_cnt = (min_div + sample_cnt - 1) / sample_cnt;
530 cnt_mul = step_cnt * sample_cnt;
531 if (step_cnt > max_step_cnt)
532 continue;
533 if (cnt_mul < best_mul) {
534 best_mul = cnt_mul;
535 sample_div = sample_cnt;
536 step_div = step_cnt;
537 if (best_mul == min_div)
538 break;
539 }
540 }
541 sample_cnt = sample_div;
542 step_cnt = step_div;
543 sclk = hclk / (2 * sample_cnt * step_cnt);
544 if (sclk > khz) {
545 dev_dbg(i2c->dev, "%s mode: unsupported speed (%ldkhz)\n",
546 (speed_hz > MAX_FS_MODE_SPEED) ? "HS" : "ST/FT",
547 (long int)khz);
548 return -ENOTSUPP;
549 }
550
551 if (i2c->dev_comp->cnt_constraint) {
552 if (--sample_cnt) {/* --sample_cnt = 0, setp_cnt needn't -1 */
553 step_cnt--;
554 }
555 } else {
556 sample_cnt--;
557 step_cnt--;
558 }
559
560 *timing_step_cnt = step_cnt;
561 *timing_sample_cnt = sample_cnt;
562
563 return 0;
564}
565
566static int i2c_set_speed(struct mt_i2c *i2c, unsigned int clk_src_in_hz)
567{
568 int ret;
569 unsigned int step_cnt;
570 unsigned int sample_cnt;
571 unsigned int l_step_cnt;
572 unsigned int l_sample_cnt;
573 unsigned int speed_hz;
574 unsigned int duty = HALF_DUTY_CYCLE;
575
576 if (i2c->ext_data.isEnable && i2c->ext_data.timing)
577 speed_hz = i2c->ext_data.timing;
578 else
579 speed_hz = i2c->speed_hz;
580
581 if (speed_hz > MAX_FS_PLUS_MODE_SPEED && !i2c->hs_only) {
582 /* Set the hign speed mode register */
583 ret = mtk_i2c_calculate_speed(i2c, clk_src_in_hz,
584 MAX_FS_MODE_SPEED, &l_step_cnt, &l_sample_cnt);
585 if (ret < 0)
586 return ret;
587
588 ret = mtk_i2c_calculate_speed(i2c, clk_src_in_hz,
589 speed_hz, &step_cnt, &sample_cnt);
590 if (ret < 0)
591 return ret;
592
593 i2c->high_speed_reg = I2C_HS_HOLD_TIME |
594 I2C_TIME_DEFAULT_VALUE | I2C_HS_SPEED |
595 (sample_cnt & I2C_TIMING_SAMPLE_COUNT_MASK) << 12 |
596 ((step_cnt - 1) & I2C_TIMING_SAMPLE_COUNT_MASK) << 8;
597
598 i2c->timing_reg =
599 (l_sample_cnt & I2C_TIMING_SAMPLE_COUNT_MASK) << 8 |
600 (l_step_cnt & I2C_TIMING_STEP_DIV_MASK) << 0;
601
602 if (i2c->dev_comp->set_ltiming) {
603 i2c->ltiming_reg = I2C_HS_HOLD_SEL | (l_sample_cnt << 6)
604 | (l_step_cnt << 0) |
605 (sample_cnt &
606 I2C_TIMING_SAMPLE_COUNT_MASK) << 12 |
607 ((step_cnt + 1) &
608 I2C_TIMING_SAMPLE_COUNT_MASK) << 9;
609 }
610 } else {
611 if (speed_hz > I2C_DEFAUT_SPEED
612 && speed_hz <= MAX_FS_MODE_SPEED
613 && i2c->dev_comp->set_ltiming)
614 duty = DUTY_CYCLE;
615
616 ret = mtk_i2c_calculate_speed(i2c, clk_src_in_hz,
617 (speed_hz * 50 / duty), &step_cnt, &sample_cnt);
618 if (ret < 0)
619 return ret;
620
621 i2c->timing_reg =
622 (sample_cnt & I2C_TIMING_SAMPLE_COUNT_MASK) << 8 |
623 (step_cnt & I2C_TIMING_STEP_DIV_MASK) << 0;
624
625 if (i2c->dev_comp->set_ltiming) {
626 ret = mtk_i2c_calculate_speed(i2c, clk_src_in_hz,
627 (speed_hz * 50 / (100 - duty)),
628 &l_step_cnt, &l_sample_cnt);
629 if (ret < 0)
630 return ret;
631
632 i2c->ltiming_reg =
633 (l_sample_cnt &
634 I2C_TIMING_SAMPLE_COUNT_MASK) << 6 |
635 (l_step_cnt & I2C_TIMING_STEP_DIV_MASK) << 0;
636 }
637 /* Disable the high speed transaction */
638 i2c->high_speed_reg = I2C_TIME_CLR_VALUE;
639 }
640
641 return 0;
642}
643
644
645#ifdef I2C_DEBUG_FS
646void i2c_dump_info1(struct mt_i2c *i2c)
647{
648 if (i2c->ext_data.isEnable && i2c->ext_data.timing)
649 dev_info(i2c->dev, "I2C structure:\nspeed %d\n",
650 i2c->ext_data.timing);
651 else
652 dev_info(i2c->dev, "I2C structure:\nspeed %d\n",
653 i2c->speed_hz);
654 dev_info(i2c->dev, "I2C structure:\nOp %x\n", i2c->op);
655 dev_info(i2c->dev,
656 "I2C structure:\nData_size %x\nIrq_stat %x\nTrans_stop %d\n",
657 i2c->msg_len, i2c->irq_stat, i2c->trans_stop);
658 dev_info(i2c->dev, "base address %p\n", i2c->base);
659 dev_info(i2c->dev,
660 "I2C register:\nSLAVE_ADDR %x\nINTR_MASK %x\n",
661 (i2c_readw(i2c, OFFSET_SLAVE_ADDR)),
662 (i2c_readw(i2c, OFFSET_INTR_MASK)));
663 dev_info(i2c->dev,
664 "I2C register:\nINTR_STAT %x\nCONTROL %x\n",
665 (i2c_readw(i2c, OFFSET_INTR_STAT)),
666 (i2c_readw(i2c, OFFSET_CONTROL)));
667 dev_info(i2c->dev,
668 "I2C register:\nTRANSFER_LEN %x\nTRANSAC_LEN %x\n",
669 (i2c_readw(i2c, OFFSET_TRANSFER_LEN)),
670 (i2c_readw(i2c, OFFSET_TRANSAC_LEN)));
671 dev_info(i2c->dev,
672 "I2C register:\nDELAY_LEN %x\nTIMING %x\n",
673 (i2c_readw(i2c, OFFSET_DELAY_LEN)),
674 (i2c_readw(i2c, OFFSET_TIMING)));
675 dev_info(i2c->dev,
676 "I2C register:\nSTART %x\nFIFO_STAT %x\n",
677 (i2c_readw(i2c, OFFSET_START)),
678 (i2c_readw(i2c, OFFSET_FIFO_STAT)));
679 dev_info(i2c->dev,
680 "I2C register:\nIO_CONFIG %x\nHS %x\n",
681 (i2c_readw(i2c, OFFSET_IO_CONFIG)),
682 (i2c_readw(i2c, OFFSET_HS)));
683 dev_info(i2c->dev,
684 "I2C register:\nDEBUGSTAT %x\nEXT_CONF %x\nPATH_DIR %x\n",
685 (i2c_readw(i2c, OFFSET_DEBUGSTAT)),
686 (i2c_readw(i2c, OFFSET_EXT_CONF)),
687 (i2c_readw(i2c, OFFSET_PATH_DIR)));
688}
689
690void i2c_dump_info(struct mt_i2c *i2c)
691{
692 /* I2CFUC(); */
693 /* int val=0; */
694 pr_info_ratelimited("%s: +++++++++++++++++++\n", __func__);
695 pr_info_ratelimited("I2C structure:\n"
696 I2CTAG "Clk=%ld,Id=%d,Op=0x%x,Irq_stat=0x%x,Total_len=0x%x\n"
697 I2CTAG "Trans_len=0x%x,Trans_num=0x%x,Trans_auxlen=0x%x,\n"
698 I2CTAG "speed=%d,Trans_stop=%u,cg_cnt=%d,hs_only=%d,\n"
699 I2CTAG "ch_offset=0x%x,ch_offset_default=0x%x\n",
700 i2c->main_clk, i2c->id, i2c->op, i2c->irq_stat, i2c->total_len,
701 i2c->msg_len, 1, i2c->msg_aux_len, i2c->speed_hz,
702 i2c->trans_stop, i2c->cg_cnt,
703 i2c->hs_only, i2c->ch_offset, i2c->ch_offset_default);
704
705 pr_info_ratelimited("base addr:0x%p\n", i2c->base);
706 pr_info_ratelimited("I2C register:\n"
707 I2CTAG "SLAVE_ADDR=0x%x,INTR_MASK=0x%x,INTR_STAT=0x%x,\n"
708 I2CTAG "CONTROL=0x%x,TRANSFER_LEN=0x%x,TRANSAC_LEN=0x%x,\n"
709 I2CTAG "DELAY_LEN=0x%x,TIMING=0x%x,LTIMING=0x%x,START=0x%x\n"
710 I2CTAG "FIFO_STAT=0x%x,IO_CONFIG=0x%x,HS=0x%x\n"
711 I2CTAG "DCM_EN=0x%x,DEBUGSTAT=0x%x,EXT_CONF=0x%x\n"
712 I2CTAG "TRANSFER_LEN_AUX=0x%x,OFFSET_DMA_FSM_DEBUG=0x%x\n"
713 I2CTAG "OFFSET_MCU_INTR=0x%x,OFFSET_CLOCK_DIV=0x%x\n",
714 (i2c_readw(i2c, OFFSET_SLAVE_ADDR)),
715 (i2c_readw(i2c, OFFSET_INTR_MASK)),
716 (i2c_readw(i2c, OFFSET_INTR_STAT)),
717 (i2c_readw(i2c, OFFSET_CONTROL)),
718 (i2c_readw(i2c, OFFSET_TRANSFER_LEN)),
719 (i2c_readw(i2c, OFFSET_TRANSAC_LEN)),
720 (i2c_readw(i2c, OFFSET_DELAY_LEN)),
721 (i2c_readw(i2c, OFFSET_TIMING)),
722 (i2c_readw(i2c, OFFSET_LTIMING)),
723 (i2c_readw(i2c, OFFSET_START)),
724 (i2c_readw(i2c, OFFSET_FIFO_STAT)),
725 (i2c_readw(i2c, OFFSET_IO_CONFIG)),
726 (i2c_readw(i2c, OFFSET_HS)),
727 (i2c_readw(i2c, OFFSET_DCM_EN)),
728 (i2c_readw(i2c, OFFSET_DEBUGSTAT)),
729 (i2c_readw(i2c, OFFSET_EXT_CONF)),
730 (i2c_readw(i2c, OFFSET_TRANSFER_LEN_AUX)),
731 (i2c_readw(i2c, OFFSET_DMA_FSM_DEBUG)),
732 (i2c_readw(i2c, OFFSET_MCU_INTR)),
733 (i2c_readw(i2c, OFFSET_CLOCK_DIV)));
734
735 pr_info_ratelimited("before enable DMA register(0x%lx):\n"
736 I2CTAG "INT_FLAG=0x%x,INT_EN=0x%x,EN=0x%x,RST=0x%x,\n"
737 I2CTAG "STOP=0x%x,FLUSH=0x%x,CON=0x%x,\n"
738 I2CTAG "TX_MEM_ADDR=0x%x, RX_MEM_ADDR=0x%x\n"
739 I2CTAG "TX_LEN=0x%x,RX_LEN=0x%x,INT_BUF_SIZE=0x%x,\n"
740 I2CTAG "DEBUGSTA=0x%x,TX_MEM_ADDR2=0x%x, RX_MEM_ADDR2=0x%x\n",
741 g_dma_regs[i2c->id].base,
742 g_dma_regs[i2c->id].int_flag,
743 g_dma_regs[i2c->id].int_en,
744 g_dma_regs[i2c->id].en,
745 g_dma_regs[i2c->id].rst,
746 g_dma_regs[i2c->id].stop,
747 g_dma_regs[i2c->id].flush,
748 g_dma_regs[i2c->id].con,
749 g_dma_regs[i2c->id].tx_mem_addr,
750 g_dma_regs[i2c->id].rx_mem_addr,
751 g_dma_regs[i2c->id].tx_len,
752 g_dma_regs[i2c->id].rx_len,
753 g_dma_regs[i2c->id].int_buf_size, g_dma_regs[i2c->id].debug_sta,
754 g_dma_regs[i2c->id].tx_mem_addr2,
755 g_dma_regs[i2c->id].rx_mem_addr2);
756 pr_info_ratelimited("DMA register(0x%p):\n"
757 I2CTAG "INT_FLAG=0x%x,INT_EN=0x%x,EN=0x%x,RST=0x%x,\n"
758 I2CTAG "STOP=0x%x,FLUSH=0x%x,CON=0x%x,\n"
759 I2CTAG "TX_MEM_ADDR=0x%x, RX_MEM_ADDR=0x%x,\n"
760 I2CTAG "TX_LEN=0x%x,RX_LEN=x%x,INT_BUF_SIZE=0x%x,\n"
761 I2CTAG "DEBUGSTA=0x%x,TX_MEM_ADDR2=0x%x, RX_MEM_ADDR2=0x%x\n",
762 i2c->pdmabase,
763 (i2c_readl_dma(i2c, OFFSET_INT_FLAG)),
764 (i2c_readl_dma(i2c, OFFSET_INT_EN)),
765 (i2c_readl_dma(i2c, OFFSET_EN)),
766 (i2c_readl_dma(i2c, OFFSET_RST)),
767 (i2c_readl_dma(i2c, OFFSET_STOP)),
768 (i2c_readl_dma(i2c, OFFSET_FLUSH)),
769 (i2c_readl_dma(i2c, OFFSET_CON)),
770 (i2c_readl_dma(i2c, OFFSET_TX_MEM_ADDR)),
771 (i2c_readl_dma(i2c, OFFSET_RX_MEM_ADDR)),
772 (i2c_readl_dma(i2c, OFFSET_TX_LEN)),
773 (i2c_readl_dma(i2c, OFFSET_RX_LEN)),
774 (i2c_readl_dma(i2c, OFFSET_INT_BUF_SIZE)),
775 (i2c_readl_dma(i2c, OFFSET_DEBUG_STA)),
776 (i2c_readl_dma(i2c, OFFSET_TX_MEM_ADDR2)),
777 (i2c_readl_dma(i2c, OFFSET_RX_MEM_ADDR2)));
778 pr_info_ratelimited("%s: -----------------------\n", __func__);
779
780 dump_i2c_info(i2c);
781 if (i2c->ccu_offset) {
782 dev_info(i2c->dev, "I2C CCU register:\n"
783 I2CTAG "SLAVE_ADDR=0x%x,INTR_MASK=0x%x,\n"
784 I2CTAG "INTR_STAT=0x%x,CONTROL=0x%x,\n"
785 I2CTAG "TRANSFER_LEN=0x%x, TRANSAC_LEN=0x%x,DELAY_LEN=0x%x\n"
786 I2CTAG "TIMING=0x%x,LTIMING=0x%x,START=0x%x,FIFO_STAT=0x%x,\n"
787 I2CTAG "IO_CONFIG=0x%x,HS=0x%x,DCM_EN=0x%x,DEBUGSTAT=0x%x,\n"
788 I2CTAG "EXT_CONF=0x%x,TRANSFER_LEN_AUX=0x%x\n"
789 I2CTAG "OFFSET_DMA_FSM_DEBUG=0x%x,OFFSET_MCU_INTR=0x%x\n",
790 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_SLAVE_ADDR)),
791 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_INTR_MASK)),
792 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_INTR_STAT)),
793 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_CONTROL)),
794 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_TRANSFER_LEN)),
795 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_TRANSAC_LEN)),
796 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_DELAY_LEN)),
797 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_TIMING)),
798 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_LTIMING)),
799 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_START)),
800 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_FIFO_STAT)),
801 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_IO_CONFIG)),
802 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_HS)),
803 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_DCM_EN)),
804 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_DEBUGSTAT)),
805 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_EXT_CONF)),
806 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_TRANSFER_LEN_AUX)),
807 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_DMA_FSM_DEBUG)),
808 (raw_i2c_readw(i2c, i2c->ccu_offset, OFFSET_MCU_INTR)));
809 }
810}
811#else
812void i2c_dump_info(struct mt_i2c *i2c)
813{
814}
815#endif
816
817void i2c_gpio_dump_info(struct mt_i2c *i2c)
818{
819 if (i2c->gpiobase) {
820 dev_info(i2c->dev, "%s +++++++++++++++++++\n", __func__);
821 /* gpio_dump_regs_range(i2c->scl_gpio_id, i2c->sda_gpio_id); */
822 dev_info(i2c->dev, "I2C gpio structure:\n"
823 I2CTAG "EH_CFG=0x%x,PU_CFG=0x%x,RSEL_CFG=0x%x\n",
824 readl(i2c->gpiobase + i2c->offset_eh_cfg),
825 readl(i2c->gpiobase + i2c->offset_pu_cfg),
826 readl(i2c->gpiobase + i2c->offset_rsel_cfg));
827 } else
828 dev_info(i2c->dev, "i2c gpiobase is NULL\n");
829}
830
831void dump_i2c_status(int id)
832{
833 if (id >= I2C_MAX_CHANNEL) {
834 pr_info("error %s, id = %d\n", __func__, id);
835 return;
836 }
837
838 if (!g_mt_i2c[id]) {
839 pr_info("error %s, g_mt_i2c[%d] == NULL\n", __func__, id);
840 return;
841 }
842
843 dump_cg_regs(g_mt_i2c[id]);
844 (void)mt_i2c_clock_enable(g_mt_i2c[id]);
845 i2c_dump_info(g_mt_i2c[id]);
846 mt_i2c_clock_disable(g_mt_i2c[id]);
847}
848EXPORT_SYMBOL(dump_i2c_status);
849
850static int mt_i2c_do_transfer(struct mt_i2c *i2c)
851{
852 u16 addr_reg = 0;
853 u16 control_reg = 0;
854 u16 ioconfig_reg = 0;
855 u16 start_reg = 0;
856 u16 int_reg = 0;
857 int tmo = i2c->adap.timeout;
858 unsigned int speed_hz = 0;
859 bool isDMA = false;
860 int data_size = 0;
861 u8 *ptr;
862 int ret = 0;
863 /* u16 ch_offset; */
864
865 i2c->trans_stop = false;
866 i2c->irq_stat = 0;
867 if (i2c->total_len > 8 || i2c->msg_aux_len > 8)
868 isDMA = true;
869 if (i2c->ext_data.isEnable && i2c->ext_data.timing)
870 speed_hz = i2c->ext_data.timing;
871 else
872 speed_hz = i2c->speed_hz;
873 if (i2c->ext_data.is_ch_offset) {
874 i2c->ch_offset = i2c->ext_data.ch_offset;
875 i2c->ch_offset_dma = i2c->ext_data.ch_offset_dma;
876 if (i2c->ext_data.ch_offset == 0) {
877 dev_info(i2c->dev, "Wrong channel offset for multi-channel\n");
878 i2c->ch_offset = i2c->ccu_offset;
879 }
880 } else {
881 i2c->ch_offset = i2c->ch_offset_default;
882 i2c->ch_offset_dma = i2c->ch_offset_dma_default;
883 }
884
885#if defined(CONFIG_ARCH_MT6765)
886 i2c_writel(i2c, OFFSET_DEBUGCTRL, 0x28);
887#endif
888#if !defined(CONFIG_MT_I2C_FPGA_ENABLE)
889 ret = i2c_set_speed(i2c,
890 clk_get_rate(i2c->clk_main) / i2c->clk_src_div);
891#else
892 ret = i2c_set_speed(i2c, I2C_CLK_RATE);
893#endif
894 if (ret) {
895 dev_info(i2c->dev, "Failed to set the speed\n");
896 return -EINVAL;
897 }
898
899 if ((i2c->dev_comp->ver == 0x2) && i2c->ltiming_reg) {
900 u32 tv1, tv2, tv;
901#if !defined(CONFIG_MT_I2C_FPGA_ENABLE)
902 tv1 = (clk_get_rate(i2c->clk_main) / i2c->clk_src_div) / 1000;
903#else
904 tv1 = I2C_CLK_RATE / 1000;
905#endif
906 tv1 = tv1 * MAX_SCL_LOW_TIME;
907 tv2 = (((i2c->ltiming_reg & LSTEP_MSK) + 1) *
908 (((i2c->ltiming_reg & LSAMPLE_MSK) >> 6) + 1));
909 tv = DIV_ROUND_UP(tv1, tv2);
910 i2c_writew(tv & 0xFFFF, i2c, OFFSET_HW_TIMEOUT);
911 /* dev_info(i2c->dev, "scl time out value %04X\n", */
912 /* (u16)(tv & 0xFFFF)); */
913 }
914 if (i2c->dev_comp->set_dt_div) {
915 if (i2c->clk_src_div > MAX_CLOCK_DIV) {
916 dev_info(i2c->dev, "Clock div error\n");
917 return -EINVAL;
918 }
919 i2c_writew(((i2c->clk_src_div - 1) << 8) +
920 (i2c->clk_src_div - 1),
921 i2c, OFFSET_CLOCK_DIV);
922 }
923
924 /* If use i2c pin from PMIC mt6397 side, need set PATH_DIR first */
925 if (i2c->have_pmic)
926 i2c_writew(I2C_CONTROL_WRAPPER, i2c, OFFSET_PATH_DIR);
927 if (speed_hz > 400000)
928 control_reg = I2C_CONTROL_ACKERR_DET_EN;
929 else
930 control_reg = I2C_CONTROL_ACKERR_DET_EN |
931 I2C_CONTROL_CLK_EXT_EN;
932 if (isDMA == true) /* DMA */
933 control_reg |=
934 I2C_CONTROL_DMA_EN |
935 I2C_CONTROL_DMAACK_EN |
936 I2C_CONTROL_ASYNC_MODE;
937
938 if (speed_hz > 400000)
939 control_reg |= I2C_CONTROL_RS;
940 if (i2c->op == I2C_MASTER_WRRD)
941 control_reg |= I2C_CONTROL_DIR_CHANGE | I2C_CONTROL_RS;
942 i2c_writew(control_reg, i2c, OFFSET_CONTROL);
943
944 /* set start condition */
945 if (speed_hz <= 100000)
946 i2c_writew(I2C_ST_START_CON, i2c, OFFSET_EXT_CONF);
947 else {
948 if (i2c->dev_comp->ext_time_config != 0)
949 i2c_writew(i2c->dev_comp->ext_time_config,
950 i2c, OFFSET_EXT_CONF);
951 else
952 i2c_writew(I2C_FS_START_CON, i2c, OFFSET_EXT_CONF);
953 }
954
955 /* delay 5 scl_clk time between two transaction */
956 /* if (~control_reg & I2C_CONTROL_RS) */
957 i2c_writew(I2C_DELAY_LEN, i2c, OFFSET_DELAY_LEN);
958
959 /* Set ioconfig */
960 if (i2c->use_push_pull) {
961 ioconfig_reg = I2C_IO_CONFIG_PUSH_PULL;
962 } else {
963 ioconfig_reg = I2C_IO_CONFIG_OPEN_DRAIN;
964 if (i2c->dev_comp->set_aed)
965 ioconfig_reg |= ((i2c->aed<<4) &
966 I2C_IO_CONFIG_AED_MASK);
967 }
968 i2c_writew(ioconfig_reg, i2c, OFFSET_IO_CONFIG);
969
970 /* set i3c high speed master code */
971 i2c->i3c_en = (i2c_readw(i2c, OFFSET_DMA_FSM_DEBUG)) & I3C_EN;
972 if (i2c->i3c_en && (i2c->speed_hz > MAX_FS_PLUS_MODE_SPEED)
973 && (!i2c->hs_only)) {
974 i2c_writew(I2C_HFIFO_ADDR_CLR, i2c, OFFSET_FIFO_ADDR_CLR);
975 i2c_writew(I3C_UNLOCK_HFIFO | I3C_NINTH_BIT | MASTER_CODE,
976 i2c, OFFSET_HFIFO_DATA);
977 }
978
979 if (i2c->dev_comp->ver != 0x2)
980 i2c_writew(i2c->timing_reg, i2c, OFFSET_TIMING);
981 else
982 i2c_writew(i2c->timing_reg | I2C_TIMEOUT_EN,
983 i2c, OFFSET_TIMING);
984 if (i2c->dev_comp->set_ltiming)
985 i2c_writew(i2c->ltiming_reg, i2c, OFFSET_LTIMING);
986 i2c_writew(i2c->high_speed_reg, i2c, OFFSET_HS);
987
988 if (i2c->have_dcm)
989 i2c_writew(I2C_DCM_ENABLE, i2c, OFFSET_DCM_EN);
990
991 addr_reg = i2c->addr << 1;
992 if (i2c->op == I2C_MASTER_RD)
993 addr_reg |= 0x1;
994 i2c_writew(addr_reg, i2c, OFFSET_SLAVE_ADDR);
995 int_reg = I2C_HS_NACKERR | I2C_ACKERR |
996 I2C_TRANSAC_COMP | I2C_ARB_LOST;
997 if (i2c->dev_comp->ver == 0x2)
998 int_reg |= I2C_BUS_ERR | I2C_TIMEOUT;
999 if (i2c->ch_offset)
1000 int_reg &= ~(I2C_HS_NACKERR | I2C_ACKERR);
1001 /* Clear interrupt status */
1002 i2c_writew(I2C_INTR_ALL, i2c, OFFSET_INTR_STAT);
1003 if (i2c->ch_offset != 0)
1004 i2c_writew(I2C_FIFO_ADDR_CLR_MCH | I2C_FIFO_ADDR_CLR,
1005 i2c, OFFSET_FIFO_ADDR_CLR);
1006 else
1007 i2c_writew(I2C_FIFO_ADDR_CLR, i2c, OFFSET_FIFO_ADDR_CLR);
1008 /* Enable interrupt */
1009 i2c_writew(int_reg, i2c, OFFSET_INTR_MASK);
1010
1011 /* Set transfer and transaction len */
1012 if (i2c->op == I2C_MASTER_WRRD) {
1013 if ((i2c->appm) && (i2c->dev_comp->idvfs_i2c)) {
1014 i2c_writew(
1015 (i2c->msg_len & 0xFF) |
1016 ((i2c->msg_aux_len<<8) & 0x1F00),
1017 i2c, OFFSET_TRANSFER_LEN);
1018 } else {
1019 i2c_writew(i2c->msg_len, i2c,
1020 OFFSET_TRANSFER_LEN);
1021 i2c_writew(i2c->msg_aux_len, i2c,
1022 OFFSET_TRANSFER_LEN_AUX);
1023 }
1024 i2c_writew(0x02, i2c, OFFSET_TRANSAC_LEN);
1025 } else if (i2c->op == I2C_MASTER_MULTI_WR) {
1026 i2c_writew(i2c->msg_len, i2c, OFFSET_TRANSFER_LEN);
1027 i2c_writew(i2c->total_len / i2c->msg_len,
1028 i2c, OFFSET_TRANSAC_LEN);
1029 } else {
1030 i2c_writew(i2c->msg_len, i2c, OFFSET_TRANSFER_LEN);
1031 i2c_writew(0x01, i2c, OFFSET_TRANSAC_LEN);
1032 }
1033
1034 /* Prepare buffer data to start transfer */
1035 if (isDMA == true && (!i2c->is_ccu_trig)) {
1036#ifdef CONFIG_MTK_LM_MODE
1037 if ((i2c->dev_comp->dma_support == 1) && (enable_4G())) {
1038 i2c_writel_dma(0x1, i2c, OFFSET_TX_MEM_ADDR2);
1039 i2c_writel_dma(0x1, i2c, OFFSET_RX_MEM_ADDR2);
1040 }
1041#endif
1042 if (i2c->op == I2C_MASTER_RD) {
1043 i2c_writel_dma(I2C_DMA_INT_FLAG_NONE,
1044 i2c, OFFSET_INT_FLAG);
1045 i2c_writel_dma(I2C_DMA_CON_RX, i2c, OFFSET_CON);
1046 i2c_writel_dma(
1047 lower_32_bits(i2c->dma_buf.paddr),
1048 i2c, OFFSET_RX_MEM_ADDR);
1049 if ((i2c->dev_comp->dma_support >= 2))
1050 i2c_writel_dma(
1051 upper_32_bits(i2c->dma_buf.paddr),
1052 i2c, OFFSET_RX_MEM_ADDR2);
1053 i2c_writel_dma(i2c->msg_len, i2c, OFFSET_RX_LEN);
1054 } else if (i2c->op == I2C_MASTER_WR ||
1055 i2c->op == I2C_MASTER_MULTI_WR) {
1056 i2c_writel_dma(I2C_DMA_INT_FLAG_NONE,
1057 i2c, OFFSET_INT_FLAG);
1058 i2c_writel_dma(I2C_DMA_CON_TX, i2c, OFFSET_CON);
1059 i2c_writel_dma(
1060 lower_32_bits(i2c->dma_buf.paddr),
1061 i2c, OFFSET_TX_MEM_ADDR);
1062 if ((i2c->dev_comp->dma_support >= 2))
1063 i2c_writel_dma(
1064 upper_32_bits(i2c->dma_buf.paddr),
1065 i2c, OFFSET_TX_MEM_ADDR2);
1066
1067 i2c_writel_dma(i2c->total_len, i2c, OFFSET_TX_LEN);
1068 } else {
1069 i2c_writel_dma(0x0000, i2c, OFFSET_INT_FLAG);
1070 i2c_writel_dma(0x0000, i2c, OFFSET_CON);
1071 i2c_writel_dma(lower_32_bits(i2c->dma_buf.paddr),
1072 i2c, OFFSET_TX_MEM_ADDR);
1073 i2c_writel_dma(lower_32_bits(i2c->dma_buf.paddr),
1074 i2c, OFFSET_RX_MEM_ADDR);
1075 if ((i2c->dev_comp->dma_support >= 2)) {
1076 i2c_writel_dma(
1077 upper_32_bits(i2c->dma_buf.paddr),
1078 i2c, OFFSET_TX_MEM_ADDR2);
1079 i2c_writel_dma(
1080 upper_32_bits(i2c->dma_buf.paddr),
1081 i2c, OFFSET_RX_MEM_ADDR2);
1082 }
1083 i2c_writel_dma(i2c->msg_len, i2c, OFFSET_TX_LEN);
1084 i2c_writel_dma(i2c->msg_aux_len, i2c, OFFSET_RX_LEN);
1085 }
1086 record_i2c_dma_info(i2c);
1087 /* flush before sending DMA start */
1088 mb();
1089 i2c_writel_dma(I2C_DMA_START_EN, i2c, OFFSET_EN);
1090 } else {
1091 if (i2c->op != I2C_MASTER_RD && (!i2c->is_ccu_trig)) {
1092 data_size = i2c->total_len;
1093 ptr = i2c->dma_buf.vaddr;
1094 while (data_size--) {
1095 i2c_writew(*ptr, i2c, OFFSET_DATA_PORT);
1096 ptr++;
1097 }
1098 }
1099 }
1100 if (i2c->dev_comp->ver == 0x2) {
1101 if (!i2c->is_ccu_trig)
1102 i2c_writew(I2C_MCU_INTR_EN, i2c, OFFSET_MCU_INTR);
1103 else {
1104 dev_info(i2c->dev, "I2C CCU trig.\n");
1105 return 0;
1106 }
1107 }
1108
1109 /* flush before sending start */
1110 mb();
1111 if (!i2c->is_hw_trig)
1112 i2c_writew(I2C_TRANSAC_START, i2c, OFFSET_START);
1113 else {
1114 dev_info(i2c->dev, "I2C hw trig.\n");
1115 return 0;
1116 }
1117
1118 tmo = wait_event_timeout(i2c->wait, i2c->trans_stop, tmo);
1119
1120 record_i2c_info(i2c, tmo);
1121
1122 if (tmo == 0) {
1123 dev_info(i2c->dev, "addr:0x%x,transfer timeout\n",
1124 i2c->addr);
1125 start_reg = i2c_readw(i2c, OFFSET_START);
1126 dev_info(i2c->dev,
1127 "timeout:start=0x%x,ch_err=0x%x\n",
1128 start_reg, i2c_readw(i2c, OFFSET_ERROR));
1129
1130 i2c_dump_info(i2c);
1131 i2c_gpio_dump_info(i2c);
1132 #if defined(CONFIG_MTK_GIC_EXT)
1133 mt_irq_dump_status(i2c->irqnr);
1134 #endif
1135 dump_cg_regs(i2c);
1136 if (i2c->ch_offset != 0)
1137 i2c_writew(I2C_FIFO_ADDR_CLR_MCH | I2C_FIFO_ADDR_CLR,
1138 i2c, OFFSET_FIFO_ADDR_CLR);
1139 else
1140 i2c_writew(I2C_FIFO_ADDR_CLR, i2c,
1141 OFFSET_FIFO_ADDR_CLR);
1142
1143 /* This slave addr is used to check whether the shadow RG is */
1144 /* mapped normally or not */
1145 dev_info(i2c->dev, "SLAVE_ADDR=0x%x (shadow RG)",
1146 i2c_readw_shadow(i2c, OFFSET_SLAVE_ADDR));
1147 mt_i2c_init_hw(i2c);
1148 if ((i2c->ch_offset) && (start_reg & I2C_RESUME_ARBIT)) {
1149 i2c_writew_shadow(I2C_RESUME_ARBIT, i2c, OFFSET_START);
1150 dev_info(i2c->dev, "bus channel transferred\n");
1151 }
1152
1153 if (start_reg & I2C_TRANSAC_START) {
1154 dev_info(i2c->dev, "bus tied low/high(0x%x)\n",
1155 start_reg);
1156 return -EIO;
1157 }
1158 return -ETIMEDOUT;
1159 }
1160 if (i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR |
1161 I2C_TIMEOUT | I2C_BUS_ERR | I2C_IBI)) {
1162 dev_info(i2c->dev,
1163 "error:addr=0x%x,irq_stat=0x%x,ch_offset=0x%x,mask:0x%x\n",
1164 i2c->addr, i2c->irq_stat, i2c->ch_offset, int_reg);
1165
1166 /* clear fifo addr:bit2,multi-chn;bit0,normal */
1167 i2c_writew(I2C_FIFO_ADDR_CLR_MCH | I2C_FIFO_ADDR_CLR,
1168 i2c, OFFSET_FIFO_ADDR_CLR);
1169
1170 if (i2c->ext_data.isEnable == false ||
1171 i2c->ext_data.isFilterMsg == false) {
1172 i2c_dump_info(i2c);
1173 i2c_gpio_dump_info(i2c);
1174 } else
1175 dev_info(i2c->dev, "addr:0x%x,ext_data skip more log\n",
1176 i2c->addr);
1177
1178 if ((i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR)))
1179 dev_info(i2c->dev, "addr:0x%x,ACK error\n", i2c->addr);
1180
1181 if (i2c->irq_stat & I2C_TIMEOUT)
1182 dev_info(i2c->dev, "addr:0x%x,SCL tied low timeout error\n",
1183 i2c->addr);
1184
1185 if ((i2c->irq_stat & I2C_BUS_ERR))
1186 dev_info(i2c->dev,
1187 "bus error:start=0x%x,ch_err=0x%x,dbg_stat=0x%x\n",
1188 i2c_readw(i2c, OFFSET_START),
1189 i2c_readw(i2c, OFFSET_ERROR),
1190 i2c_readw(i2c, OFFSET_DEBUGSTAT));
1191
1192 if ((i2c->irq_stat & I2C_IBI)) {
1193 dev_info(i2c->dev,
1194 "IBI error:start=0x%x,ch_err=0x%x,dbg_stat=0x%x\n",
1195 i2c_readw(i2c, OFFSET_START),
1196 i2c_readw(i2c, OFFSET_ERROR),
1197 i2c_readw(i2c, OFFSET_DEBUGSTAT));
1198 }
1199
1200 if ((i2c->irq_stat & I2C_TRANSAC_COMP) && i2c->ch_offset &&
1201 (!(i2c->irq_stat & I2C_BUS_ERR))) {
1202 dev_info(i2c->dev, "trans done with error");
1203 return -EREMOTEIO;
1204 }
1205
1206 /* Need init&kick if (intr_state&intr_mask) is greater than 1 */
1207 if ((i2c->irq_stat & int_reg) > 1) {
1208 mt_i2c_init_hw(i2c);
1209 if (i2c->ch_offset) {
1210 i2c_writew_shadow(I2C_RESUME_ARBIT,
1211 i2c, OFFSET_START);
1212 dev_info(i2c->dev, "bus channel transferred\n");
1213 }
1214 }
1215 return -EREMOTEIO;
1216 }
1217 if (i2c->op != I2C_MASTER_WR && isDMA == false) {
1218 if (i2c->op == I2C_MASTER_WRRD)
1219 data_size = i2c->msg_aux_len;
1220 else
1221 data_size = i2c->msg_len;
1222 ptr = i2c->dma_buf.vaddr;
1223 while (data_size--) {
1224 *ptr = i2c_readw(i2c, OFFSET_DATA_PORT);
1225 ptr++;
1226 }
1227 }
1228 dev_dbg(i2c->dev, "i2c transferred done.\n");
1229
1230 return 0;
1231}
1232
1233static inline void mt_i2c_copy_to_dma(struct mt_i2c *i2c, struct i2c_msg *msg)
1234{
1235 /*
1236 * if the operate is write, write-read, multi-write,
1237 * need to copy the data to DMA memory.
1238 */
1239 if (!(msg->flags & I2C_M_RD))
1240 memcpy(i2c->dma_buf.vaddr + i2c->total_len - msg->len,
1241 msg->buf, msg->len);
1242}
1243
1244static inline void mt_i2c_copy_from_dma(struct mt_i2c *i2c,
1245 struct i2c_msg *msg)
1246{
1247 /* if the operate is read, need to copy the data from DMA memory */
1248 if (msg->flags & I2C_M_RD)
1249 memcpy(msg->buf, i2c->dma_buf.vaddr, msg->len);
1250}
1251
1252/*
1253 * In MTK platform the STOP will be issued after each
1254 * message was transferred which is not flow the clarify
1255 * for i2c_transfer(), several I2C devices tolerate the STOP,
1256 * but some device need Repeat-Start and do not compatible with STOP
1257 * MTK platform has WRRD mode which can write then read with
1258 * Repeat-Start between two message, so we combined two
1259 * messages into one transaction.
1260 * The max read length is 4096
1261 */
1262static bool mt_i2c_should_combine(struct i2c_msg *msg)
1263{
1264 struct i2c_msg *next_msg = msg + 1;
1265
1266 if ((next_msg->len < 4096) &&
1267 msg->addr == next_msg->addr &&
1268 !(msg->flags & I2C_M_RD) &&
1269 (next_msg->flags & I2C_M_RD) == I2C_M_RD) {
1270 return true;
1271 }
1272 return false;
1273}
1274
1275static bool mt_i2c_should_batch(struct i2c_msg *prev, struct i2c_msg *next)
1276{
1277 if ((prev == NULL) || (next == NULL) ||
1278 (prev->flags & I2C_M_RD) || (next->flags & I2C_M_RD))
1279 return false;
1280 if ((next != NULL) && (prev != NULL) &&
1281 (prev->len == next->len && prev->addr == next->addr))
1282 return true;
1283 return false;
1284}
1285
1286static int __mt_i2c_transfer(struct mt_i2c *i2c,
1287 struct i2c_msg msgs[], int num)
1288{
1289 int ret;
1290 int left_num = num;
1291
1292 while (left_num--) {
1293 /* In MTK platform the max transfer number is 4096 */
1294 if (msgs->len > MAX_DMA_TRANS_SIZE) {
1295 dev_dbg(i2c->dev,
1296 " message data length is more than 255\n");
1297 ret = -EINVAL;
1298 goto err_exit;
1299 }
1300 if (msgs->addr == 0) {
1301 dev_dbg(i2c->dev, " addr is invalid.\n");
1302 ret = -EINVAL;
1303 goto err_exit;
1304 }
1305 if (msgs->buf == NULL) {
1306 dev_dbg(i2c->dev, " data buffer is NULL.\n");
1307 ret = -EINVAL;
1308 goto err_exit;
1309 }
1310
1311 i2c->addr = msgs->addr;
1312 i2c->msg_len = msgs->len;
1313 i2c->msg_aux_len = 0;
1314
1315 if ((left_num + 1 == num) ||
1316 !mt_i2c_should_batch(msgs - 1, msgs)) {
1317 i2c->total_len = msgs->len;
1318 if (msgs->flags & I2C_M_RD)
1319 i2c->op = I2C_MASTER_RD;
1320 else
1321 i2c->op = I2C_MASTER_WR;
1322 } else {
1323 i2c->total_len += msgs->len;
1324 }
1325
1326 /*
1327 * always use DMA mode.
1328 * 1st when write need copy the data of message to dma memory
1329 * 2nd when read need copy the DMA data to the message buffer.
1330 * The length should be less than 255.
1331 */
1332 mt_i2c_copy_to_dma(i2c, msgs);
1333
1334 if (left_num >= 1) {
1335 if (mt_i2c_should_batch(msgs, msgs + 1)) {
1336 i2c->op = I2C_MASTER_MULTI_WR;
1337 msgs++;
1338 continue;
1339 }
1340 if (mt_i2c_should_combine(msgs)) {
1341 i2c->msg_aux_len = (msgs + 1)->len;
1342 i2c->op = I2C_MASTER_WRRD;
1343 left_num--;
1344 }
1345 }
1346
1347 /* Use HW semaphore to protect device access between
1348 * AP and SPM, or SCP
1349 */
1350 if (i2c_get_semaphore(i2c) != 0) {
1351 dev_info(i2c->dev, "get hw semaphore failed.\n");
1352 return -EBUSY;
1353 }
1354 ret = mt_i2c_do_transfer(i2c);
1355 /* Use HW semaphore to protect device access between
1356 * AP and SPM, or SCP
1357 */
1358 if (i2c_release_semaphore(i2c) != 0) {
1359 dev_info(i2c->dev, "release hw semaphore failed.\n");
1360 ret = -EBUSY;
1361 }
1362 if (ret < 0)
1363 goto err_exit;
1364 if (i2c->op == I2C_MASTER_WRRD)
1365 mt_i2c_copy_from_dma(i2c, msgs + 1);
1366 else
1367 mt_i2c_copy_from_dma(i2c, msgs);
1368
1369 msgs++;
1370 /* after combined two messages so we need ignore one */
1371 if (left_num > 0 && i2c->op == I2C_MASTER_WRRD)
1372 msgs++;
1373 }
1374 /* the return value is number of executed messages */
1375 ret = num;
1376err_exit:
1377 return ret;
1378}
1379
1380#ifdef CONFIG_TRUSTONIC_TEE_SUPPORT
1381int i2c_tui_enable_clock(int id)
1382{
1383 struct i2c_adapter *adap;
1384 struct mt_i2c *i2c;
1385 int ret;
1386
1387 adap = i2c_get_adapter(id);
1388 if (!adap) {
1389 pr_info("Cannot get adapter\n");
1390 return -1;
1391 }
1392
1393 i2c = i2c_get_adapdata(adap);
1394 ret = clk_prepare_enable(i2c->clk_main);
1395 if (ret) {
1396 pr_info("Cannot enable main clk\n");
1397 return ret;
1398 }
1399 ret = clk_prepare_enable(i2c->clk_dma);
1400 if (ret) {
1401 pr_info("Cannot enable dma clk\n");
1402 clk_disable_unprepare(i2c->clk_main);
1403 return ret;
1404 }
1405
1406 return 0;
1407}
1408
1409int i2c_tui_disable_clock(int id)
1410{
1411 struct i2c_adapter *adap;
1412 struct mt_i2c *i2c;
1413
1414 adap = i2c_get_adapter(id);
1415 if (!adap) {
1416 pr_info("Cannot get adapter\n");
1417 return -1;
1418 }
1419
1420 i2c = i2c_get_adapdata(adap);
1421 clk_disable_unprepare(i2c->clk_dma);
1422 clk_disable_unprepare(i2c->clk_main);
1423
1424 return 0;
1425}
1426#endif
1427
1428static int mt_i2c_transfer(struct i2c_adapter *adap,
1429 struct i2c_msg msgs[], int num)
1430{
1431 int ret;
1432 struct mt_i2c *i2c = i2c_get_adapdata(adap);
1433
1434 ret = mt_i2c_clock_enable(i2c);
1435 if (ret)
1436 return -EBUSY;
1437
1438 mutex_lock(&i2c->i2c_mutex);
1439 ret = __mt_i2c_transfer(i2c, msgs, num);
1440 mutex_unlock(&i2c->i2c_mutex);
1441
1442 mt_i2c_clock_disable(i2c);
1443 return ret;
1444}
1445
1446
1447static void mt_i2c_parse_extension(struct mt_i2c_ext *pext, u32 ext_flag,
1448 u32 timing)
1449{
1450 if (ext_flag & I2C_A_FILTER_MSG)
1451 pext->isFilterMsg = true;
1452 if (timing)
1453 pext->timing = timing;
1454}
1455
1456int mtk_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num,
1457 u32 ext_flag, u32 timing)
1458{
1459 int ret;
1460 struct mt_i2c *i2c = i2c_get_adapdata(adap);
1461
1462 ret = mt_i2c_clock_enable(i2c);
1463 if (ret)
1464 return -EBUSY;
1465
1466 mutex_lock(&i2c->i2c_mutex);
1467 i2c->ext_data.isEnable = true;
1468
1469 mt_i2c_parse_extension(&i2c->ext_data, ext_flag, timing);
1470 ret = __mt_i2c_transfer(i2c, msgs, num);
1471
1472 i2c->ext_data.isEnable = false;
1473 mutex_unlock(&i2c->i2c_mutex);
1474
1475 mt_i2c_clock_disable(i2c);
1476 return ret;
1477}
1478EXPORT_SYMBOL(mtk_i2c_transfer);
1479
1480int hw_trig_i2c_enable(struct i2c_adapter *adap)
1481{
1482 struct mt_i2c *i2c = i2c_get_adapdata(adap);
1483
1484 if (!i2c->buffermode)
1485 return -1;
1486 if (mt_i2c_clock_enable(i2c))
1487 return -EBUSY;
1488
1489 mutex_lock(&i2c->i2c_mutex);
1490 i2c->is_hw_trig = true;
1491 mutex_unlock(&i2c->i2c_mutex);
1492 return 0;
1493}
1494EXPORT_SYMBOL(hw_trig_i2c_enable);
1495
1496int hw_trig_i2c_disable(struct i2c_adapter *adap)
1497{
1498 struct mt_i2c *i2c = i2c_get_adapdata(adap);
1499
1500 if (!i2c->buffermode)
1501 return -1;
1502 mutex_lock(&i2c->i2c_mutex);
1503 i2c->is_hw_trig = false;
1504 mutex_unlock(&i2c->i2c_mutex);
1505 mt_i2c_wait_done(i2c, 0);
1506 mt_i2c_clock_disable(i2c);
1507 return 0;
1508}
1509EXPORT_SYMBOL(hw_trig_i2c_disable);
1510
1511int hw_trig_i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
1512 int num)
1513{
1514 int ret;
1515 struct mt_i2c *i2c = i2c_get_adapdata(adap);
1516
1517 if (!i2c->buffermode)
1518 return -1;
1519 mutex_lock(&i2c->i2c_mutex);
1520 ret = __mt_i2c_transfer(i2c, msgs, num);
1521 mutex_unlock(&i2c->i2c_mutex);
1522 return ret;
1523}
1524EXPORT_SYMBOL(hw_trig_i2c_transfer);
1525
1526int i2c_ccu_enable(struct i2c_adapter *adap, u16 ch_offset)
1527{
1528 int ret;
1529 struct mt_i2c *i2c = i2c_get_adapdata(adap);
1530 char buf[1];
1531 /*This is just a dummy msg which is meaningless since these parameter
1532 * is actually not used.
1533 */
1534 struct i2c_msg dummy_msg = {
1535 .addr = 0x1,
1536 .flags = I2C_MASTER_RD,
1537 .len = 1,
1538 .buf = (char *)buf,
1539 };
1540 if (mt_i2c_clock_enable(i2c))
1541 return -EBUSY;
1542 mutex_lock(&i2c->i2c_mutex);
1543 i2c->is_ccu_trig = true;
1544 i2c->ext_data.ch_offset = ch_offset;
1545 i2c->ext_data.is_ch_offset = true;
1546 ret = __mt_i2c_transfer(i2c, &dummy_msg, 1);
1547 i2c->is_ccu_trig = false;
1548 i2c->ext_data.is_ch_offset = false;
1549 mutex_unlock(&i2c->i2c_mutex);
1550 return ret;
1551}
1552EXPORT_SYMBOL(i2c_ccu_enable);
1553
1554int i2c_ccu_disable(struct i2c_adapter *adap)
1555{
1556 struct mt_i2c *i2c = i2c_get_adapdata(adap);
1557
1558 mt_i2c_wait_done(i2c, i2c->ccu_offset);
1559 mt_i2c_clock_disable(i2c);
1560 return 0;
1561}
1562EXPORT_SYMBOL(i2c_ccu_disable);
1563
1564static irqreturn_t mt_i2c_irq(int irqno, void *dev_id)
1565{
1566 struct mt_i2c *i2c = dev_id;
1567
1568 #if 0
1569 /* Clear interrupt mask */
1570 i2c_writew(~(I2C_HS_NACKERR | I2C_ACKERR | I2C_TRANSAC_COMP),
1571 i2c, OFFSET_INTR_MASK);
1572 i2c->irq_stat = i2c_readw(i2c, OFFSET_INTR_STAT);
1573 i2c_writew(I2C_HS_NACKERR | I2C_ACKERR | I2C_TRANSAC_COMP,
1574 i2c, OFFSET_INTR_STAT);
1575 #endif
1576
1577 /* mask and clear all interrupt for i2c, need think of i3c~~ */
1578 i2c_writew(~(I2C_INTR_ALL), i2c, OFFSET_INTR_MASK);
1579 i2c->irq_stat = i2c_readw(i2c, OFFSET_INTR_STAT);
1580 i2c_writew(I2C_INTR_ALL, i2c, OFFSET_INTR_STAT);
1581 i2c->trans_stop = true;
1582 if (!i2c->is_hw_trig) {
1583 wake_up(&i2c->wait);
1584 if (!i2c->irq_stat) {
1585 dev_info(i2c->dev, "addr: 0x%x, irq stat 0\n",
1586 i2c->addr);
1587
1588 i2c_dump_info(i2c);
1589 #if defined(CONFIG_MTK_GIC_EXT)
1590 mt_irq_dump_status(i2c->irqnr);
1591 #endif
1592 } else {
1593 /* for bxx debug start */
1594 if ((i2c->irq_stat & (I2C_IBI | I2C_BUS_ERR))) {
1595 dev_info(i2c->dev, "[bxx]cg_cnt:%d,irq_stat:0x%x\n",
1596 i2c->cg_cnt, i2c->irq_stat);
1597 dev_info(i2c->dev, "0x84=0x%x\n",
1598 i2c_readw(i2c, OFFSET_ERROR));
1599
1600 pr_info("[bxx]0xE0=0x%x,0x1E0=0x%x,0x2E0=0x%x\n",
1601 _i2c_readw(i2c, 0xE0),
1602 _i2c_readw(i2c, 0x1E0),
1603 _i2c_readw(i2c, 0x2E0));
1604 pr_info("[bxx]0xE4=0x%x,0x1E4=0x%x,0x2E4=0x%x\n",
1605 _i2c_readw(i2c, 0xE4),
1606 _i2c_readw(i2c, 0x1E4),
1607 _i2c_readw(i2c, 0x2E4));
1608 pr_info("[bxx]0xE8=0x%x,0x1E8=0x%x,0x2E8=0x%x\n",
1609 _i2c_readw(i2c, 0xE8),
1610 _i2c_readw(i2c, 0x1E8),
1611 _i2c_readw(i2c, 0x2E8));
1612 pr_info("[bxx]0xEC=0x%x,0x1EC=0x%x,0x2EC=0x%x\n",
1613 _i2c_readw(i2c, 0xEC),
1614 _i2c_readw(i2c, 0x1EC),
1615 _i2c_readw(i2c, 0x2EC));
1616 pr_info("[bxx]0x58=0x%x,0x158=0x%x,0x258=0x%x\n",
1617 _i2c_readw(i2c, 0x58),
1618 _i2c_readw(i2c, 0x158),
1619 _i2c_readw(i2c, 0x258));
1620 /* IBI triggered */
1621 pr_info("[bxx]0x54=0x%x,0x154=0x%x,0x254=0x%x\n",
1622 _i2c_readw(i2c, 0x54),
1623 _i2c_readw(i2c, 0x154),
1624 _i2c_readw(i2c, 0x254));
1625 /* for bxx debug end */
1626 }
1627 }
1628 } else {/* dump regs info for hw trig i2c if ACK err */
1629 if (i2c->irq_stat & (I2C_HS_NACKERR | I2C_ACKERR)) {
1630 dev_info(i2c->dev, "addr:0x%x,irq_stat:0x%x,transfer ACK error\n",
1631 i2c->addr, i2c->irq_stat);
1632 i2c_dump_info(i2c);
1633 mt_i2c_init_hw(i2c);
1634 } else {
1635 dev_info(i2c->dev, "addr:0x%x, other irq_stat:0x%x\n",
1636 i2c->addr, i2c->irq_stat);
1637 }
1638 }
1639 return IRQ_HANDLED;
1640}
1641
1642static u32 mt_i2c_functionality(struct i2c_adapter *adap)
1643{
1644 return I2C_FUNC_I2C | I2C_FUNC_10BIT_ADDR | I2C_FUNC_SMBUS_EMUL;
1645}
1646
1647static const struct i2c_algorithm mt_i2c_algorithm = {
1648 .master_xfer = mt_i2c_transfer,
1649 .functionality = mt_i2c_functionality,
1650};
1651
1652static int mt_i2c_parse_dt(struct device_node *np, struct mt_i2c *i2c)
1653{
1654 int ret = -1;
1655
1656 i2c->speed_hz = I2C_DEFAUT_SPEED;
1657 of_property_read_u32(np, "clock-frequency", &i2c->speed_hz);
1658 of_property_read_u32(np, "clock-div", &i2c->clk_src_div);
1659 of_property_read_u32(np, "scl-gpio-id", &i2c->scl_gpio_id);
1660 of_property_read_u32(np, "sda-gpio-id", &i2c->sda_gpio_id);
1661 of_property_read_u32(np, "gpio_start", &i2c->gpio_start);
1662 of_property_read_u32(np, "mem_len", &i2c->mem_len);
1663 of_property_read_u32(np, "eh_cfg", &i2c->offset_eh_cfg);
1664 of_property_read_u32(np, "pu_cfg", &i2c->offset_pu_cfg);
1665 of_property_read_u32(np, "rsel_cfg", &i2c->offset_rsel_cfg);
1666 of_property_read_u32(np, "id", (u32 *)&i2c->id);
1667 of_property_read_u16(np, "clk_sta_offset",
1668 (u16 *)&i2c->clk_sta_offset);
1669 of_property_read_u8(np, "cg_bit", (u8 *)&i2c->cg_bit);
1670 of_property_read_u32(np, "aed", &i2c->aed);
1671 of_property_read_u32(np, "ch_offset_default",
1672 &i2c->ch_offset_default);
1673 of_property_read_u32(np, "ch_offset_dma_default",
1674 &i2c->ch_offset_dma_default);
1675 ret = of_property_read_u32(np, "ch_offset_ccu", &i2c->ccu_offset);
1676 if (!ret)
1677 i2c->has_ccu = true;
1678 else
1679 i2c->has_ccu = false;
1680
1681 i2c->have_pmic
1682 = of_property_read_bool(np, "mediatek,have-pmic");
1683 i2c->have_dcm
1684 = of_property_read_bool(np, "mediatek,have-dcm");
1685 i2c->use_push_pull
1686 = of_property_read_bool(np, "mediatek,use-push-pull");
1687 i2c->appm
1688 = of_property_read_bool(np, "mediatek,appm_used");
1689 i2c->gpupm
1690 = of_property_read_bool(np, "mediatek,gpupm_used");
1691 i2c->buffermode = of_property_read_bool(np, "mediatek,buffermode_used");
1692 i2c->hs_only = of_property_read_bool(np, "mediatek,hs_only");
1693 pr_info("[I2C]id:%d,freq:%d,div:%d,ch_offset:0x%x,offset_dma:0x%x,offset_ccu:0x%x\n",
1694 i2c->id, i2c->speed_hz, i2c->clk_src_div,
1695 i2c->ch_offset_default,
1696 i2c->ch_offset_dma_default, i2c->ccu_offset);
1697 if (i2c->clk_src_div == 0)
1698 return -EINVAL;
1699 return 0;
1700}
1701
1702
1703int mt_i2c_parse_comp_data(void)
1704{
1705 int ret = -1;
1706 struct device_node *comp_node;
1707
1708 comp_node = of_find_compatible_node(NULL, NULL, "mediatek,i2c_common");
1709 if (!comp_node) {
1710 pr_info("Cannot find i2c_common node\n");
1711 return -ENODEV;
1712 }
1713 of_property_read_u8(comp_node, "dma_support",
1714 (u8 *)&i2c_common_compat.dma_support);
1715 of_property_read_u8(comp_node, "idvfs",
1716 (u8 *)&i2c_common_compat.idvfs_i2c);
1717 of_property_read_u8(comp_node, "set_dt_div",
1718 (u8 *)&i2c_common_compat.set_dt_div);
1719 of_property_read_u8(comp_node, "check_max_freq",
1720 (u8 *)&i2c_common_compat.check_max_freq);
1721 of_property_read_u8(comp_node, "set_ltiming",
1722 (u8 *)&i2c_common_compat.set_ltiming);
1723 of_property_read_u8(comp_node, "set_aed",
1724 (u8 *)&i2c_common_compat.set_aed);
1725 of_property_read_u16(comp_node, "ext_time_config",
1726 (u16 *)&i2c_common_compat.ext_time_config);
1727 ret = of_property_count_u8_elems(comp_node, "clk_compatible");
1728 if (ret > 0)
1729 of_property_read_u8_array(comp_node, "clk_compatible",
1730 (u8 *)i2c_common_compat.clk_compatible, ret);
1731 else
1732 pr_info("[I2C]No clk_compatible(%d)\n", ret);
1733 of_property_read_u32(comp_node, "clk_sel_offset",
1734 (u32 *)&i2c_common_compat.clk_sel_offset);
1735 of_property_read_u32(comp_node, "arbit_offset",
1736 (u32 *)&i2c_common_compat.arbit_offset);
1737 of_property_read_u8(comp_node, "ver",
1738 (u8 *)&i2c_common_compat.ver);
1739 of_property_read_u8(comp_node, "cnt_constraint",
1740 (u8 *)&i2c_common_compat.cnt_constraint);
1741 return 0;
1742}
1743
1744static const struct of_device_id mtk_i2c_of_match[] = {
1745 { .compatible = "mediatek,i2c", .data = &i2c_common_compat},
1746 {},
1747};
1748
1749MODULE_DEVICE_TABLE(of, mtk_i2c_of_match);
1750
1751static int mt_i2c_probe(struct platform_device *pdev)
1752{
1753 int ret = 0;
1754 struct mt_i2c *i2c;
1755 unsigned int clk_src_in_hz;
1756 struct resource *res;
1757 const struct of_device_id *of_id;
1758
1759 i2c = devm_kzalloc(&pdev->dev, sizeof(struct mt_i2c), GFP_KERNEL);
1760 if (i2c == NULL)
1761 return -ENOMEM;
1762
1763 ret = mt_i2c_parse_dt(pdev->dev.of_node, i2c);
1764 if (ret)
1765 return -EINVAL;
1766
1767 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1768
1769 i2c->base = devm_ioremap_resource(&pdev->dev, res);
1770 if (IS_ERR(i2c->base))
1771 return PTR_ERR(i2c->base);
1772
1773 if (i2c->id < I2C_MAX_CHANNEL)
1774 g_mt_i2c[i2c->id] = i2c;
1775
1776 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1777
1778 i2c->pdmabase = devm_ioremap_resource(&pdev->dev, res);
1779 if (IS_ERR(i2c->pdmabase))
1780 return PTR_ERR(i2c->pdmabase);
1781
1782 i2c->gpiobase = devm_ioremap(&pdev->dev, i2c->gpio_start, i2c->mem_len);
1783 if (IS_ERR(i2c->gpiobase)) {
1784 i2c->gpiobase = NULL;
1785 dev_info(&pdev->dev, "do not have gpio baseaddress node\n");
1786 }
1787
1788 i2c->irqnr = platform_get_irq(pdev, 0);
1789 if (i2c->irqnr <= 0)
1790 return -EINVAL;
1791 init_waitqueue_head(&i2c->wait);
1792
1793 ret = devm_request_irq(&pdev->dev, i2c->irqnr, mt_i2c_irq,
1794 IRQF_NO_SUSPEND | IRQF_TRIGGER_NONE, I2C_DRV_NAME, i2c);
1795 if (ret < 0) {
1796 dev_info(&pdev->dev,
1797 "Request I2C IRQ %d fail\n", i2c->irqnr);
1798 return ret;
1799 }
1800 of_id = of_match_node(mtk_i2c_of_match, pdev->dev.of_node);
1801 if (!of_id)
1802 return -EINVAL;
1803
1804 i2c->dev_comp = of_id->data;
1805 i2c->adap.dev.of_node = pdev->dev.of_node;
1806 i2c->dev = &i2c->adap.dev;
1807 i2c->adap.dev.parent = &pdev->dev;
1808 i2c->adap.owner = THIS_MODULE;
1809 i2c->adap.algo = &mt_i2c_algorithm;
1810 i2c->adap.algo_data = NULL;
1811 i2c->adap.timeout = 2 * HZ;
1812 i2c->adap.retries = 1;
1813 i2c->adap.nr = i2c->id;
1814 spin_lock_init(&i2c->cg_lock);
1815
1816 if (i2c->dev_comp->dma_support == 2) {
1817 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(33))) {
1818 dev_info(&pdev->dev, "dma_set_mask return error.\n");
1819 return -EINVAL;
1820 }
1821 } else if (i2c->dev_comp->dma_support == 3) {
1822 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(36))) {
1823 dev_info(&pdev->dev, "dma_set_mask return error.\n");
1824 return -EINVAL;
1825 }
1826 }
1827
1828#if !defined(CONFIG_MT_I2C_FPGA_ENABLE)
1829 i2c->clk_main = devm_clk_get(&pdev->dev, "main");
1830 if (IS_ERR(i2c->clk_main)) {
1831 dev_info(&pdev->dev, "cannot get main clock\n");
1832 return PTR_ERR(i2c->clk_main);
1833 }
1834 i2c->clk_dma = devm_clk_get(&pdev->dev, "dma");
1835 if (IS_ERR(i2c->clk_dma)) {
1836 dev_info(&pdev->dev, "cannot get dma clock\n");
1837 return PTR_ERR(i2c->clk_dma);
1838 }
1839 i2c->clk_arb = devm_clk_get(&pdev->dev, "arb");
1840 if (IS_ERR(i2c->clk_arb))
1841 i2c->clk_arb = NULL;
1842 else
1843 dev_dbg(&pdev->dev, "i2c%d has the relevant arbitrator clk.\n",
1844 i2c->id);
1845 i2c->clk_pal = devm_clk_get(&pdev->dev, "pal");
1846 if (IS_ERR(i2c->clk_pal))
1847 i2c->clk_pal = NULL;
1848 else
1849 dev_dbg(&pdev->dev, "i2c%d has the relevant pal clk.\n",
1850 i2c->id);
1851#endif
1852
1853 if (i2c->have_pmic) {
1854 i2c->clk_pmic = devm_clk_get(&pdev->dev, "pmic");
1855 if (IS_ERR(i2c->clk_pmic)) {
1856 dev_info(&pdev->dev, "cannot get pmic clock\n");
1857 return PTR_ERR(i2c->clk_pmic);
1858 }
1859 clk_src_in_hz = clk_get_rate(i2c->clk_pmic) / i2c->clk_src_div;
1860 } else {
1861 clk_src_in_hz = clk_get_rate(i2c->clk_main) / i2c->clk_src_div;
1862 }
1863 i2c->main_clk = clk_src_in_hz;
1864 dev_info(&pdev->dev, "i2c%d clock source %p,clock src frequency %d\n",
1865 i2c->id, i2c->clk_main, clk_src_in_hz);
1866
1867 strlcpy(i2c->adap.name, I2C_DRV_NAME, sizeof(i2c->adap.name));
1868 mutex_init(&i2c->i2c_mutex);
1869 ret = i2c_set_speed(i2c, clk_src_in_hz);
1870 if (ret) {
1871 dev_info(&pdev->dev, "Failed to set the speed\n");
1872 return -EINVAL;
1873 }
1874 ret = mt_i2c_clock_enable(i2c);
1875 if (ret) {
1876 dev_info(&pdev->dev, "clock enable failed!\n");
1877 return ret;
1878 }
1879 mt_i2c_init_hw(i2c);
1880
1881 mt_i2c_clock_disable(i2c);
1882 if (i2c->ch_offset_default)
1883 i2c->dma_buf.vaddr = dma_alloc_coherent(&pdev->dev,
1884 (PAGE_SIZE * 2), &i2c->dma_buf.paddr, GFP_KERNEL);
1885 else
1886 i2c->dma_buf.vaddr = dma_alloc_coherent(&pdev->dev,
1887 PAGE_SIZE, &i2c->dma_buf.paddr, GFP_KERNEL);
1888
1889 if (i2c->dma_buf.vaddr == NULL) {
1890 dev_info(&pdev->dev, "dma_alloc_coherent fail\n");
1891 return -ENOMEM;
1892 }
1893 i2c_set_adapdata(&i2c->adap, i2c);
1894 /* ret = i2c_add_adapter(&i2c->adap); */
1895 ret = i2c_add_numbered_adapter(&i2c->adap);
1896 if (ret) {
1897 dev_info(&pdev->dev, "Failed to add i2c bus to i2c core\n");
1898 free_i2c_dma_bufs(i2c);
1899 return ret;
1900 }
1901 platform_set_drvdata(pdev, i2c);
1902
1903 if (!map_cg_regs(i2c))
1904 pr_info("Map cg regs successfully.\n");
1905
1906 return 0;
1907}
1908
1909static int mt_i2c_remove(struct platform_device *pdev)
1910{
1911 struct mt_i2c *i2c = platform_get_drvdata(pdev);
1912
1913 i2c_del_adapter(&i2c->adap);
1914 free_i2c_dma_bufs(i2c);
1915 platform_set_drvdata(pdev, NULL);
1916 return 0;
1917}
1918
1919
1920MODULE_DEVICE_TABLE(of, mt_i2c_match);
1921
1922#ifdef CONFIG_PM_SLEEP
1923static int mt_i2c_suspend_noirq(struct device *dev)
1924{
1925 struct platform_device *pdev = to_platform_device(dev);
1926 struct mt_i2c *i2c = platform_get_drvdata(pdev);
1927 int ret = 0;
1928
1929 spin_lock(&i2c->cg_lock);
1930 if (i2c->cg_cnt > 0) {
1931 ret = -EBUSY;
1932 dev_info(i2c->dev, "%s(%d) busy\n", __func__, i2c->cg_cnt);
1933 } else
1934 i2c->suspended = true;
1935 spin_unlock(&i2c->cg_lock);
1936
1937 return ret;
1938}
1939
1940static int mt_i2c_resume_noirq(struct device *dev)
1941{
1942 struct platform_device *pdev = to_platform_device(dev);
1943 struct mt_i2c *i2c = platform_get_drvdata(pdev);
1944
1945 spin_lock(&i2c->cg_lock);
1946 i2c->suspended = false;
1947 spin_unlock(&i2c->cg_lock);
1948
1949 if (i2c->ch_offset_default) {
1950 if (mt_i2c_clock_enable(i2c))
1951 dev_info(i2c->dev, "%s enable clock failed\n",
1952 __func__);
1953#if 0
1954 /* Disable rollback mode for multi-channel */
1955 mt_secure_call(MTK_SIP_KERNEL_I2C_SEC_WRITE,
1956 i2c->id, V2_OFFSET_ROLLBACK, 0);
1957#endif
1958 /* Enable multi-channel DMA mode at ATF */
1959 mt_secure_call(MTK_SIP_KERNEL_I2C_SEC_WRITE, i2c->id,
1960 V2_OFFSET_MULTI_DMA, I2C_SHADOW_REG_MODE, 0);
1961
1962 mt_i2c_clock_disable(i2c);
1963 }
1964 return 0;
1965}
1966
1967#endif
1968
1969static const struct dev_pm_ops mt_i2c_dev_pm_ops = {
1970#ifdef CONFIG_PM_SLEEP
1971 .suspend_noirq = mt_i2c_suspend_noirq,
1972 .resume_noirq = mt_i2c_resume_noirq,
1973#endif
1974};
1975
1976static struct platform_driver mt_i2c_driver = {
1977 .probe = mt_i2c_probe,
1978 .remove = mt_i2c_remove,
1979 .driver = {
1980 .name = I2C_DRV_NAME,
1981 .owner = THIS_MODULE,
1982 .pm = &mt_i2c_dev_pm_ops,
1983 .of_match_table = of_match_ptr(mtk_i2c_of_match),
1984 },
1985};
1986
1987#ifdef CONFIG_MTK_I2C_ARBITRATION
1988static s32 enable_arbitration(void)
1989{
1990 struct device_node *pericfg_node;
1991 void __iomem *pericfg_base;
1992
1993 pericfg_node = of_find_compatible_node(NULL, NULL, "mediatek,pericfg");
1994 if (!pericfg_node) {
1995 pr_info("Cannot find pericfg node\n");
1996 return -ENODEV;
1997 }
1998 pericfg_base = of_iomap(pericfg_node, 0);
1999 if (!pericfg_base) {
2000 pr_info("pericfg iomap failed\n");
2001 return -ENOMEM;
2002 }
2003 /* Enable the I2C arbitration */
2004 writew(0x3, pericfg_base + OFFSET_PERI_I2C_MODE_ENABLE);
2005 return 0;
2006}
2007#endif
2008
2009static s32 __init mt_i2c_init(void)
2010{
2011#ifdef CONFIG_MTK_I2C_ARBITRATION
2012 int ret;
2013
2014 ret = enable_arbitration();
2015 if (ret) {
2016 pr_info("Cannot enalbe arbitration.\n");
2017 return ret;
2018 }
2019#endif
2020
2021 if (!map_dma_regs())
2022 pr_info("Mapp dma regs successfully.\n");
2023 if (!mt_i2c_parse_comp_data())
2024 pr_info("Get compatible data from dts successfully.\n");
2025
2026 pr_info("%s: driver as platform device\n", __func__);
2027 return platform_driver_register(&mt_i2c_driver);
2028}
2029
2030static void __exit mt_i2c_exit(void)
2031{
2032 platform_driver_unregister(&mt_i2c_driver);
2033}
2034
2035module_init(mt_i2c_init);
2036module_exit(mt_i2c_exit);
2037
2038/* module_platform_driver(mt_i2c_driver); */
2039
2040MODULE_LICENSE("GPL");
2041MODULE_DESCRIPTION("MediaTek I2C Bus Driver");
2042MODULE_AUTHOR("Xudong Chen <xudong.chen@mediatek.com>");