1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Ingenic JZ4780 I2C bus driver
5 * Copyright (C) 2006 - 2009 Ingenic Semiconductor Inc.
6 * Copyright (C) 2015 Imagination Technologies
9 #include <linux/bitops.h>
10 #include <linux/clk.h>
11 #include <linux/completion.h>
12 #include <linux/delay.h>
13 #include <linux/errno.h>
14 #include <linux/i2c.h>
15 #include <linux/init.h>
16 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
23 #include <linux/time.h>
25 #define JZ4780_I2C_CTRL 0x00
26 #define JZ4780_I2C_TAR 0x04
27 #define JZ4780_I2C_SAR 0x08
28 #define JZ4780_I2C_DC 0x10
29 #define JZ4780_I2C_SHCNT 0x14
30 #define JZ4780_I2C_SLCNT 0x18
31 #define JZ4780_I2C_FHCNT 0x1C
32 #define JZ4780_I2C_FLCNT 0x20
33 #define JZ4780_I2C_INTST 0x2C
34 #define JZ4780_I2C_INTM 0x30
35 #define JZ4780_I2C_RXTL 0x38
36 #define JZ4780_I2C_TXTL 0x3C
37 #define JZ4780_I2C_CINTR 0x40
38 #define JZ4780_I2C_CRXUF 0x44
39 #define JZ4780_I2C_CRXOF 0x48
40 #define JZ4780_I2C_CTXOF 0x4C
41 #define JZ4780_I2C_CRXREQ 0x50
42 #define JZ4780_I2C_CTXABRT 0x54
43 #define JZ4780_I2C_CRXDONE 0x58
44 #define JZ4780_I2C_CACT 0x5C
45 #define JZ4780_I2C_CSTP 0x60
46 #define JZ4780_I2C_CSTT 0x64
47 #define JZ4780_I2C_CGC 0x68
48 #define JZ4780_I2C_ENB 0x6C
49 #define JZ4780_I2C_STA 0x70
50 #define JZ4780_I2C_TXABRT 0x80
51 #define JZ4780_I2C_DMACR 0x88
52 #define JZ4780_I2C_DMATDLR 0x8C
53 #define JZ4780_I2C_DMARDLR 0x90
54 #define JZ4780_I2C_SDASU 0x94
55 #define JZ4780_I2C_ACKGC 0x98
56 #define JZ4780_I2C_ENSTA 0x9C
57 #define JZ4780_I2C_SDAHD 0xD0
59 #define JZ4780_I2C_CTRL_STPHLD BIT(7)
60 #define JZ4780_I2C_CTRL_SLVDIS BIT(6)
61 #define JZ4780_I2C_CTRL_REST BIT(5)
62 #define JZ4780_I2C_CTRL_MATP BIT(4)
63 #define JZ4780_I2C_CTRL_SATP BIT(3)
64 #define JZ4780_I2C_CTRL_SPDF BIT(2)
65 #define JZ4780_I2C_CTRL_SPDS BIT(1)
66 #define JZ4780_I2C_CTRL_MD BIT(0)
68 #define JZ4780_I2C_STA_SLVACT BIT(6)
69 #define JZ4780_I2C_STA_MSTACT BIT(5)
70 #define JZ4780_I2C_STA_RFF BIT(4)
71 #define JZ4780_I2C_STA_RFNE BIT(3)
72 #define JZ4780_I2C_STA_TFE BIT(2)
73 #define JZ4780_I2C_STA_TFNF BIT(1)
74 #define JZ4780_I2C_STA_ACT BIT(0)
76 static const char * const jz4780_i2c_abrt_src
[] = {
87 "ABRT_10B_RD_NORSTRT",
95 #define JZ4780_I2C_INTST_IGC BIT(11)
96 #define JZ4780_I2C_INTST_ISTT BIT(10)
97 #define JZ4780_I2C_INTST_ISTP BIT(9)
98 #define JZ4780_I2C_INTST_IACT BIT(8)
99 #define JZ4780_I2C_INTST_RXDN BIT(7)
100 #define JZ4780_I2C_INTST_TXABT BIT(6)
101 #define JZ4780_I2C_INTST_RDREQ BIT(5)
102 #define JZ4780_I2C_INTST_TXEMP BIT(4)
103 #define JZ4780_I2C_INTST_TXOF BIT(3)
104 #define JZ4780_I2C_INTST_RXFL BIT(2)
105 #define JZ4780_I2C_INTST_RXOF BIT(1)
106 #define JZ4780_I2C_INTST_RXUF BIT(0)
108 #define JZ4780_I2C_INTM_MIGC BIT(11)
109 #define JZ4780_I2C_INTM_MISTT BIT(10)
110 #define JZ4780_I2C_INTM_MISTP BIT(9)
111 #define JZ4780_I2C_INTM_MIACT BIT(8)
112 #define JZ4780_I2C_INTM_MRXDN BIT(7)
113 #define JZ4780_I2C_INTM_MTXABT BIT(6)
114 #define JZ4780_I2C_INTM_MRDREQ BIT(5)
115 #define JZ4780_I2C_INTM_MTXEMP BIT(4)
116 #define JZ4780_I2C_INTM_MTXOF BIT(3)
117 #define JZ4780_I2C_INTM_MRXFL BIT(2)
118 #define JZ4780_I2C_INTM_MRXOF BIT(1)
119 #define JZ4780_I2C_INTM_MRXUF BIT(0)
121 #define JZ4780_I2C_DC_READ BIT(8)
123 #define JZ4780_I2C_SDAHD_HDENB BIT(8)
125 #define JZ4780_I2C_ENB_I2C BIT(0)
127 #define JZ4780_I2CSHCNT_ADJUST(n) (((n) - 8) < 6 ? 6 : ((n) - 8))
128 #define JZ4780_I2CSLCNT_ADJUST(n) (((n) - 1) < 8 ? 8 : ((n) - 1))
129 #define JZ4780_I2CFHCNT_ADJUST(n) (((n) - 8) < 6 ? 6 : ((n) - 8))
130 #define JZ4780_I2CFLCNT_ADJUST(n) (((n) - 1) < 8 ? 8 : ((n) - 1))
132 #define JZ4780_I2C_FIFO_LEN 16
134 #define RX_LEVEL (JZ4780_I2C_FIFO_LEN - TX_LEVEL - 1)
136 #define JZ4780_I2C_TIMEOUT 300
144 struct i2c_adapter adap
;
146 /* lock to protect rbuf and wbuf between xfer_rd/wr and irq handler */
149 /* beginning of lock scope */
162 int data_buf
[BUFSIZE
];
163 int cmd_buf
[BUFSIZE
];
166 /* end of lock scope */
167 struct completion trans_waitq
;
170 static inline unsigned short jz4780_i2c_readw(struct jz4780_i2c
*i2c
,
171 unsigned long offset
)
173 return readw(i2c
->iomem
+ offset
);
176 static inline void jz4780_i2c_writew(struct jz4780_i2c
*i2c
,
177 unsigned long offset
, unsigned short val
)
179 writew(val
, i2c
->iomem
+ offset
);
182 static int jz4780_i2c_disable(struct jz4780_i2c
*i2c
)
184 unsigned short regval
;
185 unsigned long loops
= 5;
187 jz4780_i2c_writew(i2c
, JZ4780_I2C_ENB
, 0);
190 regval
= jz4780_i2c_readw(i2c
, JZ4780_I2C_ENSTA
);
191 if (!(regval
& JZ4780_I2C_ENB_I2C
))
194 usleep_range(5000, 15000);
197 dev_err(&i2c
->adap
.dev
, "disable failed: ENSTA=0x%04x\n", regval
);
201 static int jz4780_i2c_enable(struct jz4780_i2c
*i2c
)
203 unsigned short regval
;
204 unsigned long loops
= 5;
206 jz4780_i2c_writew(i2c
, JZ4780_I2C_ENB
, 1);
209 regval
= jz4780_i2c_readw(i2c
, JZ4780_I2C_ENSTA
);
210 if (regval
& JZ4780_I2C_ENB_I2C
)
213 usleep_range(5000, 15000);
216 dev_err(&i2c
->adap
.dev
, "enable failed: ENSTA=0x%04x\n", regval
);
220 static int jz4780_i2c_set_target(struct jz4780_i2c
*i2c
, unsigned char address
)
222 unsigned short regval
;
223 unsigned long loops
= 5;
226 regval
= jz4780_i2c_readw(i2c
, JZ4780_I2C_STA
);
227 if ((regval
& JZ4780_I2C_STA_TFE
) &&
228 !(regval
& JZ4780_I2C_STA_MSTACT
))
231 usleep_range(5000, 15000);
235 jz4780_i2c_writew(i2c
, JZ4780_I2C_TAR
, address
);
239 dev_err(&i2c
->adap
.dev
,
240 "set device to address 0x%02x failed, STA=0x%04x\n",
246 static int jz4780_i2c_set_speed(struct jz4780_i2c
*i2c
)
248 int dev_clk_khz
= clk_get_rate(i2c
->clk
) / 1000;
249 int cnt_high
= 0; /* HIGH period count of the SCL clock */
250 int cnt_low
= 0; /* LOW period count of the SCL clock */
251 int cnt_period
= 0; /* period count of the SCL clock */
254 unsigned short tmp
= 0;
255 int i2c_clk
= i2c
->speed
;
257 if (jz4780_i2c_disable(i2c
))
258 dev_dbg(&i2c
->adap
.dev
, "i2c not disabled\n");
261 * 1 JZ4780_I2C cycle equals to cnt_period PCLK(i2c_clk)
262 * standard mode, min LOW and HIGH period are 4700 ns and 4000 ns
263 * fast mode, min LOW and HIGH period are 1300 ns and 600 ns
265 cnt_period
= dev_clk_khz
/ i2c_clk
;
268 cnt_high
= (cnt_period
* 4000) / (4700 + 4000);
270 cnt_high
= (cnt_period
* 600) / (1300 + 600);
272 cnt_low
= cnt_period
- cnt_high
;
275 * NOTE: JZ4780_I2C_CTRL_REST can't set when i2c enabled, because
276 * normal read are 2 messages, we cannot disable i2c controller
277 * between these two messages, this means that we must always set
278 * JZ4780_I2C_CTRL_REST when init JZ4780_I2C_CTRL
281 if (i2c_clk
<= 100) {
282 tmp
= JZ4780_I2C_CTRL_SPDS
| JZ4780_I2C_CTRL_REST
283 | JZ4780_I2C_CTRL_SLVDIS
| JZ4780_I2C_CTRL_MD
;
284 jz4780_i2c_writew(i2c
, JZ4780_I2C_CTRL
, tmp
);
286 jz4780_i2c_writew(i2c
, JZ4780_I2C_SHCNT
,
287 JZ4780_I2CSHCNT_ADJUST(cnt_high
));
288 jz4780_i2c_writew(i2c
, JZ4780_I2C_SLCNT
,
289 JZ4780_I2CSLCNT_ADJUST(cnt_low
));
291 tmp
= JZ4780_I2C_CTRL_SPDF
| JZ4780_I2C_CTRL_REST
292 | JZ4780_I2C_CTRL_SLVDIS
| JZ4780_I2C_CTRL_MD
;
293 jz4780_i2c_writew(i2c
, JZ4780_I2C_CTRL
, tmp
);
295 jz4780_i2c_writew(i2c
, JZ4780_I2C_FHCNT
,
296 JZ4780_I2CFHCNT_ADJUST(cnt_high
));
297 jz4780_i2c_writew(i2c
, JZ4780_I2C_FLCNT
,
298 JZ4780_I2CFLCNT_ADJUST(cnt_low
));
302 * a i2c device must internally provide a hold time at least 300ns
304 * Standard Mode: min=300ns, max=3450ns
305 * Fast Mode: min=0ns, max=900ns
307 * Standard Mode: min=250ns, max=infinite
308 * Fast Mode: min=100(250ns is recommended), max=infinite
310 * 1i2c_clk = 10^6 / dev_clk_khz
311 * on FPGA, dev_clk_khz = 12000, so 1i2c_clk = 1000/12 = 83ns
312 * on Pisces(1008M), dev_clk_khz=126000, so 1i2c_clk = 1000 / 126 = 8ns
314 * The actual hold time is (SDAHD + 1) * (i2c_clk period).
316 * Length of setup time calculated using (SDASU - 1) * (ic_clk_period)
319 if (i2c_clk
<= 100) { /* standard mode */
327 hold_time
= ((hold_time
* dev_clk_khz
) / 1000000) - 1;
328 setup_time
= ((setup_time
* dev_clk_khz
) / 1000000) + 1;
330 if (setup_time
> 255)
336 jz4780_i2c_writew(i2c
, JZ4780_I2C_SDASU
, setup_time
);
341 if (hold_time
>= 0) {
342 /*i2c hold time enable */
343 hold_time
|= JZ4780_I2C_SDAHD_HDENB
;
344 jz4780_i2c_writew(i2c
, JZ4780_I2C_SDAHD
, hold_time
);
346 /* disable hold time */
347 jz4780_i2c_writew(i2c
, JZ4780_I2C_SDAHD
, 0);
353 static int jz4780_i2c_cleanup(struct jz4780_i2c
*i2c
)
359 spin_lock_irqsave(&i2c
->lock
, flags
);
361 /* can send stop now if need */
362 tmp
= jz4780_i2c_readw(i2c
, JZ4780_I2C_CTRL
);
363 tmp
&= ~JZ4780_I2C_CTRL_STPHLD
;
364 jz4780_i2c_writew(i2c
, JZ4780_I2C_CTRL
, tmp
);
366 /* disable all interrupts first */
367 jz4780_i2c_writew(i2c
, JZ4780_I2C_INTM
, 0);
369 /* then clear all interrupts */
370 jz4780_i2c_readw(i2c
, JZ4780_I2C_CTXABRT
);
371 jz4780_i2c_readw(i2c
, JZ4780_I2C_CINTR
);
373 /* then disable the controller */
374 tmp
= jz4780_i2c_readw(i2c
, JZ4780_I2C_CTRL
);
375 tmp
&= ~JZ4780_I2C_ENB_I2C
;
376 jz4780_i2c_writew(i2c
, JZ4780_I2C_CTRL
, tmp
);
378 tmp
|= JZ4780_I2C_ENB_I2C
;
379 jz4780_i2c_writew(i2c
, JZ4780_I2C_CTRL
, tmp
);
381 spin_unlock_irqrestore(&i2c
->lock
, flags
);
383 ret
= jz4780_i2c_disable(i2c
);
385 dev_err(&i2c
->adap
.dev
,
386 "unable to disable device during cleanup!\n");
388 if (unlikely(jz4780_i2c_readw(i2c
, JZ4780_I2C_INTM
)
389 & jz4780_i2c_readw(i2c
, JZ4780_I2C_INTST
)))
390 dev_err(&i2c
->adap
.dev
,
391 "device has interrupts after a complete cleanup!\n");
396 static int jz4780_i2c_prepare(struct jz4780_i2c
*i2c
)
398 jz4780_i2c_set_speed(i2c
);
399 return jz4780_i2c_enable(i2c
);
402 static void jz4780_i2c_send_rcmd(struct jz4780_i2c
*i2c
, int cmd_count
)
406 for (i
= 0; i
< cmd_count
; i
++)
407 jz4780_i2c_writew(i2c
, JZ4780_I2C_DC
, JZ4780_I2C_DC_READ
);
410 static void jz4780_i2c_trans_done(struct jz4780_i2c
*i2c
)
412 jz4780_i2c_writew(i2c
, JZ4780_I2C_INTM
, 0);
413 complete(&i2c
->trans_waitq
);
416 static irqreturn_t
jz4780_i2c_irq(int irqno
, void *dev_id
)
419 unsigned short intst
;
420 unsigned short intmsk
;
421 struct jz4780_i2c
*i2c
= dev_id
;
424 spin_lock_irqsave(&i2c
->lock
, flags
);
425 intmsk
= jz4780_i2c_readw(i2c
, JZ4780_I2C_INTM
);
426 intst
= jz4780_i2c_readw(i2c
, JZ4780_I2C_INTST
);
430 if (intst
& JZ4780_I2C_INTST_TXABT
) {
431 jz4780_i2c_trans_done(i2c
);
435 if (intst
& JZ4780_I2C_INTST_RXOF
) {
436 dev_dbg(&i2c
->adap
.dev
, "received fifo overflow!\n");
437 jz4780_i2c_trans_done(i2c
);
442 * When reading, always drain RX FIFO before we send more Read
443 * Commands to avoid fifo overrun
445 if (i2c
->is_write
== 0) {
448 while ((jz4780_i2c_readw(i2c
, JZ4780_I2C_STA
)
449 & JZ4780_I2C_STA_RFNE
)) {
450 *(i2c
->rbuf
++) = jz4780_i2c_readw(i2c
, JZ4780_I2C_DC
)
452 i2c
->rd_data_xfered
++;
453 if (i2c
->rd_data_xfered
== i2c
->rd_total_len
) {
454 jz4780_i2c_trans_done(i2c
);
459 rd_left
= i2c
->rd_total_len
- i2c
->rd_data_xfered
;
461 if (rd_left
<= JZ4780_I2C_FIFO_LEN
)
462 jz4780_i2c_writew(i2c
, JZ4780_I2C_RXTL
, rd_left
- 1);
465 if (intst
& JZ4780_I2C_INTST_TXEMP
) {
466 if (i2c
->is_write
== 0) {
467 int cmd_left
= i2c
->rd_total_len
- i2c
->rd_cmd_xfered
;
468 int max_send
= (JZ4780_I2C_FIFO_LEN
- 1)
469 - (i2c
->rd_cmd_xfered
470 - i2c
->rd_data_xfered
);
471 int cmd_to_send
= min(cmd_left
, max_send
);
473 if (i2c
->rd_cmd_xfered
!= 0)
474 cmd_to_send
= min(cmd_to_send
,
479 jz4780_i2c_send_rcmd(i2c
, cmd_to_send
);
480 i2c
->rd_cmd_xfered
+= cmd_to_send
;
483 cmd_left
= i2c
->rd_total_len
- i2c
->rd_cmd_xfered
;
485 intmsk
= jz4780_i2c_readw(i2c
, JZ4780_I2C_INTM
);
486 intmsk
&= ~JZ4780_I2C_INTM_MTXEMP
;
487 jz4780_i2c_writew(i2c
, JZ4780_I2C_INTM
, intmsk
);
489 tmp
= jz4780_i2c_readw(i2c
, JZ4780_I2C_CTRL
);
490 tmp
&= ~JZ4780_I2C_CTRL_STPHLD
;
491 jz4780_i2c_writew(i2c
, JZ4780_I2C_CTRL
, tmp
);
495 unsigned short i2c_sta
;
497 i2c_sta
= jz4780_i2c_readw(i2c
, JZ4780_I2C_STA
);
499 while ((i2c_sta
& JZ4780_I2C_STA_TFNF
) &&
501 i2c_sta
= jz4780_i2c_readw(i2c
, JZ4780_I2C_STA
);
503 data
&= ~JZ4780_I2C_DC_READ
;
504 jz4780_i2c_writew(i2c
, JZ4780_I2C_DC
,
510 if (i2c
->wt_len
== 0) {
511 if (!i2c
->stop_hold
) {
512 tmp
= jz4780_i2c_readw(i2c
,
514 tmp
&= ~JZ4780_I2C_CTRL_STPHLD
;
515 jz4780_i2c_writew(i2c
, JZ4780_I2C_CTRL
,
519 jz4780_i2c_trans_done(i2c
);
526 spin_unlock_irqrestore(&i2c
->lock
, flags
);
530 static void jz4780_i2c_txabrt(struct jz4780_i2c
*i2c
, int src
)
534 dev_err(&i2c
->adap
.dev
, "txabrt: 0x%08x\n", src
);
535 dev_err(&i2c
->adap
.dev
, "device addr=%x\n",
536 jz4780_i2c_readw(i2c
, JZ4780_I2C_TAR
));
537 dev_err(&i2c
->adap
.dev
, "send cmd count:%d %d\n",
538 i2c
->cmd
, i2c
->cmd_buf
[i2c
->cmd
]);
539 dev_err(&i2c
->adap
.dev
, "receive data count:%d %d\n",
540 i2c
->cmd
, i2c
->data_buf
[i2c
->cmd
]);
542 for (i
= 0; i
< 16; i
++) {
544 dev_dbg(&i2c
->adap
.dev
, "I2C TXABRT[%d]=%s\n",
545 i
, jz4780_i2c_abrt_src
[i
]);
549 static inline int jz4780_i2c_xfer_read(struct jz4780_i2c
*i2c
,
550 unsigned char *buf
, int len
, int cnt
,
555 int wait_time
= JZ4780_I2C_TIMEOUT
* (len
+ 5);
561 spin_lock_irqsave(&i2c
->lock
, flags
);
566 i2c
->rd_total_len
= len
;
567 i2c
->rd_data_xfered
= 0;
568 i2c
->rd_cmd_xfered
= 0;
570 if (len
<= JZ4780_I2C_FIFO_LEN
)
571 jz4780_i2c_writew(i2c
, JZ4780_I2C_RXTL
, len
- 1);
573 jz4780_i2c_writew(i2c
, JZ4780_I2C_RXTL
, RX_LEVEL
);
575 jz4780_i2c_writew(i2c
, JZ4780_I2C_TXTL
, TX_LEVEL
);
577 jz4780_i2c_writew(i2c
, JZ4780_I2C_INTM
,
578 JZ4780_I2C_INTM_MRXFL
| JZ4780_I2C_INTM_MTXEMP
579 | JZ4780_I2C_INTM_MTXABT
| JZ4780_I2C_INTM_MRXOF
);
581 tmp
= jz4780_i2c_readw(i2c
, JZ4780_I2C_CTRL
);
582 tmp
|= JZ4780_I2C_CTRL_STPHLD
;
583 jz4780_i2c_writew(i2c
, JZ4780_I2C_CTRL
, tmp
);
585 spin_unlock_irqrestore(&i2c
->lock
, flags
);
587 timeout
= wait_for_completion_timeout(&i2c
->trans_waitq
,
588 msecs_to_jiffies(wait_time
));
591 dev_err(&i2c
->adap
.dev
, "irq read timeout\n");
592 dev_dbg(&i2c
->adap
.dev
, "send cmd count:%d %d\n",
593 i2c
->cmd
, i2c
->cmd_buf
[i2c
->cmd
]);
594 dev_dbg(&i2c
->adap
.dev
, "receive data count:%d %d\n",
595 i2c
->cmd
, i2c
->data_buf
[i2c
->cmd
]);
599 tmp
= jz4780_i2c_readw(i2c
, JZ4780_I2C_TXABRT
);
601 jz4780_i2c_txabrt(i2c
, tmp
);
608 static inline int jz4780_i2c_xfer_write(struct jz4780_i2c
*i2c
,
609 unsigned char *buf
, int len
,
613 int wait_time
= JZ4780_I2C_TIMEOUT
* (len
+ 5);
618 spin_lock_irqsave(&i2c
->lock
, flags
);
629 jz4780_i2c_writew(i2c
, JZ4780_I2C_TXTL
, TX_LEVEL
);
631 jz4780_i2c_writew(i2c
, JZ4780_I2C_INTM
, JZ4780_I2C_INTM_MTXEMP
632 | JZ4780_I2C_INTM_MTXABT
);
634 tmp
= jz4780_i2c_readw(i2c
, JZ4780_I2C_CTRL
);
635 tmp
|= JZ4780_I2C_CTRL_STPHLD
;
636 jz4780_i2c_writew(i2c
, JZ4780_I2C_CTRL
, tmp
);
638 spin_unlock_irqrestore(&i2c
->lock
, flags
);
640 timeout
= wait_for_completion_timeout(&i2c
->trans_waitq
,
641 msecs_to_jiffies(wait_time
));
642 if (timeout
&& !i2c
->stop_hold
) {
643 unsigned short i2c_sta
;
644 int write_in_process
;
646 timeout
= JZ4780_I2C_TIMEOUT
* 100;
647 for (; timeout
> 0; timeout
--) {
648 i2c_sta
= jz4780_i2c_readw(i2c
, JZ4780_I2C_STA
);
650 write_in_process
= (i2c_sta
& JZ4780_I2C_STA_MSTACT
) ||
651 !(i2c_sta
& JZ4780_I2C_STA_TFE
);
652 if (!write_in_process
)
659 dev_err(&i2c
->adap
.dev
, "write wait timeout\n");
663 tmp
= jz4780_i2c_readw(i2c
, JZ4780_I2C_TXABRT
);
665 jz4780_i2c_txabrt(i2c
, tmp
);
672 static int jz4780_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msg
,
677 struct jz4780_i2c
*i2c
= adap
->algo_data
;
679 ret
= jz4780_i2c_prepare(i2c
);
681 dev_err(&i2c
->adap
.dev
, "I2C prepare failed\n");
685 if (msg
->addr
!= jz4780_i2c_readw(i2c
, JZ4780_I2C_TAR
)) {
686 ret
= jz4780_i2c_set_target(i2c
, msg
->addr
);
690 for (i
= 0; i
< count
; i
++, msg
++) {
691 if (msg
->flags
& I2C_M_RD
)
692 ret
= jz4780_i2c_xfer_read(i2c
, msg
->buf
, msg
->len
,
695 ret
= jz4780_i2c_xfer_write(i2c
, msg
->buf
, msg
->len
,
705 jz4780_i2c_cleanup(i2c
);
709 static u32
jz4780_i2c_functionality(struct i2c_adapter
*adap
)
711 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
714 static const struct i2c_algorithm jz4780_i2c_algorithm
= {
715 .master_xfer
= jz4780_i2c_xfer
,
716 .functionality
= jz4780_i2c_functionality
,
719 static const struct of_device_id jz4780_i2c_of_matches
[] = {
720 { .compatible
= "ingenic,jz4780-i2c", },
723 MODULE_DEVICE_TABLE(of
, jz4780_i2c_of_matches
);
725 static int jz4780_i2c_probe(struct platform_device
*pdev
)
728 unsigned int clk_freq
= 0;
731 struct jz4780_i2c
*i2c
;
733 i2c
= devm_kzalloc(&pdev
->dev
, sizeof(struct jz4780_i2c
), GFP_KERNEL
);
737 i2c
->adap
.owner
= THIS_MODULE
;
738 i2c
->adap
.algo
= &jz4780_i2c_algorithm
;
739 i2c
->adap
.algo_data
= i2c
;
740 i2c
->adap
.retries
= 5;
741 i2c
->adap
.dev
.parent
= &pdev
->dev
;
742 i2c
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
743 sprintf(i2c
->adap
.name
, "%s", pdev
->name
);
745 init_completion(&i2c
->trans_waitq
);
746 spin_lock_init(&i2c
->lock
);
748 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
749 i2c
->iomem
= devm_ioremap_resource(&pdev
->dev
, r
);
750 if (IS_ERR(i2c
->iomem
))
751 return PTR_ERR(i2c
->iomem
);
753 platform_set_drvdata(pdev
, i2c
);
755 i2c
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
756 if (IS_ERR(i2c
->clk
))
757 return PTR_ERR(i2c
->clk
);
759 ret
= clk_prepare_enable(i2c
->clk
);
763 ret
= of_property_read_u32(pdev
->dev
.of_node
, "clock-frequency",
766 dev_err(&pdev
->dev
, "clock-frequency not specified in DT\n");
770 i2c
->speed
= clk_freq
/ 1000;
771 if (i2c
->speed
== 0) {
773 dev_err(&pdev
->dev
, "clock-frequency minimum is 1000\n");
776 jz4780_i2c_set_speed(i2c
);
778 dev_info(&pdev
->dev
, "Bus frequency is %d KHz\n", i2c
->speed
);
780 tmp
= jz4780_i2c_readw(i2c
, JZ4780_I2C_CTRL
);
781 tmp
&= ~JZ4780_I2C_CTRL_STPHLD
;
782 jz4780_i2c_writew(i2c
, JZ4780_I2C_CTRL
, tmp
);
784 jz4780_i2c_writew(i2c
, JZ4780_I2C_INTM
, 0x0);
786 i2c
->irq
= platform_get_irq(pdev
, 0);
787 ret
= devm_request_irq(&pdev
->dev
, i2c
->irq
, jz4780_i2c_irq
, 0,
788 dev_name(&pdev
->dev
), i2c
);
792 ret
= i2c_add_adapter(&i2c
->adap
);
799 clk_disable_unprepare(i2c
->clk
);
803 static int jz4780_i2c_remove(struct platform_device
*pdev
)
805 struct jz4780_i2c
*i2c
= platform_get_drvdata(pdev
);
807 clk_disable_unprepare(i2c
->clk
);
808 i2c_del_adapter(&i2c
->adap
);
812 static struct platform_driver jz4780_i2c_driver
= {
813 .probe
= jz4780_i2c_probe
,
814 .remove
= jz4780_i2c_remove
,
816 .name
= "jz4780-i2c",
817 .of_match_table
= of_match_ptr(jz4780_i2c_of_matches
),
821 module_platform_driver(jz4780_i2c_driver
);
823 MODULE_LICENSE("GPL");
824 MODULE_AUTHOR("ztyan<ztyan@ingenic.cn>");
825 MODULE_DESCRIPTION("i2c driver for JZ4780 SoCs");