2 * Copyright (c) 2014 MediaTek Inc.
3 * Author: Xudong Chen <xudong.chen@mediatek.com>
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.
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.
15 #include <linux/clk.h>
16 #include <linux/completion.h>
17 #include <linux/delay.h>
18 #include <linux/device.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/err.h>
21 #include <linux/errno.h>
22 #include <linux/i2c.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
26 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/of_address.h>
30 #include <linux/of_irq.h>
31 #include <linux/platform_device.h>
32 #include <linux/scatterlist.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
36 #define I2C_RS_TRANSFER (1 << 4)
37 #define I2C_HS_NACKERR (1 << 2)
38 #define I2C_ACKERR (1 << 1)
39 #define I2C_TRANSAC_COMP (1 << 0)
40 #define I2C_TRANSAC_START (1 << 0)
41 #define I2C_RS_MUL_CNFG (1 << 15)
42 #define I2C_RS_MUL_TRIG (1 << 14)
43 #define I2C_DCM_DISABLE 0x0000
44 #define I2C_IO_CONFIG_OPEN_DRAIN 0x0003
45 #define I2C_IO_CONFIG_PUSH_PULL 0x0000
46 #define I2C_SOFT_RST 0x0001
47 #define I2C_FIFO_ADDR_CLR 0x0001
48 #define I2C_DELAY_LEN 0x0002
49 #define I2C_ST_START_CON 0x8001
50 #define I2C_FS_START_CON 0x1800
51 #define I2C_TIME_CLR_VALUE 0x0000
52 #define I2C_TIME_DEFAULT_VALUE 0x0003
53 #define I2C_FS_TIME_INIT_VALUE 0x1303
54 #define I2C_WRRD_TRANAC_VALUE 0x0002
55 #define I2C_RD_TRANAC_VALUE 0x0001
57 #define I2C_DMA_CON_TX 0x0000
58 #define I2C_DMA_CON_RX 0x0001
59 #define I2C_DMA_START_EN 0x0001
60 #define I2C_DMA_INT_FLAG_NONE 0x0000
61 #define I2C_DMA_CLR_FLAG 0x0000
62 #define I2C_DMA_HARD_RST 0x0002
64 #define I2C_DEFAULT_SPEED 100000 /* hz */
65 #define MAX_FS_MODE_SPEED 400000
66 #define MAX_HS_MODE_SPEED 3400000
67 #define MAX_SAMPLE_CNT_DIV 8
68 #define MAX_STEP_CNT_DIV 64
69 #define MAX_HS_STEP_CNT_DIV 8
71 #define I2C_CONTROL_RS (0x1 << 1)
72 #define I2C_CONTROL_DMA_EN (0x1 << 2)
73 #define I2C_CONTROL_CLK_EXT_EN (0x1 << 3)
74 #define I2C_CONTROL_DIR_CHANGE (0x1 << 4)
75 #define I2C_CONTROL_ACKERR_DET_EN (0x1 << 5)
76 #define I2C_CONTROL_TRANSFER_LEN_CHANGE (0x1 << 6)
77 #define I2C_CONTROL_WRAPPER (0x1 << 0)
79 #define I2C_DRV_NAME "i2c-mt65xx"
81 enum DMA_REGS_OFFSET
{
82 OFFSET_INT_FLAG
= 0x0,
87 OFFSET_TX_MEM_ADDR
= 0x1c,
88 OFFSET_RX_MEM_ADDR
= 0x20,
93 enum i2c_trans_st_rs
{
95 I2C_TRANS_REPEATED_START
,
104 enum I2C_REGS_OFFSET
{
105 OFFSET_DATA_PORT
= 0x0,
106 OFFSET_SLAVE_ADDR
= 0x04,
107 OFFSET_INTR_MASK
= 0x08,
108 OFFSET_INTR_STAT
= 0x0c,
109 OFFSET_CONTROL
= 0x10,
110 OFFSET_TRANSFER_LEN
= 0x14,
111 OFFSET_TRANSAC_LEN
= 0x18,
112 OFFSET_DELAY_LEN
= 0x1c,
113 OFFSET_TIMING
= 0x20,
115 OFFSET_EXT_CONF
= 0x28,
116 OFFSET_FIFO_STAT
= 0x30,
117 OFFSET_FIFO_THRESH
= 0x34,
118 OFFSET_FIFO_ADDR_CLR
= 0x38,
119 OFFSET_IO_CONFIG
= 0x40,
120 OFFSET_RSV_DEBUG
= 0x44,
122 OFFSET_SOFTRESET
= 0x50,
123 OFFSET_DCM_EN
= 0x54,
124 OFFSET_PATH_DIR
= 0x60,
125 OFFSET_DEBUGSTAT
= 0x64,
126 OFFSET_DEBUGCTRL
= 0x68,
127 OFFSET_TRANSFER_LEN_AUX
= 0x6c,
130 struct mtk_i2c_compatible
{
131 const struct i2c_adapter_quirks
*quirks
;
132 unsigned char pmic_i2c
: 1;
133 unsigned char dcm
: 1;
134 unsigned char auto_restart
: 1;
138 struct i2c_adapter adap
; /* i2c host adapter */
140 struct completion msg_complete
;
142 /* set in i2c probe */
143 void __iomem
*base
; /* i2c base addr */
144 void __iomem
*pdmabase
; /* dma base address*/
145 struct clk
*clk_main
; /* main clock for i2c bus */
146 struct clk
*clk_dma
; /* DMA clock for i2c via DMA */
147 struct clk
*clk_pmic
; /* PMIC clock for i2c from PMIC */
148 bool have_pmic
; /* can use i2c pins from PMIC */
149 bool use_push_pull
; /* IO config push-pull mode */
151 u16 irq_stat
; /* interrupt status */
152 unsigned int speed_hz
; /* The speed in transfer */
153 enum mtk_trans_op op
;
156 const struct mtk_i2c_compatible
*dev_comp
;
159 static const struct i2c_adapter_quirks mt6577_i2c_quirks
= {
160 .flags
= I2C_AQ_COMB_WRITE_THEN_READ
,
162 .max_write_len
= 255,
164 .max_comb_1st_msg_len
= 255,
165 .max_comb_2nd_msg_len
= 31,
168 static const struct i2c_adapter_quirks mt8173_i2c_quirks
= {
169 .max_num_msgs
= 65535,
170 .max_write_len
= 65535,
171 .max_read_len
= 65535,
172 .max_comb_1st_msg_len
= 65535,
173 .max_comb_2nd_msg_len
= 65535,
176 static const struct mtk_i2c_compatible mt6577_compat
= {
177 .quirks
= &mt6577_i2c_quirks
,
183 static const struct mtk_i2c_compatible mt6589_compat
= {
184 .quirks
= &mt6577_i2c_quirks
,
190 static const struct mtk_i2c_compatible mt8173_compat
= {
191 .quirks
= &mt8173_i2c_quirks
,
197 static const struct of_device_id mtk_i2c_of_match
[] = {
198 { .compatible
= "mediatek,mt6577-i2c", .data
= &mt6577_compat
},
199 { .compatible
= "mediatek,mt6589-i2c", .data
= &mt6589_compat
},
200 { .compatible
= "mediatek,mt8173-i2c", .data
= &mt8173_compat
},
203 MODULE_DEVICE_TABLE(of
, mtk_i2c_of_match
);
205 static int mtk_i2c_clock_enable(struct mtk_i2c
*i2c
)
209 ret
= clk_prepare_enable(i2c
->clk_dma
);
213 ret
= clk_prepare_enable(i2c
->clk_main
);
217 if (i2c
->have_pmic
) {
218 ret
= clk_prepare_enable(i2c
->clk_pmic
);
225 clk_disable_unprepare(i2c
->clk_main
);
227 clk_disable_unprepare(i2c
->clk_dma
);
232 static void mtk_i2c_clock_disable(struct mtk_i2c
*i2c
)
235 clk_disable_unprepare(i2c
->clk_pmic
);
237 clk_disable_unprepare(i2c
->clk_main
);
238 clk_disable_unprepare(i2c
->clk_dma
);
241 static void mtk_i2c_init_hw(struct mtk_i2c
*i2c
)
245 writew(I2C_SOFT_RST
, i2c
->base
+ OFFSET_SOFTRESET
);
248 if (i2c
->use_push_pull
)
249 writew(I2C_IO_CONFIG_PUSH_PULL
, i2c
->base
+ OFFSET_IO_CONFIG
);
251 writew(I2C_IO_CONFIG_OPEN_DRAIN
, i2c
->base
+ OFFSET_IO_CONFIG
);
253 if (i2c
->dev_comp
->dcm
)
254 writew(I2C_DCM_DISABLE
, i2c
->base
+ OFFSET_DCM_EN
);
256 writew(i2c
->timing_reg
, i2c
->base
+ OFFSET_TIMING
);
257 writew(i2c
->high_speed_reg
, i2c
->base
+ OFFSET_HS
);
259 /* If use i2c pin from PMIC mt6397 side, need set PATH_DIR first */
261 writew(I2C_CONTROL_WRAPPER
, i2c
->base
+ OFFSET_PATH_DIR
);
263 control_reg
= I2C_CONTROL_ACKERR_DET_EN
|
264 I2C_CONTROL_CLK_EXT_EN
| I2C_CONTROL_DMA_EN
;
265 writew(control_reg
, i2c
->base
+ OFFSET_CONTROL
);
266 writew(I2C_DELAY_LEN
, i2c
->base
+ OFFSET_DELAY_LEN
);
268 writel(I2C_DMA_HARD_RST
, i2c
->pdmabase
+ OFFSET_RST
);
270 writel(I2C_DMA_CLR_FLAG
, i2c
->pdmabase
+ OFFSET_RST
);
274 * Calculate i2c port speed
277 * i2c_bus_freq = parent_clk / (clock_div * 2 * sample_cnt * step_cnt)
278 * clock_div: fixed in hardware, but may be various in different SoCs
280 * The calculation want to pick the highest bus frequency that is still
281 * less than or equal to i2c->speed_hz. The calculation try to get
282 * sample_cnt and step_cn
284 static int mtk_i2c_set_speed(struct mtk_i2c
*i2c
, unsigned int parent_clk
,
285 unsigned int clock_div
)
287 unsigned int clk_src
;
288 unsigned int step_cnt
;
289 unsigned int sample_cnt
;
290 unsigned int max_step_cnt
;
291 unsigned int target_speed
;
292 unsigned int base_sample_cnt
= MAX_SAMPLE_CNT_DIV
;
293 unsigned int base_step_cnt
;
294 unsigned int opt_div
;
295 unsigned int best_mul
;
296 unsigned int cnt_mul
;
298 clk_src
= parent_clk
/ clock_div
;
299 target_speed
= i2c
->speed_hz
;
301 if (target_speed
> MAX_HS_MODE_SPEED
)
302 target_speed
= MAX_HS_MODE_SPEED
;
304 if (target_speed
> MAX_FS_MODE_SPEED
)
305 max_step_cnt
= MAX_HS_STEP_CNT_DIV
;
307 max_step_cnt
= MAX_STEP_CNT_DIV
;
309 base_step_cnt
= max_step_cnt
;
310 /* Find the best combination */
311 opt_div
= DIV_ROUND_UP(clk_src
>> 1, target_speed
);
312 best_mul
= MAX_SAMPLE_CNT_DIV
* max_step_cnt
;
314 /* Search for the best pair (sample_cnt, step_cnt) with
315 * 0 < sample_cnt < MAX_SAMPLE_CNT_DIV
316 * 0 < step_cnt < max_step_cnt
317 * sample_cnt * step_cnt >= opt_div
318 * optimizing for sample_cnt * step_cnt being minimal
320 for (sample_cnt
= 1; sample_cnt
<= MAX_SAMPLE_CNT_DIV
; sample_cnt
++) {
321 step_cnt
= DIV_ROUND_UP(opt_div
, sample_cnt
);
322 cnt_mul
= step_cnt
* sample_cnt
;
323 if (step_cnt
> max_step_cnt
)
326 if (cnt_mul
< best_mul
) {
328 base_sample_cnt
= sample_cnt
;
329 base_step_cnt
= step_cnt
;
330 if (best_mul
== opt_div
)
335 sample_cnt
= base_sample_cnt
;
336 step_cnt
= base_step_cnt
;
338 if ((clk_src
/ (2 * sample_cnt
* step_cnt
)) > target_speed
) {
339 /* In this case, hardware can't support such
342 dev_dbg(i2c
->dev
, "Unsupported speed (%uhz)\n", target_speed
);
349 if (target_speed
> MAX_FS_MODE_SPEED
) {
350 /* Set the high speed mode register */
351 i2c
->timing_reg
= I2C_FS_TIME_INIT_VALUE
;
352 i2c
->high_speed_reg
= I2C_TIME_DEFAULT_VALUE
|
353 (sample_cnt
<< 12) | (step_cnt
<< 8);
355 i2c
->timing_reg
= (sample_cnt
<< 8) | (step_cnt
<< 0);
356 /* Disable the high speed transaction */
357 i2c
->high_speed_reg
= I2C_TIME_CLR_VALUE
;
363 static int mtk_i2c_do_transfer(struct mtk_i2c
*i2c
, struct i2c_msg
*msgs
,
364 int num
, int left_num
)
369 u16 restart_flag
= 0;
370 dma_addr_t rpaddr
= 0;
371 dma_addr_t wpaddr
= 0;
376 if (i2c
->dev_comp
->auto_restart
)
377 restart_flag
= I2C_RS_TRANSFER
;
379 reinit_completion(&i2c
->msg_complete
);
381 control_reg
= readw(i2c
->base
+ OFFSET_CONTROL
) &
382 ~(I2C_CONTROL_DIR_CHANGE
| I2C_CONTROL_RS
);
383 if ((i2c
->speed_hz
> 400000) || (left_num
>= 1))
384 control_reg
|= I2C_CONTROL_RS
;
386 if (i2c
->op
== I2C_MASTER_WRRD
)
387 control_reg
|= I2C_CONTROL_DIR_CHANGE
| I2C_CONTROL_RS
;
389 writew(control_reg
, i2c
->base
+ OFFSET_CONTROL
);
391 /* set start condition */
392 if (i2c
->speed_hz
<= 100000)
393 writew(I2C_ST_START_CON
, i2c
->base
+ OFFSET_EXT_CONF
);
395 writew(I2C_FS_START_CON
, i2c
->base
+ OFFSET_EXT_CONF
);
397 addr_reg
= msgs
->addr
<< 1;
398 if (i2c
->op
== I2C_MASTER_RD
)
401 writew(addr_reg
, i2c
->base
+ OFFSET_SLAVE_ADDR
);
403 /* Clear interrupt status */
404 writew(restart_flag
| I2C_HS_NACKERR
| I2C_ACKERR
|
405 I2C_TRANSAC_COMP
, i2c
->base
+ OFFSET_INTR_STAT
);
406 writew(I2C_FIFO_ADDR_CLR
, i2c
->base
+ OFFSET_FIFO_ADDR_CLR
);
408 /* Enable interrupt */
409 writew(restart_flag
| I2C_HS_NACKERR
| I2C_ACKERR
|
410 I2C_TRANSAC_COMP
, i2c
->base
+ OFFSET_INTR_MASK
);
412 /* Set transfer and transaction len */
413 if (i2c
->op
== I2C_MASTER_WRRD
) {
414 writew(msgs
->len
| ((msgs
+ 1)->len
) << 8,
415 i2c
->base
+ OFFSET_TRANSFER_LEN
);
416 writew(I2C_WRRD_TRANAC_VALUE
, i2c
->base
+ OFFSET_TRANSAC_LEN
);
418 writew(msgs
->len
, i2c
->base
+ OFFSET_TRANSFER_LEN
);
419 writew(num
, i2c
->base
+ OFFSET_TRANSAC_LEN
);
422 /* Prepare buffer data to start transfer */
423 if (i2c
->op
== I2C_MASTER_RD
) {
424 writel(I2C_DMA_INT_FLAG_NONE
, i2c
->pdmabase
+ OFFSET_INT_FLAG
);
425 writel(I2C_DMA_CON_RX
, i2c
->pdmabase
+ OFFSET_CON
);
426 rpaddr
= dma_map_single(i2c
->dev
, msgs
->buf
,
427 msgs
->len
, DMA_FROM_DEVICE
);
428 if (dma_mapping_error(i2c
->dev
, rpaddr
))
430 writel((u32
)rpaddr
, i2c
->pdmabase
+ OFFSET_RX_MEM_ADDR
);
431 writel(msgs
->len
, i2c
->pdmabase
+ OFFSET_RX_LEN
);
432 } else if (i2c
->op
== I2C_MASTER_WR
) {
433 writel(I2C_DMA_INT_FLAG_NONE
, i2c
->pdmabase
+ OFFSET_INT_FLAG
);
434 writel(I2C_DMA_CON_TX
, i2c
->pdmabase
+ OFFSET_CON
);
435 wpaddr
= dma_map_single(i2c
->dev
, msgs
->buf
,
436 msgs
->len
, DMA_TO_DEVICE
);
437 if (dma_mapping_error(i2c
->dev
, wpaddr
))
439 writel((u32
)wpaddr
, i2c
->pdmabase
+ OFFSET_TX_MEM_ADDR
);
440 writel(msgs
->len
, i2c
->pdmabase
+ OFFSET_TX_LEN
);
442 writel(I2C_DMA_CLR_FLAG
, i2c
->pdmabase
+ OFFSET_INT_FLAG
);
443 writel(I2C_DMA_CLR_FLAG
, i2c
->pdmabase
+ OFFSET_CON
);
444 wpaddr
= dma_map_single(i2c
->dev
, msgs
->buf
,
445 msgs
->len
, DMA_TO_DEVICE
);
446 if (dma_mapping_error(i2c
->dev
, wpaddr
))
448 rpaddr
= dma_map_single(i2c
->dev
, (msgs
+ 1)->buf
,
451 if (dma_mapping_error(i2c
->dev
, rpaddr
)) {
452 dma_unmap_single(i2c
->dev
, wpaddr
,
453 msgs
->len
, DMA_TO_DEVICE
);
456 writel((u32
)wpaddr
, i2c
->pdmabase
+ OFFSET_TX_MEM_ADDR
);
457 writel((u32
)rpaddr
, i2c
->pdmabase
+ OFFSET_RX_MEM_ADDR
);
458 writel(msgs
->len
, i2c
->pdmabase
+ OFFSET_TX_LEN
);
459 writel((msgs
+ 1)->len
, i2c
->pdmabase
+ OFFSET_RX_LEN
);
462 writel(I2C_DMA_START_EN
, i2c
->pdmabase
+ OFFSET_EN
);
464 if (!i2c
->dev_comp
->auto_restart
) {
465 start_reg
= I2C_TRANSAC_START
;
467 start_reg
= I2C_TRANSAC_START
| I2C_RS_MUL_TRIG
;
469 start_reg
|= I2C_RS_MUL_CNFG
;
471 writew(start_reg
, i2c
->base
+ OFFSET_START
);
473 ret
= wait_for_completion_timeout(&i2c
->msg_complete
,
476 /* Clear interrupt mask */
477 writew(~(restart_flag
| I2C_HS_NACKERR
| I2C_ACKERR
|
478 I2C_TRANSAC_COMP
), i2c
->base
+ OFFSET_INTR_MASK
);
480 if (i2c
->op
== I2C_MASTER_WR
) {
481 dma_unmap_single(i2c
->dev
, wpaddr
,
482 msgs
->len
, DMA_TO_DEVICE
);
483 } else if (i2c
->op
== I2C_MASTER_RD
) {
484 dma_unmap_single(i2c
->dev
, rpaddr
,
485 msgs
->len
, DMA_FROM_DEVICE
);
487 dma_unmap_single(i2c
->dev
, wpaddr
, msgs
->len
,
489 dma_unmap_single(i2c
->dev
, rpaddr
, (msgs
+ 1)->len
,
494 dev_dbg(i2c
->dev
, "addr: %x, transfer timeout\n", msgs
->addr
);
495 mtk_i2c_init_hw(i2c
);
499 completion_done(&i2c
->msg_complete
);
501 if (i2c
->irq_stat
& (I2C_HS_NACKERR
| I2C_ACKERR
)) {
502 dev_dbg(i2c
->dev
, "addr: %x, transfer ACK error\n", msgs
->addr
);
503 mtk_i2c_init_hw(i2c
);
510 static int mtk_i2c_transfer(struct i2c_adapter
*adap
,
511 struct i2c_msg msgs
[], int num
)
515 struct mtk_i2c
*i2c
= i2c_get_adapdata(adap
);
517 ret
= mtk_i2c_clock_enable(i2c
);
523 dev_dbg(i2c
->dev
, "data buffer is NULL.\n");
528 if (msgs
->flags
& I2C_M_RD
)
529 i2c
->op
= I2C_MASTER_RD
;
531 i2c
->op
= I2C_MASTER_WR
;
533 if (!i2c
->dev_comp
->auto_restart
) {
535 /* combined two messages into one transaction */
536 i2c
->op
= I2C_MASTER_WRRD
;
541 /* always use DMA mode. */
542 ret
= mtk_i2c_do_transfer(i2c
, msgs
, num
, left_num
);
548 /* the return value is number of executed messages */
552 mtk_i2c_clock_disable(i2c
);
556 static irqreturn_t
mtk_i2c_irq(int irqno
, void *dev_id
)
558 struct mtk_i2c
*i2c
= dev_id
;
559 u16 restart_flag
= 0;
562 if (i2c
->dev_comp
->auto_restart
)
563 restart_flag
= I2C_RS_TRANSFER
;
565 intr_stat
= readw(i2c
->base
+ OFFSET_INTR_STAT
);
566 writew(intr_stat
, i2c
->base
+ OFFSET_INTR_STAT
);
569 * when occurs ack error, i2c controller generate two interrupts
570 * first is the ack error interrupt, then the complete interrupt
571 * i2c->irq_stat need keep the two interrupt value.
573 i2c
->irq_stat
|= intr_stat
;
574 if (i2c
->irq_stat
& (I2C_TRANSAC_COMP
| restart_flag
))
575 complete(&i2c
->msg_complete
);
580 static u32
mtk_i2c_functionality(struct i2c_adapter
*adap
)
582 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
585 static const struct i2c_algorithm mtk_i2c_algorithm
= {
586 .master_xfer
= mtk_i2c_transfer
,
587 .functionality
= mtk_i2c_functionality
,
590 static int mtk_i2c_parse_dt(struct device_node
*np
, struct mtk_i2c
*i2c
,
591 unsigned int *clk_src_div
)
595 ret
= of_property_read_u32(np
, "clock-frequency", &i2c
->speed_hz
);
597 i2c
->speed_hz
= I2C_DEFAULT_SPEED
;
599 ret
= of_property_read_u32(np
, "clock-div", clk_src_div
);
603 if (*clk_src_div
== 0)
606 i2c
->have_pmic
= of_property_read_bool(np
, "mediatek,have-pmic");
608 of_property_read_bool(np
, "mediatek,use-push-pull");
613 static int mtk_i2c_probe(struct platform_device
*pdev
)
615 const struct of_device_id
*of_id
;
619 unsigned int clk_src_div
;
620 struct resource
*res
;
623 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(*i2c
), GFP_KERNEL
);
627 ret
= mtk_i2c_parse_dt(pdev
->dev
.of_node
, i2c
, &clk_src_div
);
631 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
632 i2c
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
633 if (IS_ERR(i2c
->base
))
634 return PTR_ERR(i2c
->base
);
636 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
637 i2c
->pdmabase
= devm_ioremap_resource(&pdev
->dev
, res
);
638 if (IS_ERR(i2c
->pdmabase
))
639 return PTR_ERR(i2c
->pdmabase
);
641 irq
= platform_get_irq(pdev
, 0);
645 init_completion(&i2c
->msg_complete
);
647 of_id
= of_match_node(mtk_i2c_of_match
, pdev
->dev
.of_node
);
651 i2c
->dev_comp
= of_id
->data
;
652 i2c
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
653 i2c
->dev
= &pdev
->dev
;
654 i2c
->adap
.dev
.parent
= &pdev
->dev
;
655 i2c
->adap
.owner
= THIS_MODULE
;
656 i2c
->adap
.algo
= &mtk_i2c_algorithm
;
657 i2c
->adap
.quirks
= i2c
->dev_comp
->quirks
;
658 i2c
->adap
.timeout
= 2 * HZ
;
659 i2c
->adap
.retries
= 1;
661 if (i2c
->have_pmic
&& !i2c
->dev_comp
->pmic_i2c
)
664 i2c
->clk_main
= devm_clk_get(&pdev
->dev
, "main");
665 if (IS_ERR(i2c
->clk_main
)) {
666 dev_err(&pdev
->dev
, "cannot get main clock\n");
667 return PTR_ERR(i2c
->clk_main
);
670 i2c
->clk_dma
= devm_clk_get(&pdev
->dev
, "dma");
671 if (IS_ERR(i2c
->clk_dma
)) {
672 dev_err(&pdev
->dev
, "cannot get dma clock\n");
673 return PTR_ERR(i2c
->clk_dma
);
677 if (i2c
->have_pmic
) {
678 i2c
->clk_pmic
= devm_clk_get(&pdev
->dev
, "pmic");
679 if (IS_ERR(i2c
->clk_pmic
)) {
680 dev_err(&pdev
->dev
, "cannot get pmic clock\n");
681 return PTR_ERR(i2c
->clk_pmic
);
686 strlcpy(i2c
->adap
.name
, I2C_DRV_NAME
, sizeof(i2c
->adap
.name
));
688 ret
= mtk_i2c_set_speed(i2c
, clk_get_rate(clk
), clk_src_div
);
690 dev_err(&pdev
->dev
, "Failed to set the speed.\n");
694 ret
= mtk_i2c_clock_enable(i2c
);
696 dev_err(&pdev
->dev
, "clock enable failed!\n");
699 mtk_i2c_init_hw(i2c
);
700 mtk_i2c_clock_disable(i2c
);
702 ret
= devm_request_irq(&pdev
->dev
, irq
, mtk_i2c_irq
,
703 IRQF_TRIGGER_NONE
, I2C_DRV_NAME
, i2c
);
706 "Request I2C IRQ %d fail\n", irq
);
710 i2c_set_adapdata(&i2c
->adap
, i2c
);
711 ret
= i2c_add_adapter(&i2c
->adap
);
713 dev_err(&pdev
->dev
, "Failed to add i2c bus to i2c core\n");
717 platform_set_drvdata(pdev
, i2c
);
722 static int mtk_i2c_remove(struct platform_device
*pdev
)
724 struct mtk_i2c
*i2c
= platform_get_drvdata(pdev
);
726 i2c_del_adapter(&i2c
->adap
);
731 #ifdef CONFIG_PM_SLEEP
732 static int mtk_i2c_resume(struct device
*dev
)
734 struct mtk_i2c
*i2c
= dev_get_drvdata(dev
);
736 mtk_i2c_init_hw(i2c
);
742 static const struct dev_pm_ops mtk_i2c_pm
= {
743 SET_SYSTEM_SLEEP_PM_OPS(NULL
, mtk_i2c_resume
)
746 static struct platform_driver mtk_i2c_driver
= {
747 .probe
= mtk_i2c_probe
,
748 .remove
= mtk_i2c_remove
,
750 .name
= I2C_DRV_NAME
,
752 .of_match_table
= of_match_ptr(mtk_i2c_of_match
),
756 module_platform_driver(mtk_i2c_driver
);
758 MODULE_LICENSE("GPL v2");
759 MODULE_DESCRIPTION("MediaTek I2C Bus Driver");
760 MODULE_AUTHOR("Xudong Chen <xudong.chen@mediatek.com>");